trident_main.c 123 KB

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