trident_main.c 121 KB

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