trident_main.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005
  1. /*
  2. * Maintained by Jaroslav Kysela <perex@suse.cz>
  3. * Originated by audio@tridentmicro.com
  4. * Fri Feb 19 15:55:28 MST 1999
  5. * Routines for control of Trident 4DWave (DX and NX) chip
  6. *
  7. * BUGS:
  8. *
  9. * TODO:
  10. * ---
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. *
  27. * SiS7018 S/PDIF support by Thomas Winischhofer <thomas@winischhofer.net>
  28. */
  29. #include <sound/driver.h>
  30. #include <linux/delay.h>
  31. #include <linux/init.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/pci.h>
  34. #include <linux/slab.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/gameport.h>
  37. #include <linux/dma-mapping.h>
  38. #include <sound/core.h>
  39. #include <sound/info.h>
  40. #include <sound/control.h>
  41. #include <sound/trident.h>
  42. #include <sound/asoundef.h>
  43. #include <asm/io.h>
  44. static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
  45. struct snd_trident_voice * voice,
  46. struct snd_pcm_substream *substream);
  47. static int snd_trident_pcm_mixer_free(struct snd_trident *trident,
  48. struct snd_trident_voice * voice,
  49. struct snd_pcm_substream *substream);
  50. static irqreturn_t snd_trident_interrupt(int irq, void *dev_id,
  51. struct pt_regs *regs);
  52. static int snd_trident_sis_reset(struct snd_trident *trident);
  53. static void snd_trident_clear_voices(struct snd_trident * trident,
  54. unsigned short v_min, unsigned short v_max);
  55. static int snd_trident_free(struct snd_trident *trident);
  56. /*
  57. * common I/O routines
  58. */
  59. #if 0
  60. static void snd_trident_print_voice_regs(struct snd_trident *trident, int voice)
  61. {
  62. unsigned int val, tmp;
  63. printk("Trident voice %i:\n", voice);
  64. outb(voice, TRID_REG(trident, T4D_LFO_GC_CIR));
  65. val = inl(TRID_REG(trident, CH_LBA));
  66. printk("LBA: 0x%x\n", val);
  67. val = inl(TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
  68. printk("GVSel: %i\n", val >> 31);
  69. printk("Pan: 0x%x\n", (val >> 24) & 0x7f);
  70. printk("Vol: 0x%x\n", (val >> 16) & 0xff);
  71. printk("CTRL: 0x%x\n", (val >> 12) & 0x0f);
  72. printk("EC: 0x%x\n", val & 0x0fff);
  73. if (trident->device != TRIDENT_DEVICE_ID_NX) {
  74. val = inl(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS));
  75. printk("CSO: 0x%x\n", val >> 16);
  76. printk("Alpha: 0x%x\n", (val >> 4) & 0x0fff);
  77. printk("FMS: 0x%x\n", val & 0x0f);
  78. val = inl(TRID_REG(trident, CH_DX_ESO_DELTA));
  79. printk("ESO: 0x%x\n", val >> 16);
  80. printk("Delta: 0x%x\n", val & 0xffff);
  81. val = inl(TRID_REG(trident, CH_DX_FMC_RVOL_CVOL));
  82. } else { // TRIDENT_DEVICE_ID_NX
  83. val = inl(TRID_REG(trident, CH_NX_DELTA_CSO));
  84. tmp = (val >> 24) & 0xff;
  85. printk("CSO: 0x%x\n", val & 0x00ffffff);
  86. val = inl(TRID_REG(trident, CH_NX_DELTA_ESO));
  87. tmp |= (val >> 16) & 0xff00;
  88. printk("Delta: 0x%x\n", tmp);
  89. printk("ESO: 0x%x\n", val & 0x00ffffff);
  90. val = inl(TRID_REG(trident, CH_NX_ALPHA_FMS_FMC_RVOL_CVOL));
  91. printk("Alpha: 0x%x\n", val >> 20);
  92. printk("FMS: 0x%x\n", (val >> 16) & 0x0f);
  93. }
  94. printk("FMC: 0x%x\n", (val >> 14) & 3);
  95. printk("RVol: 0x%x\n", (val >> 7) & 0x7f);
  96. printk("CVol: 0x%x\n", val & 0x7f);
  97. }
  98. #endif
  99. /*---------------------------------------------------------------------------
  100. unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
  101. Description: This routine will do all of the reading from the external
  102. CODEC (AC97).
  103. Parameters: ac97 - ac97 codec structure
  104. reg - CODEC register index, from AC97 Hal.
  105. returns: 16 bit value read from the AC97.
  106. ---------------------------------------------------------------------------*/
  107. static unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
  108. {
  109. unsigned int data = 0, treg;
  110. unsigned short count = 0xffff;
  111. unsigned long flags;
  112. struct snd_trident *trident = ac97->private_data;
  113. spin_lock_irqsave(&trident->reg_lock, flags);
  114. if (trident->device == TRIDENT_DEVICE_ID_DX) {
  115. data = (DX_AC97_BUSY_READ | (reg & 0x000000ff));
  116. outl(data, TRID_REG(trident, DX_ACR1_AC97_R));
  117. do {
  118. data = inl(TRID_REG(trident, DX_ACR1_AC97_R));
  119. if ((data & DX_AC97_BUSY_READ) == 0)
  120. break;
  121. } while (--count);
  122. } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
  123. data = (NX_AC97_BUSY_READ | (reg & 0x000000ff));
  124. treg = ac97->num == 0 ? NX_ACR2_AC97_R_PRIMARY : NX_ACR3_AC97_R_SECONDARY;
  125. outl(data, TRID_REG(trident, treg));
  126. do {
  127. data = inl(TRID_REG(trident, treg));
  128. if ((data & 0x00000C00) == 0)
  129. break;
  130. } while (--count);
  131. } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  132. data = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
  133. if (ac97->num == 1)
  134. data |= SI_AC97_SECONDARY;
  135. outl(data, TRID_REG(trident, SI_AC97_READ));
  136. do {
  137. data = inl(TRID_REG(trident, SI_AC97_READ));
  138. if ((data & (SI_AC97_BUSY_READ)) == 0)
  139. break;
  140. } while (--count);
  141. }
  142. if (count == 0 && !trident->ac97_detect) {
  143. snd_printk(KERN_ERR "ac97 codec read TIMEOUT [0x%x/0x%x]!!!\n",
  144. reg, data);
  145. data = 0;
  146. }
  147. spin_unlock_irqrestore(&trident->reg_lock, flags);
  148. return ((unsigned short) (data >> 16));
  149. }
  150. /*---------------------------------------------------------------------------
  151. void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
  152. unsigned short wdata)
  153. Description: This routine will do all of the writing to the external
  154. CODEC (AC97).
  155. Parameters: ac97 - ac97 codec structure
  156. reg - CODEC register index, from AC97 Hal.
  157. data - Lower 16 bits are the data to write to CODEC.
  158. returns: TRUE if everything went ok, else FALSE.
  159. ---------------------------------------------------------------------------*/
  160. static void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
  161. unsigned short wdata)
  162. {
  163. unsigned int address, data;
  164. unsigned short count = 0xffff;
  165. unsigned long flags;
  166. struct snd_trident *trident = ac97->private_data;
  167. data = ((unsigned long) wdata) << 16;
  168. spin_lock_irqsave(&trident->reg_lock, flags);
  169. if (trident->device == TRIDENT_DEVICE_ID_DX) {
  170. address = DX_ACR0_AC97_W;
  171. /* read AC-97 write register status */
  172. do {
  173. if ((inw(TRID_REG(trident, address)) & DX_AC97_BUSY_WRITE) == 0)
  174. break;
  175. } while (--count);
  176. data |= (DX_AC97_BUSY_WRITE | (reg & 0x000000ff));
  177. } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
  178. address = NX_ACR1_AC97_W;
  179. /* read AC-97 write register status */
  180. do {
  181. if ((inw(TRID_REG(trident, address)) & NX_AC97_BUSY_WRITE) == 0)
  182. break;
  183. } while (--count);
  184. data |= (NX_AC97_BUSY_WRITE | (ac97->num << 8) | (reg & 0x000000ff));
  185. } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  186. address = SI_AC97_WRITE;
  187. /* read AC-97 write register status */
  188. do {
  189. if ((inw(TRID_REG(trident, address)) & (SI_AC97_BUSY_WRITE)) == 0)
  190. break;
  191. } while (--count);
  192. data |= SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
  193. if (ac97->num == 1)
  194. data |= SI_AC97_SECONDARY;
  195. } else {
  196. address = 0; /* keep GCC happy */
  197. count = 0; /* return */
  198. }
  199. if (count == 0) {
  200. spin_unlock_irqrestore(&trident->reg_lock, flags);
  201. return;
  202. }
  203. outl(data, TRID_REG(trident, address));
  204. spin_unlock_irqrestore(&trident->reg_lock, flags);
  205. }
  206. /*---------------------------------------------------------------------------
  207. void snd_trident_enable_eso(struct snd_trident *trident)
  208. Description: This routine will enable end of loop interrupts.
  209. End of loop interrupts will occur when a running
  210. channel reaches ESO.
  211. Also enables middle of loop interrupts.
  212. Parameters: trident - pointer to target device class for 4DWave.
  213. ---------------------------------------------------------------------------*/
  214. static void snd_trident_enable_eso(struct snd_trident * trident)
  215. {
  216. unsigned int val;
  217. val = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
  218. val |= ENDLP_IE;
  219. val |= MIDLP_IE;
  220. if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  221. val |= BANK_B_EN;
  222. outl(val, TRID_REG(trident, T4D_LFO_GC_CIR));
  223. }
  224. /*---------------------------------------------------------------------------
  225. void snd_trident_disable_eso(struct snd_trident *trident)
  226. Description: This routine will disable end of loop interrupts.
  227. End of loop interrupts will occur when a running
  228. channel reaches ESO.
  229. Also disables middle of loop interrupts.
  230. Parameters:
  231. trident - pointer to target device class for 4DWave.
  232. returns: TRUE if everything went ok, else FALSE.
  233. ---------------------------------------------------------------------------*/
  234. static void snd_trident_disable_eso(struct snd_trident * trident)
  235. {
  236. unsigned int tmp;
  237. tmp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
  238. tmp &= ~ENDLP_IE;
  239. tmp &= ~MIDLP_IE;
  240. outl(tmp, TRID_REG(trident, T4D_LFO_GC_CIR));
  241. }
  242. /*---------------------------------------------------------------------------
  243. void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
  244. Description: Start a voice, any channel 0 thru 63.
  245. This routine automatically handles the fact that there are
  246. more than 32 channels available.
  247. Parameters : voice - Voice number 0 thru n.
  248. trident - pointer to target device class for 4DWave.
  249. Return Value: None.
  250. ---------------------------------------------------------------------------*/
  251. void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
  252. {
  253. unsigned int mask = 1 << (voice & 0x1f);
  254. unsigned int reg = (voice & 0x20) ? T4D_START_B : T4D_START_A;
  255. outl(mask, TRID_REG(trident, reg));
  256. }
  257. /*---------------------------------------------------------------------------
  258. void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
  259. Description: Stop a voice, any channel 0 thru 63.
  260. This routine automatically handles the fact that there are
  261. more than 32 channels available.
  262. Parameters : voice - Voice number 0 thru n.
  263. trident - pointer to target device class for 4DWave.
  264. Return Value: None.
  265. ---------------------------------------------------------------------------*/
  266. void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
  267. {
  268. unsigned int mask = 1 << (voice & 0x1f);
  269. unsigned int reg = (voice & 0x20) ? T4D_STOP_B : T4D_STOP_A;
  270. outl(mask, TRID_REG(trident, reg));
  271. }
  272. /*---------------------------------------------------------------------------
  273. int snd_trident_allocate_pcm_channel(struct snd_trident *trident)
  274. Description: Allocate hardware channel in Bank B (32-63).
  275. Parameters : trident - pointer to target device class for 4DWave.
  276. Return Value: hardware channel - 32-63 or -1 when no channel is available
  277. ---------------------------------------------------------------------------*/
  278. static int snd_trident_allocate_pcm_channel(struct snd_trident * trident)
  279. {
  280. int idx;
  281. if (trident->ChanPCMcnt >= trident->ChanPCM)
  282. return -1;
  283. for (idx = 31; idx >= 0; idx--) {
  284. if (!(trident->ChanMap[T4D_BANK_B] & (1 << idx))) {
  285. trident->ChanMap[T4D_BANK_B] |= 1 << idx;
  286. trident->ChanPCMcnt++;
  287. return idx + 32;
  288. }
  289. }
  290. return -1;
  291. }
  292. /*---------------------------------------------------------------------------
  293. void snd_trident_free_pcm_channel(int channel)
  294. Description: Free hardware channel in Bank B (32-63)
  295. Parameters : trident - pointer to target device class for 4DWave.
  296. channel - hardware channel number 0-63
  297. Return Value: none
  298. ---------------------------------------------------------------------------*/
  299. static void snd_trident_free_pcm_channel(struct snd_trident *trident, int channel)
  300. {
  301. if (channel < 32 || channel > 63)
  302. return;
  303. channel &= 0x1f;
  304. if (trident->ChanMap[T4D_BANK_B] & (1 << channel)) {
  305. trident->ChanMap[T4D_BANK_B] &= ~(1 << channel);
  306. trident->ChanPCMcnt--;
  307. }
  308. }
  309. /*---------------------------------------------------------------------------
  310. unsigned int snd_trident_allocate_synth_channel(void)
  311. Description: Allocate hardware channel in Bank A (0-31).
  312. Parameters : trident - pointer to target device class for 4DWave.
  313. Return Value: hardware channel - 0-31 or -1 when no channel is available
  314. ---------------------------------------------------------------------------*/
  315. static int snd_trident_allocate_synth_channel(struct snd_trident * trident)
  316. {
  317. int idx;
  318. for (idx = 31; idx >= 0; idx--) {
  319. if (!(trident->ChanMap[T4D_BANK_A] & (1 << idx))) {
  320. trident->ChanMap[T4D_BANK_A] |= 1 << idx;
  321. trident->synth.ChanSynthCount++;
  322. return idx;
  323. }
  324. }
  325. return -1;
  326. }
  327. /*---------------------------------------------------------------------------
  328. void snd_trident_free_synth_channel( int channel )
  329. Description: Free hardware channel in Bank B (0-31).
  330. Parameters : trident - pointer to target device class for 4DWave.
  331. channel - hardware channel number 0-63
  332. Return Value: none
  333. ---------------------------------------------------------------------------*/
  334. static void snd_trident_free_synth_channel(struct snd_trident *trident, int channel)
  335. {
  336. if (channel < 0 || channel > 31)
  337. return;
  338. channel &= 0x1f;
  339. if (trident->ChanMap[T4D_BANK_A] & (1 << channel)) {
  340. trident->ChanMap[T4D_BANK_A] &= ~(1 << channel);
  341. trident->synth.ChanSynthCount--;
  342. }
  343. }
  344. /*---------------------------------------------------------------------------
  345. snd_trident_write_voice_regs
  346. Description: This routine will complete and write the 5 hardware channel
  347. registers to hardware.
  348. Paramters: trident - pointer to target device class for 4DWave.
  349. voice - synthesizer voice structure
  350. Each register field.
  351. ---------------------------------------------------------------------------*/
  352. void snd_trident_write_voice_regs(struct snd_trident * trident,
  353. struct snd_trident_voice * voice)
  354. {
  355. unsigned int FmcRvolCvol;
  356. unsigned int regs[5];
  357. regs[1] = voice->LBA;
  358. regs[4] = (voice->GVSel << 31) |
  359. ((voice->Pan & 0x0000007f) << 24) |
  360. ((voice->CTRL & 0x0000000f) << 12);
  361. FmcRvolCvol = ((voice->FMC & 3) << 14) |
  362. ((voice->RVol & 0x7f) << 7) |
  363. (voice->CVol & 0x7f);
  364. switch (trident->device) {
  365. case TRIDENT_DEVICE_ID_SI7018:
  366. regs[4] |= voice->number > 31 ?
  367. (voice->Vol & 0x000003ff) :
  368. ((voice->Vol & 0x00003fc) << (16-2)) |
  369. (voice->EC & 0x00000fff);
  370. regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
  371. (voice->FMS & 0x0000000f);
  372. regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
  373. regs[3] = (voice->Attribute << 16) | FmcRvolCvol;
  374. break;
  375. case TRIDENT_DEVICE_ID_DX:
  376. regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
  377. (voice->EC & 0x00000fff);
  378. regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
  379. (voice->FMS & 0x0000000f);
  380. regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
  381. regs[3] = FmcRvolCvol;
  382. break;
  383. case TRIDENT_DEVICE_ID_NX:
  384. regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
  385. (voice->EC & 0x00000fff);
  386. regs[0] = (voice->Delta << 24) | (voice->CSO & 0x00ffffff);
  387. regs[2] = ((voice->Delta << 16) & 0xff000000) |
  388. (voice->ESO & 0x00ffffff);
  389. regs[3] = (voice->Alpha << 20) |
  390. ((voice->FMS & 0x0000000f) << 16) | FmcRvolCvol;
  391. break;
  392. default:
  393. snd_BUG();
  394. return;
  395. }
  396. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  397. outl(regs[0], TRID_REG(trident, CH_START + 0));
  398. outl(regs[1], TRID_REG(trident, CH_START + 4));
  399. outl(regs[2], TRID_REG(trident, CH_START + 8));
  400. outl(regs[3], TRID_REG(trident, CH_START + 12));
  401. outl(regs[4], TRID_REG(trident, CH_START + 16));
  402. #if 0
  403. printk("written %i channel:\n", voice->number);
  404. printk(" regs[0] = 0x%x/0x%x\n", regs[0], inl(TRID_REG(trident, CH_START + 0)));
  405. printk(" regs[1] = 0x%x/0x%x\n", regs[1], inl(TRID_REG(trident, CH_START + 4)));
  406. printk(" regs[2] = 0x%x/0x%x\n", regs[2], inl(TRID_REG(trident, CH_START + 8)));
  407. printk(" regs[3] = 0x%x/0x%x\n", regs[3], inl(TRID_REG(trident, CH_START + 12)));
  408. printk(" regs[4] = 0x%x/0x%x\n", regs[4], inl(TRID_REG(trident, CH_START + 16)));
  409. #endif
  410. }
  411. /*---------------------------------------------------------------------------
  412. snd_trident_write_cso_reg
  413. Description: This routine will write the new CSO offset
  414. register to hardware.
  415. Paramters: trident - pointer to target device class for 4DWave.
  416. voice - synthesizer voice structure
  417. CSO - new CSO value
  418. ---------------------------------------------------------------------------*/
  419. static void snd_trident_write_cso_reg(struct snd_trident * trident,
  420. struct snd_trident_voice * voice,
  421. unsigned int CSO)
  422. {
  423. voice->CSO = CSO;
  424. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  425. if (trident->device != TRIDENT_DEVICE_ID_NX) {
  426. outw(voice->CSO, TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
  427. } else {
  428. outl((voice->Delta << 24) |
  429. (voice->CSO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_CSO));
  430. }
  431. }
  432. /*---------------------------------------------------------------------------
  433. snd_trident_write_eso_reg
  434. Description: This routine will write the new ESO offset
  435. register to hardware.
  436. Paramters: trident - pointer to target device class for 4DWave.
  437. voice - synthesizer voice structure
  438. ESO - new ESO value
  439. ---------------------------------------------------------------------------*/
  440. static void snd_trident_write_eso_reg(struct snd_trident * trident,
  441. struct snd_trident_voice * voice,
  442. unsigned int ESO)
  443. {
  444. voice->ESO = ESO;
  445. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  446. if (trident->device != TRIDENT_DEVICE_ID_NX) {
  447. outw(voice->ESO, TRID_REG(trident, CH_DX_ESO_DELTA) + 2);
  448. } else {
  449. outl(((voice->Delta << 16) & 0xff000000) | (voice->ESO & 0x00ffffff),
  450. TRID_REG(trident, CH_NX_DELTA_ESO));
  451. }
  452. }
  453. /*---------------------------------------------------------------------------
  454. snd_trident_write_vol_reg
  455. Description: This routine will write the new voice volume
  456. register to hardware.
  457. Paramters: trident - pointer to target device class for 4DWave.
  458. voice - synthesizer voice structure
  459. Vol - new voice volume
  460. ---------------------------------------------------------------------------*/
  461. static void snd_trident_write_vol_reg(struct snd_trident * trident,
  462. struct snd_trident_voice * voice,
  463. unsigned int Vol)
  464. {
  465. voice->Vol = Vol;
  466. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  467. switch (trident->device) {
  468. case TRIDENT_DEVICE_ID_DX:
  469. case TRIDENT_DEVICE_ID_NX:
  470. outb(voice->Vol >> 2, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 2));
  471. break;
  472. case TRIDENT_DEVICE_ID_SI7018:
  473. // printk("voice->Vol = 0x%x\n", voice->Vol);
  474. outw((voice->CTRL << 12) | voice->Vol,
  475. TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
  476. break;
  477. }
  478. }
  479. /*---------------------------------------------------------------------------
  480. snd_trident_write_pan_reg
  481. Description: This routine will write the new voice pan
  482. register to hardware.
  483. Paramters: trident - pointer to target device class for 4DWave.
  484. voice - synthesizer voice structure
  485. Pan - new pan value
  486. ---------------------------------------------------------------------------*/
  487. static void snd_trident_write_pan_reg(struct snd_trident * trident,
  488. struct snd_trident_voice * voice,
  489. unsigned int Pan)
  490. {
  491. voice->Pan = Pan;
  492. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  493. outb(((voice->GVSel & 0x01) << 7) | (voice->Pan & 0x7f),
  494. TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 3));
  495. }
  496. /*---------------------------------------------------------------------------
  497. snd_trident_write_rvol_reg
  498. Description: This routine will write the new reverb volume
  499. register to hardware.
  500. Paramters: trident - pointer to target device class for 4DWave.
  501. voice - synthesizer voice structure
  502. RVol - new reverb volume
  503. ---------------------------------------------------------------------------*/
  504. static void snd_trident_write_rvol_reg(struct snd_trident * trident,
  505. struct snd_trident_voice * voice,
  506. unsigned int RVol)
  507. {
  508. voice->RVol = RVol;
  509. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  510. outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
  511. (voice->CVol & 0x007f),
  512. TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
  513. CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
  514. }
  515. /*---------------------------------------------------------------------------
  516. snd_trident_write_cvol_reg
  517. Description: This routine will write the new chorus volume
  518. register to hardware.
  519. Paramters: trident - pointer to target device class for 4DWave.
  520. voice - synthesizer voice structure
  521. CVol - new chorus volume
  522. ---------------------------------------------------------------------------*/
  523. static void snd_trident_write_cvol_reg(struct snd_trident * trident,
  524. struct snd_trident_voice * voice,
  525. unsigned int CVol)
  526. {
  527. voice->CVol = CVol;
  528. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  529. outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
  530. (voice->CVol & 0x007f),
  531. TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
  532. CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
  533. }
  534. /*---------------------------------------------------------------------------
  535. snd_trident_convert_rate
  536. Description: This routine converts rate in HZ to hardware delta value.
  537. Paramters: trident - pointer to target device class for 4DWave.
  538. rate - Real or Virtual channel number.
  539. Returns: Delta value.
  540. ---------------------------------------------------------------------------*/
  541. static unsigned int snd_trident_convert_rate(unsigned int rate)
  542. {
  543. unsigned int delta;
  544. // We special case 44100 and 8000 since rounding with the equation
  545. // does not give us an accurate enough value. For 11025 and 22050
  546. // the equation gives us the best answer. All other frequencies will
  547. // also use the equation. JDW
  548. if (rate == 44100)
  549. delta = 0xeb3;
  550. else if (rate == 8000)
  551. delta = 0x2ab;
  552. else if (rate == 48000)
  553. delta = 0x1000;
  554. else
  555. delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
  556. return delta;
  557. }
  558. /*---------------------------------------------------------------------------
  559. snd_trident_convert_adc_rate
  560. Description: This routine converts rate in HZ to hardware delta value.
  561. Paramters: trident - pointer to target device class for 4DWave.
  562. rate - Real or Virtual channel number.
  563. Returns: Delta value.
  564. ---------------------------------------------------------------------------*/
  565. static unsigned int snd_trident_convert_adc_rate(unsigned int rate)
  566. {
  567. unsigned int delta;
  568. // We special case 44100 and 8000 since rounding with the equation
  569. // does not give us an accurate enough value. For 11025 and 22050
  570. // the equation gives us the best answer. All other frequencies will
  571. // also use the equation. JDW
  572. if (rate == 44100)
  573. delta = 0x116a;
  574. else if (rate == 8000)
  575. delta = 0x6000;
  576. else if (rate == 48000)
  577. delta = 0x1000;
  578. else
  579. delta = ((48000 << 12) / rate) & 0x0000ffff;
  580. return delta;
  581. }
  582. /*---------------------------------------------------------------------------
  583. snd_trident_spurious_threshold
  584. Description: This routine converts rate in HZ to spurious threshold.
  585. Paramters: trident - pointer to target device class for 4DWave.
  586. rate - Real or Virtual channel number.
  587. Returns: Delta value.
  588. ---------------------------------------------------------------------------*/
  589. static unsigned int snd_trident_spurious_threshold(unsigned int rate,
  590. unsigned int period_size)
  591. {
  592. unsigned int res = (rate * period_size) / 48000;
  593. if (res < 64)
  594. res = res / 2;
  595. else
  596. res -= 32;
  597. return res;
  598. }
  599. /*---------------------------------------------------------------------------
  600. snd_trident_control_mode
  601. Description: This routine returns a control mode for a PCM channel.
  602. Paramters: trident - pointer to target device class for 4DWave.
  603. substream - PCM substream
  604. Returns: Control value.
  605. ---------------------------------------------------------------------------*/
  606. static unsigned int snd_trident_control_mode(struct snd_pcm_substream *substream)
  607. {
  608. unsigned int CTRL;
  609. struct snd_pcm_runtime *runtime = substream->runtime;
  610. /* set ctrl mode
  611. CTRL default: 8-bit (unsigned) mono, loop mode enabled
  612. */
  613. CTRL = 0x00000001;
  614. if (snd_pcm_format_width(runtime->format) == 16)
  615. CTRL |= 0x00000008; // 16-bit data
  616. if (snd_pcm_format_signed(runtime->format))
  617. CTRL |= 0x00000002; // signed data
  618. if (runtime->channels > 1)
  619. CTRL |= 0x00000004; // stereo data
  620. return CTRL;
  621. }
  622. /*
  623. * PCM part
  624. */
  625. /*---------------------------------------------------------------------------
  626. snd_trident_ioctl
  627. Description: Device I/O control handler for playback/capture parameters.
  628. Paramters: substream - PCM substream class
  629. cmd - what ioctl message to process
  630. arg - additional message infoarg
  631. Returns: Error status
  632. ---------------------------------------------------------------------------*/
  633. static int snd_trident_ioctl(struct snd_pcm_substream *substream,
  634. unsigned int cmd,
  635. void *arg)
  636. {
  637. /* FIXME: it seems that with small periods the behaviour of
  638. trident hardware is unpredictable and interrupt generator
  639. is broken */
  640. return snd_pcm_lib_ioctl(substream, cmd, arg);
  641. }
  642. /*---------------------------------------------------------------------------
  643. snd_trident_allocate_pcm_mem
  644. Description: Allocate PCM ring buffer for given substream
  645. Parameters: substream - PCM substream class
  646. hw_params - hardware parameters
  647. Returns: Error status
  648. ---------------------------------------------------------------------------*/
  649. static int snd_trident_allocate_pcm_mem(struct snd_pcm_substream *substream,
  650. struct snd_pcm_hw_params *hw_params)
  651. {
  652. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  653. struct snd_pcm_runtime *runtime = substream->runtime;
  654. struct snd_trident_voice *voice = runtime->private_data;
  655. int err;
  656. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  657. return err;
  658. if (trident->tlb.entries) {
  659. if (err > 0) { /* change */
  660. if (voice->memblk)
  661. snd_trident_free_pages(trident, voice->memblk);
  662. voice->memblk = snd_trident_alloc_pages(trident, substream);
  663. if (voice->memblk == NULL)
  664. return -ENOMEM;
  665. }
  666. }
  667. return 0;
  668. }
  669. /*---------------------------------------------------------------------------
  670. snd_trident_allocate_evoice
  671. Description: Allocate extra voice as interrupt generator
  672. Parameters: substream - PCM substream class
  673. hw_params - hardware parameters
  674. Returns: Error status
  675. ---------------------------------------------------------------------------*/
  676. static int snd_trident_allocate_evoice(struct snd_pcm_substream *substream,
  677. struct snd_pcm_hw_params *hw_params)
  678. {
  679. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  680. struct snd_pcm_runtime *runtime = substream->runtime;
  681. struct snd_trident_voice *voice = runtime->private_data;
  682. struct snd_trident_voice *evoice = voice->extra;
  683. /* voice management */
  684. if (params_buffer_size(hw_params) / 2 != params_period_size(hw_params)) {
  685. if (evoice == NULL) {
  686. evoice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  687. if (evoice == NULL)
  688. return -ENOMEM;
  689. voice->extra = evoice;
  690. evoice->substream = substream;
  691. }
  692. } else {
  693. if (evoice != NULL) {
  694. snd_trident_free_voice(trident, evoice);
  695. voice->extra = evoice = NULL;
  696. }
  697. }
  698. return 0;
  699. }
  700. /*---------------------------------------------------------------------------
  701. snd_trident_hw_params
  702. Description: Set the hardware parameters for the playback device.
  703. Parameters: substream - PCM substream class
  704. hw_params - hardware parameters
  705. Returns: Error status
  706. ---------------------------------------------------------------------------*/
  707. static int snd_trident_hw_params(struct snd_pcm_substream *substream,
  708. struct snd_pcm_hw_params *hw_params)
  709. {
  710. int err;
  711. err = snd_trident_allocate_pcm_mem(substream, hw_params);
  712. if (err >= 0)
  713. err = snd_trident_allocate_evoice(substream, hw_params);
  714. return err;
  715. }
  716. /*---------------------------------------------------------------------------
  717. snd_trident_playback_hw_free
  718. Description: Release the hardware resources for the playback device.
  719. Parameters: substream - PCM substream class
  720. Returns: Error status
  721. ---------------------------------------------------------------------------*/
  722. static int snd_trident_hw_free(struct snd_pcm_substream *substream)
  723. {
  724. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  725. struct snd_pcm_runtime *runtime = substream->runtime;
  726. struct snd_trident_voice *voice = runtime->private_data;
  727. struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
  728. if (trident->tlb.entries) {
  729. if (voice && voice->memblk) {
  730. snd_trident_free_pages(trident, voice->memblk);
  731. voice->memblk = NULL;
  732. }
  733. }
  734. snd_pcm_lib_free_pages(substream);
  735. if (evoice != NULL) {
  736. snd_trident_free_voice(trident, evoice);
  737. voice->extra = NULL;
  738. }
  739. return 0;
  740. }
  741. /*---------------------------------------------------------------------------
  742. snd_trident_playback_prepare
  743. Description: Prepare playback device for playback.
  744. Parameters: substream - PCM substream class
  745. Returns: Error status
  746. ---------------------------------------------------------------------------*/
  747. static int snd_trident_playback_prepare(struct snd_pcm_substream *substream)
  748. {
  749. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  750. struct snd_pcm_runtime *runtime = substream->runtime;
  751. struct snd_trident_voice *voice = runtime->private_data;
  752. struct snd_trident_voice *evoice = voice->extra;
  753. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
  754. spin_lock_irq(&trident->reg_lock);
  755. /* set delta (rate) value */
  756. voice->Delta = snd_trident_convert_rate(runtime->rate);
  757. voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  758. /* set Loop Begin Address */
  759. if (voice->memblk)
  760. voice->LBA = voice->memblk->offset;
  761. else
  762. voice->LBA = runtime->dma_addr;
  763. voice->CSO = 0;
  764. voice->ESO = runtime->buffer_size - 1; /* in samples */
  765. voice->CTRL = snd_trident_control_mode(substream);
  766. voice->FMC = 3;
  767. voice->GVSel = 1;
  768. voice->EC = 0;
  769. voice->Alpha = 0;
  770. voice->FMS = 0;
  771. voice->Vol = mix->vol;
  772. voice->RVol = mix->rvol;
  773. voice->CVol = mix->cvol;
  774. voice->Pan = mix->pan;
  775. voice->Attribute = 0;
  776. #if 0
  777. voice->Attribute = (1<<(30-16))|(2<<(26-16))|
  778. (0<<(24-16))|(0x1f<<(19-16));
  779. #else
  780. voice->Attribute = 0;
  781. #endif
  782. snd_trident_write_voice_regs(trident, voice);
  783. if (evoice != NULL) {
  784. evoice->Delta = voice->Delta;
  785. evoice->spurious_threshold = voice->spurious_threshold;
  786. evoice->LBA = voice->LBA;
  787. evoice->CSO = 0;
  788. evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
  789. evoice->CTRL = voice->CTRL;
  790. evoice->FMC = 3;
  791. evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
  792. evoice->EC = 0;
  793. evoice->Alpha = 0;
  794. evoice->FMS = 0;
  795. evoice->Vol = 0x3ff; /* mute */
  796. evoice->RVol = evoice->CVol = 0x7f; /* mute */
  797. evoice->Pan = 0x7f; /* mute */
  798. #if 0
  799. evoice->Attribute = (1<<(30-16))|(2<<(26-16))|
  800. (0<<(24-16))|(0x1f<<(19-16));
  801. #else
  802. evoice->Attribute = 0;
  803. #endif
  804. snd_trident_write_voice_regs(trident, evoice);
  805. evoice->isync2 = 1;
  806. evoice->isync_mark = runtime->period_size;
  807. evoice->ESO = (runtime->period_size * 2) - 1;
  808. }
  809. spin_unlock_irq(&trident->reg_lock);
  810. return 0;
  811. }
  812. /*---------------------------------------------------------------------------
  813. snd_trident_capture_hw_params
  814. Description: Set the hardware parameters for the capture device.
  815. Parameters: substream - PCM substream class
  816. hw_params - hardware parameters
  817. Returns: Error status
  818. ---------------------------------------------------------------------------*/
  819. static int snd_trident_capture_hw_params(struct snd_pcm_substream *substream,
  820. struct snd_pcm_hw_params *hw_params)
  821. {
  822. return snd_trident_allocate_pcm_mem(substream, hw_params);
  823. }
  824. /*---------------------------------------------------------------------------
  825. snd_trident_capture_prepare
  826. Description: Prepare capture device for playback.
  827. Parameters: substream - PCM substream class
  828. Returns: Error status
  829. ---------------------------------------------------------------------------*/
  830. static int snd_trident_capture_prepare(struct snd_pcm_substream *substream)
  831. {
  832. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  833. struct snd_pcm_runtime *runtime = substream->runtime;
  834. struct snd_trident_voice *voice = runtime->private_data;
  835. unsigned int val, ESO_bytes;
  836. spin_lock_irq(&trident->reg_lock);
  837. // Initilize the channel and set channel Mode
  838. outb(0, TRID_REG(trident, LEGACY_DMAR15));
  839. // Set DMA channel operation mode register
  840. outb(0x54, TRID_REG(trident, LEGACY_DMAR11));
  841. // Set channel buffer Address, DMAR0 expects contiguous PCI memory area
  842. voice->LBA = runtime->dma_addr;
  843. outl(voice->LBA, TRID_REG(trident, LEGACY_DMAR0));
  844. if (voice->memblk)
  845. voice->LBA = voice->memblk->offset;
  846. // set ESO
  847. ESO_bytes = snd_pcm_lib_buffer_bytes(substream) - 1;
  848. outb((ESO_bytes & 0x00ff0000) >> 16, TRID_REG(trident, LEGACY_DMAR6));
  849. outw((ESO_bytes & 0x0000ffff), TRID_REG(trident, LEGACY_DMAR4));
  850. ESO_bytes++;
  851. // Set channel sample rate, 4.12 format
  852. val = (((unsigned int) 48000L << 12) + (runtime->rate/2)) / runtime->rate;
  853. outw(val, TRID_REG(trident, T4D_SBDELTA_DELTA_R));
  854. // Set channel interrupt blk length
  855. if (snd_pcm_format_width(runtime->format) == 16) {
  856. val = (unsigned short) ((ESO_bytes >> 1) - 1);
  857. } else {
  858. val = (unsigned short) (ESO_bytes - 1);
  859. }
  860. outl((val << 16) | val, TRID_REG(trident, T4D_SBBL_SBCL));
  861. // Right now, set format and start to run captureing,
  862. // continuous run loop enable.
  863. trident->bDMAStart = 0x19; // 0001 1001b
  864. if (snd_pcm_format_width(runtime->format) == 16)
  865. trident->bDMAStart |= 0x80;
  866. if (snd_pcm_format_signed(runtime->format))
  867. trident->bDMAStart |= 0x20;
  868. if (runtime->channels > 1)
  869. trident->bDMAStart |= 0x40;
  870. // Prepare capture intr channel
  871. voice->Delta = snd_trident_convert_rate(runtime->rate);
  872. voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  873. voice->isync = 1;
  874. voice->isync_mark = runtime->period_size;
  875. voice->isync_max = runtime->buffer_size;
  876. // Set voice parameters
  877. voice->CSO = 0;
  878. voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
  879. voice->CTRL = snd_trident_control_mode(substream);
  880. voice->FMC = 3;
  881. voice->RVol = 0x7f;
  882. voice->CVol = 0x7f;
  883. voice->GVSel = 1;
  884. voice->Pan = 0x7f; /* mute */
  885. voice->Vol = 0x3ff; /* mute */
  886. voice->EC = 0;
  887. voice->Alpha = 0;
  888. voice->FMS = 0;
  889. voice->Attribute = 0;
  890. snd_trident_write_voice_regs(trident, voice);
  891. spin_unlock_irq(&trident->reg_lock);
  892. return 0;
  893. }
  894. /*---------------------------------------------------------------------------
  895. snd_trident_si7018_capture_hw_params
  896. Description: Set the hardware parameters for the capture device.
  897. Parameters: substream - PCM substream class
  898. hw_params - hardware parameters
  899. Returns: Error status
  900. ---------------------------------------------------------------------------*/
  901. static int snd_trident_si7018_capture_hw_params(struct snd_pcm_substream *substream,
  902. struct snd_pcm_hw_params *hw_params)
  903. {
  904. int err;
  905. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  906. return err;
  907. return snd_trident_allocate_evoice(substream, hw_params);
  908. }
  909. /*---------------------------------------------------------------------------
  910. snd_trident_si7018_capture_hw_free
  911. Description: Release the hardware resources for the capture device.
  912. Parameters: substream - PCM substream class
  913. Returns: Error status
  914. ---------------------------------------------------------------------------*/
  915. static int snd_trident_si7018_capture_hw_free(struct snd_pcm_substream *substream)
  916. {
  917. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  918. struct snd_pcm_runtime *runtime = substream->runtime;
  919. struct snd_trident_voice *voice = runtime->private_data;
  920. struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
  921. snd_pcm_lib_free_pages(substream);
  922. if (evoice != NULL) {
  923. snd_trident_free_voice(trident, evoice);
  924. voice->extra = NULL;
  925. }
  926. return 0;
  927. }
  928. /*---------------------------------------------------------------------------
  929. snd_trident_si7018_capture_prepare
  930. Description: Prepare capture device for playback.
  931. Parameters: substream - PCM substream class
  932. Returns: Error status
  933. ---------------------------------------------------------------------------*/
  934. static int snd_trident_si7018_capture_prepare(struct snd_pcm_substream *substream)
  935. {
  936. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  937. struct snd_pcm_runtime *runtime = substream->runtime;
  938. struct snd_trident_voice *voice = runtime->private_data;
  939. struct snd_trident_voice *evoice = voice->extra;
  940. spin_lock_irq(&trident->reg_lock);
  941. voice->LBA = runtime->dma_addr;
  942. voice->Delta = snd_trident_convert_adc_rate(runtime->rate);
  943. voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  944. // Set voice parameters
  945. voice->CSO = 0;
  946. voice->ESO = runtime->buffer_size - 1; /* in samples */
  947. voice->CTRL = snd_trident_control_mode(substream);
  948. voice->FMC = 0;
  949. voice->RVol = 0;
  950. voice->CVol = 0;
  951. voice->GVSel = 1;
  952. voice->Pan = T4D_DEFAULT_PCM_PAN;
  953. voice->Vol = 0;
  954. voice->EC = 0;
  955. voice->Alpha = 0;
  956. voice->FMS = 0;
  957. voice->Attribute = (2 << (30-16)) |
  958. (2 << (26-16)) |
  959. (2 << (24-16)) |
  960. (1 << (23-16));
  961. snd_trident_write_voice_regs(trident, voice);
  962. if (evoice != NULL) {
  963. evoice->Delta = snd_trident_convert_rate(runtime->rate);
  964. evoice->spurious_threshold = voice->spurious_threshold;
  965. evoice->LBA = voice->LBA;
  966. evoice->CSO = 0;
  967. evoice->ESO = (runtime->period_size * 2) + 20 - 1; /* in samples, 20 means correction */
  968. evoice->CTRL = voice->CTRL;
  969. evoice->FMC = 3;
  970. evoice->GVSel = 0;
  971. evoice->EC = 0;
  972. evoice->Alpha = 0;
  973. evoice->FMS = 0;
  974. evoice->Vol = 0x3ff; /* mute */
  975. evoice->RVol = evoice->CVol = 0x7f; /* mute */
  976. evoice->Pan = 0x7f; /* mute */
  977. evoice->Attribute = 0;
  978. snd_trident_write_voice_regs(trident, evoice);
  979. evoice->isync2 = 1;
  980. evoice->isync_mark = runtime->period_size;
  981. evoice->ESO = (runtime->period_size * 2) - 1;
  982. }
  983. spin_unlock_irq(&trident->reg_lock);
  984. return 0;
  985. }
  986. /*---------------------------------------------------------------------------
  987. snd_trident_foldback_prepare
  988. Description: Prepare foldback capture device for playback.
  989. Parameters: substream - PCM substream class
  990. Returns: Error status
  991. ---------------------------------------------------------------------------*/
  992. static int snd_trident_foldback_prepare(struct snd_pcm_substream *substream)
  993. {
  994. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  995. struct snd_pcm_runtime *runtime = substream->runtime;
  996. struct snd_trident_voice *voice = runtime->private_data;
  997. struct snd_trident_voice *evoice = voice->extra;
  998. spin_lock_irq(&trident->reg_lock);
  999. /* Set channel buffer Address */
  1000. if (voice->memblk)
  1001. voice->LBA = voice->memblk->offset;
  1002. else
  1003. voice->LBA = runtime->dma_addr;
  1004. /* set target ESO for channel */
  1005. voice->ESO = runtime->buffer_size - 1; /* in samples */
  1006. /* set sample rate */
  1007. voice->Delta = 0x1000;
  1008. voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
  1009. voice->CSO = 0;
  1010. voice->CTRL = snd_trident_control_mode(substream);
  1011. voice->FMC = 3;
  1012. voice->RVol = 0x7f;
  1013. voice->CVol = 0x7f;
  1014. voice->GVSel = 1;
  1015. voice->Pan = 0x7f; /* mute */
  1016. voice->Vol = 0x3ff; /* mute */
  1017. voice->EC = 0;
  1018. voice->Alpha = 0;
  1019. voice->FMS = 0;
  1020. voice->Attribute = 0;
  1021. /* set up capture channel */
  1022. outb(((voice->number & 0x3f) | 0x80), TRID_REG(trident, T4D_RCI + voice->foldback_chan));
  1023. snd_trident_write_voice_regs(trident, voice);
  1024. if (evoice != NULL) {
  1025. evoice->Delta = voice->Delta;
  1026. evoice->spurious_threshold = voice->spurious_threshold;
  1027. evoice->LBA = voice->LBA;
  1028. evoice->CSO = 0;
  1029. evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
  1030. evoice->CTRL = voice->CTRL;
  1031. evoice->FMC = 3;
  1032. evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
  1033. evoice->EC = 0;
  1034. evoice->Alpha = 0;
  1035. evoice->FMS = 0;
  1036. evoice->Vol = 0x3ff; /* mute */
  1037. evoice->RVol = evoice->CVol = 0x7f; /* mute */
  1038. evoice->Pan = 0x7f; /* mute */
  1039. evoice->Attribute = 0;
  1040. snd_trident_write_voice_regs(trident, evoice);
  1041. evoice->isync2 = 1;
  1042. evoice->isync_mark = runtime->period_size;
  1043. evoice->ESO = (runtime->period_size * 2) - 1;
  1044. }
  1045. spin_unlock_irq(&trident->reg_lock);
  1046. return 0;
  1047. }
  1048. /*---------------------------------------------------------------------------
  1049. snd_trident_spdif_hw_params
  1050. Description: Set the hardware parameters for the spdif device.
  1051. Parameters: substream - PCM substream class
  1052. hw_params - hardware parameters
  1053. Returns: Error status
  1054. ---------------------------------------------------------------------------*/
  1055. static int snd_trident_spdif_hw_params(struct snd_pcm_substream *substream,
  1056. struct snd_pcm_hw_params *hw_params)
  1057. {
  1058. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1059. unsigned int old_bits = 0, change = 0;
  1060. int err;
  1061. err = snd_trident_allocate_pcm_mem(substream, hw_params);
  1062. if (err < 0)
  1063. return err;
  1064. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  1065. err = snd_trident_allocate_evoice(substream, hw_params);
  1066. if (err < 0)
  1067. return err;
  1068. }
  1069. /* prepare SPDIF channel */
  1070. spin_lock_irq(&trident->reg_lock);
  1071. old_bits = trident->spdif_pcm_bits;
  1072. if (old_bits & IEC958_AES0_PROFESSIONAL)
  1073. trident->spdif_pcm_bits &= ~IEC958_AES0_PRO_FS;
  1074. else
  1075. trident->spdif_pcm_bits &= ~(IEC958_AES3_CON_FS << 24);
  1076. if (params_rate(hw_params) >= 48000) {
  1077. trident->spdif_pcm_ctrl = 0x3c; // 48000 Hz
  1078. trident->spdif_pcm_bits |=
  1079. trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
  1080. IEC958_AES0_PRO_FS_48000 :
  1081. (IEC958_AES3_CON_FS_48000 << 24);
  1082. }
  1083. else if (params_rate(hw_params) >= 44100) {
  1084. trident->spdif_pcm_ctrl = 0x3e; // 44100 Hz
  1085. trident->spdif_pcm_bits |=
  1086. trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
  1087. IEC958_AES0_PRO_FS_44100 :
  1088. (IEC958_AES3_CON_FS_44100 << 24);
  1089. }
  1090. else {
  1091. trident->spdif_pcm_ctrl = 0x3d; // 32000 Hz
  1092. trident->spdif_pcm_bits |=
  1093. trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
  1094. IEC958_AES0_PRO_FS_32000 :
  1095. (IEC958_AES3_CON_FS_32000 << 24);
  1096. }
  1097. change = old_bits != trident->spdif_pcm_bits;
  1098. spin_unlock_irq(&trident->reg_lock);
  1099. if (change)
  1100. snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE, &trident->spdif_pcm_ctl->id);
  1101. return 0;
  1102. }
  1103. /*---------------------------------------------------------------------------
  1104. snd_trident_spdif_prepare
  1105. Description: Prepare SPDIF device for playback.
  1106. Parameters: substream - PCM substream class
  1107. Returns: Error status
  1108. ---------------------------------------------------------------------------*/
  1109. static int snd_trident_spdif_prepare(struct snd_pcm_substream *substream)
  1110. {
  1111. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1112. struct snd_pcm_runtime *runtime = substream->runtime;
  1113. struct snd_trident_voice *voice = runtime->private_data;
  1114. struct snd_trident_voice *evoice = voice->extra;
  1115. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
  1116. unsigned int RESO, LBAO;
  1117. unsigned int temp;
  1118. spin_lock_irq(&trident->reg_lock);
  1119. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1120. /* set delta (rate) value */
  1121. voice->Delta = snd_trident_convert_rate(runtime->rate);
  1122. voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  1123. /* set Loop Back Address */
  1124. LBAO = runtime->dma_addr;
  1125. if (voice->memblk)
  1126. voice->LBA = voice->memblk->offset;
  1127. else
  1128. voice->LBA = LBAO;
  1129. voice->isync = 1;
  1130. voice->isync3 = 1;
  1131. voice->isync_mark = runtime->period_size;
  1132. voice->isync_max = runtime->buffer_size;
  1133. /* set target ESO for channel */
  1134. RESO = runtime->buffer_size - 1;
  1135. voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
  1136. /* set ctrl mode */
  1137. voice->CTRL = snd_trident_control_mode(substream);
  1138. voice->FMC = 3;
  1139. voice->RVol = 0x7f;
  1140. voice->CVol = 0x7f;
  1141. voice->GVSel = 1;
  1142. voice->Pan = 0x7f;
  1143. voice->Vol = 0x3ff;
  1144. voice->EC = 0;
  1145. voice->CSO = 0;
  1146. voice->Alpha = 0;
  1147. voice->FMS = 0;
  1148. voice->Attribute = 0;
  1149. /* prepare surrogate IRQ channel */
  1150. snd_trident_write_voice_regs(trident, voice);
  1151. outw((RESO & 0xffff), TRID_REG(trident, NX_SPESO));
  1152. outb((RESO >> 16), TRID_REG(trident, NX_SPESO + 2));
  1153. outl((LBAO & 0xfffffffc), TRID_REG(trident, NX_SPLBA));
  1154. outw((voice->CSO & 0xffff), TRID_REG(trident, NX_SPCTRL_SPCSO));
  1155. outb((voice->CSO >> 16), TRID_REG(trident, NX_SPCTRL_SPCSO + 2));
  1156. /* set SPDIF setting */
  1157. outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1158. outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  1159. } else { /* SiS */
  1160. /* set delta (rate) value */
  1161. voice->Delta = 0x800;
  1162. voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
  1163. /* set Loop Begin Address */
  1164. if (voice->memblk)
  1165. voice->LBA = voice->memblk->offset;
  1166. else
  1167. voice->LBA = runtime->dma_addr;
  1168. voice->CSO = 0;
  1169. voice->ESO = runtime->buffer_size - 1; /* in samples */
  1170. voice->CTRL = snd_trident_control_mode(substream);
  1171. voice->FMC = 3;
  1172. voice->GVSel = 1;
  1173. voice->EC = 0;
  1174. voice->Alpha = 0;
  1175. voice->FMS = 0;
  1176. voice->Vol = mix->vol;
  1177. voice->RVol = mix->rvol;
  1178. voice->CVol = mix->cvol;
  1179. voice->Pan = mix->pan;
  1180. voice->Attribute = (1<<(30-16))|(7<<(26-16))|
  1181. (0<<(24-16))|(0<<(19-16));
  1182. snd_trident_write_voice_regs(trident, voice);
  1183. if (evoice != NULL) {
  1184. evoice->Delta = voice->Delta;
  1185. evoice->spurious_threshold = voice->spurious_threshold;
  1186. evoice->LBA = voice->LBA;
  1187. evoice->CSO = 0;
  1188. evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
  1189. evoice->CTRL = voice->CTRL;
  1190. evoice->FMC = 3;
  1191. evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
  1192. evoice->EC = 0;
  1193. evoice->Alpha = 0;
  1194. evoice->FMS = 0;
  1195. evoice->Vol = 0x3ff; /* mute */
  1196. evoice->RVol = evoice->CVol = 0x7f; /* mute */
  1197. evoice->Pan = 0x7f; /* mute */
  1198. evoice->Attribute = 0;
  1199. snd_trident_write_voice_regs(trident, evoice);
  1200. evoice->isync2 = 1;
  1201. evoice->isync_mark = runtime->period_size;
  1202. evoice->ESO = (runtime->period_size * 2) - 1;
  1203. }
  1204. outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
  1205. temp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
  1206. temp &= ~(1<<19);
  1207. outl(temp, TRID_REG(trident, T4D_LFO_GC_CIR));
  1208. temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1209. temp |= SPDIF_EN;
  1210. outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1211. }
  1212. spin_unlock_irq(&trident->reg_lock);
  1213. return 0;
  1214. }
  1215. /*---------------------------------------------------------------------------
  1216. snd_trident_trigger
  1217. Description: Start/stop devices
  1218. Parameters: substream - PCM substream class
  1219. cmd - trigger command (STOP, GO)
  1220. Returns: Error status
  1221. ---------------------------------------------------------------------------*/
  1222. static int snd_trident_trigger(struct snd_pcm_substream *substream,
  1223. int cmd)
  1224. {
  1225. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1226. struct list_head *pos;
  1227. struct snd_pcm_substream *s;
  1228. unsigned int what, whati, capture_flag, spdif_flag;
  1229. struct snd_trident_voice *voice, *evoice;
  1230. unsigned int val, go;
  1231. switch (cmd) {
  1232. case SNDRV_PCM_TRIGGER_START:
  1233. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1234. case SNDRV_PCM_TRIGGER_RESUME:
  1235. go = 1;
  1236. break;
  1237. case SNDRV_PCM_TRIGGER_STOP:
  1238. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1239. case SNDRV_PCM_TRIGGER_SUSPEND:
  1240. go = 0;
  1241. break;
  1242. default:
  1243. return -EINVAL;
  1244. }
  1245. what = whati = capture_flag = spdif_flag = 0;
  1246. spin_lock(&trident->reg_lock);
  1247. val = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
  1248. snd_pcm_group_for_each(pos, substream) {
  1249. s = snd_pcm_group_substream_entry(pos);
  1250. if ((struct snd_trident *) snd_pcm_substream_chip(s) == trident) {
  1251. voice = s->runtime->private_data;
  1252. evoice = voice->extra;
  1253. what |= 1 << (voice->number & 0x1f);
  1254. if (evoice == NULL) {
  1255. whati |= 1 << (voice->number & 0x1f);
  1256. } else {
  1257. what |= 1 << (evoice->number & 0x1f);
  1258. whati |= 1 << (evoice->number & 0x1f);
  1259. if (go)
  1260. evoice->stimer = val;
  1261. }
  1262. if (go) {
  1263. voice->running = 1;
  1264. voice->stimer = val;
  1265. } else {
  1266. voice->running = 0;
  1267. }
  1268. snd_pcm_trigger_done(s, substream);
  1269. if (voice->capture)
  1270. capture_flag = 1;
  1271. if (voice->spdif)
  1272. spdif_flag = 1;
  1273. }
  1274. }
  1275. if (spdif_flag) {
  1276. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1277. outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  1278. outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1279. } else {
  1280. outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
  1281. val = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) | SPDIF_EN;
  1282. outl(val, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1283. }
  1284. }
  1285. if (!go)
  1286. outl(what, TRID_REG(trident, T4D_STOP_B));
  1287. val = inl(TRID_REG(trident, T4D_AINTEN_B));
  1288. if (go) {
  1289. val |= whati;
  1290. } else {
  1291. val &= ~whati;
  1292. }
  1293. outl(val, TRID_REG(trident, T4D_AINTEN_B));
  1294. if (go) {
  1295. outl(what, TRID_REG(trident, T4D_START_B));
  1296. if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
  1297. outb(trident->bDMAStart, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
  1298. } else {
  1299. if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
  1300. outb(0x00, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
  1301. }
  1302. spin_unlock(&trident->reg_lock);
  1303. return 0;
  1304. }
  1305. /*---------------------------------------------------------------------------
  1306. snd_trident_playback_pointer
  1307. Description: This routine return the playback position
  1308. Parameters: substream - PCM substream class
  1309. Returns: position of buffer
  1310. ---------------------------------------------------------------------------*/
  1311. static snd_pcm_uframes_t snd_trident_playback_pointer(struct snd_pcm_substream *substream)
  1312. {
  1313. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1314. struct snd_pcm_runtime *runtime = substream->runtime;
  1315. struct snd_trident_voice *voice = runtime->private_data;
  1316. unsigned int cso;
  1317. if (!voice->running)
  1318. return 0;
  1319. spin_lock(&trident->reg_lock);
  1320. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  1321. if (trident->device != TRIDENT_DEVICE_ID_NX) {
  1322. cso = inw(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
  1323. } else { // ID_4DWAVE_NX
  1324. cso = (unsigned int) inl(TRID_REG(trident, CH_NX_DELTA_CSO)) & 0x00ffffff;
  1325. }
  1326. spin_unlock(&trident->reg_lock);
  1327. if (cso >= runtime->buffer_size)
  1328. cso = 0;
  1329. return cso;
  1330. }
  1331. /*---------------------------------------------------------------------------
  1332. snd_trident_capture_pointer
  1333. Description: This routine return the capture position
  1334. Paramters: pcm1 - PCM device class
  1335. Returns: position of buffer
  1336. ---------------------------------------------------------------------------*/
  1337. static snd_pcm_uframes_t snd_trident_capture_pointer(struct snd_pcm_substream *substream)
  1338. {
  1339. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1340. struct snd_pcm_runtime *runtime = substream->runtime;
  1341. struct snd_trident_voice *voice = runtime->private_data;
  1342. unsigned int result;
  1343. if (!voice->running)
  1344. return 0;
  1345. result = inw(TRID_REG(trident, T4D_SBBL_SBCL));
  1346. if (runtime->channels > 1)
  1347. result >>= 1;
  1348. if (result > 0)
  1349. result = runtime->buffer_size - result;
  1350. return result;
  1351. }
  1352. /*---------------------------------------------------------------------------
  1353. snd_trident_spdif_pointer
  1354. Description: This routine return the SPDIF playback position
  1355. Parameters: substream - PCM substream class
  1356. Returns: position of buffer
  1357. ---------------------------------------------------------------------------*/
  1358. static snd_pcm_uframes_t snd_trident_spdif_pointer(struct snd_pcm_substream *substream)
  1359. {
  1360. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1361. struct snd_pcm_runtime *runtime = substream->runtime;
  1362. struct snd_trident_voice *voice = runtime->private_data;
  1363. unsigned int result;
  1364. if (!voice->running)
  1365. return 0;
  1366. result = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
  1367. return result;
  1368. }
  1369. /*
  1370. * Playback support device description
  1371. */
  1372. static struct snd_pcm_hardware snd_trident_playback =
  1373. {
  1374. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1375. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1376. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1377. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1378. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1379. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1380. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1381. .rate_min = 4000,
  1382. .rate_max = 48000,
  1383. .channels_min = 1,
  1384. .channels_max = 2,
  1385. .buffer_bytes_max = (256*1024),
  1386. .period_bytes_min = 64,
  1387. .period_bytes_max = (256*1024),
  1388. .periods_min = 1,
  1389. .periods_max = 1024,
  1390. .fifo_size = 0,
  1391. };
  1392. /*
  1393. * Capture support device description
  1394. */
  1395. static struct snd_pcm_hardware snd_trident_capture =
  1396. {
  1397. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1398. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1399. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1400. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1401. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1402. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1403. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1404. .rate_min = 4000,
  1405. .rate_max = 48000,
  1406. .channels_min = 1,
  1407. .channels_max = 2,
  1408. .buffer_bytes_max = (128*1024),
  1409. .period_bytes_min = 64,
  1410. .period_bytes_max = (128*1024),
  1411. .periods_min = 1,
  1412. .periods_max = 1024,
  1413. .fifo_size = 0,
  1414. };
  1415. /*
  1416. * Foldback capture support device description
  1417. */
  1418. static struct snd_pcm_hardware snd_trident_foldback =
  1419. {
  1420. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1421. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1422. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1423. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1424. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1425. .rates = SNDRV_PCM_RATE_48000,
  1426. .rate_min = 48000,
  1427. .rate_max = 48000,
  1428. .channels_min = 2,
  1429. .channels_max = 2,
  1430. .buffer_bytes_max = (128*1024),
  1431. .period_bytes_min = 64,
  1432. .period_bytes_max = (128*1024),
  1433. .periods_min = 1,
  1434. .periods_max = 1024,
  1435. .fifo_size = 0,
  1436. };
  1437. /*
  1438. * SPDIF playback support device description
  1439. */
  1440. static struct snd_pcm_hardware snd_trident_spdif =
  1441. {
  1442. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1443. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1444. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1445. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1446. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1447. .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  1448. SNDRV_PCM_RATE_48000),
  1449. .rate_min = 32000,
  1450. .rate_max = 48000,
  1451. .channels_min = 2,
  1452. .channels_max = 2,
  1453. .buffer_bytes_max = (128*1024),
  1454. .period_bytes_min = 64,
  1455. .period_bytes_max = (128*1024),
  1456. .periods_min = 1,
  1457. .periods_max = 1024,
  1458. .fifo_size = 0,
  1459. };
  1460. static struct snd_pcm_hardware snd_trident_spdif_7018 =
  1461. {
  1462. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1463. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1464. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1465. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1466. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1467. .rates = SNDRV_PCM_RATE_48000,
  1468. .rate_min = 48000,
  1469. .rate_max = 48000,
  1470. .channels_min = 2,
  1471. .channels_max = 2,
  1472. .buffer_bytes_max = (128*1024),
  1473. .period_bytes_min = 64,
  1474. .period_bytes_max = (128*1024),
  1475. .periods_min = 1,
  1476. .periods_max = 1024,
  1477. .fifo_size = 0,
  1478. };
  1479. static void snd_trident_pcm_free_substream(struct snd_pcm_runtime *runtime)
  1480. {
  1481. struct snd_trident_voice *voice = runtime->private_data;
  1482. struct snd_trident *trident;
  1483. if (voice) {
  1484. trident = voice->trident;
  1485. snd_trident_free_voice(trident, voice);
  1486. }
  1487. }
  1488. static int snd_trident_playback_open(struct snd_pcm_substream *substream)
  1489. {
  1490. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1491. struct snd_pcm_runtime *runtime = substream->runtime;
  1492. struct snd_trident_voice *voice;
  1493. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1494. if (voice == NULL)
  1495. return -EAGAIN;
  1496. snd_trident_pcm_mixer_build(trident, voice, substream);
  1497. voice->substream = substream;
  1498. runtime->private_data = voice;
  1499. runtime->private_free = snd_trident_pcm_free_substream;
  1500. runtime->hw = snd_trident_playback;
  1501. snd_pcm_set_sync(substream);
  1502. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1503. return 0;
  1504. }
  1505. /*---------------------------------------------------------------------------
  1506. snd_trident_playback_close
  1507. Description: This routine will close the 4DWave playback device. For now
  1508. we will simply free the dma transfer buffer.
  1509. Parameters: substream - PCM substream class
  1510. ---------------------------------------------------------------------------*/
  1511. static int snd_trident_playback_close(struct snd_pcm_substream *substream)
  1512. {
  1513. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1514. struct snd_pcm_runtime *runtime = substream->runtime;
  1515. struct snd_trident_voice *voice = runtime->private_data;
  1516. snd_trident_pcm_mixer_free(trident, voice, substream);
  1517. return 0;
  1518. }
  1519. /*---------------------------------------------------------------------------
  1520. snd_trident_spdif_open
  1521. Description: This routine will open the 4DWave SPDIF device.
  1522. Parameters: substream - PCM substream class
  1523. Returns: status - success or failure flag
  1524. ---------------------------------------------------------------------------*/
  1525. static int snd_trident_spdif_open(struct snd_pcm_substream *substream)
  1526. {
  1527. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1528. struct snd_trident_voice *voice;
  1529. struct snd_pcm_runtime *runtime = substream->runtime;
  1530. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1531. if (voice == NULL)
  1532. return -EAGAIN;
  1533. voice->spdif = 1;
  1534. voice->substream = substream;
  1535. spin_lock_irq(&trident->reg_lock);
  1536. trident->spdif_pcm_bits = trident->spdif_bits;
  1537. spin_unlock_irq(&trident->reg_lock);
  1538. runtime->private_data = voice;
  1539. runtime->private_free = snd_trident_pcm_free_substream;
  1540. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  1541. runtime->hw = snd_trident_spdif;
  1542. } else {
  1543. runtime->hw = snd_trident_spdif_7018;
  1544. }
  1545. trident->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1546. snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1547. SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
  1548. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1549. return 0;
  1550. }
  1551. /*---------------------------------------------------------------------------
  1552. snd_trident_spdif_close
  1553. Description: This routine will close the 4DWave SPDIF device.
  1554. Parameters: substream - PCM substream class
  1555. ---------------------------------------------------------------------------*/
  1556. static int snd_trident_spdif_close(struct snd_pcm_substream *substream)
  1557. {
  1558. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1559. unsigned int temp;
  1560. spin_lock_irq(&trident->reg_lock);
  1561. // restore default SPDIF setting
  1562. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1563. outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1564. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  1565. } else {
  1566. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1567. temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1568. if (trident->spdif_ctrl) {
  1569. temp |= SPDIF_EN;
  1570. } else {
  1571. temp &= ~SPDIF_EN;
  1572. }
  1573. outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1574. }
  1575. spin_unlock_irq(&trident->reg_lock);
  1576. trident->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1577. snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1578. SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
  1579. return 0;
  1580. }
  1581. /*---------------------------------------------------------------------------
  1582. snd_trident_capture_open
  1583. Description: This routine will open the 4DWave capture device.
  1584. Parameters: substream - PCM substream class
  1585. Returns: status - success or failure flag
  1586. ---------------------------------------------------------------------------*/
  1587. static int snd_trident_capture_open(struct snd_pcm_substream *substream)
  1588. {
  1589. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1590. struct snd_trident_voice *voice;
  1591. struct snd_pcm_runtime *runtime = substream->runtime;
  1592. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1593. if (voice == NULL)
  1594. return -EAGAIN;
  1595. voice->capture = 1;
  1596. voice->substream = substream;
  1597. runtime->private_data = voice;
  1598. runtime->private_free = snd_trident_pcm_free_substream;
  1599. runtime->hw = snd_trident_capture;
  1600. snd_pcm_set_sync(substream);
  1601. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1602. return 0;
  1603. }
  1604. /*---------------------------------------------------------------------------
  1605. snd_trident_capture_close
  1606. Description: This routine will close the 4DWave capture device. For now
  1607. we will simply free the dma transfer buffer.
  1608. Parameters: substream - PCM substream class
  1609. ---------------------------------------------------------------------------*/
  1610. static int snd_trident_capture_close(struct snd_pcm_substream *substream)
  1611. {
  1612. return 0;
  1613. }
  1614. /*---------------------------------------------------------------------------
  1615. snd_trident_foldback_open
  1616. Description: This routine will open the 4DWave foldback capture device.
  1617. Parameters: substream - PCM substream class
  1618. Returns: status - success or failure flag
  1619. ---------------------------------------------------------------------------*/
  1620. static int snd_trident_foldback_open(struct snd_pcm_substream *substream)
  1621. {
  1622. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1623. struct snd_trident_voice *voice;
  1624. struct snd_pcm_runtime *runtime = substream->runtime;
  1625. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1626. if (voice == NULL)
  1627. return -EAGAIN;
  1628. voice->foldback_chan = substream->number;
  1629. voice->substream = substream;
  1630. runtime->private_data = voice;
  1631. runtime->private_free = snd_trident_pcm_free_substream;
  1632. runtime->hw = snd_trident_foldback;
  1633. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1634. return 0;
  1635. }
  1636. /*---------------------------------------------------------------------------
  1637. snd_trident_foldback_close
  1638. Description: This routine will close the 4DWave foldback capture device.
  1639. For now we will simply free the dma transfer buffer.
  1640. Parameters: substream - PCM substream class
  1641. ---------------------------------------------------------------------------*/
  1642. static int snd_trident_foldback_close(struct snd_pcm_substream *substream)
  1643. {
  1644. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1645. struct snd_trident_voice *voice;
  1646. struct snd_pcm_runtime *runtime = substream->runtime;
  1647. voice = runtime->private_data;
  1648. /* stop capture channel */
  1649. spin_lock_irq(&trident->reg_lock);
  1650. outb(0x00, TRID_REG(trident, T4D_RCI + voice->foldback_chan));
  1651. spin_unlock_irq(&trident->reg_lock);
  1652. return 0;
  1653. }
  1654. /*---------------------------------------------------------------------------
  1655. PCM operations
  1656. ---------------------------------------------------------------------------*/
  1657. static struct snd_pcm_ops snd_trident_playback_ops = {
  1658. .open = snd_trident_playback_open,
  1659. .close = snd_trident_playback_close,
  1660. .ioctl = snd_trident_ioctl,
  1661. .hw_params = snd_trident_hw_params,
  1662. .hw_free = snd_trident_hw_free,
  1663. .prepare = snd_trident_playback_prepare,
  1664. .trigger = snd_trident_trigger,
  1665. .pointer = snd_trident_playback_pointer,
  1666. };
  1667. static struct snd_pcm_ops snd_trident_nx_playback_ops = {
  1668. .open = snd_trident_playback_open,
  1669. .close = snd_trident_playback_close,
  1670. .ioctl = snd_trident_ioctl,
  1671. .hw_params = snd_trident_hw_params,
  1672. .hw_free = snd_trident_hw_free,
  1673. .prepare = snd_trident_playback_prepare,
  1674. .trigger = snd_trident_trigger,
  1675. .pointer = snd_trident_playback_pointer,
  1676. .page = snd_pcm_sgbuf_ops_page,
  1677. };
  1678. static struct snd_pcm_ops snd_trident_capture_ops = {
  1679. .open = snd_trident_capture_open,
  1680. .close = snd_trident_capture_close,
  1681. .ioctl = snd_trident_ioctl,
  1682. .hw_params = snd_trident_capture_hw_params,
  1683. .hw_free = snd_trident_hw_free,
  1684. .prepare = snd_trident_capture_prepare,
  1685. .trigger = snd_trident_trigger,
  1686. .pointer = snd_trident_capture_pointer,
  1687. };
  1688. static struct snd_pcm_ops snd_trident_si7018_capture_ops = {
  1689. .open = snd_trident_capture_open,
  1690. .close = snd_trident_capture_close,
  1691. .ioctl = snd_trident_ioctl,
  1692. .hw_params = snd_trident_si7018_capture_hw_params,
  1693. .hw_free = snd_trident_si7018_capture_hw_free,
  1694. .prepare = snd_trident_si7018_capture_prepare,
  1695. .trigger = snd_trident_trigger,
  1696. .pointer = snd_trident_playback_pointer,
  1697. };
  1698. static struct snd_pcm_ops snd_trident_foldback_ops = {
  1699. .open = snd_trident_foldback_open,
  1700. .close = snd_trident_foldback_close,
  1701. .ioctl = snd_trident_ioctl,
  1702. .hw_params = snd_trident_hw_params,
  1703. .hw_free = snd_trident_hw_free,
  1704. .prepare = snd_trident_foldback_prepare,
  1705. .trigger = snd_trident_trigger,
  1706. .pointer = snd_trident_playback_pointer,
  1707. };
  1708. static struct snd_pcm_ops snd_trident_nx_foldback_ops = {
  1709. .open = snd_trident_foldback_open,
  1710. .close = snd_trident_foldback_close,
  1711. .ioctl = snd_trident_ioctl,
  1712. .hw_params = snd_trident_hw_params,
  1713. .hw_free = snd_trident_hw_free,
  1714. .prepare = snd_trident_foldback_prepare,
  1715. .trigger = snd_trident_trigger,
  1716. .pointer = snd_trident_playback_pointer,
  1717. .page = snd_pcm_sgbuf_ops_page,
  1718. };
  1719. static struct snd_pcm_ops snd_trident_spdif_ops = {
  1720. .open = snd_trident_spdif_open,
  1721. .close = snd_trident_spdif_close,
  1722. .ioctl = snd_trident_ioctl,
  1723. .hw_params = snd_trident_spdif_hw_params,
  1724. .hw_free = snd_trident_hw_free,
  1725. .prepare = snd_trident_spdif_prepare,
  1726. .trigger = snd_trident_trigger,
  1727. .pointer = snd_trident_spdif_pointer,
  1728. };
  1729. static struct snd_pcm_ops snd_trident_spdif_7018_ops = {
  1730. .open = snd_trident_spdif_open,
  1731. .close = snd_trident_spdif_close,
  1732. .ioctl = snd_trident_ioctl,
  1733. .hw_params = snd_trident_spdif_hw_params,
  1734. .hw_free = snd_trident_hw_free,
  1735. .prepare = snd_trident_spdif_prepare,
  1736. .trigger = snd_trident_trigger,
  1737. .pointer = snd_trident_playback_pointer,
  1738. };
  1739. /*---------------------------------------------------------------------------
  1740. snd_trident_pcm
  1741. Description: This routine registers the 4DWave device for PCM support.
  1742. Paramters: trident - pointer to target device class for 4DWave.
  1743. Returns: None
  1744. ---------------------------------------------------------------------------*/
  1745. int __devinit snd_trident_pcm(struct snd_trident * trident,
  1746. int device, struct snd_pcm ** rpcm)
  1747. {
  1748. struct snd_pcm *pcm;
  1749. int err;
  1750. if (rpcm)
  1751. *rpcm = NULL;
  1752. if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, trident->ChanPCM, 1, &pcm)) < 0)
  1753. return err;
  1754. pcm->private_data = trident;
  1755. if (trident->tlb.entries) {
  1756. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_nx_playback_ops);
  1757. } else {
  1758. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_playback_ops);
  1759. }
  1760. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1761. trident->device != TRIDENT_DEVICE_ID_SI7018 ?
  1762. &snd_trident_capture_ops :
  1763. &snd_trident_si7018_capture_ops);
  1764. pcm->info_flags = 0;
  1765. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1766. strcpy(pcm->name, "Trident 4DWave");
  1767. trident->pcm = pcm;
  1768. if (trident->tlb.entries) {
  1769. struct snd_pcm_substream *substream;
  1770. for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
  1771. snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
  1772. snd_dma_pci_data(trident->pci),
  1773. 64*1024, 128*1024);
  1774. snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  1775. SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
  1776. 64*1024, 128*1024);
  1777. } else {
  1778. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1779. snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
  1780. }
  1781. if (rpcm)
  1782. *rpcm = pcm;
  1783. return 0;
  1784. }
  1785. /*---------------------------------------------------------------------------
  1786. snd_trident_foldback_pcm
  1787. Description: This routine registers the 4DWave device for foldback PCM support.
  1788. Paramters: trident - pointer to target device class for 4DWave.
  1789. Returns: None
  1790. ---------------------------------------------------------------------------*/
  1791. int __devinit snd_trident_foldback_pcm(struct snd_trident * trident,
  1792. int device, struct snd_pcm ** rpcm)
  1793. {
  1794. struct snd_pcm *foldback;
  1795. int err;
  1796. int num_chan = 3;
  1797. struct snd_pcm_substream *substream;
  1798. if (rpcm)
  1799. *rpcm = NULL;
  1800. if (trident->device == TRIDENT_DEVICE_ID_NX)
  1801. num_chan = 4;
  1802. if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, 0, num_chan, &foldback)) < 0)
  1803. return err;
  1804. foldback->private_data = trident;
  1805. if (trident->tlb.entries)
  1806. snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_nx_foldback_ops);
  1807. else
  1808. snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_foldback_ops);
  1809. foldback->info_flags = 0;
  1810. strcpy(foldback->name, "Trident 4DWave");
  1811. substream = foldback->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1812. strcpy(substream->name, "Front Mixer");
  1813. substream = substream->next;
  1814. strcpy(substream->name, "Reverb Mixer");
  1815. substream = substream->next;
  1816. strcpy(substream->name, "Chorus Mixer");
  1817. if (num_chan == 4) {
  1818. substream = substream->next;
  1819. strcpy(substream->name, "Second AC'97 ADC");
  1820. }
  1821. trident->foldback = foldback;
  1822. if (trident->tlb.entries)
  1823. snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV_SG,
  1824. snd_dma_pci_data(trident->pci), 0, 128*1024);
  1825. else
  1826. snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV,
  1827. snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
  1828. if (rpcm)
  1829. *rpcm = foldback;
  1830. return 0;
  1831. }
  1832. /*---------------------------------------------------------------------------
  1833. snd_trident_spdif
  1834. Description: This routine registers the 4DWave-NX device for SPDIF support.
  1835. Paramters: trident - pointer to target device class for 4DWave-NX.
  1836. Returns: None
  1837. ---------------------------------------------------------------------------*/
  1838. int __devinit snd_trident_spdif_pcm(struct snd_trident * trident,
  1839. int device, struct snd_pcm ** rpcm)
  1840. {
  1841. struct snd_pcm *spdif;
  1842. int err;
  1843. if (rpcm)
  1844. *rpcm = NULL;
  1845. if ((err = snd_pcm_new(trident->card, "trident_dx_nx IEC958", device, 1, 0, &spdif)) < 0)
  1846. return err;
  1847. spdif->private_data = trident;
  1848. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1849. snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_ops);
  1850. } else {
  1851. snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_7018_ops);
  1852. }
  1853. spdif->info_flags = 0;
  1854. strcpy(spdif->name, "Trident 4DWave IEC958");
  1855. trident->spdif = spdif;
  1856. snd_pcm_lib_preallocate_pages_for_all(spdif, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
  1857. if (rpcm)
  1858. *rpcm = spdif;
  1859. return 0;
  1860. }
  1861. /*
  1862. * Mixer part
  1863. */
  1864. /*---------------------------------------------------------------------------
  1865. snd_trident_spdif_control
  1866. Description: enable/disable S/PDIF out from ac97 mixer
  1867. ---------------------------------------------------------------------------*/
  1868. static int snd_trident_spdif_control_info(struct snd_kcontrol *kcontrol,
  1869. struct snd_ctl_elem_info *uinfo)
  1870. {
  1871. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1872. uinfo->count = 1;
  1873. uinfo->value.integer.min = 0;
  1874. uinfo->value.integer.max = 1;
  1875. return 0;
  1876. }
  1877. static int snd_trident_spdif_control_get(struct snd_kcontrol *kcontrol,
  1878. struct snd_ctl_elem_value *ucontrol)
  1879. {
  1880. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1881. unsigned char val;
  1882. spin_lock_irq(&trident->reg_lock);
  1883. val = trident->spdif_ctrl;
  1884. ucontrol->value.integer.value[0] = val == kcontrol->private_value;
  1885. spin_unlock_irq(&trident->reg_lock);
  1886. return 0;
  1887. }
  1888. static int snd_trident_spdif_control_put(struct snd_kcontrol *kcontrol,
  1889. struct snd_ctl_elem_value *ucontrol)
  1890. {
  1891. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1892. unsigned char val;
  1893. int change;
  1894. val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
  1895. spin_lock_irq(&trident->reg_lock);
  1896. /* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
  1897. change = trident->spdif_ctrl != val;
  1898. trident->spdif_ctrl = val;
  1899. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1900. if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
  1901. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  1902. outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1903. }
  1904. } else {
  1905. if (trident->spdif == NULL) {
  1906. unsigned int temp;
  1907. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1908. temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
  1909. if (val)
  1910. temp |= SPDIF_EN;
  1911. outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1912. }
  1913. }
  1914. spin_unlock_irq(&trident->reg_lock);
  1915. return change;
  1916. }
  1917. static struct snd_kcontrol_new snd_trident_spdif_control __devinitdata =
  1918. {
  1919. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1920. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  1921. .info = snd_trident_spdif_control_info,
  1922. .get = snd_trident_spdif_control_get,
  1923. .put = snd_trident_spdif_control_put,
  1924. .private_value = 0x28,
  1925. };
  1926. /*---------------------------------------------------------------------------
  1927. snd_trident_spdif_default
  1928. Description: put/get the S/PDIF default settings
  1929. ---------------------------------------------------------------------------*/
  1930. static int snd_trident_spdif_default_info(struct snd_kcontrol *kcontrol,
  1931. struct snd_ctl_elem_info *uinfo)
  1932. {
  1933. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1934. uinfo->count = 1;
  1935. return 0;
  1936. }
  1937. static int snd_trident_spdif_default_get(struct snd_kcontrol *kcontrol,
  1938. struct snd_ctl_elem_value *ucontrol)
  1939. {
  1940. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1941. spin_lock_irq(&trident->reg_lock);
  1942. ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
  1943. ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
  1944. ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
  1945. ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
  1946. spin_unlock_irq(&trident->reg_lock);
  1947. return 0;
  1948. }
  1949. static int snd_trident_spdif_default_put(struct snd_kcontrol *kcontrol,
  1950. struct snd_ctl_elem_value *ucontrol)
  1951. {
  1952. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1953. unsigned int val;
  1954. int change;
  1955. val = (ucontrol->value.iec958.status[0] << 0) |
  1956. (ucontrol->value.iec958.status[1] << 8) |
  1957. (ucontrol->value.iec958.status[2] << 16) |
  1958. (ucontrol->value.iec958.status[3] << 24);
  1959. spin_lock_irq(&trident->reg_lock);
  1960. change = trident->spdif_bits != val;
  1961. trident->spdif_bits = val;
  1962. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1963. if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
  1964. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  1965. } else {
  1966. if (trident->spdif == NULL)
  1967. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1968. }
  1969. spin_unlock_irq(&trident->reg_lock);
  1970. return change;
  1971. }
  1972. static struct snd_kcontrol_new snd_trident_spdif_default __devinitdata =
  1973. {
  1974. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1975. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1976. .info = snd_trident_spdif_default_info,
  1977. .get = snd_trident_spdif_default_get,
  1978. .put = snd_trident_spdif_default_put
  1979. };
  1980. /*---------------------------------------------------------------------------
  1981. snd_trident_spdif_mask
  1982. Description: put/get the S/PDIF mask
  1983. ---------------------------------------------------------------------------*/
  1984. static int snd_trident_spdif_mask_info(struct snd_kcontrol *kcontrol,
  1985. struct snd_ctl_elem_info *uinfo)
  1986. {
  1987. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1988. uinfo->count = 1;
  1989. return 0;
  1990. }
  1991. static int snd_trident_spdif_mask_get(struct snd_kcontrol *kcontrol,
  1992. struct snd_ctl_elem_value *ucontrol)
  1993. {
  1994. ucontrol->value.iec958.status[0] = 0xff;
  1995. ucontrol->value.iec958.status[1] = 0xff;
  1996. ucontrol->value.iec958.status[2] = 0xff;
  1997. ucontrol->value.iec958.status[3] = 0xff;
  1998. return 0;
  1999. }
  2000. static struct snd_kcontrol_new snd_trident_spdif_mask __devinitdata =
  2001. {
  2002. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2003. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2004. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  2005. .info = snd_trident_spdif_mask_info,
  2006. .get = snd_trident_spdif_mask_get,
  2007. };
  2008. /*---------------------------------------------------------------------------
  2009. snd_trident_spdif_stream
  2010. Description: put/get the S/PDIF stream settings
  2011. ---------------------------------------------------------------------------*/
  2012. static int snd_trident_spdif_stream_info(struct snd_kcontrol *kcontrol,
  2013. struct snd_ctl_elem_info *uinfo)
  2014. {
  2015. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  2016. uinfo->count = 1;
  2017. return 0;
  2018. }
  2019. static int snd_trident_spdif_stream_get(struct snd_kcontrol *kcontrol,
  2020. struct snd_ctl_elem_value *ucontrol)
  2021. {
  2022. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2023. spin_lock_irq(&trident->reg_lock);
  2024. ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
  2025. ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
  2026. ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
  2027. ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
  2028. spin_unlock_irq(&trident->reg_lock);
  2029. return 0;
  2030. }
  2031. static int snd_trident_spdif_stream_put(struct snd_kcontrol *kcontrol,
  2032. struct snd_ctl_elem_value *ucontrol)
  2033. {
  2034. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2035. unsigned int val;
  2036. int change;
  2037. val = (ucontrol->value.iec958.status[0] << 0) |
  2038. (ucontrol->value.iec958.status[1] << 8) |
  2039. (ucontrol->value.iec958.status[2] << 16) |
  2040. (ucontrol->value.iec958.status[3] << 24);
  2041. spin_lock_irq(&trident->reg_lock);
  2042. change = trident->spdif_pcm_bits != val;
  2043. trident->spdif_pcm_bits = val;
  2044. if (trident->spdif != NULL) {
  2045. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  2046. outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  2047. } else {
  2048. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  2049. }
  2050. }
  2051. spin_unlock_irq(&trident->reg_lock);
  2052. return change;
  2053. }
  2054. static struct snd_kcontrol_new snd_trident_spdif_stream __devinitdata =
  2055. {
  2056. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2057. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2058. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  2059. .info = snd_trident_spdif_stream_info,
  2060. .get = snd_trident_spdif_stream_get,
  2061. .put = snd_trident_spdif_stream_put
  2062. };
  2063. /*---------------------------------------------------------------------------
  2064. snd_trident_ac97_control
  2065. Description: enable/disable rear path for ac97
  2066. ---------------------------------------------------------------------------*/
  2067. static int snd_trident_ac97_control_info(struct snd_kcontrol *kcontrol,
  2068. struct snd_ctl_elem_info *uinfo)
  2069. {
  2070. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2071. uinfo->count = 1;
  2072. uinfo->value.integer.min = 0;
  2073. uinfo->value.integer.max = 1;
  2074. return 0;
  2075. }
  2076. static int snd_trident_ac97_control_get(struct snd_kcontrol *kcontrol,
  2077. struct snd_ctl_elem_value *ucontrol)
  2078. {
  2079. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2080. unsigned char val;
  2081. spin_lock_irq(&trident->reg_lock);
  2082. val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2083. ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
  2084. spin_unlock_irq(&trident->reg_lock);
  2085. return 0;
  2086. }
  2087. static int snd_trident_ac97_control_put(struct snd_kcontrol *kcontrol,
  2088. struct snd_ctl_elem_value *ucontrol)
  2089. {
  2090. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2091. unsigned char val;
  2092. int change = 0;
  2093. spin_lock_irq(&trident->reg_lock);
  2094. val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2095. val &= ~(1 << kcontrol->private_value);
  2096. if (ucontrol->value.integer.value[0])
  2097. val |= 1 << kcontrol->private_value;
  2098. change = val != trident->ac97_ctrl;
  2099. trident->ac97_ctrl = val;
  2100. outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2101. spin_unlock_irq(&trident->reg_lock);
  2102. return change;
  2103. }
  2104. static struct snd_kcontrol_new snd_trident_ac97_rear_control __devinitdata =
  2105. {
  2106. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2107. .name = "Rear Path",
  2108. .info = snd_trident_ac97_control_info,
  2109. .get = snd_trident_ac97_control_get,
  2110. .put = snd_trident_ac97_control_put,
  2111. .private_value = 4,
  2112. };
  2113. /*---------------------------------------------------------------------------
  2114. snd_trident_vol_control
  2115. Description: wave & music volume control
  2116. ---------------------------------------------------------------------------*/
  2117. static int snd_trident_vol_control_info(struct snd_kcontrol *kcontrol,
  2118. struct snd_ctl_elem_info *uinfo)
  2119. {
  2120. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2121. uinfo->count = 2;
  2122. uinfo->value.integer.min = 0;
  2123. uinfo->value.integer.max = 255;
  2124. return 0;
  2125. }
  2126. static int snd_trident_vol_control_get(struct snd_kcontrol *kcontrol,
  2127. struct snd_ctl_elem_value *ucontrol)
  2128. {
  2129. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2130. unsigned int val;
  2131. val = trident->musicvol_wavevol;
  2132. ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
  2133. ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
  2134. return 0;
  2135. }
  2136. static int snd_trident_vol_control_put(struct snd_kcontrol *kcontrol,
  2137. struct snd_ctl_elem_value *ucontrol)
  2138. {
  2139. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2140. unsigned int val;
  2141. int change = 0;
  2142. spin_lock_irq(&trident->reg_lock);
  2143. val = trident->musicvol_wavevol;
  2144. val &= ~(0xffff << kcontrol->private_value);
  2145. val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
  2146. ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
  2147. change = val != trident->musicvol_wavevol;
  2148. outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  2149. spin_unlock_irq(&trident->reg_lock);
  2150. return change;
  2151. }
  2152. static struct snd_kcontrol_new snd_trident_vol_music_control __devinitdata =
  2153. {
  2154. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2155. .name = "Music Playback Volume",
  2156. .info = snd_trident_vol_control_info,
  2157. .get = snd_trident_vol_control_get,
  2158. .put = snd_trident_vol_control_put,
  2159. .private_value = 16,
  2160. };
  2161. static struct snd_kcontrol_new snd_trident_vol_wave_control __devinitdata =
  2162. {
  2163. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2164. .name = "Wave Playback Volume",
  2165. .info = snd_trident_vol_control_info,
  2166. .get = snd_trident_vol_control_get,
  2167. .put = snd_trident_vol_control_put,
  2168. .private_value = 0,
  2169. };
  2170. /*---------------------------------------------------------------------------
  2171. snd_trident_pcm_vol_control
  2172. Description: PCM front volume control
  2173. ---------------------------------------------------------------------------*/
  2174. static int snd_trident_pcm_vol_control_info(struct snd_kcontrol *kcontrol,
  2175. struct snd_ctl_elem_info *uinfo)
  2176. {
  2177. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2178. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2179. uinfo->count = 1;
  2180. uinfo->value.integer.min = 0;
  2181. uinfo->value.integer.max = 255;
  2182. if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  2183. uinfo->value.integer.max = 1023;
  2184. return 0;
  2185. }
  2186. static int snd_trident_pcm_vol_control_get(struct snd_kcontrol *kcontrol,
  2187. struct snd_ctl_elem_value *ucontrol)
  2188. {
  2189. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2190. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2191. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2192. ucontrol->value.integer.value[0] = 1023 - mix->vol;
  2193. } else {
  2194. ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
  2195. }
  2196. return 0;
  2197. }
  2198. static int snd_trident_pcm_vol_control_put(struct snd_kcontrol *kcontrol,
  2199. struct snd_ctl_elem_value *ucontrol)
  2200. {
  2201. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2202. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2203. unsigned int val;
  2204. int change = 0;
  2205. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2206. val = 1023 - (ucontrol->value.integer.value[0] & 1023);
  2207. } else {
  2208. val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
  2209. }
  2210. spin_lock_irq(&trident->reg_lock);
  2211. change = val != mix->vol;
  2212. mix->vol = val;
  2213. if (mix->voice != NULL)
  2214. snd_trident_write_vol_reg(trident, mix->voice, val);
  2215. spin_unlock_irq(&trident->reg_lock);
  2216. return change;
  2217. }
  2218. static struct snd_kcontrol_new snd_trident_pcm_vol_control __devinitdata =
  2219. {
  2220. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2221. .name = "PCM Front Playback Volume",
  2222. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2223. .count = 32,
  2224. .info = snd_trident_pcm_vol_control_info,
  2225. .get = snd_trident_pcm_vol_control_get,
  2226. .put = snd_trident_pcm_vol_control_put,
  2227. };
  2228. /*---------------------------------------------------------------------------
  2229. snd_trident_pcm_pan_control
  2230. Description: PCM front pan control
  2231. ---------------------------------------------------------------------------*/
  2232. static int snd_trident_pcm_pan_control_info(struct snd_kcontrol *kcontrol,
  2233. struct snd_ctl_elem_info *uinfo)
  2234. {
  2235. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2236. uinfo->count = 1;
  2237. uinfo->value.integer.min = 0;
  2238. uinfo->value.integer.max = 127;
  2239. return 0;
  2240. }
  2241. static int snd_trident_pcm_pan_control_get(struct snd_kcontrol *kcontrol,
  2242. struct snd_ctl_elem_value *ucontrol)
  2243. {
  2244. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2245. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2246. ucontrol->value.integer.value[0] = mix->pan;
  2247. if (ucontrol->value.integer.value[0] & 0x40) {
  2248. ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
  2249. } else {
  2250. ucontrol->value.integer.value[0] |= 0x40;
  2251. }
  2252. return 0;
  2253. }
  2254. static int snd_trident_pcm_pan_control_put(struct snd_kcontrol *kcontrol,
  2255. struct snd_ctl_elem_value *ucontrol)
  2256. {
  2257. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2258. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2259. unsigned char val;
  2260. int change = 0;
  2261. if (ucontrol->value.integer.value[0] & 0x40)
  2262. val = ucontrol->value.integer.value[0] & 0x3f;
  2263. else
  2264. val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
  2265. spin_lock_irq(&trident->reg_lock);
  2266. change = val != mix->pan;
  2267. mix->pan = val;
  2268. if (mix->voice != NULL)
  2269. snd_trident_write_pan_reg(trident, mix->voice, val);
  2270. spin_unlock_irq(&trident->reg_lock);
  2271. return change;
  2272. }
  2273. static struct snd_kcontrol_new snd_trident_pcm_pan_control __devinitdata =
  2274. {
  2275. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2276. .name = "PCM Pan Playback Control",
  2277. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2278. .count = 32,
  2279. .info = snd_trident_pcm_pan_control_info,
  2280. .get = snd_trident_pcm_pan_control_get,
  2281. .put = snd_trident_pcm_pan_control_put,
  2282. };
  2283. /*---------------------------------------------------------------------------
  2284. snd_trident_pcm_rvol_control
  2285. Description: PCM reverb volume control
  2286. ---------------------------------------------------------------------------*/
  2287. static int snd_trident_pcm_rvol_control_info(struct snd_kcontrol *kcontrol,
  2288. struct snd_ctl_elem_info *uinfo)
  2289. {
  2290. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2291. uinfo->count = 1;
  2292. uinfo->value.integer.min = 0;
  2293. uinfo->value.integer.max = 127;
  2294. return 0;
  2295. }
  2296. static int snd_trident_pcm_rvol_control_get(struct snd_kcontrol *kcontrol,
  2297. struct snd_ctl_elem_value *ucontrol)
  2298. {
  2299. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2300. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2301. ucontrol->value.integer.value[0] = 127 - mix->rvol;
  2302. return 0;
  2303. }
  2304. static int snd_trident_pcm_rvol_control_put(struct snd_kcontrol *kcontrol,
  2305. struct snd_ctl_elem_value *ucontrol)
  2306. {
  2307. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2308. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2309. unsigned short val;
  2310. int change = 0;
  2311. val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
  2312. spin_lock_irq(&trident->reg_lock);
  2313. change = val != mix->rvol;
  2314. mix->rvol = val;
  2315. if (mix->voice != NULL)
  2316. snd_trident_write_rvol_reg(trident, mix->voice, val);
  2317. spin_unlock_irq(&trident->reg_lock);
  2318. return change;
  2319. }
  2320. static struct snd_kcontrol_new snd_trident_pcm_rvol_control __devinitdata =
  2321. {
  2322. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2323. .name = "PCM Reverb Playback Volume",
  2324. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2325. .count = 32,
  2326. .info = snd_trident_pcm_rvol_control_info,
  2327. .get = snd_trident_pcm_rvol_control_get,
  2328. .put = snd_trident_pcm_rvol_control_put,
  2329. };
  2330. /*---------------------------------------------------------------------------
  2331. snd_trident_pcm_cvol_control
  2332. Description: PCM chorus volume control
  2333. ---------------------------------------------------------------------------*/
  2334. static int snd_trident_pcm_cvol_control_info(struct snd_kcontrol *kcontrol,
  2335. struct snd_ctl_elem_info *uinfo)
  2336. {
  2337. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2338. uinfo->count = 1;
  2339. uinfo->value.integer.min = 0;
  2340. uinfo->value.integer.max = 127;
  2341. return 0;
  2342. }
  2343. static int snd_trident_pcm_cvol_control_get(struct snd_kcontrol *kcontrol,
  2344. struct snd_ctl_elem_value *ucontrol)
  2345. {
  2346. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2347. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2348. ucontrol->value.integer.value[0] = 127 - mix->cvol;
  2349. return 0;
  2350. }
  2351. static int snd_trident_pcm_cvol_control_put(struct snd_kcontrol *kcontrol,
  2352. struct snd_ctl_elem_value *ucontrol)
  2353. {
  2354. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2355. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2356. unsigned short val;
  2357. int change = 0;
  2358. val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
  2359. spin_lock_irq(&trident->reg_lock);
  2360. change = val != mix->cvol;
  2361. mix->cvol = val;
  2362. if (mix->voice != NULL)
  2363. snd_trident_write_cvol_reg(trident, mix->voice, val);
  2364. spin_unlock_irq(&trident->reg_lock);
  2365. return change;
  2366. }
  2367. static struct snd_kcontrol_new snd_trident_pcm_cvol_control __devinitdata =
  2368. {
  2369. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2370. .name = "PCM Chorus Playback Volume",
  2371. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2372. .count = 32,
  2373. .info = snd_trident_pcm_cvol_control_info,
  2374. .get = snd_trident_pcm_cvol_control_get,
  2375. .put = snd_trident_pcm_cvol_control_put,
  2376. };
  2377. static void snd_trident_notify_pcm_change1(struct snd_card *card,
  2378. struct snd_kcontrol *kctl,
  2379. int num, int activate)
  2380. {
  2381. struct snd_ctl_elem_id id;
  2382. if (! kctl)
  2383. return;
  2384. if (activate)
  2385. kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  2386. else
  2387. kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  2388. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
  2389. SNDRV_CTL_EVENT_MASK_INFO,
  2390. snd_ctl_build_ioff(&id, kctl, num));
  2391. }
  2392. static void snd_trident_notify_pcm_change(struct snd_trident *trident,
  2393. struct snd_trident_pcm_mixer *tmix,
  2394. int num, int activate)
  2395. {
  2396. snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
  2397. snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
  2398. snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
  2399. snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
  2400. }
  2401. static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
  2402. struct snd_trident_voice *voice,
  2403. struct snd_pcm_substream *substream)
  2404. {
  2405. struct snd_trident_pcm_mixer *tmix;
  2406. snd_assert(trident != NULL && voice != NULL && substream != NULL, return -EINVAL);
  2407. tmix = &trident->pcm_mixer[substream->number];
  2408. tmix->voice = voice;
  2409. tmix->vol = T4D_DEFAULT_PCM_VOL;
  2410. tmix->pan = T4D_DEFAULT_PCM_PAN;
  2411. tmix->rvol = T4D_DEFAULT_PCM_RVOL;
  2412. tmix->cvol = T4D_DEFAULT_PCM_CVOL;
  2413. snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
  2414. return 0;
  2415. }
  2416. static int snd_trident_pcm_mixer_free(struct snd_trident *trident, struct snd_trident_voice *voice, struct snd_pcm_substream *substream)
  2417. {
  2418. struct snd_trident_pcm_mixer *tmix;
  2419. snd_assert(trident != NULL && substream != NULL, return -EINVAL);
  2420. tmix = &trident->pcm_mixer[substream->number];
  2421. tmix->voice = NULL;
  2422. snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
  2423. return 0;
  2424. }
  2425. /*---------------------------------------------------------------------------
  2426. snd_trident_mixer
  2427. Description: This routine registers the 4DWave device for mixer support.
  2428. Paramters: trident - pointer to target device class for 4DWave.
  2429. Returns: None
  2430. ---------------------------------------------------------------------------*/
  2431. static int __devinit snd_trident_mixer(struct snd_trident * trident, int pcm_spdif_device)
  2432. {
  2433. struct snd_ac97_template _ac97;
  2434. struct snd_card *card = trident->card;
  2435. struct snd_kcontrol *kctl;
  2436. struct snd_ctl_elem_value *uctl;
  2437. int idx, err, retries = 2;
  2438. static struct snd_ac97_bus_ops ops = {
  2439. .write = snd_trident_codec_write,
  2440. .read = snd_trident_codec_read,
  2441. };
  2442. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  2443. if (!uctl)
  2444. return -ENOMEM;
  2445. if ((err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus)) < 0)
  2446. goto __out;
  2447. memset(&_ac97, 0, sizeof(_ac97));
  2448. _ac97.private_data = trident;
  2449. trident->ac97_detect = 1;
  2450. __again:
  2451. if ((err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97)) < 0) {
  2452. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2453. if ((err = snd_trident_sis_reset(trident)) < 0)
  2454. goto __out;
  2455. if (retries-- > 0)
  2456. goto __again;
  2457. err = -EIO;
  2458. }
  2459. goto __out;
  2460. }
  2461. /* secondary codec? */
  2462. if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
  2463. (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
  2464. _ac97.num = 1;
  2465. err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
  2466. if (err < 0)
  2467. snd_printk(KERN_ERR "SI7018: the secondary codec - invalid access\n");
  2468. #if 0 // only for my testing purpose --jk
  2469. {
  2470. struct snd_ac97 *mc97;
  2471. err = snd_ac97_modem(trident->card, &_ac97, &mc97);
  2472. if (err < 0)
  2473. snd_printk(KERN_ERR "snd_ac97_modem returned error %i\n", err);
  2474. }
  2475. #endif
  2476. }
  2477. trident->ac97_detect = 0;
  2478. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  2479. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident))) < 0)
  2480. goto __out;
  2481. kctl->put(kctl, uctl);
  2482. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident))) < 0)
  2483. goto __out;
  2484. kctl->put(kctl, uctl);
  2485. outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  2486. } else {
  2487. outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  2488. }
  2489. for (idx = 0; idx < 32; idx++) {
  2490. struct snd_trident_pcm_mixer *tmix;
  2491. tmix = &trident->pcm_mixer[idx];
  2492. tmix->voice = NULL;
  2493. }
  2494. if ((trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident)) == NULL)
  2495. goto __nomem;
  2496. if ((err = snd_ctl_add(card, trident->ctl_vol)))
  2497. goto __out;
  2498. if ((trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident)) == NULL)
  2499. goto __nomem;
  2500. if ((err = snd_ctl_add(card, trident->ctl_pan)))
  2501. goto __out;
  2502. if ((trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident)) == NULL)
  2503. goto __nomem;
  2504. if ((err = snd_ctl_add(card, trident->ctl_rvol)))
  2505. goto __out;
  2506. if ((trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident)) == NULL)
  2507. goto __nomem;
  2508. if ((err = snd_ctl_add(card, trident->ctl_cvol)))
  2509. goto __out;
  2510. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  2511. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident))) < 0)
  2512. goto __out;
  2513. kctl->put(kctl, uctl);
  2514. }
  2515. if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2516. kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
  2517. if (kctl == NULL) {
  2518. err = -ENOMEM;
  2519. goto __out;
  2520. }
  2521. if (trident->ac97->ext_id & AC97_EI_SPDIF)
  2522. kctl->id.index++;
  2523. if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
  2524. kctl->id.index++;
  2525. idx = kctl->id.index;
  2526. if ((err = snd_ctl_add(card, kctl)) < 0)
  2527. goto __out;
  2528. kctl->put(kctl, uctl);
  2529. kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
  2530. if (kctl == NULL) {
  2531. err = -ENOMEM;
  2532. goto __out;
  2533. }
  2534. kctl->id.index = idx;
  2535. kctl->id.device = pcm_spdif_device;
  2536. if ((err = snd_ctl_add(card, kctl)) < 0)
  2537. goto __out;
  2538. kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
  2539. if (kctl == NULL) {
  2540. err = -ENOMEM;
  2541. goto __out;
  2542. }
  2543. kctl->id.index = idx;
  2544. kctl->id.device = pcm_spdif_device;
  2545. if ((err = snd_ctl_add(card, kctl)) < 0)
  2546. goto __out;
  2547. kctl = snd_ctl_new1(&snd_trident_spdif_stream, trident);
  2548. if (kctl == NULL) {
  2549. err = -ENOMEM;
  2550. goto __out;
  2551. }
  2552. kctl->id.index = idx;
  2553. kctl->id.device = pcm_spdif_device;
  2554. if ((err = snd_ctl_add(card, kctl)) < 0)
  2555. goto __out;
  2556. trident->spdif_pcm_ctl = kctl;
  2557. }
  2558. err = 0;
  2559. goto __out;
  2560. __nomem:
  2561. err = -ENOMEM;
  2562. __out:
  2563. kfree(uctl);
  2564. return err;
  2565. }
  2566. /*
  2567. * gameport interface
  2568. */
  2569. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  2570. static unsigned char snd_trident_gameport_read(struct gameport *gameport)
  2571. {
  2572. struct snd_trident *chip = gameport_get_port_data(gameport);
  2573. snd_assert(chip, return 0);
  2574. return inb(TRID_REG(chip, GAMEPORT_LEGACY));
  2575. }
  2576. static void snd_trident_gameport_trigger(struct gameport *gameport)
  2577. {
  2578. struct snd_trident *chip = gameport_get_port_data(gameport);
  2579. snd_assert(chip, return);
  2580. outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
  2581. }
  2582. static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  2583. {
  2584. struct snd_trident *chip = gameport_get_port_data(gameport);
  2585. int i;
  2586. snd_assert(chip, return 0);
  2587. *buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
  2588. for (i = 0; i < 4; i++) {
  2589. axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
  2590. if (axes[i] == 0xffff) axes[i] = -1;
  2591. }
  2592. return 0;
  2593. }
  2594. static int snd_trident_gameport_open(struct gameport *gameport, int mode)
  2595. {
  2596. struct snd_trident *chip = gameport_get_port_data(gameport);
  2597. snd_assert(chip, return 0);
  2598. switch (mode) {
  2599. case GAMEPORT_MODE_COOKED:
  2600. outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
  2601. msleep(20);
  2602. return 0;
  2603. case GAMEPORT_MODE_RAW:
  2604. outb(0, TRID_REG(chip, GAMEPORT_GCR));
  2605. return 0;
  2606. default:
  2607. return -1;
  2608. }
  2609. }
  2610. int __devinit snd_trident_create_gameport(struct snd_trident *chip)
  2611. {
  2612. struct gameport *gp;
  2613. chip->gameport = gp = gameport_allocate_port();
  2614. if (!gp) {
  2615. printk(KERN_ERR "trident: cannot allocate memory for gameport\n");
  2616. return -ENOMEM;
  2617. }
  2618. gameport_set_name(gp, "Trident 4DWave");
  2619. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  2620. gameport_set_dev_parent(gp, &chip->pci->dev);
  2621. gameport_set_port_data(gp, chip);
  2622. gp->fuzz = 64;
  2623. gp->read = snd_trident_gameport_read;
  2624. gp->trigger = snd_trident_gameport_trigger;
  2625. gp->cooked_read = snd_trident_gameport_cooked_read;
  2626. gp->open = snd_trident_gameport_open;
  2627. gameport_register_port(gp);
  2628. return 0;
  2629. }
  2630. static inline void snd_trident_free_gameport(struct snd_trident *chip)
  2631. {
  2632. if (chip->gameport) {
  2633. gameport_unregister_port(chip->gameport);
  2634. chip->gameport = NULL;
  2635. }
  2636. }
  2637. #else
  2638. int __devinit snd_trident_create_gameport(struct snd_trident *chip) { return -ENOSYS; }
  2639. static inline void snd_trident_free_gameport(struct snd_trident *chip) { }
  2640. #endif /* CONFIG_GAMEPORT */
  2641. /*
  2642. * delay for 1 tick
  2643. */
  2644. static inline void do_delay(struct snd_trident *chip)
  2645. {
  2646. schedule_timeout_uninterruptible(1);
  2647. }
  2648. /*
  2649. * SiS reset routine
  2650. */
  2651. static int snd_trident_sis_reset(struct snd_trident *trident)
  2652. {
  2653. unsigned long end_time;
  2654. unsigned int i;
  2655. int r;
  2656. r = trident->in_suspend ? 0 : 2; /* count of retries */
  2657. __si7018_retry:
  2658. pci_write_config_byte(trident->pci, 0x46, 0x04); /* SOFTWARE RESET */
  2659. udelay(100);
  2660. pci_write_config_byte(trident->pci, 0x46, 0x00);
  2661. udelay(100);
  2662. /* disable AC97 GPIO interrupt */
  2663. outb(0x00, TRID_REG(trident, SI_AC97_GPIO));
  2664. /* initialize serial interface, force cold reset */
  2665. i = PCMOUT|SURROUT|CENTEROUT|LFEOUT|SECONDARY_ID|COLD_RESET;
  2666. outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  2667. udelay(1000);
  2668. /* remove cold reset */
  2669. i &= ~COLD_RESET;
  2670. outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  2671. udelay(2000);
  2672. /* wait, until the codec is ready */
  2673. end_time = (jiffies + (HZ * 3) / 4) + 1;
  2674. do {
  2675. if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0)
  2676. goto __si7018_ok;
  2677. do_delay(trident);
  2678. } while (time_after_eq(end_time, jiffies));
  2679. snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)));
  2680. if (r-- > 0) {
  2681. end_time = jiffies + HZ;
  2682. do {
  2683. do_delay(trident);
  2684. } while (time_after_eq(end_time, jiffies));
  2685. goto __si7018_retry;
  2686. }
  2687. __si7018_ok:
  2688. /* wait for the second codec */
  2689. do {
  2690. if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_SECONDARY_READY) != 0)
  2691. break;
  2692. do_delay(trident);
  2693. } while (time_after_eq(end_time, jiffies));
  2694. /* enable 64 channel mode */
  2695. outl(BANK_B_EN, TRID_REG(trident, T4D_LFO_GC_CIR));
  2696. return 0;
  2697. }
  2698. /*
  2699. * /proc interface
  2700. */
  2701. static void snd_trident_proc_read(struct snd_info_entry *entry,
  2702. struct snd_info_buffer *buffer)
  2703. {
  2704. struct snd_trident *trident = entry->private_data;
  2705. char *s;
  2706. switch (trident->device) {
  2707. case TRIDENT_DEVICE_ID_SI7018:
  2708. s = "SiS 7018 Audio";
  2709. break;
  2710. case TRIDENT_DEVICE_ID_DX:
  2711. s = "Trident 4DWave PCI DX";
  2712. break;
  2713. case TRIDENT_DEVICE_ID_NX:
  2714. s = "Trident 4DWave PCI NX";
  2715. break;
  2716. default:
  2717. s = "???";
  2718. }
  2719. snd_iprintf(buffer, "%s\n\n", s);
  2720. snd_iprintf(buffer, "Spurious IRQs : %d\n", trident->spurious_irq_count);
  2721. snd_iprintf(buffer, "Spurious IRQ dlta: %d\n", trident->spurious_irq_max_delta);
  2722. if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018)
  2723. snd_iprintf(buffer, "IEC958 Mixer Out : %s\n", trident->spdif_ctrl == 0x28 ? "on" : "off");
  2724. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  2725. snd_iprintf(buffer, "Rear Speakers : %s\n", trident->ac97_ctrl & 0x00000010 ? "on" : "off");
  2726. if (trident->tlb.entries) {
  2727. snd_iprintf(buffer,"\nVirtual Memory\n");
  2728. snd_iprintf(buffer, "Memory Maximum : %d\n", trident->tlb.memhdr->size);
  2729. snd_iprintf(buffer, "Memory Used : %d\n", trident->tlb.memhdr->used);
  2730. snd_iprintf(buffer, "Memory Free : %d\n", snd_util_mem_avail(trident->tlb.memhdr));
  2731. }
  2732. }
  2733. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  2734. snd_iprintf(buffer,"\nWavetable Synth\n");
  2735. snd_iprintf(buffer, "Memory Maximum : %d\n", trident->synth.max_size);
  2736. snd_iprintf(buffer, "Memory Used : %d\n", trident->synth.current_size);
  2737. snd_iprintf(buffer, "Memory Free : %d\n", (trident->synth.max_size-trident->synth.current_size));
  2738. #endif
  2739. }
  2740. static void __devinit snd_trident_proc_init(struct snd_trident * trident)
  2741. {
  2742. struct snd_info_entry *entry;
  2743. const char *s = "trident";
  2744. if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  2745. s = "sis7018";
  2746. if (! snd_card_proc_new(trident->card, s, &entry))
  2747. snd_info_set_text_ops(entry, trident, 1024, snd_trident_proc_read);
  2748. }
  2749. static int snd_trident_dev_free(struct snd_device *device)
  2750. {
  2751. struct snd_trident *trident = device->device_data;
  2752. return snd_trident_free(trident);
  2753. }
  2754. /*---------------------------------------------------------------------------
  2755. snd_trident_tlb_alloc
  2756. Description: Allocate and set up the TLB page table on 4D NX.
  2757. Each entry has 4 bytes (physical PCI address).
  2758. Paramters: trident - pointer to target device class for 4DWave.
  2759. Returns: 0 or negative error code
  2760. ---------------------------------------------------------------------------*/
  2761. static int __devinit snd_trident_tlb_alloc(struct snd_trident *trident)
  2762. {
  2763. int i;
  2764. /* TLB array must be aligned to 16kB !!! so we allocate
  2765. 32kB region and correct offset when necessary */
  2766. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
  2767. 2 * SNDRV_TRIDENT_MAX_PAGES * 4, &trident->tlb.buffer) < 0) {
  2768. snd_printk(KERN_ERR "trident: unable to allocate TLB buffer\n");
  2769. return -ENOMEM;
  2770. }
  2771. trident->tlb.entries = (unsigned int*)(((unsigned long)trident->tlb.buffer.area + SNDRV_TRIDENT_MAX_PAGES * 4 - 1) & ~(SNDRV_TRIDENT_MAX_PAGES * 4 - 1));
  2772. trident->tlb.entries_dmaaddr = (trident->tlb.buffer.addr + SNDRV_TRIDENT_MAX_PAGES * 4 - 1) & ~(SNDRV_TRIDENT_MAX_PAGES * 4 - 1);
  2773. /* allocate shadow TLB page table (virtual addresses) */
  2774. trident->tlb.shadow_entries = vmalloc(SNDRV_TRIDENT_MAX_PAGES*sizeof(unsigned long));
  2775. if (trident->tlb.shadow_entries == NULL) {
  2776. snd_printk(KERN_ERR "trident: unable to allocate shadow TLB entries\n");
  2777. return -ENOMEM;
  2778. }
  2779. /* allocate and setup silent page and initialise TLB entries */
  2780. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
  2781. SNDRV_TRIDENT_PAGE_SIZE, &trident->tlb.silent_page) < 0) {
  2782. snd_printk(KERN_ERR "trident: unable to allocate silent page\n");
  2783. return -ENOMEM;
  2784. }
  2785. memset(trident->tlb.silent_page.area, 0, SNDRV_TRIDENT_PAGE_SIZE);
  2786. for (i = 0; i < SNDRV_TRIDENT_MAX_PAGES; i++) {
  2787. trident->tlb.entries[i] = cpu_to_le32(trident->tlb.silent_page.addr & ~(SNDRV_TRIDENT_PAGE_SIZE-1));
  2788. trident->tlb.shadow_entries[i] = (unsigned long)trident->tlb.silent_page.area;
  2789. }
  2790. /* use emu memory block manager code to manage tlb page allocation */
  2791. trident->tlb.memhdr = snd_util_memhdr_new(SNDRV_TRIDENT_PAGE_SIZE * SNDRV_TRIDENT_MAX_PAGES);
  2792. if (trident->tlb.memhdr == NULL)
  2793. return -ENOMEM;
  2794. trident->tlb.memhdr->block_extra_size = sizeof(struct snd_trident_memblk_arg);
  2795. return 0;
  2796. }
  2797. /*
  2798. * initialize 4D DX chip
  2799. */
  2800. static void snd_trident_stop_all_voices(struct snd_trident *trident)
  2801. {
  2802. outl(0xffffffff, TRID_REG(trident, T4D_STOP_A));
  2803. outl(0xffffffff, TRID_REG(trident, T4D_STOP_B));
  2804. outl(0, TRID_REG(trident, T4D_AINTEN_A));
  2805. outl(0, TRID_REG(trident, T4D_AINTEN_B));
  2806. }
  2807. static int snd_trident_4d_dx_init(struct snd_trident *trident)
  2808. {
  2809. struct pci_dev *pci = trident->pci;
  2810. unsigned long end_time;
  2811. /* reset the legacy configuration and whole audio/wavetable block */
  2812. pci_write_config_dword(pci, 0x40, 0); /* DDMA */
  2813. pci_write_config_byte(pci, 0x44, 0); /* ports */
  2814. pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
  2815. pci_write_config_byte(pci, 0x46, 4); /* reset */
  2816. udelay(100);
  2817. pci_write_config_byte(pci, 0x46, 0); /* release reset */
  2818. udelay(100);
  2819. /* warm reset of the AC'97 codec */
  2820. outl(0x00000001, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
  2821. udelay(100);
  2822. outl(0x00000000, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
  2823. /* DAC on, disable SB IRQ and try to force ADC valid signal */
  2824. trident->ac97_ctrl = 0x0000004a;
  2825. outl(trident->ac97_ctrl, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
  2826. /* wait, until the codec is ready */
  2827. end_time = (jiffies + (HZ * 3) / 4) + 1;
  2828. do {
  2829. if ((inl(TRID_REG(trident, DX_ACR2_AC97_COM_STAT)) & 0x0010) != 0)
  2830. goto __dx_ok;
  2831. do_delay(trident);
  2832. } while (time_after_eq(end_time, jiffies));
  2833. snd_printk(KERN_ERR "AC'97 codec ready error\n");
  2834. return -EIO;
  2835. __dx_ok:
  2836. snd_trident_stop_all_voices(trident);
  2837. return 0;
  2838. }
  2839. /*
  2840. * initialize 4D NX chip
  2841. */
  2842. static int snd_trident_4d_nx_init(struct snd_trident *trident)
  2843. {
  2844. struct pci_dev *pci = trident->pci;
  2845. unsigned long end_time;
  2846. /* reset the legacy configuration and whole audio/wavetable block */
  2847. pci_write_config_dword(pci, 0x40, 0); /* DDMA */
  2848. pci_write_config_byte(pci, 0x44, 0); /* ports */
  2849. pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
  2850. pci_write_config_byte(pci, 0x46, 1); /* reset */
  2851. udelay(100);
  2852. pci_write_config_byte(pci, 0x46, 0); /* release reset */
  2853. udelay(100);
  2854. /* warm reset of the AC'97 codec */
  2855. outl(0x00000001, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2856. udelay(100);
  2857. outl(0x00000000, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2858. /* wait, until the codec is ready */
  2859. end_time = (jiffies + (HZ * 3) / 4) + 1;
  2860. do {
  2861. if ((inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)) & 0x0008) != 0)
  2862. goto __nx_ok;
  2863. do_delay(trident);
  2864. } while (time_after_eq(end_time, jiffies));
  2865. snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)));
  2866. return -EIO;
  2867. __nx_ok:
  2868. /* DAC on */
  2869. trident->ac97_ctrl = 0x00000002;
  2870. outl(trident->ac97_ctrl, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2871. /* disable SB IRQ */
  2872. outl(NX_SB_IRQ_DISABLE, TRID_REG(trident, T4D_MISCINT));
  2873. snd_trident_stop_all_voices(trident);
  2874. if (trident->tlb.entries != NULL) {
  2875. unsigned int i;
  2876. /* enable virtual addressing via TLB */
  2877. i = trident->tlb.entries_dmaaddr;
  2878. i |= 0x00000001;
  2879. outl(i, TRID_REG(trident, NX_TLBC));
  2880. } else {
  2881. outl(0, TRID_REG(trident, NX_TLBC));
  2882. }
  2883. /* initialize S/PDIF */
  2884. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  2885. outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  2886. return 0;
  2887. }
  2888. /*
  2889. * initialize sis7018 chip
  2890. */
  2891. static int snd_trident_sis_init(struct snd_trident *trident)
  2892. {
  2893. int err;
  2894. if ((err = snd_trident_sis_reset(trident)) < 0)
  2895. return err;
  2896. snd_trident_stop_all_voices(trident);
  2897. /* initialize S/PDIF */
  2898. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  2899. return 0;
  2900. }
  2901. /*---------------------------------------------------------------------------
  2902. snd_trident_create
  2903. Description: This routine will create the device specific class for
  2904. the 4DWave card. It will also perform basic initialization.
  2905. Paramters: card - which card to create
  2906. pci - interface to PCI bus resource info
  2907. dma1ptr - playback dma buffer
  2908. dma2ptr - capture dma buffer
  2909. irqptr - interrupt resource info
  2910. Returns: 4DWave device class private data
  2911. ---------------------------------------------------------------------------*/
  2912. int __devinit snd_trident_create(struct snd_card *card,
  2913. struct pci_dev *pci,
  2914. int pcm_streams,
  2915. int pcm_spdif_device,
  2916. int max_wavetable_size,
  2917. struct snd_trident ** rtrident)
  2918. {
  2919. struct snd_trident *trident;
  2920. int i, err;
  2921. struct snd_trident_voice *voice;
  2922. struct snd_trident_pcm_mixer *tmix;
  2923. static struct snd_device_ops ops = {
  2924. .dev_free = snd_trident_dev_free,
  2925. };
  2926. *rtrident = NULL;
  2927. /* enable PCI device */
  2928. if ((err = pci_enable_device(pci)) < 0)
  2929. return err;
  2930. /* check, if we can restrict PCI DMA transfers to 30 bits */
  2931. if (pci_set_dma_mask(pci, DMA_30BIT_MASK) < 0 ||
  2932. pci_set_consistent_dma_mask(pci, DMA_30BIT_MASK) < 0) {
  2933. snd_printk(KERN_ERR "architecture does not support 30bit PCI busmaster DMA\n");
  2934. pci_disable_device(pci);
  2935. return -ENXIO;
  2936. }
  2937. trident = kzalloc(sizeof(*trident), GFP_KERNEL);
  2938. if (trident == NULL) {
  2939. pci_disable_device(pci);
  2940. return -ENOMEM;
  2941. }
  2942. trident->device = (pci->vendor << 16) | pci->device;
  2943. trident->card = card;
  2944. trident->pci = pci;
  2945. spin_lock_init(&trident->reg_lock);
  2946. spin_lock_init(&trident->event_lock);
  2947. spin_lock_init(&trident->voice_alloc);
  2948. if (pcm_streams < 1)
  2949. pcm_streams = 1;
  2950. if (pcm_streams > 32)
  2951. pcm_streams = 32;
  2952. trident->ChanPCM = pcm_streams;
  2953. if (max_wavetable_size < 0 )
  2954. max_wavetable_size = 0;
  2955. trident->synth.max_size = max_wavetable_size * 1024;
  2956. trident->irq = -1;
  2957. trident->midi_port = TRID_REG(trident, T4D_MPU401_BASE);
  2958. pci_set_master(pci);
  2959. if ((err = pci_request_regions(pci, "Trident Audio")) < 0) {
  2960. kfree(trident);
  2961. pci_disable_device(pci);
  2962. return err;
  2963. }
  2964. trident->port = pci_resource_start(pci, 0);
  2965. if (request_irq(pci->irq, snd_trident_interrupt, SA_INTERRUPT|SA_SHIRQ,
  2966. "Trident Audio", trident)) {
  2967. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  2968. snd_trident_free(trident);
  2969. return -EBUSY;
  2970. }
  2971. trident->irq = pci->irq;
  2972. /* allocate 16k-aligned TLB for NX cards */
  2973. trident->tlb.entries = NULL;
  2974. trident->tlb.buffer.area = NULL;
  2975. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  2976. if ((err = snd_trident_tlb_alloc(trident)) < 0) {
  2977. snd_trident_free(trident);
  2978. return err;
  2979. }
  2980. }
  2981. trident->spdif_bits = trident->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
  2982. /* initialize chip */
  2983. switch (trident->device) {
  2984. case TRIDENT_DEVICE_ID_DX:
  2985. err = snd_trident_4d_dx_init(trident);
  2986. break;
  2987. case TRIDENT_DEVICE_ID_NX:
  2988. err = snd_trident_4d_nx_init(trident);
  2989. break;
  2990. case TRIDENT_DEVICE_ID_SI7018:
  2991. err = snd_trident_sis_init(trident);
  2992. break;
  2993. default:
  2994. snd_BUG();
  2995. break;
  2996. }
  2997. if (err < 0) {
  2998. snd_trident_free(trident);
  2999. return err;
  3000. }
  3001. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, trident, &ops)) < 0) {
  3002. snd_trident_free(trident);
  3003. return err;
  3004. }
  3005. if ((err = snd_trident_mixer(trident, pcm_spdif_device)) < 0)
  3006. return err;
  3007. /* initialise synth voices */
  3008. for (i = 0; i < 64; i++) {
  3009. voice = &trident->synth.voices[i];
  3010. voice->number = i;
  3011. voice->trident = trident;
  3012. }
  3013. /* initialize pcm mixer entries */
  3014. for (i = 0; i < 32; i++) {
  3015. tmix = &trident->pcm_mixer[i];
  3016. tmix->vol = T4D_DEFAULT_PCM_VOL;
  3017. tmix->pan = T4D_DEFAULT_PCM_PAN;
  3018. tmix->rvol = T4D_DEFAULT_PCM_RVOL;
  3019. tmix->cvol = T4D_DEFAULT_PCM_CVOL;
  3020. }
  3021. snd_trident_enable_eso(trident);
  3022. snd_trident_proc_init(trident);
  3023. snd_card_set_dev(card, &pci->dev);
  3024. *rtrident = trident;
  3025. return 0;
  3026. }
  3027. /*---------------------------------------------------------------------------
  3028. snd_trident_free
  3029. Description: This routine will free the device specific class for
  3030. the 4DWave card.
  3031. Paramters: trident - device specific private data for 4DWave card
  3032. Returns: None.
  3033. ---------------------------------------------------------------------------*/
  3034. static int snd_trident_free(struct snd_trident *trident)
  3035. {
  3036. snd_trident_free_gameport(trident);
  3037. snd_trident_disable_eso(trident);
  3038. // Disable S/PDIF out
  3039. if (trident->device == TRIDENT_DEVICE_ID_NX)
  3040. outb(0x00, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  3041. else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  3042. outl(0, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  3043. }
  3044. if (trident->tlb.buffer.area) {
  3045. outl(0, TRID_REG(trident, NX_TLBC));
  3046. if (trident->tlb.memhdr)
  3047. snd_util_memhdr_free(trident->tlb.memhdr);
  3048. if (trident->tlb.silent_page.area)
  3049. snd_dma_free_pages(&trident->tlb.silent_page);
  3050. vfree(trident->tlb.shadow_entries);
  3051. snd_dma_free_pages(&trident->tlb.buffer);
  3052. }
  3053. if (trident->irq >= 0)
  3054. free_irq(trident->irq, trident);
  3055. pci_release_regions(trident->pci);
  3056. pci_disable_device(trident->pci);
  3057. kfree(trident);
  3058. return 0;
  3059. }
  3060. /*---------------------------------------------------------------------------
  3061. snd_trident_interrupt
  3062. Description: ISR for Trident 4DWave device
  3063. Paramters: trident - device specific private data for 4DWave card
  3064. Problems: It seems that Trident chips generates interrupts more than
  3065. one time in special cases. The spurious interrupts are
  3066. detected via sample timer (T4D_STIMER) and computing
  3067. corresponding delta value. The limits are detected with
  3068. the method try & fail so it is possible that it won't
  3069. work on all computers. [jaroslav]
  3070. Returns: None.
  3071. ---------------------------------------------------------------------------*/
  3072. static irqreturn_t snd_trident_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  3073. {
  3074. struct snd_trident *trident = dev_id;
  3075. unsigned int audio_int, chn_int, stimer, channel, mask, tmp;
  3076. int delta;
  3077. struct snd_trident_voice *voice;
  3078. audio_int = inl(TRID_REG(trident, T4D_MISCINT));
  3079. if ((audio_int & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
  3080. return IRQ_NONE;
  3081. if (audio_int & ADDRESS_IRQ) {
  3082. // get interrupt status for all channels
  3083. spin_lock(&trident->reg_lock);
  3084. stimer = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
  3085. chn_int = inl(TRID_REG(trident, T4D_AINT_A));
  3086. if (chn_int == 0)
  3087. goto __skip1;
  3088. outl(chn_int, TRID_REG(trident, T4D_AINT_A)); /* ack */
  3089. __skip1:
  3090. chn_int = inl(TRID_REG(trident, T4D_AINT_B));
  3091. if (chn_int == 0)
  3092. goto __skip2;
  3093. for (channel = 63; channel >= 32; channel--) {
  3094. mask = 1 << (channel&0x1f);
  3095. if ((chn_int & mask) == 0)
  3096. continue;
  3097. voice = &trident->synth.voices[channel];
  3098. if (!voice->pcm || voice->substream == NULL) {
  3099. outl(mask, TRID_REG(trident, T4D_STOP_B));
  3100. continue;
  3101. }
  3102. delta = (int)stimer - (int)voice->stimer;
  3103. if (delta < 0)
  3104. delta = -delta;
  3105. if ((unsigned int)delta < voice->spurious_threshold) {
  3106. /* do some statistics here */
  3107. trident->spurious_irq_count++;
  3108. if (trident->spurious_irq_max_delta < (unsigned int)delta)
  3109. trident->spurious_irq_max_delta = delta;
  3110. continue;
  3111. }
  3112. voice->stimer = stimer;
  3113. if (voice->isync) {
  3114. if (!voice->isync3) {
  3115. tmp = inw(TRID_REG(trident, T4D_SBBL_SBCL));
  3116. if (trident->bDMAStart & 0x40)
  3117. tmp >>= 1;
  3118. if (tmp > 0)
  3119. tmp = voice->isync_max - tmp;
  3120. } else {
  3121. tmp = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
  3122. }
  3123. if (tmp < voice->isync_mark) {
  3124. if (tmp > 0x10)
  3125. tmp = voice->isync_ESO - 7;
  3126. else
  3127. tmp = voice->isync_ESO + 2;
  3128. /* update ESO for IRQ voice to preserve sync */
  3129. snd_trident_stop_voice(trident, voice->number);
  3130. snd_trident_write_eso_reg(trident, voice, tmp);
  3131. snd_trident_start_voice(trident, voice->number);
  3132. }
  3133. } else if (voice->isync2) {
  3134. voice->isync2 = 0;
  3135. /* write original ESO and update CSO for IRQ voice to preserve sync */
  3136. snd_trident_stop_voice(trident, voice->number);
  3137. snd_trident_write_cso_reg(trident, voice, voice->isync_mark);
  3138. snd_trident_write_eso_reg(trident, voice, voice->ESO);
  3139. snd_trident_start_voice(trident, voice->number);
  3140. }
  3141. #if 0
  3142. if (voice->extra) {
  3143. /* update CSO for extra voice to preserve sync */
  3144. snd_trident_stop_voice(trident, voice->extra->number);
  3145. snd_trident_write_cso_reg(trident, voice->extra, 0);
  3146. snd_trident_start_voice(trident, voice->extra->number);
  3147. }
  3148. #endif
  3149. spin_unlock(&trident->reg_lock);
  3150. snd_pcm_period_elapsed(voice->substream);
  3151. spin_lock(&trident->reg_lock);
  3152. }
  3153. outl(chn_int, TRID_REG(trident, T4D_AINT_B)); /* ack */
  3154. __skip2:
  3155. spin_unlock(&trident->reg_lock);
  3156. }
  3157. if (audio_int & MPU401_IRQ) {
  3158. if (trident->rmidi) {
  3159. snd_mpu401_uart_interrupt(irq, trident->rmidi->private_data, regs);
  3160. } else {
  3161. inb(TRID_REG(trident, T4D_MPUR0));
  3162. }
  3163. }
  3164. // outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), TRID_REG(trident, T4D_MISCINT));
  3165. return IRQ_HANDLED;
  3166. }
  3167. /*---------------------------------------------------------------------------
  3168. snd_trident_attach_synthesizer
  3169. Description: Attach synthesizer hooks
  3170. Paramters: trident - device specific private data for 4DWave card
  3171. Returns: None.
  3172. ---------------------------------------------------------------------------*/
  3173. int snd_trident_attach_synthesizer(struct snd_trident *trident)
  3174. {
  3175. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  3176. if (snd_seq_device_new(trident->card, 1, SNDRV_SEQ_DEV_ID_TRIDENT,
  3177. sizeof(struct snd_trident *), &trident->seq_dev) >= 0) {
  3178. strcpy(trident->seq_dev->name, "4DWave");
  3179. *(struct snd_trident **)SNDRV_SEQ_DEVICE_ARGPTR(trident->seq_dev) = trident;
  3180. }
  3181. #endif
  3182. return 0;
  3183. }
  3184. struct snd_trident_voice *snd_trident_alloc_voice(struct snd_trident * trident, int type, int client, int port)
  3185. {
  3186. struct snd_trident_voice *pvoice;
  3187. unsigned long flags;
  3188. int idx;
  3189. spin_lock_irqsave(&trident->voice_alloc, flags);
  3190. if (type == SNDRV_TRIDENT_VOICE_TYPE_PCM) {
  3191. idx = snd_trident_allocate_pcm_channel(trident);
  3192. if(idx < 0) {
  3193. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3194. return NULL;
  3195. }
  3196. pvoice = &trident->synth.voices[idx];
  3197. pvoice->use = 1;
  3198. pvoice->pcm = 1;
  3199. pvoice->capture = 0;
  3200. pvoice->spdif = 0;
  3201. pvoice->memblk = NULL;
  3202. pvoice->substream = NULL;
  3203. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3204. return pvoice;
  3205. }
  3206. if (type == SNDRV_TRIDENT_VOICE_TYPE_SYNTH) {
  3207. idx = snd_trident_allocate_synth_channel(trident);
  3208. if(idx < 0) {
  3209. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3210. return NULL;
  3211. }
  3212. pvoice = &trident->synth.voices[idx];
  3213. pvoice->use = 1;
  3214. pvoice->synth = 1;
  3215. pvoice->client = client;
  3216. pvoice->port = port;
  3217. pvoice->memblk = NULL;
  3218. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3219. return pvoice;
  3220. }
  3221. if (type == SNDRV_TRIDENT_VOICE_TYPE_MIDI) {
  3222. }
  3223. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3224. return NULL;
  3225. }
  3226. void snd_trident_free_voice(struct snd_trident * trident, struct snd_trident_voice *voice)
  3227. {
  3228. unsigned long flags;
  3229. void (*private_free)(struct snd_trident_voice *);
  3230. void *private_data;
  3231. if (voice == NULL || !voice->use)
  3232. return;
  3233. snd_trident_clear_voices(trident, voice->number, voice->number);
  3234. spin_lock_irqsave(&trident->voice_alloc, flags);
  3235. private_free = voice->private_free;
  3236. private_data = voice->private_data;
  3237. voice->private_free = NULL;
  3238. voice->private_data = NULL;
  3239. if (voice->pcm)
  3240. snd_trident_free_pcm_channel(trident, voice->number);
  3241. if (voice->synth)
  3242. snd_trident_free_synth_channel(trident, voice->number);
  3243. voice->use = voice->pcm = voice->synth = voice->midi = 0;
  3244. voice->capture = voice->spdif = 0;
  3245. voice->sample_ops = NULL;
  3246. voice->substream = NULL;
  3247. voice->extra = NULL;
  3248. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3249. if (private_free)
  3250. private_free(voice);
  3251. }
  3252. static void snd_trident_clear_voices(struct snd_trident * trident, unsigned short v_min, unsigned short v_max)
  3253. {
  3254. unsigned int i, val, mask[2] = { 0, 0 };
  3255. snd_assert(v_min <= 63, return);
  3256. snd_assert(v_max <= 63, return);
  3257. for (i = v_min; i <= v_max; i++)
  3258. mask[i >> 5] |= 1 << (i & 0x1f);
  3259. if (mask[0]) {
  3260. outl(mask[0], TRID_REG(trident, T4D_STOP_A));
  3261. val = inl(TRID_REG(trident, T4D_AINTEN_A));
  3262. outl(val & ~mask[0], TRID_REG(trident, T4D_AINTEN_A));
  3263. }
  3264. if (mask[1]) {
  3265. outl(mask[1], TRID_REG(trident, T4D_STOP_B));
  3266. val = inl(TRID_REG(trident, T4D_AINTEN_B));
  3267. outl(val & ~mask[1], TRID_REG(trident, T4D_AINTEN_B));
  3268. }
  3269. }
  3270. #ifdef CONFIG_PM
  3271. int snd_trident_suspend(struct pci_dev *pci, pm_message_t state)
  3272. {
  3273. struct snd_card *card = pci_get_drvdata(pci);
  3274. struct snd_trident *trident = card->private_data;
  3275. trident->in_suspend = 1;
  3276. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  3277. snd_pcm_suspend_all(trident->pcm);
  3278. snd_pcm_suspend_all(trident->foldback);
  3279. snd_pcm_suspend_all(trident->spdif);
  3280. snd_ac97_suspend(trident->ac97);
  3281. snd_ac97_suspend(trident->ac97_sec);
  3282. switch (trident->device) {
  3283. case TRIDENT_DEVICE_ID_DX:
  3284. case TRIDENT_DEVICE_ID_NX:
  3285. break; /* TODO */
  3286. case TRIDENT_DEVICE_ID_SI7018:
  3287. break;
  3288. }
  3289. pci_disable_device(pci);
  3290. pci_save_state(pci);
  3291. return 0;
  3292. }
  3293. int snd_trident_resume(struct pci_dev *pci)
  3294. {
  3295. struct snd_card *card = pci_get_drvdata(pci);
  3296. struct snd_trident *trident = card->private_data;
  3297. pci_restore_state(pci);
  3298. pci_enable_device(pci);
  3299. pci_set_master(pci); /* to be sure */
  3300. switch (trident->device) {
  3301. case TRIDENT_DEVICE_ID_DX:
  3302. snd_trident_4d_dx_init(trident);
  3303. break;
  3304. case TRIDENT_DEVICE_ID_NX:
  3305. snd_trident_4d_nx_init(trident);
  3306. break;
  3307. case TRIDENT_DEVICE_ID_SI7018:
  3308. snd_trident_sis_init(trident);
  3309. break;
  3310. }
  3311. snd_ac97_resume(trident->ac97);
  3312. snd_ac97_resume(trident->ac97_sec);
  3313. /* restore some registers */
  3314. outl(trident->musicvol_wavevol, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  3315. snd_trident_enable_eso(trident);
  3316. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  3317. trident->in_suspend = 0;
  3318. return 0;
  3319. }
  3320. #endif /* CONFIG_PM */
  3321. EXPORT_SYMBOL(snd_trident_alloc_voice);
  3322. EXPORT_SYMBOL(snd_trident_free_voice);
  3323. EXPORT_SYMBOL(snd_trident_start_voice);
  3324. EXPORT_SYMBOL(snd_trident_stop_voice);
  3325. EXPORT_SYMBOL(snd_trident_write_voice_regs);
  3326. /* trident_memory.c symbols */
  3327. EXPORT_SYMBOL(snd_trident_synth_alloc);
  3328. EXPORT_SYMBOL(snd_trident_synth_free);
  3329. EXPORT_SYMBOL(snd_trident_synth_copy_from_user);