trident_main.c 123 KB

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