trident_main.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996
  1. /*
  2. * Maintained by Jaroslav Kysela <perex@perex.cz>
  3. * Originated by audio@tridentmicro.com
  4. * Fri Feb 19 15:55:28 MST 1999
  5. * Routines for control of Trident 4DWave (DX and NX) chip
  6. *
  7. * BUGS:
  8. *
  9. * TODO:
  10. * ---
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. *
  27. * SiS7018 S/PDIF support by Thomas Winischhofer <thomas@winischhofer.net>
  28. */
  29. #include <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 snd_pcm_substream *s;
  1230. unsigned int what, whati, capture_flag, spdif_flag;
  1231. struct snd_trident_voice *voice, *evoice;
  1232. unsigned int val, go;
  1233. switch (cmd) {
  1234. case SNDRV_PCM_TRIGGER_START:
  1235. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1236. case SNDRV_PCM_TRIGGER_RESUME:
  1237. go = 1;
  1238. break;
  1239. case SNDRV_PCM_TRIGGER_STOP:
  1240. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1241. case SNDRV_PCM_TRIGGER_SUSPEND:
  1242. go = 0;
  1243. break;
  1244. default:
  1245. return -EINVAL;
  1246. }
  1247. what = whati = capture_flag = spdif_flag = 0;
  1248. spin_lock(&trident->reg_lock);
  1249. val = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
  1250. snd_pcm_group_for_each_entry(s, substream) {
  1251. if ((struct snd_trident *) snd_pcm_substream_chip(s) == trident) {
  1252. voice = s->runtime->private_data;
  1253. evoice = voice->extra;
  1254. what |= 1 << (voice->number & 0x1f);
  1255. if (evoice == NULL) {
  1256. whati |= 1 << (voice->number & 0x1f);
  1257. } else {
  1258. what |= 1 << (evoice->number & 0x1f);
  1259. whati |= 1 << (evoice->number & 0x1f);
  1260. if (go)
  1261. evoice->stimer = val;
  1262. }
  1263. if (go) {
  1264. voice->running = 1;
  1265. voice->stimer = val;
  1266. } else {
  1267. voice->running = 0;
  1268. }
  1269. snd_pcm_trigger_done(s, substream);
  1270. if (voice->capture)
  1271. capture_flag = 1;
  1272. if (voice->spdif)
  1273. spdif_flag = 1;
  1274. }
  1275. }
  1276. if (spdif_flag) {
  1277. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1278. outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  1279. outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1280. } else {
  1281. outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
  1282. val = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) | SPDIF_EN;
  1283. outl(val, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1284. }
  1285. }
  1286. if (!go)
  1287. outl(what, TRID_REG(trident, T4D_STOP_B));
  1288. val = inl(TRID_REG(trident, T4D_AINTEN_B));
  1289. if (go) {
  1290. val |= whati;
  1291. } else {
  1292. val &= ~whati;
  1293. }
  1294. outl(val, TRID_REG(trident, T4D_AINTEN_B));
  1295. if (go) {
  1296. outl(what, TRID_REG(trident, T4D_START_B));
  1297. if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
  1298. outb(trident->bDMAStart, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
  1299. } else {
  1300. if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
  1301. outb(0x00, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
  1302. }
  1303. spin_unlock(&trident->reg_lock);
  1304. return 0;
  1305. }
  1306. /*---------------------------------------------------------------------------
  1307. snd_trident_playback_pointer
  1308. Description: This routine return the playback position
  1309. Parameters: substream - PCM substream class
  1310. Returns: position of buffer
  1311. ---------------------------------------------------------------------------*/
  1312. static snd_pcm_uframes_t snd_trident_playback_pointer(struct snd_pcm_substream *substream)
  1313. {
  1314. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1315. struct snd_pcm_runtime *runtime = substream->runtime;
  1316. struct snd_trident_voice *voice = runtime->private_data;
  1317. unsigned int cso;
  1318. if (!voice->running)
  1319. return 0;
  1320. spin_lock(&trident->reg_lock);
  1321. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  1322. if (trident->device != TRIDENT_DEVICE_ID_NX) {
  1323. cso = inw(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
  1324. } else { // ID_4DWAVE_NX
  1325. cso = (unsigned int) inl(TRID_REG(trident, CH_NX_DELTA_CSO)) & 0x00ffffff;
  1326. }
  1327. spin_unlock(&trident->reg_lock);
  1328. if (cso >= runtime->buffer_size)
  1329. cso = 0;
  1330. return cso;
  1331. }
  1332. /*---------------------------------------------------------------------------
  1333. snd_trident_capture_pointer
  1334. Description: This routine return the capture position
  1335. Paramters: pcm1 - PCM device class
  1336. Returns: position of buffer
  1337. ---------------------------------------------------------------------------*/
  1338. static snd_pcm_uframes_t snd_trident_capture_pointer(struct snd_pcm_substream *substream)
  1339. {
  1340. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1341. struct snd_pcm_runtime *runtime = substream->runtime;
  1342. struct snd_trident_voice *voice = runtime->private_data;
  1343. unsigned int result;
  1344. if (!voice->running)
  1345. return 0;
  1346. result = inw(TRID_REG(trident, T4D_SBBL_SBCL));
  1347. if (runtime->channels > 1)
  1348. result >>= 1;
  1349. if (result > 0)
  1350. result = runtime->buffer_size - result;
  1351. return result;
  1352. }
  1353. /*---------------------------------------------------------------------------
  1354. snd_trident_spdif_pointer
  1355. Description: This routine return the SPDIF playback position
  1356. Parameters: substream - PCM substream class
  1357. Returns: position of buffer
  1358. ---------------------------------------------------------------------------*/
  1359. static snd_pcm_uframes_t snd_trident_spdif_pointer(struct snd_pcm_substream *substream)
  1360. {
  1361. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1362. struct snd_pcm_runtime *runtime = substream->runtime;
  1363. struct snd_trident_voice *voice = runtime->private_data;
  1364. unsigned int result;
  1365. if (!voice->running)
  1366. return 0;
  1367. result = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
  1368. return result;
  1369. }
  1370. /*
  1371. * Playback support device description
  1372. */
  1373. static struct snd_pcm_hardware snd_trident_playback =
  1374. {
  1375. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1376. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1377. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1378. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1379. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1380. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1381. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1382. .rate_min = 4000,
  1383. .rate_max = 48000,
  1384. .channels_min = 1,
  1385. .channels_max = 2,
  1386. .buffer_bytes_max = (256*1024),
  1387. .period_bytes_min = 64,
  1388. .period_bytes_max = (256*1024),
  1389. .periods_min = 1,
  1390. .periods_max = 1024,
  1391. .fifo_size = 0,
  1392. };
  1393. /*
  1394. * Capture support device description
  1395. */
  1396. static struct snd_pcm_hardware snd_trident_capture =
  1397. {
  1398. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1399. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1400. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1401. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1402. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1403. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1404. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1405. .rate_min = 4000,
  1406. .rate_max = 48000,
  1407. .channels_min = 1,
  1408. .channels_max = 2,
  1409. .buffer_bytes_max = (128*1024),
  1410. .period_bytes_min = 64,
  1411. .period_bytes_max = (128*1024),
  1412. .periods_min = 1,
  1413. .periods_max = 1024,
  1414. .fifo_size = 0,
  1415. };
  1416. /*
  1417. * Foldback capture support device description
  1418. */
  1419. static struct snd_pcm_hardware snd_trident_foldback =
  1420. {
  1421. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1422. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1423. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1424. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1425. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1426. .rates = SNDRV_PCM_RATE_48000,
  1427. .rate_min = 48000,
  1428. .rate_max = 48000,
  1429. .channels_min = 2,
  1430. .channels_max = 2,
  1431. .buffer_bytes_max = (128*1024),
  1432. .period_bytes_min = 64,
  1433. .period_bytes_max = (128*1024),
  1434. .periods_min = 1,
  1435. .periods_max = 1024,
  1436. .fifo_size = 0,
  1437. };
  1438. /*
  1439. * SPDIF playback support device description
  1440. */
  1441. static struct snd_pcm_hardware snd_trident_spdif =
  1442. {
  1443. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1444. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1445. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1446. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1447. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1448. .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  1449. SNDRV_PCM_RATE_48000),
  1450. .rate_min = 32000,
  1451. .rate_max = 48000,
  1452. .channels_min = 2,
  1453. .channels_max = 2,
  1454. .buffer_bytes_max = (128*1024),
  1455. .period_bytes_min = 64,
  1456. .period_bytes_max = (128*1024),
  1457. .periods_min = 1,
  1458. .periods_max = 1024,
  1459. .fifo_size = 0,
  1460. };
  1461. static struct snd_pcm_hardware snd_trident_spdif_7018 =
  1462. {
  1463. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1464. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1465. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1466. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1467. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1468. .rates = SNDRV_PCM_RATE_48000,
  1469. .rate_min = 48000,
  1470. .rate_max = 48000,
  1471. .channels_min = 2,
  1472. .channels_max = 2,
  1473. .buffer_bytes_max = (128*1024),
  1474. .period_bytes_min = 64,
  1475. .period_bytes_max = (128*1024),
  1476. .periods_min = 1,
  1477. .periods_max = 1024,
  1478. .fifo_size = 0,
  1479. };
  1480. static void snd_trident_pcm_free_substream(struct snd_pcm_runtime *runtime)
  1481. {
  1482. struct snd_trident_voice *voice = runtime->private_data;
  1483. struct snd_trident *trident;
  1484. if (voice) {
  1485. trident = voice->trident;
  1486. snd_trident_free_voice(trident, voice);
  1487. }
  1488. }
  1489. static int snd_trident_playback_open(struct snd_pcm_substream *substream)
  1490. {
  1491. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1492. struct snd_pcm_runtime *runtime = substream->runtime;
  1493. struct snd_trident_voice *voice;
  1494. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1495. if (voice == NULL)
  1496. return -EAGAIN;
  1497. snd_trident_pcm_mixer_build(trident, voice, substream);
  1498. voice->substream = substream;
  1499. runtime->private_data = voice;
  1500. runtime->private_free = snd_trident_pcm_free_substream;
  1501. runtime->hw = snd_trident_playback;
  1502. snd_pcm_set_sync(substream);
  1503. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1504. return 0;
  1505. }
  1506. /*---------------------------------------------------------------------------
  1507. snd_trident_playback_close
  1508. Description: This routine will close the 4DWave playback device. For now
  1509. we will simply free the dma transfer buffer.
  1510. Parameters: substream - PCM substream class
  1511. ---------------------------------------------------------------------------*/
  1512. static int snd_trident_playback_close(struct snd_pcm_substream *substream)
  1513. {
  1514. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1515. struct snd_pcm_runtime *runtime = substream->runtime;
  1516. struct snd_trident_voice *voice = runtime->private_data;
  1517. snd_trident_pcm_mixer_free(trident, voice, substream);
  1518. return 0;
  1519. }
  1520. /*---------------------------------------------------------------------------
  1521. snd_trident_spdif_open
  1522. Description: This routine will open the 4DWave SPDIF device.
  1523. Parameters: substream - PCM substream class
  1524. Returns: status - success or failure flag
  1525. ---------------------------------------------------------------------------*/
  1526. static int snd_trident_spdif_open(struct snd_pcm_substream *substream)
  1527. {
  1528. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1529. struct snd_trident_voice *voice;
  1530. struct snd_pcm_runtime *runtime = substream->runtime;
  1531. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1532. if (voice == NULL)
  1533. return -EAGAIN;
  1534. voice->spdif = 1;
  1535. voice->substream = substream;
  1536. spin_lock_irq(&trident->reg_lock);
  1537. trident->spdif_pcm_bits = trident->spdif_bits;
  1538. spin_unlock_irq(&trident->reg_lock);
  1539. runtime->private_data = voice;
  1540. runtime->private_free = snd_trident_pcm_free_substream;
  1541. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  1542. runtime->hw = snd_trident_spdif;
  1543. } else {
  1544. runtime->hw = snd_trident_spdif_7018;
  1545. }
  1546. trident->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1547. snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1548. SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
  1549. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1550. return 0;
  1551. }
  1552. /*---------------------------------------------------------------------------
  1553. snd_trident_spdif_close
  1554. Description: This routine will close the 4DWave SPDIF device.
  1555. Parameters: substream - PCM substream class
  1556. ---------------------------------------------------------------------------*/
  1557. static int snd_trident_spdif_close(struct snd_pcm_substream *substream)
  1558. {
  1559. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1560. unsigned int temp;
  1561. spin_lock_irq(&trident->reg_lock);
  1562. // restore default SPDIF setting
  1563. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1564. outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1565. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  1566. } else {
  1567. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1568. temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1569. if (trident->spdif_ctrl) {
  1570. temp |= SPDIF_EN;
  1571. } else {
  1572. temp &= ~SPDIF_EN;
  1573. }
  1574. outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1575. }
  1576. spin_unlock_irq(&trident->reg_lock);
  1577. trident->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1578. snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1579. SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
  1580. return 0;
  1581. }
  1582. /*---------------------------------------------------------------------------
  1583. snd_trident_capture_open
  1584. Description: This routine will open the 4DWave capture device.
  1585. Parameters: substream - PCM substream class
  1586. Returns: status - success or failure flag
  1587. ---------------------------------------------------------------------------*/
  1588. static int snd_trident_capture_open(struct snd_pcm_substream *substream)
  1589. {
  1590. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1591. struct snd_trident_voice *voice;
  1592. struct snd_pcm_runtime *runtime = substream->runtime;
  1593. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1594. if (voice == NULL)
  1595. return -EAGAIN;
  1596. voice->capture = 1;
  1597. voice->substream = substream;
  1598. runtime->private_data = voice;
  1599. runtime->private_free = snd_trident_pcm_free_substream;
  1600. runtime->hw = snd_trident_capture;
  1601. snd_pcm_set_sync(substream);
  1602. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1603. return 0;
  1604. }
  1605. /*---------------------------------------------------------------------------
  1606. snd_trident_capture_close
  1607. Description: This routine will close the 4DWave capture device. For now
  1608. we will simply free the dma transfer buffer.
  1609. Parameters: substream - PCM substream class
  1610. ---------------------------------------------------------------------------*/
  1611. static int snd_trident_capture_close(struct snd_pcm_substream *substream)
  1612. {
  1613. return 0;
  1614. }
  1615. /*---------------------------------------------------------------------------
  1616. snd_trident_foldback_open
  1617. Description: This routine will open the 4DWave foldback capture device.
  1618. Parameters: substream - PCM substream class
  1619. Returns: status - success or failure flag
  1620. ---------------------------------------------------------------------------*/
  1621. static int snd_trident_foldback_open(struct snd_pcm_substream *substream)
  1622. {
  1623. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1624. struct snd_trident_voice *voice;
  1625. struct snd_pcm_runtime *runtime = substream->runtime;
  1626. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1627. if (voice == NULL)
  1628. return -EAGAIN;
  1629. voice->foldback_chan = substream->number;
  1630. voice->substream = substream;
  1631. runtime->private_data = voice;
  1632. runtime->private_free = snd_trident_pcm_free_substream;
  1633. runtime->hw = snd_trident_foldback;
  1634. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1635. return 0;
  1636. }
  1637. /*---------------------------------------------------------------------------
  1638. snd_trident_foldback_close
  1639. Description: This routine will close the 4DWave foldback capture device.
  1640. For now we will simply free the dma transfer buffer.
  1641. Parameters: substream - PCM substream class
  1642. ---------------------------------------------------------------------------*/
  1643. static int snd_trident_foldback_close(struct snd_pcm_substream *substream)
  1644. {
  1645. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1646. struct snd_trident_voice *voice;
  1647. struct snd_pcm_runtime *runtime = substream->runtime;
  1648. voice = runtime->private_data;
  1649. /* stop capture channel */
  1650. spin_lock_irq(&trident->reg_lock);
  1651. outb(0x00, TRID_REG(trident, T4D_RCI + voice->foldback_chan));
  1652. spin_unlock_irq(&trident->reg_lock);
  1653. return 0;
  1654. }
  1655. /*---------------------------------------------------------------------------
  1656. PCM operations
  1657. ---------------------------------------------------------------------------*/
  1658. static struct snd_pcm_ops snd_trident_playback_ops = {
  1659. .open = snd_trident_playback_open,
  1660. .close = snd_trident_playback_close,
  1661. .ioctl = snd_trident_ioctl,
  1662. .hw_params = snd_trident_hw_params,
  1663. .hw_free = snd_trident_hw_free,
  1664. .prepare = snd_trident_playback_prepare,
  1665. .trigger = snd_trident_trigger,
  1666. .pointer = snd_trident_playback_pointer,
  1667. };
  1668. static struct snd_pcm_ops snd_trident_nx_playback_ops = {
  1669. .open = snd_trident_playback_open,
  1670. .close = snd_trident_playback_close,
  1671. .ioctl = snd_trident_ioctl,
  1672. .hw_params = snd_trident_hw_params,
  1673. .hw_free = snd_trident_hw_free,
  1674. .prepare = snd_trident_playback_prepare,
  1675. .trigger = snd_trident_trigger,
  1676. .pointer = snd_trident_playback_pointer,
  1677. .page = snd_pcm_sgbuf_ops_page,
  1678. };
  1679. static struct snd_pcm_ops snd_trident_capture_ops = {
  1680. .open = snd_trident_capture_open,
  1681. .close = snd_trident_capture_close,
  1682. .ioctl = snd_trident_ioctl,
  1683. .hw_params = snd_trident_capture_hw_params,
  1684. .hw_free = snd_trident_hw_free,
  1685. .prepare = snd_trident_capture_prepare,
  1686. .trigger = snd_trident_trigger,
  1687. .pointer = snd_trident_capture_pointer,
  1688. };
  1689. static struct snd_pcm_ops snd_trident_si7018_capture_ops = {
  1690. .open = snd_trident_capture_open,
  1691. .close = snd_trident_capture_close,
  1692. .ioctl = snd_trident_ioctl,
  1693. .hw_params = snd_trident_si7018_capture_hw_params,
  1694. .hw_free = snd_trident_si7018_capture_hw_free,
  1695. .prepare = snd_trident_si7018_capture_prepare,
  1696. .trigger = snd_trident_trigger,
  1697. .pointer = snd_trident_playback_pointer,
  1698. };
  1699. static struct snd_pcm_ops snd_trident_foldback_ops = {
  1700. .open = snd_trident_foldback_open,
  1701. .close = snd_trident_foldback_close,
  1702. .ioctl = snd_trident_ioctl,
  1703. .hw_params = snd_trident_hw_params,
  1704. .hw_free = snd_trident_hw_free,
  1705. .prepare = snd_trident_foldback_prepare,
  1706. .trigger = snd_trident_trigger,
  1707. .pointer = snd_trident_playback_pointer,
  1708. };
  1709. static struct snd_pcm_ops snd_trident_nx_foldback_ops = {
  1710. .open = snd_trident_foldback_open,
  1711. .close = snd_trident_foldback_close,
  1712. .ioctl = snd_trident_ioctl,
  1713. .hw_params = snd_trident_hw_params,
  1714. .hw_free = snd_trident_hw_free,
  1715. .prepare = snd_trident_foldback_prepare,
  1716. .trigger = snd_trident_trigger,
  1717. .pointer = snd_trident_playback_pointer,
  1718. .page = snd_pcm_sgbuf_ops_page,
  1719. };
  1720. static struct snd_pcm_ops snd_trident_spdif_ops = {
  1721. .open = snd_trident_spdif_open,
  1722. .close = snd_trident_spdif_close,
  1723. .ioctl = snd_trident_ioctl,
  1724. .hw_params = snd_trident_spdif_hw_params,
  1725. .hw_free = snd_trident_hw_free,
  1726. .prepare = snd_trident_spdif_prepare,
  1727. .trigger = snd_trident_trigger,
  1728. .pointer = snd_trident_spdif_pointer,
  1729. };
  1730. static struct snd_pcm_ops snd_trident_spdif_7018_ops = {
  1731. .open = snd_trident_spdif_open,
  1732. .close = snd_trident_spdif_close,
  1733. .ioctl = snd_trident_ioctl,
  1734. .hw_params = snd_trident_spdif_hw_params,
  1735. .hw_free = snd_trident_hw_free,
  1736. .prepare = snd_trident_spdif_prepare,
  1737. .trigger = snd_trident_trigger,
  1738. .pointer = snd_trident_playback_pointer,
  1739. };
  1740. /*---------------------------------------------------------------------------
  1741. snd_trident_pcm
  1742. Description: This routine registers the 4DWave device for PCM support.
  1743. Paramters: trident - pointer to target device class for 4DWave.
  1744. Returns: None
  1745. ---------------------------------------------------------------------------*/
  1746. int __devinit snd_trident_pcm(struct snd_trident * trident,
  1747. int device, struct snd_pcm ** rpcm)
  1748. {
  1749. struct snd_pcm *pcm;
  1750. int err;
  1751. if (rpcm)
  1752. *rpcm = NULL;
  1753. if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, trident->ChanPCM, 1, &pcm)) < 0)
  1754. return err;
  1755. pcm->private_data = trident;
  1756. if (trident->tlb.entries) {
  1757. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_nx_playback_ops);
  1758. } else {
  1759. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_playback_ops);
  1760. }
  1761. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1762. trident->device != TRIDENT_DEVICE_ID_SI7018 ?
  1763. &snd_trident_capture_ops :
  1764. &snd_trident_si7018_capture_ops);
  1765. pcm->info_flags = 0;
  1766. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1767. strcpy(pcm->name, "Trident 4DWave");
  1768. trident->pcm = pcm;
  1769. if (trident->tlb.entries) {
  1770. struct snd_pcm_substream *substream;
  1771. for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
  1772. snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
  1773. snd_dma_pci_data(trident->pci),
  1774. 64*1024, 128*1024);
  1775. snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  1776. SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
  1777. 64*1024, 128*1024);
  1778. } else {
  1779. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1780. snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
  1781. }
  1782. if (rpcm)
  1783. *rpcm = pcm;
  1784. return 0;
  1785. }
  1786. /*---------------------------------------------------------------------------
  1787. snd_trident_foldback_pcm
  1788. Description: This routine registers the 4DWave device for foldback PCM support.
  1789. Paramters: trident - pointer to target device class for 4DWave.
  1790. Returns: None
  1791. ---------------------------------------------------------------------------*/
  1792. int __devinit snd_trident_foldback_pcm(struct snd_trident * trident,
  1793. int device, struct snd_pcm ** rpcm)
  1794. {
  1795. struct snd_pcm *foldback;
  1796. int err;
  1797. int num_chan = 3;
  1798. struct snd_pcm_substream *substream;
  1799. if (rpcm)
  1800. *rpcm = NULL;
  1801. if (trident->device == TRIDENT_DEVICE_ID_NX)
  1802. num_chan = 4;
  1803. if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, 0, num_chan, &foldback)) < 0)
  1804. return err;
  1805. foldback->private_data = trident;
  1806. if (trident->tlb.entries)
  1807. snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_nx_foldback_ops);
  1808. else
  1809. snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_foldback_ops);
  1810. foldback->info_flags = 0;
  1811. strcpy(foldback->name, "Trident 4DWave");
  1812. substream = foldback->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1813. strcpy(substream->name, "Front Mixer");
  1814. substream = substream->next;
  1815. strcpy(substream->name, "Reverb Mixer");
  1816. substream = substream->next;
  1817. strcpy(substream->name, "Chorus Mixer");
  1818. if (num_chan == 4) {
  1819. substream = substream->next;
  1820. strcpy(substream->name, "Second AC'97 ADC");
  1821. }
  1822. trident->foldback = foldback;
  1823. if (trident->tlb.entries)
  1824. snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV_SG,
  1825. snd_dma_pci_data(trident->pci), 0, 128*1024);
  1826. else
  1827. snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV,
  1828. snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
  1829. if (rpcm)
  1830. *rpcm = foldback;
  1831. return 0;
  1832. }
  1833. /*---------------------------------------------------------------------------
  1834. snd_trident_spdif
  1835. Description: This routine registers the 4DWave-NX device for SPDIF support.
  1836. Paramters: trident - pointer to target device class for 4DWave-NX.
  1837. Returns: None
  1838. ---------------------------------------------------------------------------*/
  1839. int __devinit snd_trident_spdif_pcm(struct snd_trident * trident,
  1840. int device, struct snd_pcm ** rpcm)
  1841. {
  1842. struct snd_pcm *spdif;
  1843. int err;
  1844. if (rpcm)
  1845. *rpcm = NULL;
  1846. if ((err = snd_pcm_new(trident->card, "trident_dx_nx IEC958", device, 1, 0, &spdif)) < 0)
  1847. return err;
  1848. spdif->private_data = trident;
  1849. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1850. snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_ops);
  1851. } else {
  1852. snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_7018_ops);
  1853. }
  1854. spdif->info_flags = 0;
  1855. strcpy(spdif->name, "Trident 4DWave IEC958");
  1856. trident->spdif = spdif;
  1857. snd_pcm_lib_preallocate_pages_for_all(spdif, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
  1858. if (rpcm)
  1859. *rpcm = spdif;
  1860. return 0;
  1861. }
  1862. /*
  1863. * Mixer part
  1864. */
  1865. /*---------------------------------------------------------------------------
  1866. snd_trident_spdif_control
  1867. Description: enable/disable S/PDIF out from ac97 mixer
  1868. ---------------------------------------------------------------------------*/
  1869. #define snd_trident_spdif_control_info snd_ctl_boolean_mono_info
  1870. static int snd_trident_spdif_control_get(struct snd_kcontrol *kcontrol,
  1871. struct snd_ctl_elem_value *ucontrol)
  1872. {
  1873. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1874. unsigned char val;
  1875. spin_lock_irq(&trident->reg_lock);
  1876. val = trident->spdif_ctrl;
  1877. ucontrol->value.integer.value[0] = val == kcontrol->private_value;
  1878. spin_unlock_irq(&trident->reg_lock);
  1879. return 0;
  1880. }
  1881. static int snd_trident_spdif_control_put(struct snd_kcontrol *kcontrol,
  1882. struct snd_ctl_elem_value *ucontrol)
  1883. {
  1884. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1885. unsigned char val;
  1886. int change;
  1887. val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
  1888. spin_lock_irq(&trident->reg_lock);
  1889. /* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
  1890. change = trident->spdif_ctrl != val;
  1891. trident->spdif_ctrl = val;
  1892. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1893. if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
  1894. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  1895. outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1896. }
  1897. } else {
  1898. if (trident->spdif == NULL) {
  1899. unsigned int temp;
  1900. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1901. temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
  1902. if (val)
  1903. temp |= SPDIF_EN;
  1904. outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1905. }
  1906. }
  1907. spin_unlock_irq(&trident->reg_lock);
  1908. return change;
  1909. }
  1910. static struct snd_kcontrol_new snd_trident_spdif_control __devinitdata =
  1911. {
  1912. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1913. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  1914. .info = snd_trident_spdif_control_info,
  1915. .get = snd_trident_spdif_control_get,
  1916. .put = snd_trident_spdif_control_put,
  1917. .private_value = 0x28,
  1918. };
  1919. /*---------------------------------------------------------------------------
  1920. snd_trident_spdif_default
  1921. Description: put/get the S/PDIF default settings
  1922. ---------------------------------------------------------------------------*/
  1923. static int snd_trident_spdif_default_info(struct snd_kcontrol *kcontrol,
  1924. struct snd_ctl_elem_info *uinfo)
  1925. {
  1926. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1927. uinfo->count = 1;
  1928. return 0;
  1929. }
  1930. static int snd_trident_spdif_default_get(struct snd_kcontrol *kcontrol,
  1931. struct snd_ctl_elem_value *ucontrol)
  1932. {
  1933. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1934. spin_lock_irq(&trident->reg_lock);
  1935. ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
  1936. ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
  1937. ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
  1938. ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
  1939. spin_unlock_irq(&trident->reg_lock);
  1940. return 0;
  1941. }
  1942. static int snd_trident_spdif_default_put(struct snd_kcontrol *kcontrol,
  1943. struct snd_ctl_elem_value *ucontrol)
  1944. {
  1945. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1946. unsigned int val;
  1947. int change;
  1948. val = (ucontrol->value.iec958.status[0] << 0) |
  1949. (ucontrol->value.iec958.status[1] << 8) |
  1950. (ucontrol->value.iec958.status[2] << 16) |
  1951. (ucontrol->value.iec958.status[3] << 24);
  1952. spin_lock_irq(&trident->reg_lock);
  1953. change = trident->spdif_bits != val;
  1954. trident->spdif_bits = val;
  1955. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1956. if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
  1957. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  1958. } else {
  1959. if (trident->spdif == NULL)
  1960. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1961. }
  1962. spin_unlock_irq(&trident->reg_lock);
  1963. return change;
  1964. }
  1965. static struct snd_kcontrol_new snd_trident_spdif_default __devinitdata =
  1966. {
  1967. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1968. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1969. .info = snd_trident_spdif_default_info,
  1970. .get = snd_trident_spdif_default_get,
  1971. .put = snd_trident_spdif_default_put
  1972. };
  1973. /*---------------------------------------------------------------------------
  1974. snd_trident_spdif_mask
  1975. Description: put/get the S/PDIF mask
  1976. ---------------------------------------------------------------------------*/
  1977. static int snd_trident_spdif_mask_info(struct snd_kcontrol *kcontrol,
  1978. struct snd_ctl_elem_info *uinfo)
  1979. {
  1980. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1981. uinfo->count = 1;
  1982. return 0;
  1983. }
  1984. static int snd_trident_spdif_mask_get(struct snd_kcontrol *kcontrol,
  1985. struct snd_ctl_elem_value *ucontrol)
  1986. {
  1987. ucontrol->value.iec958.status[0] = 0xff;
  1988. ucontrol->value.iec958.status[1] = 0xff;
  1989. ucontrol->value.iec958.status[2] = 0xff;
  1990. ucontrol->value.iec958.status[3] = 0xff;
  1991. return 0;
  1992. }
  1993. static struct snd_kcontrol_new snd_trident_spdif_mask __devinitdata =
  1994. {
  1995. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1996. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1997. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  1998. .info = snd_trident_spdif_mask_info,
  1999. .get = snd_trident_spdif_mask_get,
  2000. };
  2001. /*---------------------------------------------------------------------------
  2002. snd_trident_spdif_stream
  2003. Description: put/get the S/PDIF stream settings
  2004. ---------------------------------------------------------------------------*/
  2005. static int snd_trident_spdif_stream_info(struct snd_kcontrol *kcontrol,
  2006. struct snd_ctl_elem_info *uinfo)
  2007. {
  2008. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  2009. uinfo->count = 1;
  2010. return 0;
  2011. }
  2012. static int snd_trident_spdif_stream_get(struct snd_kcontrol *kcontrol,
  2013. struct snd_ctl_elem_value *ucontrol)
  2014. {
  2015. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2016. spin_lock_irq(&trident->reg_lock);
  2017. ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
  2018. ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
  2019. ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
  2020. ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
  2021. spin_unlock_irq(&trident->reg_lock);
  2022. return 0;
  2023. }
  2024. static int snd_trident_spdif_stream_put(struct snd_kcontrol *kcontrol,
  2025. struct snd_ctl_elem_value *ucontrol)
  2026. {
  2027. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2028. unsigned int val;
  2029. int change;
  2030. val = (ucontrol->value.iec958.status[0] << 0) |
  2031. (ucontrol->value.iec958.status[1] << 8) |
  2032. (ucontrol->value.iec958.status[2] << 16) |
  2033. (ucontrol->value.iec958.status[3] << 24);
  2034. spin_lock_irq(&trident->reg_lock);
  2035. change = trident->spdif_pcm_bits != val;
  2036. trident->spdif_pcm_bits = val;
  2037. if (trident->spdif != NULL) {
  2038. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  2039. outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  2040. } else {
  2041. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  2042. }
  2043. }
  2044. spin_unlock_irq(&trident->reg_lock);
  2045. return change;
  2046. }
  2047. static struct snd_kcontrol_new snd_trident_spdif_stream __devinitdata =
  2048. {
  2049. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2050. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2051. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  2052. .info = snd_trident_spdif_stream_info,
  2053. .get = snd_trident_spdif_stream_get,
  2054. .put = snd_trident_spdif_stream_put
  2055. };
  2056. /*---------------------------------------------------------------------------
  2057. snd_trident_ac97_control
  2058. Description: enable/disable rear path for ac97
  2059. ---------------------------------------------------------------------------*/
  2060. #define snd_trident_ac97_control_info snd_ctl_boolean_mono_info
  2061. static int snd_trident_ac97_control_get(struct snd_kcontrol *kcontrol,
  2062. struct snd_ctl_elem_value *ucontrol)
  2063. {
  2064. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2065. unsigned char val;
  2066. spin_lock_irq(&trident->reg_lock);
  2067. val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2068. ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
  2069. spin_unlock_irq(&trident->reg_lock);
  2070. return 0;
  2071. }
  2072. static int snd_trident_ac97_control_put(struct snd_kcontrol *kcontrol,
  2073. struct snd_ctl_elem_value *ucontrol)
  2074. {
  2075. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2076. unsigned char val;
  2077. int change = 0;
  2078. spin_lock_irq(&trident->reg_lock);
  2079. val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2080. val &= ~(1 << kcontrol->private_value);
  2081. if (ucontrol->value.integer.value[0])
  2082. val |= 1 << kcontrol->private_value;
  2083. change = val != trident->ac97_ctrl;
  2084. trident->ac97_ctrl = val;
  2085. outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2086. spin_unlock_irq(&trident->reg_lock);
  2087. return change;
  2088. }
  2089. static struct snd_kcontrol_new snd_trident_ac97_rear_control __devinitdata =
  2090. {
  2091. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2092. .name = "Rear Path",
  2093. .info = snd_trident_ac97_control_info,
  2094. .get = snd_trident_ac97_control_get,
  2095. .put = snd_trident_ac97_control_put,
  2096. .private_value = 4,
  2097. };
  2098. /*---------------------------------------------------------------------------
  2099. snd_trident_vol_control
  2100. Description: wave & music volume control
  2101. ---------------------------------------------------------------------------*/
  2102. static int snd_trident_vol_control_info(struct snd_kcontrol *kcontrol,
  2103. struct snd_ctl_elem_info *uinfo)
  2104. {
  2105. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2106. uinfo->count = 2;
  2107. uinfo->value.integer.min = 0;
  2108. uinfo->value.integer.max = 255;
  2109. return 0;
  2110. }
  2111. static int snd_trident_vol_control_get(struct snd_kcontrol *kcontrol,
  2112. struct snd_ctl_elem_value *ucontrol)
  2113. {
  2114. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2115. unsigned int val;
  2116. val = trident->musicvol_wavevol;
  2117. ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
  2118. ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
  2119. return 0;
  2120. }
  2121. static const DECLARE_TLV_DB_SCALE(db_scale_gvol, -6375, 25, 0);
  2122. static int snd_trident_vol_control_put(struct snd_kcontrol *kcontrol,
  2123. struct snd_ctl_elem_value *ucontrol)
  2124. {
  2125. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2126. unsigned int val;
  2127. int change = 0;
  2128. spin_lock_irq(&trident->reg_lock);
  2129. val = trident->musicvol_wavevol;
  2130. val &= ~(0xffff << kcontrol->private_value);
  2131. val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
  2132. ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
  2133. change = val != trident->musicvol_wavevol;
  2134. outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  2135. spin_unlock_irq(&trident->reg_lock);
  2136. return change;
  2137. }
  2138. static struct snd_kcontrol_new snd_trident_vol_music_control __devinitdata =
  2139. {
  2140. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2141. .name = "Music Playback Volume",
  2142. .info = snd_trident_vol_control_info,
  2143. .get = snd_trident_vol_control_get,
  2144. .put = snd_trident_vol_control_put,
  2145. .private_value = 16,
  2146. .tlv = { .p = db_scale_gvol },
  2147. };
  2148. static struct snd_kcontrol_new snd_trident_vol_wave_control __devinitdata =
  2149. {
  2150. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2151. .name = "Wave Playback Volume",
  2152. .info = snd_trident_vol_control_info,
  2153. .get = snd_trident_vol_control_get,
  2154. .put = snd_trident_vol_control_put,
  2155. .private_value = 0,
  2156. .tlv = { .p = db_scale_gvol },
  2157. };
  2158. /*---------------------------------------------------------------------------
  2159. snd_trident_pcm_vol_control
  2160. Description: PCM front volume control
  2161. ---------------------------------------------------------------------------*/
  2162. static int snd_trident_pcm_vol_control_info(struct snd_kcontrol *kcontrol,
  2163. struct snd_ctl_elem_info *uinfo)
  2164. {
  2165. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2166. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2167. uinfo->count = 1;
  2168. uinfo->value.integer.min = 0;
  2169. uinfo->value.integer.max = 255;
  2170. if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  2171. uinfo->value.integer.max = 1023;
  2172. return 0;
  2173. }
  2174. static int snd_trident_pcm_vol_control_get(struct snd_kcontrol *kcontrol,
  2175. struct snd_ctl_elem_value *ucontrol)
  2176. {
  2177. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2178. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2179. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2180. ucontrol->value.integer.value[0] = 1023 - mix->vol;
  2181. } else {
  2182. ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
  2183. }
  2184. return 0;
  2185. }
  2186. static int snd_trident_pcm_vol_control_put(struct snd_kcontrol *kcontrol,
  2187. struct snd_ctl_elem_value *ucontrol)
  2188. {
  2189. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2190. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2191. unsigned int val;
  2192. int change = 0;
  2193. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2194. val = 1023 - (ucontrol->value.integer.value[0] & 1023);
  2195. } else {
  2196. val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
  2197. }
  2198. spin_lock_irq(&trident->reg_lock);
  2199. change = val != mix->vol;
  2200. mix->vol = val;
  2201. if (mix->voice != NULL)
  2202. snd_trident_write_vol_reg(trident, mix->voice, val);
  2203. spin_unlock_irq(&trident->reg_lock);
  2204. return change;
  2205. }
  2206. static struct snd_kcontrol_new snd_trident_pcm_vol_control __devinitdata =
  2207. {
  2208. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2209. .name = "PCM Front Playback Volume",
  2210. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2211. .count = 32,
  2212. .info = snd_trident_pcm_vol_control_info,
  2213. .get = snd_trident_pcm_vol_control_get,
  2214. .put = snd_trident_pcm_vol_control_put,
  2215. /* FIXME: no tlv yet */
  2216. };
  2217. /*---------------------------------------------------------------------------
  2218. snd_trident_pcm_pan_control
  2219. Description: PCM front pan control
  2220. ---------------------------------------------------------------------------*/
  2221. static int snd_trident_pcm_pan_control_info(struct snd_kcontrol *kcontrol,
  2222. struct snd_ctl_elem_info *uinfo)
  2223. {
  2224. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2225. uinfo->count = 1;
  2226. uinfo->value.integer.min = 0;
  2227. uinfo->value.integer.max = 127;
  2228. return 0;
  2229. }
  2230. static int snd_trident_pcm_pan_control_get(struct snd_kcontrol *kcontrol,
  2231. struct snd_ctl_elem_value *ucontrol)
  2232. {
  2233. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2234. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2235. ucontrol->value.integer.value[0] = mix->pan;
  2236. if (ucontrol->value.integer.value[0] & 0x40) {
  2237. ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
  2238. } else {
  2239. ucontrol->value.integer.value[0] |= 0x40;
  2240. }
  2241. return 0;
  2242. }
  2243. static int snd_trident_pcm_pan_control_put(struct snd_kcontrol *kcontrol,
  2244. struct snd_ctl_elem_value *ucontrol)
  2245. {
  2246. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2247. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2248. unsigned char val;
  2249. int change = 0;
  2250. if (ucontrol->value.integer.value[0] & 0x40)
  2251. val = ucontrol->value.integer.value[0] & 0x3f;
  2252. else
  2253. val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
  2254. spin_lock_irq(&trident->reg_lock);
  2255. change = val != mix->pan;
  2256. mix->pan = val;
  2257. if (mix->voice != NULL)
  2258. snd_trident_write_pan_reg(trident, mix->voice, val);
  2259. spin_unlock_irq(&trident->reg_lock);
  2260. return change;
  2261. }
  2262. static struct snd_kcontrol_new snd_trident_pcm_pan_control __devinitdata =
  2263. {
  2264. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2265. .name = "PCM Pan Playback Control",
  2266. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2267. .count = 32,
  2268. .info = snd_trident_pcm_pan_control_info,
  2269. .get = snd_trident_pcm_pan_control_get,
  2270. .put = snd_trident_pcm_pan_control_put,
  2271. };
  2272. /*---------------------------------------------------------------------------
  2273. snd_trident_pcm_rvol_control
  2274. Description: PCM reverb volume control
  2275. ---------------------------------------------------------------------------*/
  2276. static int snd_trident_pcm_rvol_control_info(struct snd_kcontrol *kcontrol,
  2277. struct snd_ctl_elem_info *uinfo)
  2278. {
  2279. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2280. uinfo->count = 1;
  2281. uinfo->value.integer.min = 0;
  2282. uinfo->value.integer.max = 127;
  2283. return 0;
  2284. }
  2285. static int snd_trident_pcm_rvol_control_get(struct snd_kcontrol *kcontrol,
  2286. struct snd_ctl_elem_value *ucontrol)
  2287. {
  2288. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2289. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2290. ucontrol->value.integer.value[0] = 127 - mix->rvol;
  2291. return 0;
  2292. }
  2293. static int snd_trident_pcm_rvol_control_put(struct snd_kcontrol *kcontrol,
  2294. struct snd_ctl_elem_value *ucontrol)
  2295. {
  2296. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2297. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2298. unsigned short val;
  2299. int change = 0;
  2300. val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
  2301. spin_lock_irq(&trident->reg_lock);
  2302. change = val != mix->rvol;
  2303. mix->rvol = val;
  2304. if (mix->voice != NULL)
  2305. snd_trident_write_rvol_reg(trident, mix->voice, val);
  2306. spin_unlock_irq(&trident->reg_lock);
  2307. return change;
  2308. }
  2309. static const DECLARE_TLV_DB_SCALE(db_scale_crvol, -3175, 25, 1);
  2310. static struct snd_kcontrol_new snd_trident_pcm_rvol_control __devinitdata =
  2311. {
  2312. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2313. .name = "PCM Reverb Playback Volume",
  2314. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2315. .count = 32,
  2316. .info = snd_trident_pcm_rvol_control_info,
  2317. .get = snd_trident_pcm_rvol_control_get,
  2318. .put = snd_trident_pcm_rvol_control_put,
  2319. .tlv = { .p = db_scale_crvol },
  2320. };
  2321. /*---------------------------------------------------------------------------
  2322. snd_trident_pcm_cvol_control
  2323. Description: PCM chorus volume control
  2324. ---------------------------------------------------------------------------*/
  2325. static int snd_trident_pcm_cvol_control_info(struct snd_kcontrol *kcontrol,
  2326. struct snd_ctl_elem_info *uinfo)
  2327. {
  2328. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2329. uinfo->count = 1;
  2330. uinfo->value.integer.min = 0;
  2331. uinfo->value.integer.max = 127;
  2332. return 0;
  2333. }
  2334. static int snd_trident_pcm_cvol_control_get(struct snd_kcontrol *kcontrol,
  2335. struct snd_ctl_elem_value *ucontrol)
  2336. {
  2337. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2338. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2339. ucontrol->value.integer.value[0] = 127 - mix->cvol;
  2340. return 0;
  2341. }
  2342. static int snd_trident_pcm_cvol_control_put(struct snd_kcontrol *kcontrol,
  2343. struct snd_ctl_elem_value *ucontrol)
  2344. {
  2345. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2346. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2347. unsigned short val;
  2348. int change = 0;
  2349. val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
  2350. spin_lock_irq(&trident->reg_lock);
  2351. change = val != mix->cvol;
  2352. mix->cvol = val;
  2353. if (mix->voice != NULL)
  2354. snd_trident_write_cvol_reg(trident, mix->voice, val);
  2355. spin_unlock_irq(&trident->reg_lock);
  2356. return change;
  2357. }
  2358. static struct snd_kcontrol_new snd_trident_pcm_cvol_control __devinitdata =
  2359. {
  2360. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2361. .name = "PCM Chorus Playback Volume",
  2362. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2363. .count = 32,
  2364. .info = snd_trident_pcm_cvol_control_info,
  2365. .get = snd_trident_pcm_cvol_control_get,
  2366. .put = snd_trident_pcm_cvol_control_put,
  2367. .tlv = { .p = db_scale_crvol },
  2368. };
  2369. static void snd_trident_notify_pcm_change1(struct snd_card *card,
  2370. struct snd_kcontrol *kctl,
  2371. int num, int activate)
  2372. {
  2373. struct snd_ctl_elem_id id;
  2374. if (! kctl)
  2375. return;
  2376. if (activate)
  2377. kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  2378. else
  2379. kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  2380. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
  2381. SNDRV_CTL_EVENT_MASK_INFO,
  2382. snd_ctl_build_ioff(&id, kctl, num));
  2383. }
  2384. static void snd_trident_notify_pcm_change(struct snd_trident *trident,
  2385. struct snd_trident_pcm_mixer *tmix,
  2386. int num, int activate)
  2387. {
  2388. snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
  2389. snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
  2390. snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
  2391. snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
  2392. }
  2393. static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
  2394. struct snd_trident_voice *voice,
  2395. struct snd_pcm_substream *substream)
  2396. {
  2397. struct snd_trident_pcm_mixer *tmix;
  2398. snd_assert(trident != NULL && voice != NULL && substream != NULL, return -EINVAL);
  2399. tmix = &trident->pcm_mixer[substream->number];
  2400. tmix->voice = voice;
  2401. tmix->vol = T4D_DEFAULT_PCM_VOL;
  2402. tmix->pan = T4D_DEFAULT_PCM_PAN;
  2403. tmix->rvol = T4D_DEFAULT_PCM_RVOL;
  2404. tmix->cvol = T4D_DEFAULT_PCM_CVOL;
  2405. snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
  2406. return 0;
  2407. }
  2408. static int snd_trident_pcm_mixer_free(struct snd_trident *trident, struct snd_trident_voice *voice, struct snd_pcm_substream *substream)
  2409. {
  2410. struct snd_trident_pcm_mixer *tmix;
  2411. snd_assert(trident != NULL && substream != NULL, return -EINVAL);
  2412. tmix = &trident->pcm_mixer[substream->number];
  2413. tmix->voice = NULL;
  2414. snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
  2415. return 0;
  2416. }
  2417. /*---------------------------------------------------------------------------
  2418. snd_trident_mixer
  2419. Description: This routine registers the 4DWave device for mixer support.
  2420. Paramters: trident - pointer to target device class for 4DWave.
  2421. Returns: None
  2422. ---------------------------------------------------------------------------*/
  2423. static int __devinit snd_trident_mixer(struct snd_trident * trident, int pcm_spdif_device)
  2424. {
  2425. struct snd_ac97_template _ac97;
  2426. struct snd_card *card = trident->card;
  2427. struct snd_kcontrol *kctl;
  2428. struct snd_ctl_elem_value *uctl;
  2429. int idx, err, retries = 2;
  2430. static struct snd_ac97_bus_ops ops = {
  2431. .write = snd_trident_codec_write,
  2432. .read = snd_trident_codec_read,
  2433. };
  2434. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  2435. if (!uctl)
  2436. return -ENOMEM;
  2437. if ((err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus)) < 0)
  2438. goto __out;
  2439. memset(&_ac97, 0, sizeof(_ac97));
  2440. _ac97.private_data = trident;
  2441. trident->ac97_detect = 1;
  2442. __again:
  2443. if ((err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97)) < 0) {
  2444. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2445. if ((err = snd_trident_sis_reset(trident)) < 0)
  2446. goto __out;
  2447. if (retries-- > 0)
  2448. goto __again;
  2449. err = -EIO;
  2450. }
  2451. goto __out;
  2452. }
  2453. /* secondary codec? */
  2454. if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
  2455. (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
  2456. _ac97.num = 1;
  2457. err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
  2458. if (err < 0)
  2459. snd_printk(KERN_ERR "SI7018: the secondary codec - invalid access\n");
  2460. #if 0 // only for my testing purpose --jk
  2461. {
  2462. struct snd_ac97 *mc97;
  2463. err = snd_ac97_modem(trident->card, &_ac97, &mc97);
  2464. if (err < 0)
  2465. snd_printk(KERN_ERR "snd_ac97_modem returned error %i\n", err);
  2466. }
  2467. #endif
  2468. }
  2469. trident->ac97_detect = 0;
  2470. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  2471. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident))) < 0)
  2472. goto __out;
  2473. kctl->put(kctl, uctl);
  2474. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident))) < 0)
  2475. goto __out;
  2476. kctl->put(kctl, uctl);
  2477. outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  2478. } else {
  2479. outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  2480. }
  2481. for (idx = 0; idx < 32; idx++) {
  2482. struct snd_trident_pcm_mixer *tmix;
  2483. tmix = &trident->pcm_mixer[idx];
  2484. tmix->voice = NULL;
  2485. }
  2486. if ((trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident)) == NULL)
  2487. goto __nomem;
  2488. if ((err = snd_ctl_add(card, trident->ctl_vol)))
  2489. goto __out;
  2490. if ((trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident)) == NULL)
  2491. goto __nomem;
  2492. if ((err = snd_ctl_add(card, trident->ctl_pan)))
  2493. goto __out;
  2494. if ((trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident)) == NULL)
  2495. goto __nomem;
  2496. if ((err = snd_ctl_add(card, trident->ctl_rvol)))
  2497. goto __out;
  2498. if ((trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident)) == NULL)
  2499. goto __nomem;
  2500. if ((err = snd_ctl_add(card, trident->ctl_cvol)))
  2501. goto __out;
  2502. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  2503. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident))) < 0)
  2504. goto __out;
  2505. kctl->put(kctl, uctl);
  2506. }
  2507. if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2508. kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
  2509. if (kctl == NULL) {
  2510. err = -ENOMEM;
  2511. goto __out;
  2512. }
  2513. if (trident->ac97->ext_id & AC97_EI_SPDIF)
  2514. kctl->id.index++;
  2515. if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
  2516. kctl->id.index++;
  2517. idx = kctl->id.index;
  2518. if ((err = snd_ctl_add(card, kctl)) < 0)
  2519. goto __out;
  2520. kctl->put(kctl, uctl);
  2521. kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
  2522. if (kctl == NULL) {
  2523. err = -ENOMEM;
  2524. goto __out;
  2525. }
  2526. kctl->id.index = idx;
  2527. kctl->id.device = pcm_spdif_device;
  2528. if ((err = snd_ctl_add(card, kctl)) < 0)
  2529. goto __out;
  2530. kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
  2531. if (kctl == NULL) {
  2532. err = -ENOMEM;
  2533. goto __out;
  2534. }
  2535. kctl->id.index = idx;
  2536. kctl->id.device = pcm_spdif_device;
  2537. if ((err = snd_ctl_add(card, kctl)) < 0)
  2538. goto __out;
  2539. kctl = snd_ctl_new1(&snd_trident_spdif_stream, 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. trident->spdif_pcm_ctl = kctl;
  2549. }
  2550. err = 0;
  2551. goto __out;
  2552. __nomem:
  2553. err = -ENOMEM;
  2554. __out:
  2555. kfree(uctl);
  2556. return err;
  2557. }
  2558. /*
  2559. * gameport interface
  2560. */
  2561. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  2562. static unsigned char snd_trident_gameport_read(struct gameport *gameport)
  2563. {
  2564. struct snd_trident *chip = gameport_get_port_data(gameport);
  2565. snd_assert(chip, return 0);
  2566. return inb(TRID_REG(chip, GAMEPORT_LEGACY));
  2567. }
  2568. static void snd_trident_gameport_trigger(struct gameport *gameport)
  2569. {
  2570. struct snd_trident *chip = gameport_get_port_data(gameport);
  2571. snd_assert(chip, return);
  2572. outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
  2573. }
  2574. static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  2575. {
  2576. struct snd_trident *chip = gameport_get_port_data(gameport);
  2577. int i;
  2578. snd_assert(chip, return 0);
  2579. *buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
  2580. for (i = 0; i < 4; i++) {
  2581. axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
  2582. if (axes[i] == 0xffff) axes[i] = -1;
  2583. }
  2584. return 0;
  2585. }
  2586. static int snd_trident_gameport_open(struct gameport *gameport, int mode)
  2587. {
  2588. struct snd_trident *chip = gameport_get_port_data(gameport);
  2589. snd_assert(chip, return 0);
  2590. switch (mode) {
  2591. case GAMEPORT_MODE_COOKED:
  2592. outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
  2593. msleep(20);
  2594. return 0;
  2595. case GAMEPORT_MODE_RAW:
  2596. outb(0, TRID_REG(chip, GAMEPORT_GCR));
  2597. return 0;
  2598. default:
  2599. return -1;
  2600. }
  2601. }
  2602. int __devinit snd_trident_create_gameport(struct snd_trident *chip)
  2603. {
  2604. struct gameport *gp;
  2605. chip->gameport = gp = gameport_allocate_port();
  2606. if (!gp) {
  2607. printk(KERN_ERR "trident: cannot allocate memory for gameport\n");
  2608. return -ENOMEM;
  2609. }
  2610. gameport_set_name(gp, "Trident 4DWave");
  2611. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  2612. gameport_set_dev_parent(gp, &chip->pci->dev);
  2613. gameport_set_port_data(gp, chip);
  2614. gp->fuzz = 64;
  2615. gp->read = snd_trident_gameport_read;
  2616. gp->trigger = snd_trident_gameport_trigger;
  2617. gp->cooked_read = snd_trident_gameport_cooked_read;
  2618. gp->open = snd_trident_gameport_open;
  2619. gameport_register_port(gp);
  2620. return 0;
  2621. }
  2622. static inline void snd_trident_free_gameport(struct snd_trident *chip)
  2623. {
  2624. if (chip->gameport) {
  2625. gameport_unregister_port(chip->gameport);
  2626. chip->gameport = NULL;
  2627. }
  2628. }
  2629. #else
  2630. int __devinit snd_trident_create_gameport(struct snd_trident *chip) { return -ENOSYS; }
  2631. static inline void snd_trident_free_gameport(struct snd_trident *chip) { }
  2632. #endif /* CONFIG_GAMEPORT */
  2633. /*
  2634. * delay for 1 tick
  2635. */
  2636. static inline void do_delay(struct snd_trident *chip)
  2637. {
  2638. schedule_timeout_uninterruptible(1);
  2639. }
  2640. /*
  2641. * SiS reset routine
  2642. */
  2643. static int snd_trident_sis_reset(struct snd_trident *trident)
  2644. {
  2645. unsigned long end_time;
  2646. unsigned int i;
  2647. int r;
  2648. r = trident->in_suspend ? 0 : 2; /* count of retries */
  2649. __si7018_retry:
  2650. pci_write_config_byte(trident->pci, 0x46, 0x04); /* SOFTWARE RESET */
  2651. udelay(100);
  2652. pci_write_config_byte(trident->pci, 0x46, 0x00);
  2653. udelay(100);
  2654. /* disable AC97 GPIO interrupt */
  2655. outb(0x00, TRID_REG(trident, SI_AC97_GPIO));
  2656. /* initialize serial interface, force cold reset */
  2657. i = PCMOUT|SURROUT|CENTEROUT|LFEOUT|SECONDARY_ID|COLD_RESET;
  2658. outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  2659. udelay(1000);
  2660. /* remove cold reset */
  2661. i &= ~COLD_RESET;
  2662. outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  2663. udelay(2000);
  2664. /* wait, until the codec is ready */
  2665. end_time = (jiffies + (HZ * 3) / 4) + 1;
  2666. do {
  2667. if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0)
  2668. goto __si7018_ok;
  2669. do_delay(trident);
  2670. } while (time_after_eq(end_time, jiffies));
  2671. snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)));
  2672. if (r-- > 0) {
  2673. end_time = jiffies + HZ;
  2674. do {
  2675. do_delay(trident);
  2676. } while (time_after_eq(end_time, jiffies));
  2677. goto __si7018_retry;
  2678. }
  2679. __si7018_ok:
  2680. /* wait for the second codec */
  2681. do {
  2682. if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_SECONDARY_READY) != 0)
  2683. break;
  2684. do_delay(trident);
  2685. } while (time_after_eq(end_time, jiffies));
  2686. /* enable 64 channel mode */
  2687. outl(BANK_B_EN, TRID_REG(trident, T4D_LFO_GC_CIR));
  2688. return 0;
  2689. }
  2690. /*
  2691. * /proc interface
  2692. */
  2693. static void snd_trident_proc_read(struct snd_info_entry *entry,
  2694. struct snd_info_buffer *buffer)
  2695. {
  2696. struct snd_trident *trident = entry->private_data;
  2697. char *s;
  2698. switch (trident->device) {
  2699. case TRIDENT_DEVICE_ID_SI7018:
  2700. s = "SiS 7018 Audio";
  2701. break;
  2702. case TRIDENT_DEVICE_ID_DX:
  2703. s = "Trident 4DWave PCI DX";
  2704. break;
  2705. case TRIDENT_DEVICE_ID_NX:
  2706. s = "Trident 4DWave PCI NX";
  2707. break;
  2708. default:
  2709. s = "???";
  2710. }
  2711. snd_iprintf(buffer, "%s\n\n", s);
  2712. snd_iprintf(buffer, "Spurious IRQs : %d\n", trident->spurious_irq_count);
  2713. snd_iprintf(buffer, "Spurious IRQ dlta: %d\n", trident->spurious_irq_max_delta);
  2714. if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018)
  2715. snd_iprintf(buffer, "IEC958 Mixer Out : %s\n", trident->spdif_ctrl == 0x28 ? "on" : "off");
  2716. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  2717. snd_iprintf(buffer, "Rear Speakers : %s\n", trident->ac97_ctrl & 0x00000010 ? "on" : "off");
  2718. if (trident->tlb.entries) {
  2719. snd_iprintf(buffer,"\nVirtual Memory\n");
  2720. snd_iprintf(buffer, "Memory Maximum : %d\n", trident->tlb.memhdr->size);
  2721. snd_iprintf(buffer, "Memory Used : %d\n", trident->tlb.memhdr->used);
  2722. snd_iprintf(buffer, "Memory Free : %d\n", snd_util_mem_avail(trident->tlb.memhdr));
  2723. }
  2724. }
  2725. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  2726. snd_iprintf(buffer,"\nWavetable Synth\n");
  2727. snd_iprintf(buffer, "Memory Maximum : %d\n", trident->synth.max_size);
  2728. snd_iprintf(buffer, "Memory Used : %d\n", trident->synth.current_size);
  2729. snd_iprintf(buffer, "Memory Free : %d\n", (trident->synth.max_size-trident->synth.current_size));
  2730. #endif
  2731. }
  2732. static void __devinit snd_trident_proc_init(struct snd_trident * trident)
  2733. {
  2734. struct snd_info_entry *entry;
  2735. const char *s = "trident";
  2736. if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  2737. s = "sis7018";
  2738. if (! snd_card_proc_new(trident->card, s, &entry))
  2739. snd_info_set_text_ops(entry, trident, snd_trident_proc_read);
  2740. }
  2741. static int snd_trident_dev_free(struct snd_device *device)
  2742. {
  2743. struct snd_trident *trident = device->device_data;
  2744. return snd_trident_free(trident);
  2745. }
  2746. /*---------------------------------------------------------------------------
  2747. snd_trident_tlb_alloc
  2748. Description: Allocate and set up the TLB page table on 4D NX.
  2749. Each entry has 4 bytes (physical PCI address).
  2750. Paramters: trident - pointer to target device class for 4DWave.
  2751. Returns: 0 or negative error code
  2752. ---------------------------------------------------------------------------*/
  2753. static int __devinit snd_trident_tlb_alloc(struct snd_trident *trident)
  2754. {
  2755. int i;
  2756. /* TLB array must be aligned to 16kB !!! so we allocate
  2757. 32kB region and correct offset when necessary */
  2758. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
  2759. 2 * SNDRV_TRIDENT_MAX_PAGES * 4, &trident->tlb.buffer) < 0) {
  2760. snd_printk(KERN_ERR "trident: unable to allocate TLB buffer\n");
  2761. return -ENOMEM;
  2762. }
  2763. trident->tlb.entries = (unsigned int*)ALIGN((unsigned long)trident->tlb.buffer.area, SNDRV_TRIDENT_MAX_PAGES * 4);
  2764. trident->tlb.entries_dmaaddr = ALIGN(trident->tlb.buffer.addr, SNDRV_TRIDENT_MAX_PAGES * 4);
  2765. /* allocate shadow TLB page table (virtual addresses) */
  2766. trident->tlb.shadow_entries = vmalloc(SNDRV_TRIDENT_MAX_PAGES*sizeof(unsigned long));
  2767. if (trident->tlb.shadow_entries == NULL) {
  2768. snd_printk(KERN_ERR "trident: unable to allocate shadow TLB entries\n");
  2769. return -ENOMEM;
  2770. }
  2771. /* allocate and setup silent page and initialise TLB entries */
  2772. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
  2773. SNDRV_TRIDENT_PAGE_SIZE, &trident->tlb.silent_page) < 0) {
  2774. snd_printk(KERN_ERR "trident: unable to allocate silent page\n");
  2775. return -ENOMEM;
  2776. }
  2777. memset(trident->tlb.silent_page.area, 0, SNDRV_TRIDENT_PAGE_SIZE);
  2778. for (i = 0; i < SNDRV_TRIDENT_MAX_PAGES; i++) {
  2779. trident->tlb.entries[i] = cpu_to_le32(trident->tlb.silent_page.addr & ~(SNDRV_TRIDENT_PAGE_SIZE-1));
  2780. trident->tlb.shadow_entries[i] = (unsigned long)trident->tlb.silent_page.area;
  2781. }
  2782. /* use emu memory block manager code to manage tlb page allocation */
  2783. trident->tlb.memhdr = snd_util_memhdr_new(SNDRV_TRIDENT_PAGE_SIZE * SNDRV_TRIDENT_MAX_PAGES);
  2784. if (trident->tlb.memhdr == NULL)
  2785. return -ENOMEM;
  2786. trident->tlb.memhdr->block_extra_size = sizeof(struct snd_trident_memblk_arg);
  2787. return 0;
  2788. }
  2789. /*
  2790. * initialize 4D DX chip
  2791. */
  2792. static void snd_trident_stop_all_voices(struct snd_trident *trident)
  2793. {
  2794. outl(0xffffffff, TRID_REG(trident, T4D_STOP_A));
  2795. outl(0xffffffff, TRID_REG(trident, T4D_STOP_B));
  2796. outl(0, TRID_REG(trident, T4D_AINTEN_A));
  2797. outl(0, TRID_REG(trident, T4D_AINTEN_B));
  2798. }
  2799. static int snd_trident_4d_dx_init(struct snd_trident *trident)
  2800. {
  2801. struct pci_dev *pci = trident->pci;
  2802. unsigned long end_time;
  2803. /* reset the legacy configuration and whole audio/wavetable block */
  2804. pci_write_config_dword(pci, 0x40, 0); /* DDMA */
  2805. pci_write_config_byte(pci, 0x44, 0); /* ports */
  2806. pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
  2807. pci_write_config_byte(pci, 0x46, 4); /* reset */
  2808. udelay(100);
  2809. pci_write_config_byte(pci, 0x46, 0); /* release reset */
  2810. udelay(100);
  2811. /* warm reset of the AC'97 codec */
  2812. outl(0x00000001, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
  2813. udelay(100);
  2814. outl(0x00000000, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
  2815. /* DAC on, disable SB IRQ and try to force ADC valid signal */
  2816. trident->ac97_ctrl = 0x0000004a;
  2817. outl(trident->ac97_ctrl, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
  2818. /* wait, until the codec is ready */
  2819. end_time = (jiffies + (HZ * 3) / 4) + 1;
  2820. do {
  2821. if ((inl(TRID_REG(trident, DX_ACR2_AC97_COM_STAT)) & 0x0010) != 0)
  2822. goto __dx_ok;
  2823. do_delay(trident);
  2824. } while (time_after_eq(end_time, jiffies));
  2825. snd_printk(KERN_ERR "AC'97 codec ready error\n");
  2826. return -EIO;
  2827. __dx_ok:
  2828. snd_trident_stop_all_voices(trident);
  2829. return 0;
  2830. }
  2831. /*
  2832. * initialize 4D NX chip
  2833. */
  2834. static int snd_trident_4d_nx_init(struct snd_trident *trident)
  2835. {
  2836. struct pci_dev *pci = trident->pci;
  2837. unsigned long end_time;
  2838. /* reset the legacy configuration and whole audio/wavetable block */
  2839. pci_write_config_dword(pci, 0x40, 0); /* DDMA */
  2840. pci_write_config_byte(pci, 0x44, 0); /* ports */
  2841. pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
  2842. pci_write_config_byte(pci, 0x46, 1); /* reset */
  2843. udelay(100);
  2844. pci_write_config_byte(pci, 0x46, 0); /* release reset */
  2845. udelay(100);
  2846. /* warm reset of the AC'97 codec */
  2847. outl(0x00000001, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2848. udelay(100);
  2849. outl(0x00000000, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2850. /* wait, until the codec is ready */
  2851. end_time = (jiffies + (HZ * 3) / 4) + 1;
  2852. do {
  2853. if ((inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)) & 0x0008) != 0)
  2854. goto __nx_ok;
  2855. do_delay(trident);
  2856. } while (time_after_eq(end_time, jiffies));
  2857. snd_printk(KERN_ERR "AC'97 codec ready error [0x%x]\n", inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)));
  2858. return -EIO;
  2859. __nx_ok:
  2860. /* DAC on */
  2861. trident->ac97_ctrl = 0x00000002;
  2862. outl(trident->ac97_ctrl, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2863. /* disable SB IRQ */
  2864. outl(NX_SB_IRQ_DISABLE, TRID_REG(trident, T4D_MISCINT));
  2865. snd_trident_stop_all_voices(trident);
  2866. if (trident->tlb.entries != NULL) {
  2867. unsigned int i;
  2868. /* enable virtual addressing via TLB */
  2869. i = trident->tlb.entries_dmaaddr;
  2870. i |= 0x00000001;
  2871. outl(i, TRID_REG(trident, NX_TLBC));
  2872. } else {
  2873. outl(0, TRID_REG(trident, NX_TLBC));
  2874. }
  2875. /* initialize S/PDIF */
  2876. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  2877. outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  2878. return 0;
  2879. }
  2880. /*
  2881. * initialize sis7018 chip
  2882. */
  2883. static int snd_trident_sis_init(struct snd_trident *trident)
  2884. {
  2885. int err;
  2886. if ((err = snd_trident_sis_reset(trident)) < 0)
  2887. return err;
  2888. snd_trident_stop_all_voices(trident);
  2889. /* initialize S/PDIF */
  2890. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  2891. return 0;
  2892. }
  2893. /*---------------------------------------------------------------------------
  2894. snd_trident_create
  2895. Description: This routine will create the device specific class for
  2896. the 4DWave card. It will also perform basic initialization.
  2897. Paramters: card - which card to create
  2898. pci - interface to PCI bus resource info
  2899. dma1ptr - playback dma buffer
  2900. dma2ptr - capture dma buffer
  2901. irqptr - interrupt resource info
  2902. Returns: 4DWave device class private data
  2903. ---------------------------------------------------------------------------*/
  2904. int __devinit snd_trident_create(struct snd_card *card,
  2905. struct pci_dev *pci,
  2906. int pcm_streams,
  2907. int pcm_spdif_device,
  2908. int max_wavetable_size,
  2909. struct snd_trident ** rtrident)
  2910. {
  2911. struct snd_trident *trident;
  2912. int i, err;
  2913. struct snd_trident_voice *voice;
  2914. struct snd_trident_pcm_mixer *tmix;
  2915. static struct snd_device_ops ops = {
  2916. .dev_free = snd_trident_dev_free,
  2917. };
  2918. *rtrident = NULL;
  2919. /* enable PCI device */
  2920. if ((err = pci_enable_device(pci)) < 0)
  2921. return err;
  2922. /* check, if we can restrict PCI DMA transfers to 30 bits */
  2923. if (pci_set_dma_mask(pci, DMA_30BIT_MASK) < 0 ||
  2924. pci_set_consistent_dma_mask(pci, DMA_30BIT_MASK) < 0) {
  2925. snd_printk(KERN_ERR "architecture does not support 30bit PCI busmaster DMA\n");
  2926. pci_disable_device(pci);
  2927. return -ENXIO;
  2928. }
  2929. trident = kzalloc(sizeof(*trident), GFP_KERNEL);
  2930. if (trident == NULL) {
  2931. pci_disable_device(pci);
  2932. return -ENOMEM;
  2933. }
  2934. trident->device = (pci->vendor << 16) | pci->device;
  2935. trident->card = card;
  2936. trident->pci = pci;
  2937. spin_lock_init(&trident->reg_lock);
  2938. spin_lock_init(&trident->event_lock);
  2939. spin_lock_init(&trident->voice_alloc);
  2940. if (pcm_streams < 1)
  2941. pcm_streams = 1;
  2942. if (pcm_streams > 32)
  2943. pcm_streams = 32;
  2944. trident->ChanPCM = pcm_streams;
  2945. if (max_wavetable_size < 0 )
  2946. max_wavetable_size = 0;
  2947. trident->synth.max_size = max_wavetable_size * 1024;
  2948. trident->irq = -1;
  2949. trident->midi_port = TRID_REG(trident, T4D_MPU401_BASE);
  2950. pci_set_master(pci);
  2951. if ((err = pci_request_regions(pci, "Trident Audio")) < 0) {
  2952. kfree(trident);
  2953. pci_disable_device(pci);
  2954. return err;
  2955. }
  2956. trident->port = pci_resource_start(pci, 0);
  2957. if (request_irq(pci->irq, snd_trident_interrupt, IRQF_SHARED,
  2958. "Trident Audio", trident)) {
  2959. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  2960. snd_trident_free(trident);
  2961. return -EBUSY;
  2962. }
  2963. trident->irq = pci->irq;
  2964. /* allocate 16k-aligned TLB for NX cards */
  2965. trident->tlb.entries = NULL;
  2966. trident->tlb.buffer.area = NULL;
  2967. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  2968. if ((err = snd_trident_tlb_alloc(trident)) < 0) {
  2969. snd_trident_free(trident);
  2970. return err;
  2971. }
  2972. }
  2973. trident->spdif_bits = trident->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
  2974. /* initialize chip */
  2975. switch (trident->device) {
  2976. case TRIDENT_DEVICE_ID_DX:
  2977. err = snd_trident_4d_dx_init(trident);
  2978. break;
  2979. case TRIDENT_DEVICE_ID_NX:
  2980. err = snd_trident_4d_nx_init(trident);
  2981. break;
  2982. case TRIDENT_DEVICE_ID_SI7018:
  2983. err = snd_trident_sis_init(trident);
  2984. break;
  2985. default:
  2986. snd_BUG();
  2987. break;
  2988. }
  2989. if (err < 0) {
  2990. snd_trident_free(trident);
  2991. return err;
  2992. }
  2993. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, trident, &ops)) < 0) {
  2994. snd_trident_free(trident);
  2995. return err;
  2996. }
  2997. if ((err = snd_trident_mixer(trident, pcm_spdif_device)) < 0)
  2998. return err;
  2999. /* initialise synth voices */
  3000. for (i = 0; i < 64; i++) {
  3001. voice = &trident->synth.voices[i];
  3002. voice->number = i;
  3003. voice->trident = trident;
  3004. }
  3005. /* initialize pcm mixer entries */
  3006. for (i = 0; i < 32; i++) {
  3007. tmix = &trident->pcm_mixer[i];
  3008. tmix->vol = T4D_DEFAULT_PCM_VOL;
  3009. tmix->pan = T4D_DEFAULT_PCM_PAN;
  3010. tmix->rvol = T4D_DEFAULT_PCM_RVOL;
  3011. tmix->cvol = T4D_DEFAULT_PCM_CVOL;
  3012. }
  3013. snd_trident_enable_eso(trident);
  3014. snd_trident_proc_init(trident);
  3015. snd_card_set_dev(card, &pci->dev);
  3016. *rtrident = trident;
  3017. return 0;
  3018. }
  3019. /*---------------------------------------------------------------------------
  3020. snd_trident_free
  3021. Description: This routine will free the device specific class for
  3022. the 4DWave card.
  3023. Paramters: trident - device specific private data for 4DWave card
  3024. Returns: None.
  3025. ---------------------------------------------------------------------------*/
  3026. static int snd_trident_free(struct snd_trident *trident)
  3027. {
  3028. snd_trident_free_gameport(trident);
  3029. snd_trident_disable_eso(trident);
  3030. // Disable S/PDIF out
  3031. if (trident->device == TRIDENT_DEVICE_ID_NX)
  3032. outb(0x00, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  3033. else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  3034. outl(0, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  3035. }
  3036. if (trident->tlb.buffer.area) {
  3037. outl(0, TRID_REG(trident, NX_TLBC));
  3038. if (trident->tlb.memhdr)
  3039. snd_util_memhdr_free(trident->tlb.memhdr);
  3040. if (trident->tlb.silent_page.area)
  3041. snd_dma_free_pages(&trident->tlb.silent_page);
  3042. vfree(trident->tlb.shadow_entries);
  3043. snd_dma_free_pages(&trident->tlb.buffer);
  3044. }
  3045. if (trident->irq >= 0)
  3046. free_irq(trident->irq, trident);
  3047. pci_release_regions(trident->pci);
  3048. pci_disable_device(trident->pci);
  3049. kfree(trident);
  3050. return 0;
  3051. }
  3052. /*---------------------------------------------------------------------------
  3053. snd_trident_interrupt
  3054. Description: ISR for Trident 4DWave device
  3055. Paramters: trident - device specific private data for 4DWave card
  3056. Problems: It seems that Trident chips generates interrupts more than
  3057. one time in special cases. The spurious interrupts are
  3058. detected via sample timer (T4D_STIMER) and computing
  3059. corresponding delta value. The limits are detected with
  3060. the method try & fail so it is possible that it won't
  3061. work on all computers. [jaroslav]
  3062. Returns: None.
  3063. ---------------------------------------------------------------------------*/
  3064. static irqreturn_t snd_trident_interrupt(int irq, void *dev_id)
  3065. {
  3066. struct snd_trident *trident = dev_id;
  3067. unsigned int audio_int, chn_int, stimer, channel, mask, tmp;
  3068. int delta;
  3069. struct snd_trident_voice *voice;
  3070. audio_int = inl(TRID_REG(trident, T4D_MISCINT));
  3071. if ((audio_int & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
  3072. return IRQ_NONE;
  3073. if (audio_int & ADDRESS_IRQ) {
  3074. // get interrupt status for all channels
  3075. spin_lock(&trident->reg_lock);
  3076. stimer = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
  3077. chn_int = inl(TRID_REG(trident, T4D_AINT_A));
  3078. if (chn_int == 0)
  3079. goto __skip1;
  3080. outl(chn_int, TRID_REG(trident, T4D_AINT_A)); /* ack */
  3081. __skip1:
  3082. chn_int = inl(TRID_REG(trident, T4D_AINT_B));
  3083. if (chn_int == 0)
  3084. goto __skip2;
  3085. for (channel = 63; channel >= 32; channel--) {
  3086. mask = 1 << (channel&0x1f);
  3087. if ((chn_int & mask) == 0)
  3088. continue;
  3089. voice = &trident->synth.voices[channel];
  3090. if (!voice->pcm || voice->substream == NULL) {
  3091. outl(mask, TRID_REG(trident, T4D_STOP_B));
  3092. continue;
  3093. }
  3094. delta = (int)stimer - (int)voice->stimer;
  3095. if (delta < 0)
  3096. delta = -delta;
  3097. if ((unsigned int)delta < voice->spurious_threshold) {
  3098. /* do some statistics here */
  3099. trident->spurious_irq_count++;
  3100. if (trident->spurious_irq_max_delta < (unsigned int)delta)
  3101. trident->spurious_irq_max_delta = delta;
  3102. continue;
  3103. }
  3104. voice->stimer = stimer;
  3105. if (voice->isync) {
  3106. if (!voice->isync3) {
  3107. tmp = inw(TRID_REG(trident, T4D_SBBL_SBCL));
  3108. if (trident->bDMAStart & 0x40)
  3109. tmp >>= 1;
  3110. if (tmp > 0)
  3111. tmp = voice->isync_max - tmp;
  3112. } else {
  3113. tmp = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
  3114. }
  3115. if (tmp < voice->isync_mark) {
  3116. if (tmp > 0x10)
  3117. tmp = voice->isync_ESO - 7;
  3118. else
  3119. tmp = voice->isync_ESO + 2;
  3120. /* update ESO for IRQ voice to preserve sync */
  3121. snd_trident_stop_voice(trident, voice->number);
  3122. snd_trident_write_eso_reg(trident, voice, tmp);
  3123. snd_trident_start_voice(trident, voice->number);
  3124. }
  3125. } else if (voice->isync2) {
  3126. voice->isync2 = 0;
  3127. /* write original ESO and update CSO for IRQ voice to preserve sync */
  3128. snd_trident_stop_voice(trident, voice->number);
  3129. snd_trident_write_cso_reg(trident, voice, voice->isync_mark);
  3130. snd_trident_write_eso_reg(trident, voice, voice->ESO);
  3131. snd_trident_start_voice(trident, voice->number);
  3132. }
  3133. #if 0
  3134. if (voice->extra) {
  3135. /* update CSO for extra voice to preserve sync */
  3136. snd_trident_stop_voice(trident, voice->extra->number);
  3137. snd_trident_write_cso_reg(trident, voice->extra, 0);
  3138. snd_trident_start_voice(trident, voice->extra->number);
  3139. }
  3140. #endif
  3141. spin_unlock(&trident->reg_lock);
  3142. snd_pcm_period_elapsed(voice->substream);
  3143. spin_lock(&trident->reg_lock);
  3144. }
  3145. outl(chn_int, TRID_REG(trident, T4D_AINT_B)); /* ack */
  3146. __skip2:
  3147. spin_unlock(&trident->reg_lock);
  3148. }
  3149. if (audio_int & MPU401_IRQ) {
  3150. if (trident->rmidi) {
  3151. snd_mpu401_uart_interrupt(irq, trident->rmidi->private_data);
  3152. } else {
  3153. inb(TRID_REG(trident, T4D_MPUR0));
  3154. }
  3155. }
  3156. // outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), TRID_REG(trident, T4D_MISCINT));
  3157. return IRQ_HANDLED;
  3158. }
  3159. /*---------------------------------------------------------------------------
  3160. snd_trident_attach_synthesizer
  3161. Description: Attach synthesizer hooks
  3162. Paramters: trident - device specific private data for 4DWave card
  3163. Returns: None.
  3164. ---------------------------------------------------------------------------*/
  3165. int snd_trident_attach_synthesizer(struct snd_trident *trident)
  3166. {
  3167. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  3168. if (snd_seq_device_new(trident->card, 1, SNDRV_SEQ_DEV_ID_TRIDENT,
  3169. sizeof(struct snd_trident *), &trident->seq_dev) >= 0) {
  3170. strcpy(trident->seq_dev->name, "4DWave");
  3171. *(struct snd_trident **)SNDRV_SEQ_DEVICE_ARGPTR(trident->seq_dev) = trident;
  3172. }
  3173. #endif
  3174. return 0;
  3175. }
  3176. struct snd_trident_voice *snd_trident_alloc_voice(struct snd_trident * trident, int type, int client, int port)
  3177. {
  3178. struct snd_trident_voice *pvoice;
  3179. unsigned long flags;
  3180. int idx;
  3181. spin_lock_irqsave(&trident->voice_alloc, flags);
  3182. if (type == SNDRV_TRIDENT_VOICE_TYPE_PCM) {
  3183. idx = snd_trident_allocate_pcm_channel(trident);
  3184. if(idx < 0) {
  3185. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3186. return NULL;
  3187. }
  3188. pvoice = &trident->synth.voices[idx];
  3189. pvoice->use = 1;
  3190. pvoice->pcm = 1;
  3191. pvoice->capture = 0;
  3192. pvoice->spdif = 0;
  3193. pvoice->memblk = NULL;
  3194. pvoice->substream = NULL;
  3195. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3196. return pvoice;
  3197. }
  3198. if (type == SNDRV_TRIDENT_VOICE_TYPE_SYNTH) {
  3199. idx = snd_trident_allocate_synth_channel(trident);
  3200. if(idx < 0) {
  3201. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3202. return NULL;
  3203. }
  3204. pvoice = &trident->synth.voices[idx];
  3205. pvoice->use = 1;
  3206. pvoice->synth = 1;
  3207. pvoice->client = client;
  3208. pvoice->port = port;
  3209. pvoice->memblk = NULL;
  3210. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3211. return pvoice;
  3212. }
  3213. if (type == SNDRV_TRIDENT_VOICE_TYPE_MIDI) {
  3214. }
  3215. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3216. return NULL;
  3217. }
  3218. EXPORT_SYMBOL(snd_trident_alloc_voice);
  3219. void snd_trident_free_voice(struct snd_trident * trident, struct snd_trident_voice *voice)
  3220. {
  3221. unsigned long flags;
  3222. void (*private_free)(struct snd_trident_voice *);
  3223. void *private_data;
  3224. if (voice == NULL || !voice->use)
  3225. return;
  3226. snd_trident_clear_voices(trident, voice->number, voice->number);
  3227. spin_lock_irqsave(&trident->voice_alloc, flags);
  3228. private_free = voice->private_free;
  3229. private_data = voice->private_data;
  3230. voice->private_free = NULL;
  3231. voice->private_data = NULL;
  3232. if (voice->pcm)
  3233. snd_trident_free_pcm_channel(trident, voice->number);
  3234. if (voice->synth)
  3235. snd_trident_free_synth_channel(trident, voice->number);
  3236. voice->use = voice->pcm = voice->synth = voice->midi = 0;
  3237. voice->capture = voice->spdif = 0;
  3238. voice->sample_ops = NULL;
  3239. voice->substream = NULL;
  3240. voice->extra = NULL;
  3241. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3242. if (private_free)
  3243. private_free(voice);
  3244. }
  3245. EXPORT_SYMBOL(snd_trident_free_voice);
  3246. static void snd_trident_clear_voices(struct snd_trident * trident, unsigned short v_min, unsigned short v_max)
  3247. {
  3248. unsigned int i, val, mask[2] = { 0, 0 };
  3249. snd_assert(v_min <= 63, return);
  3250. snd_assert(v_max <= 63, return);
  3251. for (i = v_min; i <= v_max; i++)
  3252. mask[i >> 5] |= 1 << (i & 0x1f);
  3253. if (mask[0]) {
  3254. outl(mask[0], TRID_REG(trident, T4D_STOP_A));
  3255. val = inl(TRID_REG(trident, T4D_AINTEN_A));
  3256. outl(val & ~mask[0], TRID_REG(trident, T4D_AINTEN_A));
  3257. }
  3258. if (mask[1]) {
  3259. outl(mask[1], TRID_REG(trident, T4D_STOP_B));
  3260. val = inl(TRID_REG(trident, T4D_AINTEN_B));
  3261. outl(val & ~mask[1], TRID_REG(trident, T4D_AINTEN_B));
  3262. }
  3263. }
  3264. #ifdef CONFIG_PM
  3265. int snd_trident_suspend(struct pci_dev *pci, pm_message_t state)
  3266. {
  3267. struct snd_card *card = pci_get_drvdata(pci);
  3268. struct snd_trident *trident = card->private_data;
  3269. trident->in_suspend = 1;
  3270. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  3271. snd_pcm_suspend_all(trident->pcm);
  3272. snd_pcm_suspend_all(trident->foldback);
  3273. snd_pcm_suspend_all(trident->spdif);
  3274. snd_ac97_suspend(trident->ac97);
  3275. snd_ac97_suspend(trident->ac97_sec);
  3276. pci_disable_device(pci);
  3277. pci_save_state(pci);
  3278. pci_set_power_state(pci, pci_choose_state(pci, state));
  3279. return 0;
  3280. }
  3281. int snd_trident_resume(struct pci_dev *pci)
  3282. {
  3283. struct snd_card *card = pci_get_drvdata(pci);
  3284. struct snd_trident *trident = card->private_data;
  3285. pci_set_power_state(pci, PCI_D0);
  3286. pci_restore_state(pci);
  3287. if (pci_enable_device(pci) < 0) {
  3288. printk(KERN_ERR "trident: pci_enable_device failed, "
  3289. "disabling device\n");
  3290. snd_card_disconnect(card);
  3291. return -EIO;
  3292. }
  3293. pci_set_master(pci);
  3294. switch (trident->device) {
  3295. case TRIDENT_DEVICE_ID_DX:
  3296. snd_trident_4d_dx_init(trident);
  3297. break;
  3298. case TRIDENT_DEVICE_ID_NX:
  3299. snd_trident_4d_nx_init(trident);
  3300. break;
  3301. case TRIDENT_DEVICE_ID_SI7018:
  3302. snd_trident_sis_init(trident);
  3303. break;
  3304. }
  3305. snd_ac97_resume(trident->ac97);
  3306. snd_ac97_resume(trident->ac97_sec);
  3307. /* restore some registers */
  3308. outl(trident->musicvol_wavevol, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  3309. snd_trident_enable_eso(trident);
  3310. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  3311. trident->in_suspend = 0;
  3312. return 0;
  3313. }
  3314. #endif /* CONFIG_PM */