trident_main.c 121 KB

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