trident_main.c 122 KB

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