trident_main.c 121 KB

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