trident_main.c 122 KB

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