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