trident_main.c 123 KB

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