trident_main.c 122 KB

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