trident_main.c 122 KB

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