trident_main.c 121 KB

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