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. }
  385. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  386. outl(regs[0], TRID_REG(trident, CH_START + 0));
  387. outl(regs[1], TRID_REG(trident, CH_START + 4));
  388. outl(regs[2], TRID_REG(trident, CH_START + 8));
  389. outl(regs[3], TRID_REG(trident, CH_START + 12));
  390. outl(regs[4], TRID_REG(trident, CH_START + 16));
  391. #if 0
  392. printk("written %i channel:\n", voice->number);
  393. printk(" regs[0] = 0x%x/0x%x\n", regs[0], inl(TRID_REG(trident, CH_START + 0)));
  394. printk(" regs[1] = 0x%x/0x%x\n", regs[1], inl(TRID_REG(trident, CH_START + 4)));
  395. printk(" regs[2] = 0x%x/0x%x\n", regs[2], inl(TRID_REG(trident, CH_START + 8)));
  396. printk(" regs[3] = 0x%x/0x%x\n", regs[3], inl(TRID_REG(trident, CH_START + 12)));
  397. printk(" regs[4] = 0x%x/0x%x\n", regs[4], inl(TRID_REG(trident, CH_START + 16)));
  398. #endif
  399. }
  400. /*---------------------------------------------------------------------------
  401. snd_trident_write_cso_reg
  402. Description: This routine will write the new CSO offset
  403. register to hardware.
  404. Paramters: trident - pointer to target device class for 4DWave.
  405. voice - synthesizer voice structure
  406. CSO - new CSO value
  407. ---------------------------------------------------------------------------*/
  408. static void snd_trident_write_cso_reg(trident_t * trident, snd_trident_voice_t * voice, unsigned int CSO)
  409. {
  410. voice->CSO = CSO;
  411. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  412. if (trident->device != TRIDENT_DEVICE_ID_NX) {
  413. outw(voice->CSO, TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
  414. } else {
  415. outl((voice->Delta << 24) | (voice->CSO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_CSO));
  416. }
  417. }
  418. /*---------------------------------------------------------------------------
  419. snd_trident_write_eso_reg
  420. Description: This routine will write the new ESO offset
  421. register to hardware.
  422. Paramters: trident - pointer to target device class for 4DWave.
  423. voice - synthesizer voice structure
  424. ESO - new ESO value
  425. ---------------------------------------------------------------------------*/
  426. static void snd_trident_write_eso_reg(trident_t * trident, snd_trident_voice_t * voice, unsigned int ESO)
  427. {
  428. voice->ESO = ESO;
  429. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  430. if (trident->device != TRIDENT_DEVICE_ID_NX) {
  431. outw(voice->ESO, TRID_REG(trident, CH_DX_ESO_DELTA) + 2);
  432. } else {
  433. outl(((voice->Delta << 16) & 0xff000000) | (voice->ESO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_ESO));
  434. }
  435. }
  436. /*---------------------------------------------------------------------------
  437. snd_trident_write_vol_reg
  438. Description: This routine will write the new voice volume
  439. register to hardware.
  440. Paramters: trident - pointer to target device class for 4DWave.
  441. voice - synthesizer voice structure
  442. Vol - new voice volume
  443. ---------------------------------------------------------------------------*/
  444. static void snd_trident_write_vol_reg(trident_t * trident, snd_trident_voice_t * voice, unsigned int Vol)
  445. {
  446. voice->Vol = Vol;
  447. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  448. switch (trident->device) {
  449. case TRIDENT_DEVICE_ID_DX:
  450. case TRIDENT_DEVICE_ID_NX:
  451. outb(voice->Vol >> 2, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 2));
  452. break;
  453. case TRIDENT_DEVICE_ID_SI7018:
  454. // printk("voice->Vol = 0x%x\n", voice->Vol);
  455. outw((voice->CTRL << 12) | voice->Vol, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
  456. break;
  457. }
  458. }
  459. /*---------------------------------------------------------------------------
  460. snd_trident_write_pan_reg
  461. Description: This routine will write the new voice pan
  462. register to hardware.
  463. Paramters: trident - pointer to target device class for 4DWave.
  464. voice - synthesizer voice structure
  465. Pan - new pan value
  466. ---------------------------------------------------------------------------*/
  467. static void snd_trident_write_pan_reg(trident_t * trident, snd_trident_voice_t * voice, unsigned int Pan)
  468. {
  469. voice->Pan = Pan;
  470. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  471. outb(((voice->GVSel & 0x01) << 7) | (voice->Pan & 0x7f), TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 3));
  472. }
  473. /*---------------------------------------------------------------------------
  474. snd_trident_write_rvol_reg
  475. Description: This routine will write the new reverb volume
  476. register to hardware.
  477. Paramters: trident - pointer to target device class for 4DWave.
  478. voice - synthesizer voice structure
  479. RVol - new reverb volume
  480. ---------------------------------------------------------------------------*/
  481. static void snd_trident_write_rvol_reg(trident_t * trident, snd_trident_voice_t * voice, unsigned int RVol)
  482. {
  483. voice->RVol = RVol;
  484. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  485. outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) | (voice->CVol & 0x007f),
  486. TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ? CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
  487. }
  488. /*---------------------------------------------------------------------------
  489. snd_trident_write_cvol_reg
  490. Description: This routine will write the new chorus volume
  491. register to hardware.
  492. Paramters: trident - pointer to target device class for 4DWave.
  493. voice - synthesizer voice structure
  494. CVol - new chorus volume
  495. ---------------------------------------------------------------------------*/
  496. static void snd_trident_write_cvol_reg(trident_t * trident, snd_trident_voice_t * voice, unsigned int CVol)
  497. {
  498. voice->CVol = CVol;
  499. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  500. outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) | (voice->CVol & 0x007f),
  501. TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ? CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
  502. }
  503. /*---------------------------------------------------------------------------
  504. snd_trident_convert_rate
  505. Description: This routine converts rate in HZ to hardware delta value.
  506. Paramters: trident - pointer to target device class for 4DWave.
  507. rate - Real or Virtual channel number.
  508. Returns: Delta value.
  509. ---------------------------------------------------------------------------*/
  510. static unsigned int snd_trident_convert_rate(unsigned int rate)
  511. {
  512. unsigned int delta;
  513. // We special case 44100 and 8000 since rounding with the equation
  514. // does not give us an accurate enough value. For 11025 and 22050
  515. // the equation gives us the best answer. All other frequencies will
  516. // also use the equation. JDW
  517. if (rate == 44100)
  518. delta = 0xeb3;
  519. else if (rate == 8000)
  520. delta = 0x2ab;
  521. else if (rate == 48000)
  522. delta = 0x1000;
  523. else
  524. delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
  525. return delta;
  526. }
  527. /*---------------------------------------------------------------------------
  528. snd_trident_convert_adc_rate
  529. Description: This routine converts rate in HZ to hardware delta value.
  530. Paramters: trident - pointer to target device class for 4DWave.
  531. rate - Real or Virtual channel number.
  532. Returns: Delta value.
  533. ---------------------------------------------------------------------------*/
  534. static unsigned int snd_trident_convert_adc_rate(unsigned int rate)
  535. {
  536. unsigned int delta;
  537. // We special case 44100 and 8000 since rounding with the equation
  538. // does not give us an accurate enough value. For 11025 and 22050
  539. // the equation gives us the best answer. All other frequencies will
  540. // also use the equation. JDW
  541. if (rate == 44100)
  542. delta = 0x116a;
  543. else if (rate == 8000)
  544. delta = 0x6000;
  545. else if (rate == 48000)
  546. delta = 0x1000;
  547. else
  548. delta = ((48000 << 12) / rate) & 0x0000ffff;
  549. return delta;
  550. }
  551. /*---------------------------------------------------------------------------
  552. snd_trident_spurious_threshold
  553. Description: This routine converts rate in HZ to spurious threshold.
  554. Paramters: trident - pointer to target device class for 4DWave.
  555. rate - Real or Virtual channel number.
  556. Returns: Delta value.
  557. ---------------------------------------------------------------------------*/
  558. static unsigned int snd_trident_spurious_threshold(unsigned int rate, unsigned int period_size)
  559. {
  560. unsigned int res = (rate * period_size) / 48000;
  561. if (res < 64)
  562. res = res / 2;
  563. else
  564. res -= 32;
  565. return res;
  566. }
  567. /*---------------------------------------------------------------------------
  568. snd_trident_control_mode
  569. Description: This routine returns a control mode for a PCM channel.
  570. Paramters: trident - pointer to target device class for 4DWave.
  571. substream - PCM substream
  572. Returns: Control value.
  573. ---------------------------------------------------------------------------*/
  574. static unsigned int snd_trident_control_mode(snd_pcm_substream_t *substream)
  575. {
  576. unsigned int CTRL;
  577. snd_pcm_runtime_t *runtime = substream->runtime;
  578. /* set ctrl mode
  579. CTRL default: 8-bit (unsigned) mono, loop mode enabled
  580. */
  581. CTRL = 0x00000001;
  582. if (snd_pcm_format_width(runtime->format) == 16)
  583. CTRL |= 0x00000008; // 16-bit data
  584. if (snd_pcm_format_signed(runtime->format))
  585. CTRL |= 0x00000002; // signed data
  586. if (runtime->channels > 1)
  587. CTRL |= 0x00000004; // stereo data
  588. return CTRL;
  589. }
  590. /*
  591. * PCM part
  592. */
  593. /*---------------------------------------------------------------------------
  594. snd_trident_ioctl
  595. Description: Device I/O control handler for playback/capture parameters.
  596. Paramters: substream - PCM substream class
  597. cmd - what ioctl message to process
  598. arg - additional message infoarg
  599. Returns: Error status
  600. ---------------------------------------------------------------------------*/
  601. static int snd_trident_ioctl(snd_pcm_substream_t * substream,
  602. unsigned int cmd,
  603. void *arg)
  604. {
  605. /* FIXME: it seems that with small periods the behaviour of
  606. trident hardware is unpredictable and interrupt generator
  607. is broken */
  608. return snd_pcm_lib_ioctl(substream, cmd, arg);
  609. }
  610. /*---------------------------------------------------------------------------
  611. snd_trident_allocate_pcm_mem
  612. Description: Allocate PCM ring buffer for given substream
  613. Parameters: substream - PCM substream class
  614. hw_params - hardware parameters
  615. Returns: Error status
  616. ---------------------------------------------------------------------------*/
  617. static int snd_trident_allocate_pcm_mem(snd_pcm_substream_t * substream,
  618. snd_pcm_hw_params_t * hw_params)
  619. {
  620. trident_t *trident = snd_pcm_substream_chip(substream);
  621. snd_pcm_runtime_t *runtime = substream->runtime;
  622. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  623. int err;
  624. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  625. return err;
  626. if (trident->tlb.entries) {
  627. if (err > 0) { /* change */
  628. if (voice->memblk)
  629. snd_trident_free_pages(trident, voice->memblk);
  630. voice->memblk = snd_trident_alloc_pages(trident, substream);
  631. if (voice->memblk == NULL)
  632. return -ENOMEM;
  633. }
  634. }
  635. return 0;
  636. }
  637. /*---------------------------------------------------------------------------
  638. snd_trident_allocate_evoice
  639. Description: Allocate extra voice as interrupt generator
  640. Parameters: substream - PCM substream class
  641. hw_params - hardware parameters
  642. Returns: Error status
  643. ---------------------------------------------------------------------------*/
  644. static int snd_trident_allocate_evoice(snd_pcm_substream_t * substream,
  645. snd_pcm_hw_params_t * hw_params)
  646. {
  647. trident_t *trident = snd_pcm_substream_chip(substream);
  648. snd_pcm_runtime_t *runtime = substream->runtime;
  649. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  650. snd_trident_voice_t *evoice = voice->extra;
  651. /* voice management */
  652. if (params_buffer_size(hw_params) / 2 != params_period_size(hw_params)) {
  653. if (evoice == NULL) {
  654. evoice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  655. if (evoice == NULL)
  656. return -ENOMEM;
  657. voice->extra = evoice;
  658. evoice->substream = substream;
  659. }
  660. } else {
  661. if (evoice != NULL) {
  662. snd_trident_free_voice(trident, evoice);
  663. voice->extra = evoice = NULL;
  664. }
  665. }
  666. return 0;
  667. }
  668. /*---------------------------------------------------------------------------
  669. snd_trident_hw_params
  670. Description: Set the hardware parameters for the playback device.
  671. Parameters: substream - PCM substream class
  672. hw_params - hardware parameters
  673. Returns: Error status
  674. ---------------------------------------------------------------------------*/
  675. static int snd_trident_hw_params(snd_pcm_substream_t * substream,
  676. snd_pcm_hw_params_t * hw_params)
  677. {
  678. int err;
  679. err = snd_trident_allocate_pcm_mem(substream, hw_params);
  680. if (err >= 0)
  681. err = snd_trident_allocate_evoice(substream, hw_params);
  682. return err;
  683. }
  684. /*---------------------------------------------------------------------------
  685. snd_trident_playback_hw_free
  686. Description: Release the hardware resources for the playback device.
  687. Parameters: substream - PCM substream class
  688. Returns: Error status
  689. ---------------------------------------------------------------------------*/
  690. static int snd_trident_hw_free(snd_pcm_substream_t * substream)
  691. {
  692. trident_t *trident = snd_pcm_substream_chip(substream);
  693. snd_pcm_runtime_t *runtime = substream->runtime;
  694. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  695. snd_trident_voice_t *evoice = voice ? voice->extra : NULL;
  696. if (trident->tlb.entries) {
  697. if (voice && voice->memblk) {
  698. snd_trident_free_pages(trident, voice->memblk);
  699. voice->memblk = NULL;
  700. }
  701. }
  702. snd_pcm_lib_free_pages(substream);
  703. if (evoice != NULL) {
  704. snd_trident_free_voice(trident, evoice);
  705. voice->extra = NULL;
  706. }
  707. return 0;
  708. }
  709. /*---------------------------------------------------------------------------
  710. snd_trident_playback_prepare
  711. Description: Prepare playback device for playback.
  712. Parameters: substream - PCM substream class
  713. Returns: Error status
  714. ---------------------------------------------------------------------------*/
  715. static int snd_trident_playback_prepare(snd_pcm_substream_t * substream)
  716. {
  717. trident_t *trident = snd_pcm_substream_chip(substream);
  718. snd_pcm_runtime_t *runtime = substream->runtime;
  719. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  720. snd_trident_voice_t *evoice = voice->extra;
  721. snd_trident_pcm_mixer_t *mix = &trident->pcm_mixer[substream->number];
  722. spin_lock_irq(&trident->reg_lock);
  723. /* set delta (rate) value */
  724. voice->Delta = snd_trident_convert_rate(runtime->rate);
  725. voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  726. /* set Loop Begin Address */
  727. if (voice->memblk)
  728. voice->LBA = voice->memblk->offset;
  729. else
  730. voice->LBA = runtime->dma_addr;
  731. voice->CSO = 0;
  732. voice->ESO = runtime->buffer_size - 1; /* in samples */
  733. voice->CTRL = snd_trident_control_mode(substream);
  734. voice->FMC = 3;
  735. voice->GVSel = 1;
  736. voice->EC = 0;
  737. voice->Alpha = 0;
  738. voice->FMS = 0;
  739. voice->Vol = mix->vol;
  740. voice->RVol = mix->rvol;
  741. voice->CVol = mix->cvol;
  742. voice->Pan = mix->pan;
  743. voice->Attribute = 0;
  744. #if 0
  745. voice->Attribute = (1<<(30-16))|(2<<(26-16))|
  746. (0<<(24-16))|(0x1f<<(19-16));
  747. #else
  748. voice->Attribute = 0;
  749. #endif
  750. snd_trident_write_voice_regs(trident, voice);
  751. if (evoice != NULL) {
  752. evoice->Delta = voice->Delta;
  753. evoice->spurious_threshold = voice->spurious_threshold;
  754. evoice->LBA = voice->LBA;
  755. evoice->CSO = 0;
  756. evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
  757. evoice->CTRL = voice->CTRL;
  758. evoice->FMC = 3;
  759. evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
  760. evoice->EC = 0;
  761. evoice->Alpha = 0;
  762. evoice->FMS = 0;
  763. evoice->Vol = 0x3ff; /* mute */
  764. evoice->RVol = evoice->CVol = 0x7f; /* mute */
  765. evoice->Pan = 0x7f; /* mute */
  766. #if 0
  767. evoice->Attribute = (1<<(30-16))|(2<<(26-16))|
  768. (0<<(24-16))|(0x1f<<(19-16));
  769. #else
  770. evoice->Attribute = 0;
  771. #endif
  772. snd_trident_write_voice_regs(trident, evoice);
  773. evoice->isync2 = 1;
  774. evoice->isync_mark = runtime->period_size;
  775. evoice->ESO = (runtime->period_size * 2) - 1;
  776. }
  777. spin_unlock_irq(&trident->reg_lock);
  778. return 0;
  779. }
  780. /*---------------------------------------------------------------------------
  781. snd_trident_capture_hw_params
  782. Description: Set the hardware parameters for the capture device.
  783. Parameters: substream - PCM substream class
  784. hw_params - hardware parameters
  785. Returns: Error status
  786. ---------------------------------------------------------------------------*/
  787. static int snd_trident_capture_hw_params(snd_pcm_substream_t * substream,
  788. snd_pcm_hw_params_t * hw_params)
  789. {
  790. return snd_trident_allocate_pcm_mem(substream, hw_params);
  791. }
  792. /*---------------------------------------------------------------------------
  793. snd_trident_capture_prepare
  794. Description: Prepare capture device for playback.
  795. Parameters: substream - PCM substream class
  796. Returns: Error status
  797. ---------------------------------------------------------------------------*/
  798. static int snd_trident_capture_prepare(snd_pcm_substream_t * substream)
  799. {
  800. trident_t *trident = snd_pcm_substream_chip(substream);
  801. snd_pcm_runtime_t *runtime = substream->runtime;
  802. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  803. unsigned int val, ESO_bytes;
  804. spin_lock_irq(&trident->reg_lock);
  805. // Initilize the channel and set channel Mode
  806. outb(0, TRID_REG(trident, LEGACY_DMAR15));
  807. // Set DMA channel operation mode register
  808. outb(0x54, TRID_REG(trident, LEGACY_DMAR11));
  809. // Set channel buffer Address, DMAR0 expects contiguous PCI memory area
  810. voice->LBA = runtime->dma_addr;
  811. outl(voice->LBA, TRID_REG(trident, LEGACY_DMAR0));
  812. if (voice->memblk)
  813. voice->LBA = voice->memblk->offset;
  814. // set ESO
  815. ESO_bytes = snd_pcm_lib_buffer_bytes(substream) - 1;
  816. outb((ESO_bytes & 0x00ff0000) >> 16, TRID_REG(trident, LEGACY_DMAR6));
  817. outw((ESO_bytes & 0x0000ffff), TRID_REG(trident, LEGACY_DMAR4));
  818. ESO_bytes++;
  819. // Set channel sample rate, 4.12 format
  820. val = (((unsigned int) 48000L << 12) + (runtime->rate/2)) / runtime->rate;
  821. outw(val, TRID_REG(trident, T4D_SBDELTA_DELTA_R));
  822. // Set channel interrupt blk length
  823. if (snd_pcm_format_width(runtime->format) == 16) {
  824. val = (unsigned short) ((ESO_bytes >> 1) - 1);
  825. } else {
  826. val = (unsigned short) (ESO_bytes - 1);
  827. }
  828. outl((val << 16) | val, TRID_REG(trident, T4D_SBBL_SBCL));
  829. // Right now, set format and start to run captureing,
  830. // continuous run loop enable.
  831. trident->bDMAStart = 0x19; // 0001 1001b
  832. if (snd_pcm_format_width(runtime->format) == 16)
  833. trident->bDMAStart |= 0x80;
  834. if (snd_pcm_format_signed(runtime->format))
  835. trident->bDMAStart |= 0x20;
  836. if (runtime->channels > 1)
  837. trident->bDMAStart |= 0x40;
  838. // Prepare capture intr channel
  839. voice->Delta = snd_trident_convert_rate(runtime->rate);
  840. voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  841. voice->isync = 1;
  842. voice->isync_mark = runtime->period_size;
  843. voice->isync_max = runtime->buffer_size;
  844. // Set voice parameters
  845. voice->CSO = 0;
  846. voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
  847. voice->CTRL = snd_trident_control_mode(substream);
  848. voice->FMC = 3;
  849. voice->RVol = 0x7f;
  850. voice->CVol = 0x7f;
  851. voice->GVSel = 1;
  852. voice->Pan = 0x7f; /* mute */
  853. voice->Vol = 0x3ff; /* mute */
  854. voice->EC = 0;
  855. voice->Alpha = 0;
  856. voice->FMS = 0;
  857. voice->Attribute = 0;
  858. snd_trident_write_voice_regs(trident, voice);
  859. spin_unlock_irq(&trident->reg_lock);
  860. return 0;
  861. }
  862. /*---------------------------------------------------------------------------
  863. snd_trident_si7018_capture_hw_params
  864. Description: Set the hardware parameters for the capture device.
  865. Parameters: substream - PCM substream class
  866. hw_params - hardware parameters
  867. Returns: Error status
  868. ---------------------------------------------------------------------------*/
  869. static int snd_trident_si7018_capture_hw_params(snd_pcm_substream_t * substream,
  870. snd_pcm_hw_params_t * hw_params)
  871. {
  872. int err;
  873. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  874. return err;
  875. return snd_trident_allocate_evoice(substream, hw_params);
  876. }
  877. /*---------------------------------------------------------------------------
  878. snd_trident_si7018_capture_hw_free
  879. Description: Release the hardware resources for the capture device.
  880. Parameters: substream - PCM substream class
  881. Returns: Error status
  882. ---------------------------------------------------------------------------*/
  883. static int snd_trident_si7018_capture_hw_free(snd_pcm_substream_t * substream)
  884. {
  885. trident_t *trident = snd_pcm_substream_chip(substream);
  886. snd_pcm_runtime_t *runtime = substream->runtime;
  887. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  888. snd_trident_voice_t *evoice = voice ? voice->extra : NULL;
  889. snd_pcm_lib_free_pages(substream);
  890. if (evoice != NULL) {
  891. snd_trident_free_voice(trident, evoice);
  892. voice->extra = NULL;
  893. }
  894. return 0;
  895. }
  896. /*---------------------------------------------------------------------------
  897. snd_trident_si7018_capture_prepare
  898. Description: Prepare capture device for playback.
  899. Parameters: substream - PCM substream class
  900. Returns: Error status
  901. ---------------------------------------------------------------------------*/
  902. static int snd_trident_si7018_capture_prepare(snd_pcm_substream_t * substream)
  903. {
  904. trident_t *trident = snd_pcm_substream_chip(substream);
  905. snd_pcm_runtime_t *runtime = substream->runtime;
  906. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  907. snd_trident_voice_t *evoice = voice->extra;
  908. spin_lock_irq(&trident->reg_lock);
  909. voice->LBA = runtime->dma_addr;
  910. voice->Delta = snd_trident_convert_adc_rate(runtime->rate);
  911. voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  912. // Set voice parameters
  913. voice->CSO = 0;
  914. voice->ESO = runtime->buffer_size - 1; /* in samples */
  915. voice->CTRL = snd_trident_control_mode(substream);
  916. voice->FMC = 0;
  917. voice->RVol = 0;
  918. voice->CVol = 0;
  919. voice->GVSel = 1;
  920. voice->Pan = T4D_DEFAULT_PCM_PAN;
  921. voice->Vol = 0;
  922. voice->EC = 0;
  923. voice->Alpha = 0;
  924. voice->FMS = 0;
  925. voice->Attribute = (2 << (30-16)) |
  926. (2 << (26-16)) |
  927. (2 << (24-16)) |
  928. (1 << (23-16));
  929. snd_trident_write_voice_regs(trident, voice);
  930. if (evoice != NULL) {
  931. evoice->Delta = snd_trident_convert_rate(runtime->rate);
  932. evoice->spurious_threshold = voice->spurious_threshold;
  933. evoice->LBA = voice->LBA;
  934. evoice->CSO = 0;
  935. evoice->ESO = (runtime->period_size * 2) + 20 - 1; /* in samples, 20 means correction */
  936. evoice->CTRL = voice->CTRL;
  937. evoice->FMC = 3;
  938. evoice->GVSel = 0;
  939. evoice->EC = 0;
  940. evoice->Alpha = 0;
  941. evoice->FMS = 0;
  942. evoice->Vol = 0x3ff; /* mute */
  943. evoice->RVol = evoice->CVol = 0x7f; /* mute */
  944. evoice->Pan = 0x7f; /* mute */
  945. evoice->Attribute = 0;
  946. snd_trident_write_voice_regs(trident, evoice);
  947. evoice->isync2 = 1;
  948. evoice->isync_mark = runtime->period_size;
  949. evoice->ESO = (runtime->period_size * 2) - 1;
  950. }
  951. spin_unlock_irq(&trident->reg_lock);
  952. return 0;
  953. }
  954. /*---------------------------------------------------------------------------
  955. snd_trident_foldback_prepare
  956. Description: Prepare foldback capture device for playback.
  957. Parameters: substream - PCM substream class
  958. Returns: Error status
  959. ---------------------------------------------------------------------------*/
  960. static int snd_trident_foldback_prepare(snd_pcm_substream_t * substream)
  961. {
  962. trident_t *trident = snd_pcm_substream_chip(substream);
  963. snd_pcm_runtime_t *runtime = substream->runtime;
  964. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  965. snd_trident_voice_t *evoice = voice->extra;
  966. spin_lock_irq(&trident->reg_lock);
  967. /* Set channel buffer Address */
  968. if (voice->memblk)
  969. voice->LBA = voice->memblk->offset;
  970. else
  971. voice->LBA = runtime->dma_addr;
  972. /* set target ESO for channel */
  973. voice->ESO = runtime->buffer_size - 1; /* in samples */
  974. /* set sample rate */
  975. voice->Delta = 0x1000;
  976. voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
  977. voice->CSO = 0;
  978. voice->CTRL = snd_trident_control_mode(substream);
  979. voice->FMC = 3;
  980. voice->RVol = 0x7f;
  981. voice->CVol = 0x7f;
  982. voice->GVSel = 1;
  983. voice->Pan = 0x7f; /* mute */
  984. voice->Vol = 0x3ff; /* mute */
  985. voice->EC = 0;
  986. voice->Alpha = 0;
  987. voice->FMS = 0;
  988. voice->Attribute = 0;
  989. /* set up capture channel */
  990. outb(((voice->number & 0x3f) | 0x80), TRID_REG(trident, T4D_RCI + voice->foldback_chan));
  991. snd_trident_write_voice_regs(trident, voice);
  992. if (evoice != NULL) {
  993. evoice->Delta = voice->Delta;
  994. evoice->spurious_threshold = voice->spurious_threshold;
  995. evoice->LBA = voice->LBA;
  996. evoice->CSO = 0;
  997. evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
  998. evoice->CTRL = voice->CTRL;
  999. evoice->FMC = 3;
  1000. evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
  1001. evoice->EC = 0;
  1002. evoice->Alpha = 0;
  1003. evoice->FMS = 0;
  1004. evoice->Vol = 0x3ff; /* mute */
  1005. evoice->RVol = evoice->CVol = 0x7f; /* mute */
  1006. evoice->Pan = 0x7f; /* mute */
  1007. evoice->Attribute = 0;
  1008. snd_trident_write_voice_regs(trident, evoice);
  1009. evoice->isync2 = 1;
  1010. evoice->isync_mark = runtime->period_size;
  1011. evoice->ESO = (runtime->period_size * 2) - 1;
  1012. }
  1013. spin_unlock_irq(&trident->reg_lock);
  1014. return 0;
  1015. }
  1016. /*---------------------------------------------------------------------------
  1017. snd_trident_spdif_hw_params
  1018. Description: Set the hardware parameters for the spdif device.
  1019. Parameters: substream - PCM substream class
  1020. hw_params - hardware parameters
  1021. Returns: Error status
  1022. ---------------------------------------------------------------------------*/
  1023. static int snd_trident_spdif_hw_params(snd_pcm_substream_t * substream,
  1024. snd_pcm_hw_params_t * hw_params)
  1025. {
  1026. trident_t *trident = snd_pcm_substream_chip(substream);
  1027. unsigned int old_bits = 0, change = 0;
  1028. int err;
  1029. err = snd_trident_allocate_pcm_mem(substream, hw_params);
  1030. if (err < 0)
  1031. return err;
  1032. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  1033. err = snd_trident_allocate_evoice(substream, hw_params);
  1034. if (err < 0)
  1035. return err;
  1036. }
  1037. /* prepare SPDIF channel */
  1038. spin_lock_irq(&trident->reg_lock);
  1039. old_bits = trident->spdif_pcm_bits;
  1040. if (old_bits & IEC958_AES0_PROFESSIONAL)
  1041. trident->spdif_pcm_bits &= ~IEC958_AES0_PRO_FS;
  1042. else
  1043. trident->spdif_pcm_bits &= ~(IEC958_AES3_CON_FS << 24);
  1044. if (params_rate(hw_params) >= 48000) {
  1045. trident->spdif_pcm_ctrl = 0x3c; // 48000 Hz
  1046. trident->spdif_pcm_bits |=
  1047. trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
  1048. IEC958_AES0_PRO_FS_48000 :
  1049. (IEC958_AES3_CON_FS_48000 << 24);
  1050. }
  1051. else if (params_rate(hw_params) >= 44100) {
  1052. trident->spdif_pcm_ctrl = 0x3e; // 44100 Hz
  1053. trident->spdif_pcm_bits |=
  1054. trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
  1055. IEC958_AES0_PRO_FS_44100 :
  1056. (IEC958_AES3_CON_FS_44100 << 24);
  1057. }
  1058. else {
  1059. trident->spdif_pcm_ctrl = 0x3d; // 32000 Hz
  1060. trident->spdif_pcm_bits |=
  1061. trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
  1062. IEC958_AES0_PRO_FS_32000 :
  1063. (IEC958_AES3_CON_FS_32000 << 24);
  1064. }
  1065. change = old_bits != trident->spdif_pcm_bits;
  1066. spin_unlock_irq(&trident->reg_lock);
  1067. if (change)
  1068. snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE, &trident->spdif_pcm_ctl->id);
  1069. return 0;
  1070. }
  1071. /*---------------------------------------------------------------------------
  1072. snd_trident_spdif_prepare
  1073. Description: Prepare SPDIF device for playback.
  1074. Parameters: substream - PCM substream class
  1075. Returns: Error status
  1076. ---------------------------------------------------------------------------*/
  1077. static int snd_trident_spdif_prepare(snd_pcm_substream_t * substream)
  1078. {
  1079. trident_t *trident = snd_pcm_substream_chip(substream);
  1080. snd_pcm_runtime_t *runtime = substream->runtime;
  1081. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  1082. snd_trident_voice_t *evoice = voice->extra;
  1083. snd_trident_pcm_mixer_t *mix = &trident->pcm_mixer[substream->number];
  1084. unsigned int RESO, LBAO;
  1085. unsigned int temp;
  1086. spin_lock_irq(&trident->reg_lock);
  1087. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1088. /* set delta (rate) value */
  1089. voice->Delta = snd_trident_convert_rate(runtime->rate);
  1090. voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  1091. /* set Loop Back Address */
  1092. LBAO = runtime->dma_addr;
  1093. if (voice->memblk)
  1094. voice->LBA = voice->memblk->offset;
  1095. else
  1096. voice->LBA = LBAO;
  1097. voice->isync = 1;
  1098. voice->isync3 = 1;
  1099. voice->isync_mark = runtime->period_size;
  1100. voice->isync_max = runtime->buffer_size;
  1101. /* set target ESO for channel */
  1102. RESO = runtime->buffer_size - 1;
  1103. voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
  1104. /* set ctrl mode */
  1105. voice->CTRL = snd_trident_control_mode(substream);
  1106. voice->FMC = 3;
  1107. voice->RVol = 0x7f;
  1108. voice->CVol = 0x7f;
  1109. voice->GVSel = 1;
  1110. voice->Pan = 0x7f;
  1111. voice->Vol = 0x3ff;
  1112. voice->EC = 0;
  1113. voice->CSO = 0;
  1114. voice->Alpha = 0;
  1115. voice->FMS = 0;
  1116. voice->Attribute = 0;
  1117. /* prepare surrogate IRQ channel */
  1118. snd_trident_write_voice_regs(trident, voice);
  1119. outw((RESO & 0xffff), TRID_REG(trident, NX_SPESO));
  1120. outb((RESO >> 16), TRID_REG(trident, NX_SPESO + 2));
  1121. outl((LBAO & 0xfffffffc), TRID_REG(trident, NX_SPLBA));
  1122. outw((voice->CSO & 0xffff), TRID_REG(trident, NX_SPCTRL_SPCSO));
  1123. outb((voice->CSO >> 16), TRID_REG(trident, NX_SPCTRL_SPCSO + 2));
  1124. /* set SPDIF setting */
  1125. outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1126. outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  1127. } else { /* SiS */
  1128. /* set delta (rate) value */
  1129. voice->Delta = 0x800;
  1130. voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
  1131. /* set Loop Begin Address */
  1132. if (voice->memblk)
  1133. voice->LBA = voice->memblk->offset;
  1134. else
  1135. voice->LBA = runtime->dma_addr;
  1136. voice->CSO = 0;
  1137. voice->ESO = runtime->buffer_size - 1; /* in samples */
  1138. voice->CTRL = snd_trident_control_mode(substream);
  1139. voice->FMC = 3;
  1140. voice->GVSel = 1;
  1141. voice->EC = 0;
  1142. voice->Alpha = 0;
  1143. voice->FMS = 0;
  1144. voice->Vol = mix->vol;
  1145. voice->RVol = mix->rvol;
  1146. voice->CVol = mix->cvol;
  1147. voice->Pan = mix->pan;
  1148. voice->Attribute = (1<<(30-16))|(7<<(26-16))|
  1149. (0<<(24-16))|(0<<(19-16));
  1150. snd_trident_write_voice_regs(trident, voice);
  1151. if (evoice != NULL) {
  1152. evoice->Delta = voice->Delta;
  1153. evoice->spurious_threshold = voice->spurious_threshold;
  1154. evoice->LBA = voice->LBA;
  1155. evoice->CSO = 0;
  1156. evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
  1157. evoice->CTRL = voice->CTRL;
  1158. evoice->FMC = 3;
  1159. evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
  1160. evoice->EC = 0;
  1161. evoice->Alpha = 0;
  1162. evoice->FMS = 0;
  1163. evoice->Vol = 0x3ff; /* mute */
  1164. evoice->RVol = evoice->CVol = 0x7f; /* mute */
  1165. evoice->Pan = 0x7f; /* mute */
  1166. evoice->Attribute = 0;
  1167. snd_trident_write_voice_regs(trident, evoice);
  1168. evoice->isync2 = 1;
  1169. evoice->isync_mark = runtime->period_size;
  1170. evoice->ESO = (runtime->period_size * 2) - 1;
  1171. }
  1172. outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
  1173. temp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
  1174. temp &= ~(1<<19);
  1175. outl(temp, TRID_REG(trident, T4D_LFO_GC_CIR));
  1176. temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1177. temp |= SPDIF_EN;
  1178. outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1179. }
  1180. spin_unlock_irq(&trident->reg_lock);
  1181. return 0;
  1182. }
  1183. /*---------------------------------------------------------------------------
  1184. snd_trident_trigger
  1185. Description: Start/stop devices
  1186. Parameters: substream - PCM substream class
  1187. cmd - trigger command (STOP, GO)
  1188. Returns: Error status
  1189. ---------------------------------------------------------------------------*/
  1190. static int snd_trident_trigger(snd_pcm_substream_t *substream,
  1191. int cmd)
  1192. {
  1193. trident_t *trident = snd_pcm_substream_chip(substream);
  1194. struct list_head *pos;
  1195. snd_pcm_substream_t *s;
  1196. unsigned int what, whati, capture_flag, spdif_flag;
  1197. snd_trident_voice_t *voice, *evoice;
  1198. unsigned int val, go;
  1199. switch (cmd) {
  1200. case SNDRV_PCM_TRIGGER_START:
  1201. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1202. case SNDRV_PCM_TRIGGER_RESUME:
  1203. go = 1;
  1204. break;
  1205. case SNDRV_PCM_TRIGGER_STOP:
  1206. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1207. case SNDRV_PCM_TRIGGER_SUSPEND:
  1208. go = 0;
  1209. break;
  1210. default:
  1211. return -EINVAL;
  1212. }
  1213. what = whati = capture_flag = spdif_flag = 0;
  1214. spin_lock(&trident->reg_lock);
  1215. val = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
  1216. snd_pcm_group_for_each(pos, substream) {
  1217. s = snd_pcm_group_substream_entry(pos);
  1218. if ((trident_t *) snd_pcm_substream_chip(s) == trident) {
  1219. voice = (snd_trident_voice_t *) s->runtime->private_data;
  1220. evoice = voice->extra;
  1221. what |= 1 << (voice->number & 0x1f);
  1222. if (evoice == NULL) {
  1223. whati |= 1 << (voice->number & 0x1f);
  1224. } else {
  1225. what |= 1 << (evoice->number & 0x1f);
  1226. whati |= 1 << (evoice->number & 0x1f);
  1227. if (go)
  1228. evoice->stimer = val;
  1229. }
  1230. if (go) {
  1231. voice->running = 1;
  1232. voice->stimer = val;
  1233. } else {
  1234. voice->running = 0;
  1235. }
  1236. snd_pcm_trigger_done(s, substream);
  1237. if (voice->capture)
  1238. capture_flag = 1;
  1239. if (voice->spdif)
  1240. spdif_flag = 1;
  1241. }
  1242. }
  1243. if (spdif_flag) {
  1244. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1245. outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  1246. outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1247. } else {
  1248. outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
  1249. val = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) | SPDIF_EN;
  1250. outl(val, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1251. }
  1252. }
  1253. if (!go)
  1254. outl(what, TRID_REG(trident, T4D_STOP_B));
  1255. val = inl(TRID_REG(trident, T4D_AINTEN_B));
  1256. if (go) {
  1257. val |= whati;
  1258. } else {
  1259. val &= ~whati;
  1260. }
  1261. outl(val, TRID_REG(trident, T4D_AINTEN_B));
  1262. if (go) {
  1263. outl(what, TRID_REG(trident, T4D_START_B));
  1264. if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
  1265. outb(trident->bDMAStart, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
  1266. } else {
  1267. if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
  1268. outb(0x00, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
  1269. }
  1270. spin_unlock(&trident->reg_lock);
  1271. return 0;
  1272. }
  1273. /*---------------------------------------------------------------------------
  1274. snd_trident_playback_pointer
  1275. Description: This routine return the playback position
  1276. Parameters: substream - PCM substream class
  1277. Returns: position of buffer
  1278. ---------------------------------------------------------------------------*/
  1279. static snd_pcm_uframes_t snd_trident_playback_pointer(snd_pcm_substream_t * substream)
  1280. {
  1281. trident_t *trident = snd_pcm_substream_chip(substream);
  1282. snd_pcm_runtime_t *runtime = substream->runtime;
  1283. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  1284. unsigned int cso;
  1285. if (!voice->running)
  1286. return 0;
  1287. spin_lock(&trident->reg_lock);
  1288. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  1289. if (trident->device != TRIDENT_DEVICE_ID_NX) {
  1290. cso = inw(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
  1291. } else { // ID_4DWAVE_NX
  1292. cso = (unsigned int) inl(TRID_REG(trident, CH_NX_DELTA_CSO)) & 0x00ffffff;
  1293. }
  1294. spin_unlock(&trident->reg_lock);
  1295. if (cso >= runtime->buffer_size)
  1296. cso = 0;
  1297. return cso;
  1298. }
  1299. /*---------------------------------------------------------------------------
  1300. snd_trident_capture_pointer
  1301. Description: This routine return the capture position
  1302. Paramters: pcm1 - PCM device class
  1303. Returns: position of buffer
  1304. ---------------------------------------------------------------------------*/
  1305. static snd_pcm_uframes_t snd_trident_capture_pointer(snd_pcm_substream_t * substream)
  1306. {
  1307. trident_t *trident = snd_pcm_substream_chip(substream);
  1308. snd_pcm_runtime_t *runtime = substream->runtime;
  1309. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  1310. unsigned int result;
  1311. if (!voice->running)
  1312. return 0;
  1313. result = inw(TRID_REG(trident, T4D_SBBL_SBCL));
  1314. if (runtime->channels > 1)
  1315. result >>= 1;
  1316. if (result > 0)
  1317. result = runtime->buffer_size - result;
  1318. return result;
  1319. }
  1320. /*---------------------------------------------------------------------------
  1321. snd_trident_spdif_pointer
  1322. Description: This routine return the SPDIF playback position
  1323. Parameters: substream - PCM substream class
  1324. Returns: position of buffer
  1325. ---------------------------------------------------------------------------*/
  1326. static snd_pcm_uframes_t snd_trident_spdif_pointer(snd_pcm_substream_t * substream)
  1327. {
  1328. trident_t *trident = snd_pcm_substream_chip(substream);
  1329. snd_pcm_runtime_t *runtime = substream->runtime;
  1330. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  1331. unsigned int result;
  1332. if (!voice->running)
  1333. return 0;
  1334. result = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
  1335. return result;
  1336. }
  1337. /*
  1338. * Playback support device description
  1339. */
  1340. static snd_pcm_hardware_t snd_trident_playback =
  1341. {
  1342. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1343. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1344. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1345. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  1346. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1347. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1348. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1349. .rate_min = 4000,
  1350. .rate_max = 48000,
  1351. .channels_min = 1,
  1352. .channels_max = 2,
  1353. .buffer_bytes_max = (256*1024),
  1354. .period_bytes_min = 64,
  1355. .period_bytes_max = (256*1024),
  1356. .periods_min = 1,
  1357. .periods_max = 1024,
  1358. .fifo_size = 0,
  1359. };
  1360. /*
  1361. * Capture support device description
  1362. */
  1363. static snd_pcm_hardware_t snd_trident_capture =
  1364. {
  1365. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1366. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1367. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1368. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  1369. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1370. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1371. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1372. .rate_min = 4000,
  1373. .rate_max = 48000,
  1374. .channels_min = 1,
  1375. .channels_max = 2,
  1376. .buffer_bytes_max = (128*1024),
  1377. .period_bytes_min = 64,
  1378. .period_bytes_max = (128*1024),
  1379. .periods_min = 1,
  1380. .periods_max = 1024,
  1381. .fifo_size = 0,
  1382. };
  1383. /*
  1384. * Foldback capture support device description
  1385. */
  1386. static snd_pcm_hardware_t snd_trident_foldback =
  1387. {
  1388. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1389. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1390. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1391. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  1392. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1393. .rates = SNDRV_PCM_RATE_48000,
  1394. .rate_min = 48000,
  1395. .rate_max = 48000,
  1396. .channels_min = 2,
  1397. .channels_max = 2,
  1398. .buffer_bytes_max = (128*1024),
  1399. .period_bytes_min = 64,
  1400. .period_bytes_max = (128*1024),
  1401. .periods_min = 1,
  1402. .periods_max = 1024,
  1403. .fifo_size = 0,
  1404. };
  1405. /*
  1406. * SPDIF playback support device description
  1407. */
  1408. static snd_pcm_hardware_t snd_trident_spdif =
  1409. {
  1410. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1411. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1412. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1413. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  1414. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1415. .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  1416. SNDRV_PCM_RATE_48000),
  1417. .rate_min = 32000,
  1418. .rate_max = 48000,
  1419. .channels_min = 2,
  1420. .channels_max = 2,
  1421. .buffer_bytes_max = (128*1024),
  1422. .period_bytes_min = 64,
  1423. .period_bytes_max = (128*1024),
  1424. .periods_min = 1,
  1425. .periods_max = 1024,
  1426. .fifo_size = 0,
  1427. };
  1428. static snd_pcm_hardware_t snd_trident_spdif_7018 =
  1429. {
  1430. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1431. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1432. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1433. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  1434. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1435. .rates = SNDRV_PCM_RATE_48000,
  1436. .rate_min = 48000,
  1437. .rate_max = 48000,
  1438. .channels_min = 2,
  1439. .channels_max = 2,
  1440. .buffer_bytes_max = (128*1024),
  1441. .period_bytes_min = 64,
  1442. .period_bytes_max = (128*1024),
  1443. .periods_min = 1,
  1444. .periods_max = 1024,
  1445. .fifo_size = 0,
  1446. };
  1447. static void snd_trident_pcm_free_substream(snd_pcm_runtime_t *runtime)
  1448. {
  1449. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  1450. trident_t *trident;
  1451. if (voice) {
  1452. trident = voice->trident;
  1453. snd_trident_free_voice(trident, voice);
  1454. }
  1455. }
  1456. static int snd_trident_playback_open(snd_pcm_substream_t * substream)
  1457. {
  1458. trident_t *trident = snd_pcm_substream_chip(substream);
  1459. snd_pcm_runtime_t *runtime = substream->runtime;
  1460. snd_trident_voice_t *voice;
  1461. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1462. if (voice == NULL)
  1463. return -EAGAIN;
  1464. snd_trident_pcm_mixer_build(trident, voice, substream);
  1465. voice->substream = substream;
  1466. runtime->private_data = voice;
  1467. runtime->private_free = snd_trident_pcm_free_substream;
  1468. runtime->hw = snd_trident_playback;
  1469. snd_pcm_set_sync(substream);
  1470. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1471. return 0;
  1472. }
  1473. /*---------------------------------------------------------------------------
  1474. snd_trident_playback_close
  1475. Description: This routine will close the 4DWave playback device. For now
  1476. we will simply free the dma transfer buffer.
  1477. Parameters: substream - PCM substream class
  1478. ---------------------------------------------------------------------------*/
  1479. static int snd_trident_playback_close(snd_pcm_substream_t * substream)
  1480. {
  1481. trident_t *trident = snd_pcm_substream_chip(substream);
  1482. snd_pcm_runtime_t *runtime = substream->runtime;
  1483. snd_trident_voice_t *voice = (snd_trident_voice_t *) runtime->private_data;
  1484. snd_trident_pcm_mixer_free(trident, voice, substream);
  1485. return 0;
  1486. }
  1487. /*---------------------------------------------------------------------------
  1488. snd_trident_spdif_open
  1489. Description: This routine will open the 4DWave SPDIF device.
  1490. Parameters: substream - PCM substream class
  1491. Returns: status - success or failure flag
  1492. ---------------------------------------------------------------------------*/
  1493. static int snd_trident_spdif_open(snd_pcm_substream_t * substream)
  1494. {
  1495. trident_t *trident = snd_pcm_substream_chip(substream);
  1496. snd_trident_voice_t *voice;
  1497. snd_pcm_runtime_t *runtime = substream->runtime;
  1498. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1499. if (voice == NULL)
  1500. return -EAGAIN;
  1501. voice->spdif = 1;
  1502. voice->substream = substream;
  1503. spin_lock_irq(&trident->reg_lock);
  1504. trident->spdif_pcm_bits = trident->spdif_bits;
  1505. spin_unlock_irq(&trident->reg_lock);
  1506. runtime->private_data = voice;
  1507. runtime->private_free = snd_trident_pcm_free_substream;
  1508. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  1509. runtime->hw = snd_trident_spdif;
  1510. } else {
  1511. runtime->hw = snd_trident_spdif_7018;
  1512. }
  1513. trident->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1514. snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1515. SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
  1516. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1517. return 0;
  1518. }
  1519. /*---------------------------------------------------------------------------
  1520. snd_trident_spdif_close
  1521. Description: This routine will close the 4DWave SPDIF device.
  1522. Parameters: substream - PCM substream class
  1523. ---------------------------------------------------------------------------*/
  1524. static int snd_trident_spdif_close(snd_pcm_substream_t * substream)
  1525. {
  1526. trident_t *trident = snd_pcm_substream_chip(substream);
  1527. unsigned int temp;
  1528. spin_lock_irq(&trident->reg_lock);
  1529. // restore default SPDIF setting
  1530. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1531. outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1532. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  1533. } else {
  1534. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1535. temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1536. if (trident->spdif_ctrl) {
  1537. temp |= SPDIF_EN;
  1538. } else {
  1539. temp &= ~SPDIF_EN;
  1540. }
  1541. outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1542. }
  1543. spin_unlock_irq(&trident->reg_lock);
  1544. trident->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1545. snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1546. SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
  1547. return 0;
  1548. }
  1549. /*---------------------------------------------------------------------------
  1550. snd_trident_capture_open
  1551. Description: This routine will open the 4DWave capture device.
  1552. Parameters: substream - PCM substream class
  1553. Returns: status - success or failure flag
  1554. ---------------------------------------------------------------------------*/
  1555. static int snd_trident_capture_open(snd_pcm_substream_t * substream)
  1556. {
  1557. trident_t *trident = snd_pcm_substream_chip(substream);
  1558. snd_trident_voice_t *voice;
  1559. snd_pcm_runtime_t *runtime = substream->runtime;
  1560. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1561. if (voice == NULL)
  1562. return -EAGAIN;
  1563. voice->capture = 1;
  1564. voice->substream = substream;
  1565. runtime->private_data = voice;
  1566. runtime->private_free = snd_trident_pcm_free_substream;
  1567. runtime->hw = snd_trident_capture;
  1568. snd_pcm_set_sync(substream);
  1569. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1570. return 0;
  1571. }
  1572. /*---------------------------------------------------------------------------
  1573. snd_trident_capture_close
  1574. Description: This routine will close the 4DWave capture device. For now
  1575. we will simply free the dma transfer buffer.
  1576. Parameters: substream - PCM substream class
  1577. ---------------------------------------------------------------------------*/
  1578. static int snd_trident_capture_close(snd_pcm_substream_t * substream)
  1579. {
  1580. return 0;
  1581. }
  1582. /*---------------------------------------------------------------------------
  1583. snd_trident_foldback_open
  1584. Description: This routine will open the 4DWave foldback capture device.
  1585. Parameters: substream - PCM substream class
  1586. Returns: status - success or failure flag
  1587. ---------------------------------------------------------------------------*/
  1588. static int snd_trident_foldback_open(snd_pcm_substream_t * substream)
  1589. {
  1590. trident_t *trident = snd_pcm_substream_chip(substream);
  1591. snd_trident_voice_t *voice;
  1592. snd_pcm_runtime_t *runtime = substream->runtime;
  1593. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1594. if (voice == NULL)
  1595. return -EAGAIN;
  1596. voice->foldback_chan = substream->number;
  1597. voice->substream = substream;
  1598. runtime->private_data = voice;
  1599. runtime->private_free = snd_trident_pcm_free_substream;
  1600. runtime->hw = snd_trident_foldback;
  1601. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1602. return 0;
  1603. }
  1604. /*---------------------------------------------------------------------------
  1605. snd_trident_foldback_close
  1606. Description: This routine will close the 4DWave foldback capture device.
  1607. For now we will simply free the dma transfer buffer.
  1608. Parameters: substream - PCM substream class
  1609. ---------------------------------------------------------------------------*/
  1610. static int snd_trident_foldback_close(snd_pcm_substream_t * substream)
  1611. {
  1612. trident_t *trident = snd_pcm_substream_chip(substream);
  1613. snd_trident_voice_t *voice;
  1614. snd_pcm_runtime_t *runtime = substream->runtime;
  1615. voice = (snd_trident_voice_t *) runtime->private_data;
  1616. /* stop capture channel */
  1617. spin_lock_irq(&trident->reg_lock);
  1618. outb(0x00, TRID_REG(trident, T4D_RCI + voice->foldback_chan));
  1619. spin_unlock_irq(&trident->reg_lock);
  1620. return 0;
  1621. }
  1622. /*---------------------------------------------------------------------------
  1623. PCM operations
  1624. ---------------------------------------------------------------------------*/
  1625. static snd_pcm_ops_t snd_trident_playback_ops = {
  1626. .open = snd_trident_playback_open,
  1627. .close = snd_trident_playback_close,
  1628. .ioctl = snd_trident_ioctl,
  1629. .hw_params = snd_trident_hw_params,
  1630. .hw_free = snd_trident_hw_free,
  1631. .prepare = snd_trident_playback_prepare,
  1632. .trigger = snd_trident_trigger,
  1633. .pointer = snd_trident_playback_pointer,
  1634. };
  1635. static snd_pcm_ops_t snd_trident_nx_playback_ops = {
  1636. .open = snd_trident_playback_open,
  1637. .close = snd_trident_playback_close,
  1638. .ioctl = snd_trident_ioctl,
  1639. .hw_params = snd_trident_hw_params,
  1640. .hw_free = snd_trident_hw_free,
  1641. .prepare = snd_trident_playback_prepare,
  1642. .trigger = snd_trident_trigger,
  1643. .pointer = snd_trident_playback_pointer,
  1644. .page = snd_pcm_sgbuf_ops_page,
  1645. };
  1646. static snd_pcm_ops_t snd_trident_capture_ops = {
  1647. .open = snd_trident_capture_open,
  1648. .close = snd_trident_capture_close,
  1649. .ioctl = snd_trident_ioctl,
  1650. .hw_params = snd_trident_capture_hw_params,
  1651. .hw_free = snd_trident_hw_free,
  1652. .prepare = snd_trident_capture_prepare,
  1653. .trigger = snd_trident_trigger,
  1654. .pointer = snd_trident_capture_pointer,
  1655. };
  1656. static snd_pcm_ops_t snd_trident_si7018_capture_ops = {
  1657. .open = snd_trident_capture_open,
  1658. .close = snd_trident_capture_close,
  1659. .ioctl = snd_trident_ioctl,
  1660. .hw_params = snd_trident_si7018_capture_hw_params,
  1661. .hw_free = snd_trident_si7018_capture_hw_free,
  1662. .prepare = snd_trident_si7018_capture_prepare,
  1663. .trigger = snd_trident_trigger,
  1664. .pointer = snd_trident_playback_pointer,
  1665. };
  1666. static snd_pcm_ops_t snd_trident_foldback_ops = {
  1667. .open = snd_trident_foldback_open,
  1668. .close = snd_trident_foldback_close,
  1669. .ioctl = snd_trident_ioctl,
  1670. .hw_params = snd_trident_hw_params,
  1671. .hw_free = snd_trident_hw_free,
  1672. .prepare = snd_trident_foldback_prepare,
  1673. .trigger = snd_trident_trigger,
  1674. .pointer = snd_trident_playback_pointer,
  1675. };
  1676. static snd_pcm_ops_t snd_trident_nx_foldback_ops = {
  1677. .open = snd_trident_foldback_open,
  1678. .close = snd_trident_foldback_close,
  1679. .ioctl = snd_trident_ioctl,
  1680. .hw_params = snd_trident_hw_params,
  1681. .hw_free = snd_trident_hw_free,
  1682. .prepare = snd_trident_foldback_prepare,
  1683. .trigger = snd_trident_trigger,
  1684. .pointer = snd_trident_playback_pointer,
  1685. .page = snd_pcm_sgbuf_ops_page,
  1686. };
  1687. static snd_pcm_ops_t snd_trident_spdif_ops = {
  1688. .open = snd_trident_spdif_open,
  1689. .close = snd_trident_spdif_close,
  1690. .ioctl = snd_trident_ioctl,
  1691. .hw_params = snd_trident_spdif_hw_params,
  1692. .hw_free = snd_trident_hw_free,
  1693. .prepare = snd_trident_spdif_prepare,
  1694. .trigger = snd_trident_trigger,
  1695. .pointer = snd_trident_spdif_pointer,
  1696. };
  1697. static snd_pcm_ops_t snd_trident_spdif_7018_ops = {
  1698. .open = snd_trident_spdif_open,
  1699. .close = snd_trident_spdif_close,
  1700. .ioctl = snd_trident_ioctl,
  1701. .hw_params = snd_trident_spdif_hw_params,
  1702. .hw_free = snd_trident_hw_free,
  1703. .prepare = snd_trident_spdif_prepare,
  1704. .trigger = snd_trident_trigger,
  1705. .pointer = snd_trident_playback_pointer,
  1706. };
  1707. /*---------------------------------------------------------------------------
  1708. snd_trident_pcm_free
  1709. Description: This routine release the 4DWave private data.
  1710. Paramters: private_data - pointer to 4DWave device info.
  1711. Returns: None
  1712. ---------------------------------------------------------------------------*/
  1713. static void snd_trident_pcm_free(snd_pcm_t *pcm)
  1714. {
  1715. trident_t *trident = pcm->private_data;
  1716. trident->pcm = NULL;
  1717. snd_pcm_lib_preallocate_free_for_all(pcm);
  1718. }
  1719. static void snd_trident_foldback_pcm_free(snd_pcm_t *pcm)
  1720. {
  1721. trident_t *trident = pcm->private_data;
  1722. trident->foldback = NULL;
  1723. snd_pcm_lib_preallocate_free_for_all(pcm);
  1724. }
  1725. static void snd_trident_spdif_pcm_free(snd_pcm_t *pcm)
  1726. {
  1727. trident_t *trident = pcm->private_data;
  1728. trident->spdif = NULL;
  1729. snd_pcm_lib_preallocate_free_for_all(pcm);
  1730. }
  1731. /*---------------------------------------------------------------------------
  1732. snd_trident_pcm
  1733. Description: This routine registers the 4DWave device for PCM support.
  1734. Paramters: trident - pointer to target device class for 4DWave.
  1735. Returns: None
  1736. ---------------------------------------------------------------------------*/
  1737. int __devinit snd_trident_pcm(trident_t * trident, int device, snd_pcm_t ** rpcm)
  1738. {
  1739. snd_pcm_t *pcm;
  1740. int err;
  1741. if (rpcm)
  1742. *rpcm = NULL;
  1743. if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, trident->ChanPCM, 1, &pcm)) < 0)
  1744. return err;
  1745. pcm->private_data = trident;
  1746. pcm->private_free = snd_trident_pcm_free;
  1747. if (trident->tlb.entries) {
  1748. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_nx_playback_ops);
  1749. } else {
  1750. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_playback_ops);
  1751. }
  1752. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1753. trident->device != TRIDENT_DEVICE_ID_SI7018 ?
  1754. &snd_trident_capture_ops :
  1755. &snd_trident_si7018_capture_ops);
  1756. pcm->info_flags = 0;
  1757. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1758. strcpy(pcm->name, "Trident 4DWave");
  1759. trident->pcm = pcm;
  1760. if (trident->tlb.entries) {
  1761. snd_pcm_substream_t *substream;
  1762. for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
  1763. snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
  1764. snd_dma_pci_data(trident->pci),
  1765. 64*1024, 128*1024);
  1766. snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  1767. SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
  1768. 64*1024, 128*1024);
  1769. } else {
  1770. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1771. snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
  1772. }
  1773. if (rpcm)
  1774. *rpcm = pcm;
  1775. return 0;
  1776. }
  1777. /*---------------------------------------------------------------------------
  1778. snd_trident_foldback_pcm
  1779. Description: This routine registers the 4DWave device for foldback PCM support.
  1780. Paramters: trident - pointer to target device class for 4DWave.
  1781. Returns: None
  1782. ---------------------------------------------------------------------------*/
  1783. int __devinit snd_trident_foldback_pcm(trident_t * trident, int device, snd_pcm_t ** rpcm)
  1784. {
  1785. snd_pcm_t *foldback;
  1786. int err;
  1787. int num_chan = 3;
  1788. snd_pcm_substream_t *substream;
  1789. if (rpcm)
  1790. *rpcm = NULL;
  1791. if (trident->device == TRIDENT_DEVICE_ID_NX)
  1792. num_chan = 4;
  1793. if ((err = snd_pcm_new(trident->card, "trident_dx_nx", device, 0, num_chan, &foldback)) < 0)
  1794. return err;
  1795. foldback->private_data = trident;
  1796. foldback->private_free = snd_trident_foldback_pcm_free;
  1797. if (trident->tlb.entries)
  1798. snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_nx_foldback_ops);
  1799. else
  1800. snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_foldback_ops);
  1801. foldback->info_flags = 0;
  1802. strcpy(foldback->name, "Trident 4DWave");
  1803. substream = foldback->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1804. strcpy(substream->name, "Front Mixer");
  1805. substream = substream->next;
  1806. strcpy(substream->name, "Reverb Mixer");
  1807. substream = substream->next;
  1808. strcpy(substream->name, "Chorus Mixer");
  1809. if (num_chan == 4) {
  1810. substream = substream->next;
  1811. strcpy(substream->name, "Second AC'97 ADC");
  1812. }
  1813. trident->foldback = foldback;
  1814. if (trident->tlb.entries)
  1815. snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV_SG,
  1816. snd_dma_pci_data(trident->pci), 0, 128*1024);
  1817. else
  1818. snd_pcm_lib_preallocate_pages_for_all(foldback, SNDRV_DMA_TYPE_DEV,
  1819. snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
  1820. if (rpcm)
  1821. *rpcm = foldback;
  1822. return 0;
  1823. }
  1824. /*---------------------------------------------------------------------------
  1825. snd_trident_spdif
  1826. Description: This routine registers the 4DWave-NX device for SPDIF support.
  1827. Paramters: trident - pointer to target device class for 4DWave-NX.
  1828. Returns: None
  1829. ---------------------------------------------------------------------------*/
  1830. int __devinit snd_trident_spdif_pcm(trident_t * trident, int device, snd_pcm_t ** rpcm)
  1831. {
  1832. snd_pcm_t *spdif;
  1833. int err;
  1834. if (rpcm)
  1835. *rpcm = NULL;
  1836. if ((err = snd_pcm_new(trident->card, "trident_dx_nx IEC958", device, 1, 0, &spdif)) < 0)
  1837. return err;
  1838. spdif->private_data = trident;
  1839. spdif->private_free = snd_trident_spdif_pcm_free;
  1840. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1841. snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_ops);
  1842. } else {
  1843. snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_7018_ops);
  1844. }
  1845. spdif->info_flags = 0;
  1846. strcpy(spdif->name, "Trident 4DWave IEC958");
  1847. trident->spdif = spdif;
  1848. snd_pcm_lib_preallocate_pages_for_all(spdif, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci), 64*1024, 128*1024);
  1849. if (rpcm)
  1850. *rpcm = spdif;
  1851. return 0;
  1852. }
  1853. /*
  1854. * Mixer part
  1855. */
  1856. /*---------------------------------------------------------------------------
  1857. snd_trident_spdif_control
  1858. Description: enable/disable S/PDIF out from ac97 mixer
  1859. ---------------------------------------------------------------------------*/
  1860. static int snd_trident_spdif_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1861. {
  1862. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1863. uinfo->count = 1;
  1864. uinfo->value.integer.min = 0;
  1865. uinfo->value.integer.max = 1;
  1866. return 0;
  1867. }
  1868. static int snd_trident_spdif_control_get(snd_kcontrol_t * kcontrol,
  1869. snd_ctl_elem_value_t * ucontrol)
  1870. {
  1871. trident_t *trident = snd_kcontrol_chip(kcontrol);
  1872. unsigned char val;
  1873. spin_lock_irq(&trident->reg_lock);
  1874. val = trident->spdif_ctrl;
  1875. ucontrol->value.integer.value[0] = val == kcontrol->private_value;
  1876. spin_unlock_irq(&trident->reg_lock);
  1877. return 0;
  1878. }
  1879. static int snd_trident_spdif_control_put(snd_kcontrol_t * kcontrol,
  1880. snd_ctl_elem_value_t * ucontrol)
  1881. {
  1882. trident_t *trident = snd_kcontrol_chip(kcontrol);
  1883. unsigned char val;
  1884. int change;
  1885. val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
  1886. spin_lock_irq(&trident->reg_lock);
  1887. /* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
  1888. change = trident->spdif_ctrl != val;
  1889. trident->spdif_ctrl = val;
  1890. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1891. if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
  1892. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  1893. outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1894. }
  1895. } else {
  1896. if (trident->spdif == NULL) {
  1897. unsigned int temp;
  1898. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1899. temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
  1900. if (val)
  1901. temp |= SPDIF_EN;
  1902. outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1903. }
  1904. }
  1905. spin_unlock_irq(&trident->reg_lock);
  1906. return change;
  1907. }
  1908. static snd_kcontrol_new_t snd_trident_spdif_control __devinitdata =
  1909. {
  1910. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1911. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  1912. .info = snd_trident_spdif_control_info,
  1913. .get = snd_trident_spdif_control_get,
  1914. .put = snd_trident_spdif_control_put,
  1915. .private_value = 0x28,
  1916. };
  1917. /*---------------------------------------------------------------------------
  1918. snd_trident_spdif_default
  1919. Description: put/get the S/PDIF default settings
  1920. ---------------------------------------------------------------------------*/
  1921. static int snd_trident_spdif_default_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1922. {
  1923. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1924. uinfo->count = 1;
  1925. return 0;
  1926. }
  1927. static int snd_trident_spdif_default_get(snd_kcontrol_t * kcontrol,
  1928. snd_ctl_elem_value_t * ucontrol)
  1929. {
  1930. trident_t *trident = snd_kcontrol_chip(kcontrol);
  1931. spin_lock_irq(&trident->reg_lock);
  1932. ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
  1933. ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
  1934. ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
  1935. ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
  1936. spin_unlock_irq(&trident->reg_lock);
  1937. return 0;
  1938. }
  1939. static int snd_trident_spdif_default_put(snd_kcontrol_t * kcontrol,
  1940. snd_ctl_elem_value_t * ucontrol)
  1941. {
  1942. trident_t *trident = snd_kcontrol_chip(kcontrol);
  1943. unsigned int val;
  1944. int change;
  1945. val = (ucontrol->value.iec958.status[0] << 0) |
  1946. (ucontrol->value.iec958.status[1] << 8) |
  1947. (ucontrol->value.iec958.status[2] << 16) |
  1948. (ucontrol->value.iec958.status[3] << 24);
  1949. spin_lock_irq(&trident->reg_lock);
  1950. change = trident->spdif_bits != val;
  1951. trident->spdif_bits = val;
  1952. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1953. if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
  1954. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  1955. } else {
  1956. if (trident->spdif == NULL)
  1957. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1958. }
  1959. spin_unlock_irq(&trident->reg_lock);
  1960. return change;
  1961. }
  1962. static snd_kcontrol_new_t snd_trident_spdif_default __devinitdata =
  1963. {
  1964. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1965. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1966. .info = snd_trident_spdif_default_info,
  1967. .get = snd_trident_spdif_default_get,
  1968. .put = snd_trident_spdif_default_put
  1969. };
  1970. /*---------------------------------------------------------------------------
  1971. snd_trident_spdif_mask
  1972. Description: put/get the S/PDIF mask
  1973. ---------------------------------------------------------------------------*/
  1974. static int snd_trident_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1975. {
  1976. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1977. uinfo->count = 1;
  1978. return 0;
  1979. }
  1980. static int snd_trident_spdif_mask_get(snd_kcontrol_t * kcontrol,
  1981. snd_ctl_elem_value_t * ucontrol)
  1982. {
  1983. ucontrol->value.iec958.status[0] = 0xff;
  1984. ucontrol->value.iec958.status[1] = 0xff;
  1985. ucontrol->value.iec958.status[2] = 0xff;
  1986. ucontrol->value.iec958.status[3] = 0xff;
  1987. return 0;
  1988. }
  1989. static snd_kcontrol_new_t snd_trident_spdif_mask __devinitdata =
  1990. {
  1991. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1992. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1993. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  1994. .info = snd_trident_spdif_mask_info,
  1995. .get = snd_trident_spdif_mask_get,
  1996. };
  1997. /*---------------------------------------------------------------------------
  1998. snd_trident_spdif_stream
  1999. Description: put/get the S/PDIF stream settings
  2000. ---------------------------------------------------------------------------*/
  2001. static int snd_trident_spdif_stream_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  2002. {
  2003. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  2004. uinfo->count = 1;
  2005. return 0;
  2006. }
  2007. static int snd_trident_spdif_stream_get(snd_kcontrol_t * kcontrol,
  2008. snd_ctl_elem_value_t * ucontrol)
  2009. {
  2010. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2011. spin_lock_irq(&trident->reg_lock);
  2012. ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
  2013. ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
  2014. ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
  2015. ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
  2016. spin_unlock_irq(&trident->reg_lock);
  2017. return 0;
  2018. }
  2019. static int snd_trident_spdif_stream_put(snd_kcontrol_t * kcontrol,
  2020. snd_ctl_elem_value_t * ucontrol)
  2021. {
  2022. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2023. unsigned int val;
  2024. int change;
  2025. val = (ucontrol->value.iec958.status[0] << 0) |
  2026. (ucontrol->value.iec958.status[1] << 8) |
  2027. (ucontrol->value.iec958.status[2] << 16) |
  2028. (ucontrol->value.iec958.status[3] << 24);
  2029. spin_lock_irq(&trident->reg_lock);
  2030. change = trident->spdif_pcm_bits != val;
  2031. trident->spdif_pcm_bits = val;
  2032. if (trident->spdif != NULL) {
  2033. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  2034. outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  2035. } else {
  2036. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  2037. }
  2038. }
  2039. spin_unlock_irq(&trident->reg_lock);
  2040. return change;
  2041. }
  2042. static snd_kcontrol_new_t snd_trident_spdif_stream __devinitdata =
  2043. {
  2044. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2045. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2046. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  2047. .info = snd_trident_spdif_stream_info,
  2048. .get = snd_trident_spdif_stream_get,
  2049. .put = snd_trident_spdif_stream_put
  2050. };
  2051. /*---------------------------------------------------------------------------
  2052. snd_trident_ac97_control
  2053. Description: enable/disable rear path for ac97
  2054. ---------------------------------------------------------------------------*/
  2055. static int snd_trident_ac97_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  2056. {
  2057. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2058. uinfo->count = 1;
  2059. uinfo->value.integer.min = 0;
  2060. uinfo->value.integer.max = 1;
  2061. return 0;
  2062. }
  2063. static int snd_trident_ac97_control_get(snd_kcontrol_t * kcontrol,
  2064. snd_ctl_elem_value_t * ucontrol)
  2065. {
  2066. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2067. unsigned char val;
  2068. spin_lock_irq(&trident->reg_lock);
  2069. val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2070. ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
  2071. spin_unlock_irq(&trident->reg_lock);
  2072. return 0;
  2073. }
  2074. static int snd_trident_ac97_control_put(snd_kcontrol_t * kcontrol,
  2075. snd_ctl_elem_value_t * ucontrol)
  2076. {
  2077. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2078. unsigned char val;
  2079. int change = 0;
  2080. spin_lock_irq(&trident->reg_lock);
  2081. val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2082. val &= ~(1 << kcontrol->private_value);
  2083. if (ucontrol->value.integer.value[0])
  2084. val |= 1 << kcontrol->private_value;
  2085. change = val != trident->ac97_ctrl;
  2086. trident->ac97_ctrl = val;
  2087. outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2088. spin_unlock_irq(&trident->reg_lock);
  2089. return change;
  2090. }
  2091. static snd_kcontrol_new_t snd_trident_ac97_rear_control __devinitdata =
  2092. {
  2093. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2094. .name = "Rear Path",
  2095. .info = snd_trident_ac97_control_info,
  2096. .get = snd_trident_ac97_control_get,
  2097. .put = snd_trident_ac97_control_put,
  2098. .private_value = 4,
  2099. };
  2100. /*---------------------------------------------------------------------------
  2101. snd_trident_vol_control
  2102. Description: wave & music volume control
  2103. ---------------------------------------------------------------------------*/
  2104. static int snd_trident_vol_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  2105. {
  2106. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2107. uinfo->count = 2;
  2108. uinfo->value.integer.min = 0;
  2109. uinfo->value.integer.max = 255;
  2110. return 0;
  2111. }
  2112. static int snd_trident_vol_control_get(snd_kcontrol_t * kcontrol,
  2113. snd_ctl_elem_value_t * ucontrol)
  2114. {
  2115. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2116. unsigned int val;
  2117. val = trident->musicvol_wavevol;
  2118. ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
  2119. ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
  2120. return 0;
  2121. }
  2122. static int snd_trident_vol_control_put(snd_kcontrol_t * kcontrol,
  2123. snd_ctl_elem_value_t * ucontrol)
  2124. {
  2125. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2126. unsigned int val;
  2127. int change = 0;
  2128. spin_lock_irq(&trident->reg_lock);
  2129. val = trident->musicvol_wavevol;
  2130. val &= ~(0xffff << kcontrol->private_value);
  2131. val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
  2132. ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
  2133. change = val != trident->musicvol_wavevol;
  2134. outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  2135. spin_unlock_irq(&trident->reg_lock);
  2136. return change;
  2137. }
  2138. static snd_kcontrol_new_t snd_trident_vol_music_control __devinitdata =
  2139. {
  2140. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2141. .name = "Music Playback Volume",
  2142. .info = snd_trident_vol_control_info,
  2143. .get = snd_trident_vol_control_get,
  2144. .put = snd_trident_vol_control_put,
  2145. .private_value = 16,
  2146. };
  2147. static snd_kcontrol_new_t snd_trident_vol_wave_control __devinitdata =
  2148. {
  2149. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2150. .name = "Wave Playback Volume",
  2151. .info = snd_trident_vol_control_info,
  2152. .get = snd_trident_vol_control_get,
  2153. .put = snd_trident_vol_control_put,
  2154. .private_value = 0,
  2155. };
  2156. /*---------------------------------------------------------------------------
  2157. snd_trident_pcm_vol_control
  2158. Description: PCM front volume control
  2159. ---------------------------------------------------------------------------*/
  2160. static int snd_trident_pcm_vol_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  2161. {
  2162. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2163. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2164. uinfo->count = 1;
  2165. uinfo->value.integer.min = 0;
  2166. uinfo->value.integer.max = 255;
  2167. if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  2168. uinfo->value.integer.max = 1023;
  2169. return 0;
  2170. }
  2171. static int snd_trident_pcm_vol_control_get(snd_kcontrol_t * kcontrol,
  2172. snd_ctl_elem_value_t * ucontrol)
  2173. {
  2174. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2175. snd_trident_pcm_mixer_t *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2176. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2177. ucontrol->value.integer.value[0] = 1023 - mix->vol;
  2178. } else {
  2179. ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
  2180. }
  2181. return 0;
  2182. }
  2183. static int snd_trident_pcm_vol_control_put(snd_kcontrol_t * kcontrol,
  2184. snd_ctl_elem_value_t * ucontrol)
  2185. {
  2186. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2187. snd_trident_pcm_mixer_t *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2188. unsigned int val;
  2189. int change = 0;
  2190. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2191. val = 1023 - (ucontrol->value.integer.value[0] & 1023);
  2192. } else {
  2193. val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
  2194. }
  2195. spin_lock_irq(&trident->reg_lock);
  2196. change = val != mix->vol;
  2197. mix->vol = val;
  2198. if (mix->voice != NULL)
  2199. snd_trident_write_vol_reg(trident, mix->voice, val);
  2200. spin_unlock_irq(&trident->reg_lock);
  2201. return change;
  2202. }
  2203. static snd_kcontrol_new_t snd_trident_pcm_vol_control __devinitdata =
  2204. {
  2205. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2206. .name = "PCM Front Playback Volume",
  2207. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2208. .count = 32,
  2209. .info = snd_trident_pcm_vol_control_info,
  2210. .get = snd_trident_pcm_vol_control_get,
  2211. .put = snd_trident_pcm_vol_control_put,
  2212. };
  2213. /*---------------------------------------------------------------------------
  2214. snd_trident_pcm_pan_control
  2215. Description: PCM front pan control
  2216. ---------------------------------------------------------------------------*/
  2217. static int snd_trident_pcm_pan_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  2218. {
  2219. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2220. uinfo->count = 1;
  2221. uinfo->value.integer.min = 0;
  2222. uinfo->value.integer.max = 127;
  2223. return 0;
  2224. }
  2225. static int snd_trident_pcm_pan_control_get(snd_kcontrol_t * kcontrol,
  2226. snd_ctl_elem_value_t * ucontrol)
  2227. {
  2228. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2229. snd_trident_pcm_mixer_t *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2230. ucontrol->value.integer.value[0] = mix->pan;
  2231. if (ucontrol->value.integer.value[0] & 0x40) {
  2232. ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
  2233. } else {
  2234. ucontrol->value.integer.value[0] |= 0x40;
  2235. }
  2236. return 0;
  2237. }
  2238. static int snd_trident_pcm_pan_control_put(snd_kcontrol_t * kcontrol,
  2239. snd_ctl_elem_value_t * ucontrol)
  2240. {
  2241. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2242. snd_trident_pcm_mixer_t *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2243. unsigned char val;
  2244. int change = 0;
  2245. if (ucontrol->value.integer.value[0] & 0x40)
  2246. val = ucontrol->value.integer.value[0] & 0x3f;
  2247. else
  2248. val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
  2249. spin_lock_irq(&trident->reg_lock);
  2250. change = val != mix->pan;
  2251. mix->pan = val;
  2252. if (mix->voice != NULL)
  2253. snd_trident_write_pan_reg(trident, mix->voice, val);
  2254. spin_unlock_irq(&trident->reg_lock);
  2255. return change;
  2256. }
  2257. static snd_kcontrol_new_t snd_trident_pcm_pan_control __devinitdata =
  2258. {
  2259. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2260. .name = "PCM Pan Playback Control",
  2261. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2262. .count = 32,
  2263. .info = snd_trident_pcm_pan_control_info,
  2264. .get = snd_trident_pcm_pan_control_get,
  2265. .put = snd_trident_pcm_pan_control_put,
  2266. };
  2267. /*---------------------------------------------------------------------------
  2268. snd_trident_pcm_rvol_control
  2269. Description: PCM reverb volume control
  2270. ---------------------------------------------------------------------------*/
  2271. static int snd_trident_pcm_rvol_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  2272. {
  2273. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2274. uinfo->count = 1;
  2275. uinfo->value.integer.min = 0;
  2276. uinfo->value.integer.max = 127;
  2277. return 0;
  2278. }
  2279. static int snd_trident_pcm_rvol_control_get(snd_kcontrol_t * kcontrol,
  2280. snd_ctl_elem_value_t * ucontrol)
  2281. {
  2282. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2283. snd_trident_pcm_mixer_t *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2284. ucontrol->value.integer.value[0] = 127 - mix->rvol;
  2285. return 0;
  2286. }
  2287. static int snd_trident_pcm_rvol_control_put(snd_kcontrol_t * kcontrol,
  2288. snd_ctl_elem_value_t * ucontrol)
  2289. {
  2290. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2291. snd_trident_pcm_mixer_t *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2292. unsigned short val;
  2293. int change = 0;
  2294. val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
  2295. spin_lock_irq(&trident->reg_lock);
  2296. change = val != mix->rvol;
  2297. mix->rvol = val;
  2298. if (mix->voice != NULL)
  2299. snd_trident_write_rvol_reg(trident, mix->voice, val);
  2300. spin_unlock_irq(&trident->reg_lock);
  2301. return change;
  2302. }
  2303. static snd_kcontrol_new_t snd_trident_pcm_rvol_control __devinitdata =
  2304. {
  2305. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2306. .name = "PCM Reverb Playback Volume",
  2307. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2308. .count = 32,
  2309. .info = snd_trident_pcm_rvol_control_info,
  2310. .get = snd_trident_pcm_rvol_control_get,
  2311. .put = snd_trident_pcm_rvol_control_put,
  2312. };
  2313. /*---------------------------------------------------------------------------
  2314. snd_trident_pcm_cvol_control
  2315. Description: PCM chorus volume control
  2316. ---------------------------------------------------------------------------*/
  2317. static int snd_trident_pcm_cvol_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  2318. {
  2319. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2320. uinfo->count = 1;
  2321. uinfo->value.integer.min = 0;
  2322. uinfo->value.integer.max = 127;
  2323. return 0;
  2324. }
  2325. static int snd_trident_pcm_cvol_control_get(snd_kcontrol_t * kcontrol,
  2326. snd_ctl_elem_value_t * ucontrol)
  2327. {
  2328. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2329. snd_trident_pcm_mixer_t *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2330. ucontrol->value.integer.value[0] = 127 - mix->cvol;
  2331. return 0;
  2332. }
  2333. static int snd_trident_pcm_cvol_control_put(snd_kcontrol_t * kcontrol,
  2334. snd_ctl_elem_value_t * ucontrol)
  2335. {
  2336. trident_t *trident = snd_kcontrol_chip(kcontrol);
  2337. snd_trident_pcm_mixer_t *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2338. unsigned short val;
  2339. int change = 0;
  2340. val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
  2341. spin_lock_irq(&trident->reg_lock);
  2342. change = val != mix->cvol;
  2343. mix->cvol = val;
  2344. if (mix->voice != NULL)
  2345. snd_trident_write_cvol_reg(trident, mix->voice, val);
  2346. spin_unlock_irq(&trident->reg_lock);
  2347. return change;
  2348. }
  2349. static snd_kcontrol_new_t snd_trident_pcm_cvol_control __devinitdata =
  2350. {
  2351. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2352. .name = "PCM Chorus Playback Volume",
  2353. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2354. .count = 32,
  2355. .info = snd_trident_pcm_cvol_control_info,
  2356. .get = snd_trident_pcm_cvol_control_get,
  2357. .put = snd_trident_pcm_cvol_control_put,
  2358. };
  2359. static void snd_trident_notify_pcm_change1(snd_card_t * card, snd_kcontrol_t *kctl, int num, int activate)
  2360. {
  2361. snd_ctl_elem_id_t id;
  2362. snd_runtime_check(kctl != NULL, return);
  2363. if (activate)
  2364. kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  2365. else
  2366. kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  2367. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
  2368. SNDRV_CTL_EVENT_MASK_INFO,
  2369. snd_ctl_build_ioff(&id, kctl, num));
  2370. }
  2371. static void snd_trident_notify_pcm_change(trident_t *trident, snd_trident_pcm_mixer_t *tmix, int num, int activate)
  2372. {
  2373. snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
  2374. snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
  2375. snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
  2376. snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
  2377. }
  2378. static int snd_trident_pcm_mixer_build(trident_t *trident, snd_trident_voice_t *voice, snd_pcm_substream_t *substream)
  2379. {
  2380. snd_trident_pcm_mixer_t *tmix;
  2381. snd_assert(trident != NULL && voice != NULL && substream != NULL, return -EINVAL);
  2382. tmix = &trident->pcm_mixer[substream->number];
  2383. tmix->voice = voice;
  2384. tmix->vol = T4D_DEFAULT_PCM_VOL;
  2385. tmix->pan = T4D_DEFAULT_PCM_PAN;
  2386. tmix->rvol = T4D_DEFAULT_PCM_RVOL;
  2387. tmix->cvol = T4D_DEFAULT_PCM_CVOL;
  2388. snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
  2389. return 0;
  2390. }
  2391. static int snd_trident_pcm_mixer_free(trident_t *trident, snd_trident_voice_t *voice, snd_pcm_substream_t *substream)
  2392. {
  2393. snd_trident_pcm_mixer_t *tmix;
  2394. snd_assert(trident != NULL && substream != NULL, return -EINVAL);
  2395. tmix = &trident->pcm_mixer[substream->number];
  2396. tmix->voice = NULL;
  2397. snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
  2398. return 0;
  2399. }
  2400. /*---------------------------------------------------------------------------
  2401. snd_trident_mixer
  2402. Description: This routine registers the 4DWave device for mixer support.
  2403. Paramters: trident - pointer to target device class for 4DWave.
  2404. Returns: None
  2405. ---------------------------------------------------------------------------*/
  2406. static int __devinit snd_trident_mixer(trident_t * trident, int pcm_spdif_device)
  2407. {
  2408. ac97_template_t _ac97;
  2409. snd_card_t * card = trident->card;
  2410. snd_kcontrol_t *kctl;
  2411. snd_ctl_elem_value_t *uctl;
  2412. int idx, err, retries = 2;
  2413. static ac97_bus_ops_t ops = {
  2414. .write = snd_trident_codec_write,
  2415. .read = snd_trident_codec_read,
  2416. };
  2417. uctl = kcalloc(1, sizeof(*uctl), GFP_KERNEL);
  2418. if (!uctl)
  2419. return -ENOMEM;
  2420. if ((err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus)) < 0)
  2421. goto __out;
  2422. memset(&_ac97, 0, sizeof(_ac97));
  2423. _ac97.private_data = trident;
  2424. trident->ac97_detect = 1;
  2425. __again:
  2426. if ((err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97)) < 0) {
  2427. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2428. if ((err = snd_trident_sis_reset(trident)) < 0)
  2429. goto __out;
  2430. if (retries-- > 0)
  2431. goto __again;
  2432. err = -EIO;
  2433. }
  2434. goto __out;
  2435. }
  2436. /* secondary codec? */
  2437. if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
  2438. (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
  2439. _ac97.num = 1;
  2440. err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
  2441. if (err < 0)
  2442. snd_printk("SI7018: the secondary codec - invalid access\n");
  2443. #if 0 // only for my testing purpose --jk
  2444. {
  2445. ac97_t *mc97;
  2446. err = snd_ac97_modem(trident->card, &_ac97, &mc97);
  2447. if (err < 0)
  2448. snd_printk("snd_ac97_modem returned error %i\n", err);
  2449. }
  2450. #endif
  2451. }
  2452. trident->ac97_detect = 0;
  2453. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  2454. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident))) < 0)
  2455. goto __out;
  2456. kctl->put(kctl, uctl);
  2457. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident))) < 0)
  2458. goto __out;
  2459. kctl->put(kctl, uctl);
  2460. outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  2461. } else {
  2462. outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  2463. }
  2464. for (idx = 0; idx < 32; idx++) {
  2465. snd_trident_pcm_mixer_t *tmix;
  2466. tmix = &trident->pcm_mixer[idx];
  2467. tmix->voice = NULL;
  2468. }
  2469. if ((trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident)) == NULL)
  2470. goto __nomem;
  2471. if ((err = snd_ctl_add(card, trident->ctl_vol)))
  2472. goto __out;
  2473. if ((trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident)) == NULL)
  2474. goto __nomem;
  2475. if ((err = snd_ctl_add(card, trident->ctl_pan)))
  2476. goto __out;
  2477. if ((trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident)) == NULL)
  2478. goto __nomem;
  2479. if ((err = snd_ctl_add(card, trident->ctl_rvol)))
  2480. goto __out;
  2481. if ((trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident)) == NULL)
  2482. goto __nomem;
  2483. if ((err = snd_ctl_add(card, trident->ctl_cvol)))
  2484. goto __out;
  2485. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  2486. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident))) < 0)
  2487. goto __out;
  2488. kctl->put(kctl, uctl);
  2489. }
  2490. if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2491. kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
  2492. if (kctl == NULL) {
  2493. err = -ENOMEM;
  2494. goto __out;
  2495. }
  2496. if (trident->ac97->ext_id & AC97_EI_SPDIF)
  2497. kctl->id.index++;
  2498. if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
  2499. kctl->id.index++;
  2500. idx = kctl->id.index;
  2501. if ((err = snd_ctl_add(card, kctl)) < 0)
  2502. goto __out;
  2503. kctl->put(kctl, uctl);
  2504. kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
  2505. if (kctl == NULL) {
  2506. err = -ENOMEM;
  2507. goto __out;
  2508. }
  2509. kctl->id.index = idx;
  2510. kctl->id.device = pcm_spdif_device;
  2511. if ((err = snd_ctl_add(card, kctl)) < 0)
  2512. goto __out;
  2513. kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
  2514. if (kctl == NULL) {
  2515. err = -ENOMEM;
  2516. goto __out;
  2517. }
  2518. kctl->id.index = idx;
  2519. kctl->id.device = pcm_spdif_device;
  2520. if ((err = snd_ctl_add(card, kctl)) < 0)
  2521. goto __out;
  2522. kctl = snd_ctl_new1(&snd_trident_spdif_stream, trident);
  2523. if (kctl == NULL) {
  2524. err = -ENOMEM;
  2525. goto __out;
  2526. }
  2527. kctl->id.index = idx;
  2528. kctl->id.device = pcm_spdif_device;
  2529. if ((err = snd_ctl_add(card, kctl)) < 0)
  2530. goto __out;
  2531. trident->spdif_pcm_ctl = kctl;
  2532. }
  2533. err = 0;
  2534. goto __out;
  2535. __nomem:
  2536. err = -ENOMEM;
  2537. __out:
  2538. kfree(uctl);
  2539. return err;
  2540. }
  2541. /*
  2542. * gameport interface
  2543. */
  2544. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  2545. static unsigned char snd_trident_gameport_read(struct gameport *gameport)
  2546. {
  2547. trident_t *chip = gameport_get_port_data(gameport);
  2548. snd_assert(chip, return 0);
  2549. return inb(TRID_REG(chip, GAMEPORT_LEGACY));
  2550. }
  2551. static void snd_trident_gameport_trigger(struct gameport *gameport)
  2552. {
  2553. trident_t *chip = gameport_get_port_data(gameport);
  2554. snd_assert(chip, return);
  2555. outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
  2556. }
  2557. static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  2558. {
  2559. trident_t *chip = gameport_get_port_data(gameport);
  2560. int i;
  2561. snd_assert(chip, return 0);
  2562. *buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
  2563. for (i = 0; i < 4; i++) {
  2564. axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
  2565. if (axes[i] == 0xffff) axes[i] = -1;
  2566. }
  2567. return 0;
  2568. }
  2569. static int snd_trident_gameport_open(struct gameport *gameport, int mode)
  2570. {
  2571. trident_t *chip = gameport_get_port_data(gameport);
  2572. snd_assert(chip, return 0);
  2573. switch (mode) {
  2574. case GAMEPORT_MODE_COOKED:
  2575. outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
  2576. set_current_state(TASK_UNINTERRUPTIBLE);
  2577. schedule_timeout(1 + 20 * HZ / 1000); /* 20msec */
  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. inline static 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);