trident_main.c 122 KB

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