cmipci.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421
  1. /*
  2. * Driver for C-Media CMI8338 and 8738 PCI soundcards.
  3. * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /* Does not work. Warning may block system in capture mode */
  20. /* #define USE_VAR48KRATE */
  21. #include <asm/io.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/pci.h>
  26. #include <linux/slab.h>
  27. #include <linux/gameport.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/mutex.h>
  30. #include <sound/core.h>
  31. #include <sound/info.h>
  32. #include <sound/control.h>
  33. #include <sound/pcm.h>
  34. #include <sound/rawmidi.h>
  35. #include <sound/mpu401.h>
  36. #include <sound/opl3.h>
  37. #include <sound/sb.h>
  38. #include <sound/asoundef.h>
  39. #include <sound/initval.h>
  40. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  41. MODULE_DESCRIPTION("C-Media CMI8x38 PCI");
  42. MODULE_LICENSE("GPL");
  43. MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8738},"
  44. "{C-Media,CMI8738B},"
  45. "{C-Media,CMI8338A},"
  46. "{C-Media,CMI8338B}}");
  47. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  48. #define SUPPORT_JOYSTICK 1
  49. #endif
  50. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  51. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  52. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable switches */
  53. static long mpu_port[SNDRV_CARDS];
  54. static long fm_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
  55. static int soft_ac3[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
  56. #ifdef SUPPORT_JOYSTICK
  57. static int joystick_port[SNDRV_CARDS];
  58. #endif
  59. module_param_array(index, int, NULL, 0444);
  60. MODULE_PARM_DESC(index, "Index value for C-Media PCI soundcard.");
  61. module_param_array(id, charp, NULL, 0444);
  62. MODULE_PARM_DESC(id, "ID string for C-Media PCI soundcard.");
  63. module_param_array(enable, bool, NULL, 0444);
  64. MODULE_PARM_DESC(enable, "Enable C-Media PCI soundcard.");
  65. module_param_array(mpu_port, long, NULL, 0444);
  66. MODULE_PARM_DESC(mpu_port, "MPU-401 port.");
  67. module_param_array(fm_port, long, NULL, 0444);
  68. MODULE_PARM_DESC(fm_port, "FM port.");
  69. module_param_array(soft_ac3, bool, NULL, 0444);
  70. MODULE_PARM_DESC(soft_ac3, "Sofware-conversion of raw SPDIF packets (model 033 only).");
  71. #ifdef SUPPORT_JOYSTICK
  72. module_param_array(joystick_port, int, NULL, 0444);
  73. MODULE_PARM_DESC(joystick_port, "Joystick port address.");
  74. #endif
  75. /*
  76. * CM8x38 registers definition
  77. */
  78. #define CM_REG_FUNCTRL0 0x00
  79. #define CM_RST_CH1 0x00080000
  80. #define CM_RST_CH0 0x00040000
  81. #define CM_CHEN1 0x00020000 /* ch1: enable */
  82. #define CM_CHEN0 0x00010000 /* ch0: enable */
  83. #define CM_PAUSE1 0x00000008 /* ch1: pause */
  84. #define CM_PAUSE0 0x00000004 /* ch0: pause */
  85. #define CM_CHADC1 0x00000002 /* ch1, 0:playback, 1:record */
  86. #define CM_CHADC0 0x00000001 /* ch0, 0:playback, 1:record */
  87. #define CM_REG_FUNCTRL1 0x04
  88. #define CM_DSFC_MASK 0x0000E000 /* channel 1 (DAC?) sampling frequency */
  89. #define CM_DSFC_SHIFT 13
  90. #define CM_ASFC_MASK 0x00001C00 /* channel 0 (ADC?) sampling frequency */
  91. #define CM_ASFC_SHIFT 10
  92. #define CM_SPDF_1 0x00000200 /* SPDIF IN/OUT at channel B */
  93. #define CM_SPDF_0 0x00000100 /* SPDIF OUT only channel A */
  94. #define CM_SPDFLOOP 0x00000080 /* ext. SPDIIF/IN -> OUT loopback */
  95. #define CM_SPDO2DAC 0x00000040 /* SPDIF/OUT can be heard from internal DAC */
  96. #define CM_INTRM 0x00000020 /* master control block (MCB) interrupt enabled */
  97. #define CM_BREQ 0x00000010 /* bus master enabled */
  98. #define CM_VOICE_EN 0x00000008 /* legacy voice (SB16,FM) */
  99. #define CM_UART_EN 0x00000004 /* legacy UART */
  100. #define CM_JYSTK_EN 0x00000002 /* legacy joystick */
  101. #define CM_ZVPORT 0x00000001 /* ZVPORT */
  102. #define CM_REG_CHFORMAT 0x08
  103. #define CM_CHB3D5C 0x80000000 /* 5,6 channels */
  104. #define CM_FMOFFSET2 0x40000000 /* initial FM PCM offset 2 when Fmute=1 */
  105. #define CM_CHB3D 0x20000000 /* 4 channels */
  106. #define CM_CHIP_MASK1 0x1f000000
  107. #define CM_CHIP_037 0x01000000
  108. #define CM_SETLAT48 0x00800000 /* set latency timer 48h */
  109. #define CM_EDGEIRQ 0x00400000 /* emulated edge trigger legacy IRQ */
  110. #define CM_SPD24SEL39 0x00200000 /* 24-bit spdif: model 039 */
  111. #define CM_AC3EN1 0x00100000 /* enable AC3: model 037 */
  112. #define CM_SPDIF_SELECT1 0x00080000 /* for model <= 037 ? */
  113. #define CM_SPD24SEL 0x00020000 /* 24bit spdif: model 037 */
  114. /* #define CM_SPDIF_INVERSE 0x00010000 */ /* ??? */
  115. #define CM_ADCBITLEN_MASK 0x0000C000
  116. #define CM_ADCBITLEN_16 0x00000000
  117. #define CM_ADCBITLEN_15 0x00004000
  118. #define CM_ADCBITLEN_14 0x00008000
  119. #define CM_ADCBITLEN_13 0x0000C000
  120. #define CM_ADCDACLEN_MASK 0x00003000 /* model 037 */
  121. #define CM_ADCDACLEN_060 0x00000000
  122. #define CM_ADCDACLEN_066 0x00001000
  123. #define CM_ADCDACLEN_130 0x00002000
  124. #define CM_ADCDACLEN_280 0x00003000
  125. #define CM_ADCDLEN_MASK 0x00003000 /* model 039 */
  126. #define CM_ADCDLEN_ORIGINAL 0x00000000
  127. #define CM_ADCDLEN_EXTRA 0x00001000
  128. #define CM_ADCDLEN_24K 0x00002000
  129. #define CM_ADCDLEN_WEIGHT 0x00003000
  130. #define CM_CH1_SRATE_176K 0x00000800
  131. #define CM_CH1_SRATE_96K 0x00000800 /* model 055? */
  132. #define CM_CH1_SRATE_88K 0x00000400
  133. #define CM_CH0_SRATE_176K 0x00000200
  134. #define CM_CH0_SRATE_96K 0x00000200 /* model 055? */
  135. #define CM_CH0_SRATE_88K 0x00000100
  136. #define CM_CH0_SRATE_128K 0x00000300
  137. #define CM_CH0_SRATE_MASK 0x00000300
  138. #define CM_SPDIF_INVERSE2 0x00000080 /* model 055? */
  139. #define CM_DBLSPDS 0x00000040 /* double SPDIF sample rate 88.2/96 */
  140. #define CM_POLVALID 0x00000020 /* inverse SPDIF/IN valid bit */
  141. #define CM_SPDLOCKED 0x00000010
  142. #define CM_CH1FMT_MASK 0x0000000C /* bit 3: 16 bits, bit 2: stereo */
  143. #define CM_CH1FMT_SHIFT 2
  144. #define CM_CH0FMT_MASK 0x00000003 /* bit 1: 16 bits, bit 0: stereo */
  145. #define CM_CH0FMT_SHIFT 0
  146. #define CM_REG_INT_HLDCLR 0x0C
  147. #define CM_CHIP_MASK2 0xff000000
  148. #define CM_CHIP_8768 0x20000000
  149. #define CM_CHIP_055 0x08000000
  150. #define CM_CHIP_039 0x04000000
  151. #define CM_CHIP_039_6CH 0x01000000
  152. #define CM_UNKNOWN_INT_EN 0x00080000 /* ? */
  153. #define CM_TDMA_INT_EN 0x00040000
  154. #define CM_CH1_INT_EN 0x00020000
  155. #define CM_CH0_INT_EN 0x00010000
  156. #define CM_REG_INT_STATUS 0x10
  157. #define CM_INTR 0x80000000
  158. #define CM_VCO 0x08000000 /* Voice Control? CMI8738 */
  159. #define CM_MCBINT 0x04000000 /* Master Control Block abort cond.? */
  160. #define CM_UARTINT 0x00010000
  161. #define CM_LTDMAINT 0x00008000
  162. #define CM_HTDMAINT 0x00004000
  163. #define CM_XDO46 0x00000080 /* Modell 033? Direct programming EEPROM (read data register) */
  164. #define CM_LHBTOG 0x00000040 /* High/Low status from DMA ctrl register */
  165. #define CM_LEG_HDMA 0x00000020 /* Legacy is in High DMA channel */
  166. #define CM_LEG_STEREO 0x00000010 /* Legacy is in Stereo mode */
  167. #define CM_CH1BUSY 0x00000008
  168. #define CM_CH0BUSY 0x00000004
  169. #define CM_CHINT1 0x00000002
  170. #define CM_CHINT0 0x00000001
  171. #define CM_REG_LEGACY_CTRL 0x14
  172. #define CM_NXCHG 0x80000000 /* don't map base reg dword->sample */
  173. #define CM_VMPU_MASK 0x60000000 /* MPU401 i/o port address */
  174. #define CM_VMPU_330 0x00000000
  175. #define CM_VMPU_320 0x20000000
  176. #define CM_VMPU_310 0x40000000
  177. #define CM_VMPU_300 0x60000000
  178. #define CM_ENWR8237 0x10000000 /* enable bus master to write 8237 base reg */
  179. #define CM_VSBSEL_MASK 0x0C000000 /* SB16 base address */
  180. #define CM_VSBSEL_220 0x00000000
  181. #define CM_VSBSEL_240 0x04000000
  182. #define CM_VSBSEL_260 0x08000000
  183. #define CM_VSBSEL_280 0x0C000000
  184. #define CM_FMSEL_MASK 0x03000000 /* FM OPL3 base address */
  185. #define CM_FMSEL_388 0x00000000
  186. #define CM_FMSEL_3C8 0x01000000
  187. #define CM_FMSEL_3E0 0x02000000
  188. #define CM_FMSEL_3E8 0x03000000
  189. #define CM_ENSPDOUT 0x00800000 /* enable XSPDIF/OUT to I/O interface */
  190. #define CM_SPDCOPYRHT 0x00400000 /* spdif in/out copyright bit */
  191. #define CM_DAC2SPDO 0x00200000 /* enable wave+fm_midi -> SPDIF/OUT */
  192. #define CM_INVIDWEN 0x00100000 /* internal vendor ID write enable, model 039? */
  193. #define CM_SETRETRY 0x00100000 /* 0: legacy i/o wait (default), 1: legacy i/o bus retry */
  194. #define CM_C_EEACCESS 0x00080000 /* direct programming eeprom regs */
  195. #define CM_C_EECS 0x00040000
  196. #define CM_C_EEDI46 0x00020000
  197. #define CM_C_EECK46 0x00010000
  198. #define CM_CHB3D6C 0x00008000 /* 5.1 channels support */
  199. #define CM_CENTR2LIN 0x00004000 /* line-in as center out */
  200. #define CM_BASE2LIN 0x00002000 /* line-in as bass out */
  201. #define CM_EXBASEN 0x00001000 /* external bass input enable */
  202. #define CM_REG_MISC_CTRL 0x18
  203. #define CM_PWD 0x80000000 /* power down */
  204. #define CM_RESET 0x40000000
  205. #define CM_SFIL_MASK 0x30000000 /* filter control at front end DAC, model 037? */
  206. #define CM_VMGAIN 0x10000000 /* analog master amp +6dB, model 039? */
  207. #define CM_TXVX 0x08000000 /* model 037? */
  208. #define CM_N4SPK3D 0x04000000 /* copy front to rear */
  209. #define CM_SPDO5V 0x02000000 /* 5V spdif output (1 = 0.5v (coax)) */
  210. #define CM_SPDIF48K 0x01000000 /* write */
  211. #define CM_SPATUS48K 0x01000000 /* read */
  212. #define CM_ENDBDAC 0x00800000 /* enable double dac */
  213. #define CM_XCHGDAC 0x00400000 /* 0: front=ch0, 1: front=ch1 */
  214. #define CM_SPD32SEL 0x00200000 /* 0: 16bit SPDIF, 1: 32bit */
  215. #define CM_SPDFLOOPI 0x00100000 /* int. SPDIF-OUT -> int. IN */
  216. #define CM_FM_EN 0x00080000 /* enable legacy FM */
  217. #define CM_AC3EN2 0x00040000 /* enable AC3: model 039 */
  218. #define CM_ENWRASID 0x00010000 /* choose writable internal SUBID (audio) */
  219. #define CM_VIDWPDSB 0x00010000 /* model 037? */
  220. #define CM_SPDF_AC97 0x00008000 /* 0: SPDIF/OUT 44.1K, 1: 48K */
  221. #define CM_MASK_EN 0x00004000 /* activate channel mask on legacy DMA */
  222. #define CM_ENWRMSID 0x00002000 /* choose writable internal SUBID (modem) */
  223. #define CM_VIDWPPRT 0x00002000 /* model 037? */
  224. #define CM_SFILENB 0x00001000 /* filter stepping at front end DAC, model 037? */
  225. #define CM_MMODE_MASK 0x00000E00 /* model DAA interface mode */
  226. #define CM_SPDIF_SELECT2 0x00000100 /* for model > 039 ? */
  227. #define CM_ENCENTER 0x00000080
  228. #define CM_FLINKON 0x00000040 /* force modem link detection on, model 037 */
  229. #define CM_MUTECH1 0x00000040 /* mute PCI ch1 to DAC */
  230. #define CM_FLINKOFF 0x00000020 /* force modem link detection off, model 037 */
  231. #define CM_MIDSMP 0x00000010 /* 1/2 interpolation at front end DAC */
  232. #define CM_UPDDMA_MASK 0x0000000C /* TDMA position update notification */
  233. #define CM_UPDDMA_2048 0x00000000
  234. #define CM_UPDDMA_1024 0x00000004
  235. #define CM_UPDDMA_512 0x00000008
  236. #define CM_UPDDMA_256 0x0000000C
  237. #define CM_TWAIT_MASK 0x00000003 /* model 037 */
  238. #define CM_TWAIT1 0x00000002 /* FM i/o cycle, 0: 48, 1: 64 PCICLKs */
  239. #define CM_TWAIT0 0x00000001 /* i/o cycle, 0: 4, 1: 6 PCICLKs */
  240. #define CM_REG_TDMA_POSITION 0x1C
  241. #define CM_TDMA_CNT_MASK 0xFFFF0000 /* current byte/word count */
  242. #define CM_TDMA_ADR_MASK 0x0000FFFF /* current address */
  243. /* byte */
  244. #define CM_REG_MIXER0 0x20
  245. #define CM_REG_SBVR 0x20 /* write: sb16 version */
  246. #define CM_REG_DEV 0x20 /* read: hardware device version */
  247. #define CM_REG_MIXER21 0x21
  248. #define CM_UNKNOWN_21_MASK 0x78 /* ? */
  249. #define CM_X_ADPCM 0x04 /* SB16 ADPCM enable */
  250. #define CM_PROINV 0x02 /* SBPro left/right channel switching */
  251. #define CM_X_SB16 0x01 /* SB16 compatible */
  252. #define CM_REG_SB16_DATA 0x22
  253. #define CM_REG_SB16_ADDR 0x23
  254. #define CM_REFFREQ_XIN (315*1000*1000)/22 /* 14.31818 Mhz reference clock frequency pin XIN */
  255. #define CM_ADCMULT_XIN 512 /* Guessed (487 best for 44.1kHz, not for 88/176kHz) */
  256. #define CM_TOLERANCE_RATE 0.001 /* Tolerance sample rate pitch (1000ppm) */
  257. #define CM_MAXIMUM_RATE 80000000 /* Note more than 80MHz */
  258. #define CM_REG_MIXER1 0x24
  259. #define CM_FMMUTE 0x80 /* mute FM */
  260. #define CM_FMMUTE_SHIFT 7
  261. #define CM_WSMUTE 0x40 /* mute PCM */
  262. #define CM_WSMUTE_SHIFT 6
  263. #define CM_REAR2LIN 0x20 /* lin-in -> rear line out */
  264. #define CM_REAR2LIN_SHIFT 5
  265. #define CM_REAR2FRONT 0x10 /* exchange rear/front */
  266. #define CM_REAR2FRONT_SHIFT 4
  267. #define CM_WAVEINL 0x08 /* digital wave rec. left chan */
  268. #define CM_WAVEINL_SHIFT 3
  269. #define CM_WAVEINR 0x04 /* digical wave rec. right */
  270. #define CM_WAVEINR_SHIFT 2
  271. #define CM_X3DEN 0x02 /* 3D surround enable */
  272. #define CM_X3DEN_SHIFT 1
  273. #define CM_CDPLAY 0x01 /* enable SPDIF/IN PCM -> DAC */
  274. #define CM_CDPLAY_SHIFT 0
  275. #define CM_REG_MIXER2 0x25
  276. #define CM_RAUXREN 0x80 /* AUX right capture */
  277. #define CM_RAUXREN_SHIFT 7
  278. #define CM_RAUXLEN 0x40 /* AUX left capture */
  279. #define CM_RAUXLEN_SHIFT 6
  280. #define CM_VAUXRM 0x20 /* AUX right mute */
  281. #define CM_VAUXRM_SHIFT 5
  282. #define CM_VAUXLM 0x10 /* AUX left mute */
  283. #define CM_VAUXLM_SHIFT 4
  284. #define CM_VADMIC_MASK 0x0e /* mic gain level (0-3) << 1 */
  285. #define CM_VADMIC_SHIFT 1
  286. #define CM_MICGAINZ 0x01 /* mic boost */
  287. #define CM_MICGAINZ_SHIFT 0
  288. #define CM_REG_MIXER3 0x24
  289. #define CM_REG_AUX_VOL 0x26
  290. #define CM_VAUXL_MASK 0xf0
  291. #define CM_VAUXR_MASK 0x0f
  292. #define CM_REG_MISC 0x27
  293. #define CM_UNKNOWN_27_MASK 0xd8 /* ? */
  294. #define CM_XGPO1 0x20
  295. // #define CM_XGPBIO 0x04
  296. #define CM_MIC_CENTER_LFE 0x04 /* mic as center/lfe out? (model 039 or later?) */
  297. #define CM_SPDIF_INVERSE 0x04 /* spdif input phase inverse (model 037) */
  298. #define CM_SPDVALID 0x02 /* spdif input valid check */
  299. #define CM_DMAUTO 0x01 /* SB16 DMA auto detect */
  300. #define CM_REG_AC97 0x28 /* hmmm.. do we have ac97 link? */
  301. /*
  302. * For CMI-8338 (0x28 - 0x2b) .. is this valid for CMI-8738
  303. * or identical with AC97 codec?
  304. */
  305. #define CM_REG_EXTERN_CODEC CM_REG_AC97
  306. /*
  307. * MPU401 pci port index address 0x40 - 0x4f (CMI-8738 spec ver. 0.6)
  308. */
  309. #define CM_REG_MPU_PCI 0x40
  310. /*
  311. * FM pci port index address 0x50 - 0x5f (CMI-8738 spec ver. 0.6)
  312. */
  313. #define CM_REG_FM_PCI 0x50
  314. /*
  315. * access from SB-mixer port
  316. */
  317. #define CM_REG_EXTENT_IND 0xf0
  318. #define CM_VPHONE_MASK 0xe0 /* Phone volume control (0-3) << 5 */
  319. #define CM_VPHONE_SHIFT 5
  320. #define CM_VPHOM 0x10 /* Phone mute control */
  321. #define CM_VSPKM 0x08 /* Speaker mute control, default high */
  322. #define CM_RLOOPREN 0x04 /* Rec. R-channel enable */
  323. #define CM_RLOOPLEN 0x02 /* Rec. L-channel enable */
  324. #define CM_VADMIC3 0x01 /* Mic record boost */
  325. /*
  326. * CMI-8338 spec ver 0.5 (this is not valid for CMI-8738):
  327. * the 8 registers 0xf8 - 0xff are used for programming m/n counter by the PLL
  328. * unit (readonly?).
  329. */
  330. #define CM_REG_PLL 0xf8
  331. /*
  332. * extended registers
  333. */
  334. #define CM_REG_CH0_FRAME1 0x80 /* write: base address */
  335. #define CM_REG_CH0_FRAME2 0x84 /* read: current address */
  336. #define CM_REG_CH1_FRAME1 0x88 /* 0-15: count of samples at bus master; buffer size */
  337. #define CM_REG_CH1_FRAME2 0x8C /* 16-31: count of samples at codec; fragment size */
  338. #define CM_REG_EXT_MISC 0x90
  339. #define CM_ADC48K44K 0x10000000 /* ADC parameters group, 0: 44k, 1: 48k */
  340. #define CM_CHB3D8C 0x00200000 /* 7.1 channels support */
  341. #define CM_SPD32FMT 0x00100000 /* SPDIF/IN 32k sample rate */
  342. #define CM_ADC2SPDIF 0x00080000 /* ADC output to SPDIF/OUT */
  343. #define CM_SHAREADC 0x00040000 /* DAC in ADC as Center/LFE */
  344. #define CM_REALTCMP 0x00020000 /* monitor the CMPL/CMPR of ADC */
  345. #define CM_INVLRCK 0x00010000 /* invert ZVPORT's LRCK */
  346. #define CM_UNKNOWN_90_MASK 0x0000FFFF /* ? */
  347. /*
  348. * size of i/o region
  349. */
  350. #define CM_EXTENT_CODEC 0x100
  351. #define CM_EXTENT_MIDI 0x2
  352. #define CM_EXTENT_SYNTH 0x4
  353. /*
  354. * channels for playback / capture
  355. */
  356. #define CM_CH_PLAY 0
  357. #define CM_CH_CAPT 1
  358. /*
  359. * flags to check device open/close
  360. */
  361. #define CM_OPEN_NONE 0
  362. #define CM_OPEN_CH_MASK 0x01
  363. #define CM_OPEN_DAC 0x10
  364. #define CM_OPEN_ADC 0x20
  365. #define CM_OPEN_SPDIF 0x40
  366. #define CM_OPEN_MCHAN 0x80
  367. #define CM_OPEN_PLAYBACK (CM_CH_PLAY | CM_OPEN_DAC)
  368. #define CM_OPEN_PLAYBACK2 (CM_CH_CAPT | CM_OPEN_DAC)
  369. #define CM_OPEN_PLAYBACK_MULTI (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_MCHAN)
  370. #define CM_OPEN_CAPTURE (CM_CH_CAPT | CM_OPEN_ADC)
  371. #define CM_OPEN_SPDIF_PLAYBACK (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_SPDIF)
  372. #define CM_OPEN_SPDIF_CAPTURE (CM_CH_CAPT | CM_OPEN_ADC | CM_OPEN_SPDIF)
  373. #if CM_CH_PLAY == 1
  374. #define CM_PLAYBACK_SRATE_176K CM_CH1_SRATE_176K
  375. #define CM_PLAYBACK_SPDF CM_SPDF_1
  376. #define CM_CAPTURE_SPDF CM_SPDF_0
  377. #else
  378. #define CM_PLAYBACK_SRATE_176K CM_CH0_SRATE_176K
  379. #define CM_PLAYBACK_SPDF CM_SPDF_0
  380. #define CM_CAPTURE_SPDF CM_SPDF_1
  381. #endif
  382. /*
  383. * driver data
  384. */
  385. struct cmipci_pcm {
  386. struct snd_pcm_substream *substream;
  387. u8 running; /* dac/adc running? */
  388. u8 fmt; /* format bits */
  389. u8 is_dac;
  390. u8 needs_silencing;
  391. unsigned int dma_size; /* in frames */
  392. unsigned int shift;
  393. unsigned int ch; /* channel (0/1) */
  394. unsigned int offset; /* physical address of the buffer */
  395. };
  396. /* mixer elements toggled/resumed during ac3 playback */
  397. struct cmipci_mixer_auto_switches {
  398. const char *name; /* switch to toggle */
  399. int toggle_on; /* value to change when ac3 mode */
  400. };
  401. static const struct cmipci_mixer_auto_switches cm_saved_mixer[] = {
  402. {"PCM Playback Switch", 0},
  403. {"IEC958 Output Switch", 1},
  404. {"IEC958 Mix Analog", 0},
  405. // {"IEC958 Out To DAC", 1}, // no longer used
  406. {"IEC958 Loop", 0},
  407. };
  408. #define CM_SAVED_MIXERS ARRAY_SIZE(cm_saved_mixer)
  409. struct cmipci {
  410. struct snd_card *card;
  411. struct pci_dev *pci;
  412. unsigned int device; /* device ID */
  413. int irq;
  414. unsigned long iobase;
  415. unsigned int ctrl; /* FUNCTRL0 current value */
  416. struct snd_pcm *pcm; /* DAC/ADC PCM */
  417. struct snd_pcm *pcm2; /* 2nd DAC */
  418. struct snd_pcm *pcm_spdif; /* SPDIF */
  419. int chip_version;
  420. int max_channels;
  421. unsigned int can_ac3_sw: 1;
  422. unsigned int can_ac3_hw: 1;
  423. unsigned int can_multi_ch: 1;
  424. unsigned int can_96k: 1; /* samplerate above 48k */
  425. unsigned int do_soft_ac3: 1;
  426. unsigned int spdif_playback_avail: 1; /* spdif ready? */
  427. unsigned int spdif_playback_enabled: 1; /* spdif switch enabled? */
  428. int spdif_counter; /* for software AC3 */
  429. unsigned int dig_status;
  430. unsigned int dig_pcm_status;
  431. struct snd_pcm_hardware *hw_info[3]; /* for playbacks */
  432. int opened[2]; /* open mode */
  433. struct mutex open_mutex;
  434. unsigned int mixer_insensitive: 1;
  435. struct snd_kcontrol *mixer_res_ctl[CM_SAVED_MIXERS];
  436. int mixer_res_status[CM_SAVED_MIXERS];
  437. struct cmipci_pcm channel[2]; /* ch0 - DAC, ch1 - ADC or 2nd DAC */
  438. /* external MIDI */
  439. struct snd_rawmidi *rmidi;
  440. #ifdef SUPPORT_JOYSTICK
  441. struct gameport *gameport;
  442. #endif
  443. spinlock_t reg_lock;
  444. #ifdef CONFIG_PM
  445. unsigned int saved_regs[0x20];
  446. unsigned char saved_mixers[0x20];
  447. #endif
  448. };
  449. /* read/write operations for dword register */
  450. static inline void snd_cmipci_write(struct cmipci *cm, unsigned int cmd, unsigned int data)
  451. {
  452. outl(data, cm->iobase + cmd);
  453. }
  454. static inline unsigned int snd_cmipci_read(struct cmipci *cm, unsigned int cmd)
  455. {
  456. return inl(cm->iobase + cmd);
  457. }
  458. /* read/write operations for word register */
  459. static inline void snd_cmipci_write_w(struct cmipci *cm, unsigned int cmd, unsigned short data)
  460. {
  461. outw(data, cm->iobase + cmd);
  462. }
  463. static inline unsigned short snd_cmipci_read_w(struct cmipci *cm, unsigned int cmd)
  464. {
  465. return inw(cm->iobase + cmd);
  466. }
  467. /* read/write operations for byte register */
  468. static inline void snd_cmipci_write_b(struct cmipci *cm, unsigned int cmd, unsigned char data)
  469. {
  470. outb(data, cm->iobase + cmd);
  471. }
  472. static inline unsigned char snd_cmipci_read_b(struct cmipci *cm, unsigned int cmd)
  473. {
  474. return inb(cm->iobase + cmd);
  475. }
  476. /* bit operations for dword register */
  477. static int snd_cmipci_set_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
  478. {
  479. unsigned int val, oval;
  480. val = oval = inl(cm->iobase + cmd);
  481. val |= flag;
  482. if (val == oval)
  483. return 0;
  484. outl(val, cm->iobase + cmd);
  485. return 1;
  486. }
  487. static int snd_cmipci_clear_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
  488. {
  489. unsigned int val, oval;
  490. val = oval = inl(cm->iobase + cmd);
  491. val &= ~flag;
  492. if (val == oval)
  493. return 0;
  494. outl(val, cm->iobase + cmd);
  495. return 1;
  496. }
  497. /* bit operations for byte register */
  498. static int snd_cmipci_set_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
  499. {
  500. unsigned char val, oval;
  501. val = oval = inb(cm->iobase + cmd);
  502. val |= flag;
  503. if (val == oval)
  504. return 0;
  505. outb(val, cm->iobase + cmd);
  506. return 1;
  507. }
  508. static int snd_cmipci_clear_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
  509. {
  510. unsigned char val, oval;
  511. val = oval = inb(cm->iobase + cmd);
  512. val &= ~flag;
  513. if (val == oval)
  514. return 0;
  515. outb(val, cm->iobase + cmd);
  516. return 1;
  517. }
  518. /*
  519. * PCM interface
  520. */
  521. /*
  522. * calculate frequency
  523. */
  524. static unsigned int rates[] = { 5512, 11025, 22050, 44100, 8000, 16000, 32000, 48000 };
  525. static unsigned int snd_cmipci_rate_freq(unsigned int rate)
  526. {
  527. unsigned int i;
  528. for (i = 0; i < ARRAY_SIZE(rates); i++) {
  529. if (rates[i] == rate)
  530. return i;
  531. }
  532. snd_BUG();
  533. return 0;
  534. }
  535. #ifdef USE_VAR48KRATE
  536. /*
  537. * Determine PLL values for frequency setup, maybe the CMI8338 (CMI8738???)
  538. * does it this way .. maybe not. Never get any information from C-Media about
  539. * that <werner@suse.de>.
  540. */
  541. static int snd_cmipci_pll_rmn(unsigned int rate, unsigned int adcmult, int *r, int *m, int *n)
  542. {
  543. unsigned int delta, tolerance;
  544. int xm, xn, xr;
  545. for (*r = 0; rate < CM_MAXIMUM_RATE/adcmult; *r += (1<<5))
  546. rate <<= 1;
  547. *n = -1;
  548. if (*r > 0xff)
  549. goto out;
  550. tolerance = rate*CM_TOLERANCE_RATE;
  551. for (xn = (1+2); xn < (0x1f+2); xn++) {
  552. for (xm = (1+2); xm < (0xff+2); xm++) {
  553. xr = ((CM_REFFREQ_XIN/adcmult) * xm) / xn;
  554. if (xr < rate)
  555. delta = rate - xr;
  556. else
  557. delta = xr - rate;
  558. /*
  559. * If we found one, remember this,
  560. * and try to find a closer one
  561. */
  562. if (delta < tolerance) {
  563. tolerance = delta;
  564. *m = xm - 2;
  565. *n = xn - 2;
  566. }
  567. }
  568. }
  569. out:
  570. return (*n > -1);
  571. }
  572. /*
  573. * Program pll register bits, I assume that the 8 registers 0xf8 upto 0xff
  574. * are mapped onto the 8 ADC/DAC sampling frequency which can be choosen
  575. * at the register CM_REG_FUNCTRL1 (0x04).
  576. * Problem: other ways are also possible (any information about that?)
  577. */
  578. static void snd_cmipci_set_pll(struct cmipci *cm, unsigned int rate, unsigned int slot)
  579. {
  580. unsigned int reg = CM_REG_PLL + slot;
  581. /*
  582. * Guess that this programs at reg. 0x04 the pos 15:13/12:10
  583. * for DSFC/ASFC (000 upto 111).
  584. */
  585. /* FIXME: Init (Do we've to set an other register first before programming?) */
  586. /* FIXME: Is this correct? Or shouldn't the m/n/r values be used for that? */
  587. snd_cmipci_write_b(cm, reg, rate>>8);
  588. snd_cmipci_write_b(cm, reg, rate&0xff);
  589. /* FIXME: Setup (Do we've to set an other register first to enable this?) */
  590. }
  591. #endif /* USE_VAR48KRATE */
  592. static int snd_cmipci_hw_params(struct snd_pcm_substream *substream,
  593. struct snd_pcm_hw_params *hw_params)
  594. {
  595. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  596. }
  597. static int snd_cmipci_playback2_hw_params(struct snd_pcm_substream *substream,
  598. struct snd_pcm_hw_params *hw_params)
  599. {
  600. struct cmipci *cm = snd_pcm_substream_chip(substream);
  601. if (params_channels(hw_params) > 2) {
  602. mutex_lock(&cm->open_mutex);
  603. if (cm->opened[CM_CH_PLAY]) {
  604. mutex_unlock(&cm->open_mutex);
  605. return -EBUSY;
  606. }
  607. /* reserve the channel A */
  608. cm->opened[CM_CH_PLAY] = CM_OPEN_PLAYBACK_MULTI;
  609. mutex_unlock(&cm->open_mutex);
  610. }
  611. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  612. }
  613. static void snd_cmipci_ch_reset(struct cmipci *cm, int ch)
  614. {
  615. int reset = CM_RST_CH0 << (cm->channel[ch].ch);
  616. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
  617. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
  618. udelay(10);
  619. }
  620. static int snd_cmipci_hw_free(struct snd_pcm_substream *substream)
  621. {
  622. return snd_pcm_lib_free_pages(substream);
  623. }
  624. /*
  625. */
  626. static unsigned int hw_channels[] = {1, 2, 4, 6, 8};
  627. static struct snd_pcm_hw_constraint_list hw_constraints_channels_4 = {
  628. .count = 3,
  629. .list = hw_channels,
  630. .mask = 0,
  631. };
  632. static struct snd_pcm_hw_constraint_list hw_constraints_channels_6 = {
  633. .count = 4,
  634. .list = hw_channels,
  635. .mask = 0,
  636. };
  637. static struct snd_pcm_hw_constraint_list hw_constraints_channels_8 = {
  638. .count = 5,
  639. .list = hw_channels,
  640. .mask = 0,
  641. };
  642. static int set_dac_channels(struct cmipci *cm, struct cmipci_pcm *rec, int channels)
  643. {
  644. if (channels > 2) {
  645. if (!cm->can_multi_ch || !rec->ch)
  646. return -EINVAL;
  647. if (rec->fmt != 0x03) /* stereo 16bit only */
  648. return -EINVAL;
  649. }
  650. if (cm->can_multi_ch) {
  651. spin_lock_irq(&cm->reg_lock);
  652. if (channels > 2) {
  653. snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
  654. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  655. } else {
  656. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
  657. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  658. }
  659. if (channels == 8)
  660. snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
  661. else
  662. snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
  663. if (channels == 6) {
  664. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
  665. snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
  666. } else {
  667. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
  668. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
  669. }
  670. if (channels == 4)
  671. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
  672. else
  673. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
  674. spin_unlock_irq(&cm->reg_lock);
  675. }
  676. return 0;
  677. }
  678. /*
  679. * prepare playback/capture channel
  680. * channel to be used must have been set in rec->ch.
  681. */
  682. static int snd_cmipci_pcm_prepare(struct cmipci *cm, struct cmipci_pcm *rec,
  683. struct snd_pcm_substream *substream)
  684. {
  685. unsigned int reg, freq, freq_ext, val;
  686. unsigned int period_size;
  687. struct snd_pcm_runtime *runtime = substream->runtime;
  688. rec->fmt = 0;
  689. rec->shift = 0;
  690. if (snd_pcm_format_width(runtime->format) >= 16) {
  691. rec->fmt |= 0x02;
  692. if (snd_pcm_format_width(runtime->format) > 16)
  693. rec->shift++; /* 24/32bit */
  694. }
  695. if (runtime->channels > 1)
  696. rec->fmt |= 0x01;
  697. if (rec->is_dac && set_dac_channels(cm, rec, runtime->channels) < 0) {
  698. snd_printd("cannot set dac channels\n");
  699. return -EINVAL;
  700. }
  701. rec->offset = runtime->dma_addr;
  702. /* buffer and period sizes in frame */
  703. rec->dma_size = runtime->buffer_size << rec->shift;
  704. period_size = runtime->period_size << rec->shift;
  705. if (runtime->channels > 2) {
  706. /* multi-channels */
  707. rec->dma_size = (rec->dma_size * runtime->channels) / 2;
  708. period_size = (period_size * runtime->channels) / 2;
  709. }
  710. spin_lock_irq(&cm->reg_lock);
  711. /* set buffer address */
  712. reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
  713. snd_cmipci_write(cm, reg, rec->offset);
  714. /* program sample counts */
  715. reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
  716. snd_cmipci_write_w(cm, reg, rec->dma_size - 1);
  717. snd_cmipci_write_w(cm, reg + 2, period_size - 1);
  718. /* set adc/dac flag */
  719. val = rec->ch ? CM_CHADC1 : CM_CHADC0;
  720. if (rec->is_dac)
  721. cm->ctrl &= ~val;
  722. else
  723. cm->ctrl |= val;
  724. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  725. //snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
  726. /* set sample rate */
  727. freq = 0;
  728. freq_ext = 0;
  729. if (runtime->rate > 48000)
  730. switch (runtime->rate) {
  731. case 88200: freq_ext = CM_CH0_SRATE_88K; break;
  732. case 96000: freq_ext = CM_CH0_SRATE_96K; break;
  733. case 128000: freq_ext = CM_CH0_SRATE_128K; break;
  734. default: snd_BUG(); break;
  735. }
  736. else
  737. freq = snd_cmipci_rate_freq(runtime->rate);
  738. val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
  739. if (rec->ch) {
  740. val &= ~CM_DSFC_MASK;
  741. val |= (freq << CM_DSFC_SHIFT) & CM_DSFC_MASK;
  742. } else {
  743. val &= ~CM_ASFC_MASK;
  744. val |= (freq << CM_ASFC_SHIFT) & CM_ASFC_MASK;
  745. }
  746. snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
  747. //snd_printd("cmipci: functrl1 = %08x\n", val);
  748. /* set format */
  749. val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
  750. if (rec->ch) {
  751. val &= ~CM_CH1FMT_MASK;
  752. val |= rec->fmt << CM_CH1FMT_SHIFT;
  753. } else {
  754. val &= ~CM_CH0FMT_MASK;
  755. val |= rec->fmt << CM_CH0FMT_SHIFT;
  756. }
  757. if (cm->can_96k) {
  758. val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
  759. val |= freq_ext << (rec->ch * 2);
  760. }
  761. snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
  762. //snd_printd("cmipci: chformat = %08x\n", val);
  763. if (!rec->is_dac && cm->chip_version) {
  764. if (runtime->rate > 44100)
  765. snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_ADC48K44K);
  766. else
  767. snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_ADC48K44K);
  768. }
  769. rec->running = 0;
  770. spin_unlock_irq(&cm->reg_lock);
  771. return 0;
  772. }
  773. /*
  774. * PCM trigger/stop
  775. */
  776. static int snd_cmipci_pcm_trigger(struct cmipci *cm, struct cmipci_pcm *rec,
  777. int cmd)
  778. {
  779. unsigned int inthld, chen, reset, pause;
  780. int result = 0;
  781. inthld = CM_CH0_INT_EN << rec->ch;
  782. chen = CM_CHEN0 << rec->ch;
  783. reset = CM_RST_CH0 << rec->ch;
  784. pause = CM_PAUSE0 << rec->ch;
  785. spin_lock(&cm->reg_lock);
  786. switch (cmd) {
  787. case SNDRV_PCM_TRIGGER_START:
  788. rec->running = 1;
  789. /* set interrupt */
  790. snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, inthld);
  791. cm->ctrl |= chen;
  792. /* enable channel */
  793. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  794. //snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
  795. break;
  796. case SNDRV_PCM_TRIGGER_STOP:
  797. rec->running = 0;
  798. /* disable interrupt */
  799. snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, inthld);
  800. /* reset */
  801. cm->ctrl &= ~chen;
  802. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
  803. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
  804. rec->needs_silencing = rec->is_dac;
  805. break;
  806. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  807. case SNDRV_PCM_TRIGGER_SUSPEND:
  808. cm->ctrl |= pause;
  809. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  810. break;
  811. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  812. case SNDRV_PCM_TRIGGER_RESUME:
  813. cm->ctrl &= ~pause;
  814. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  815. break;
  816. default:
  817. result = -EINVAL;
  818. break;
  819. }
  820. spin_unlock(&cm->reg_lock);
  821. return result;
  822. }
  823. /*
  824. * return the current pointer
  825. */
  826. static snd_pcm_uframes_t snd_cmipci_pcm_pointer(struct cmipci *cm, struct cmipci_pcm *rec,
  827. struct snd_pcm_substream *substream)
  828. {
  829. size_t ptr;
  830. unsigned int reg;
  831. if (!rec->running)
  832. return 0;
  833. #if 1 // this seems better..
  834. reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
  835. ptr = rec->dma_size - (snd_cmipci_read_w(cm, reg) + 1);
  836. ptr >>= rec->shift;
  837. #else
  838. reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
  839. ptr = snd_cmipci_read(cm, reg) - rec->offset;
  840. ptr = bytes_to_frames(substream->runtime, ptr);
  841. #endif
  842. if (substream->runtime->channels > 2)
  843. ptr = (ptr * 2) / substream->runtime->channels;
  844. return ptr;
  845. }
  846. /*
  847. * playback
  848. */
  849. static int snd_cmipci_playback_trigger(struct snd_pcm_substream *substream,
  850. int cmd)
  851. {
  852. struct cmipci *cm = snd_pcm_substream_chip(substream);
  853. return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], cmd);
  854. }
  855. static snd_pcm_uframes_t snd_cmipci_playback_pointer(struct snd_pcm_substream *substream)
  856. {
  857. struct cmipci *cm = snd_pcm_substream_chip(substream);
  858. return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_PLAY], substream);
  859. }
  860. /*
  861. * capture
  862. */
  863. static int snd_cmipci_capture_trigger(struct snd_pcm_substream *substream,
  864. int cmd)
  865. {
  866. struct cmipci *cm = snd_pcm_substream_chip(substream);
  867. return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], cmd);
  868. }
  869. static snd_pcm_uframes_t snd_cmipci_capture_pointer(struct snd_pcm_substream *substream)
  870. {
  871. struct cmipci *cm = snd_pcm_substream_chip(substream);
  872. return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_CAPT], substream);
  873. }
  874. /*
  875. * hw preparation for spdif
  876. */
  877. static int snd_cmipci_spdif_default_info(struct snd_kcontrol *kcontrol,
  878. struct snd_ctl_elem_info *uinfo)
  879. {
  880. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  881. uinfo->count = 1;
  882. return 0;
  883. }
  884. static int snd_cmipci_spdif_default_get(struct snd_kcontrol *kcontrol,
  885. struct snd_ctl_elem_value *ucontrol)
  886. {
  887. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  888. int i;
  889. spin_lock_irq(&chip->reg_lock);
  890. for (i = 0; i < 4; i++)
  891. ucontrol->value.iec958.status[i] = (chip->dig_status >> (i * 8)) & 0xff;
  892. spin_unlock_irq(&chip->reg_lock);
  893. return 0;
  894. }
  895. static int snd_cmipci_spdif_default_put(struct snd_kcontrol *kcontrol,
  896. struct snd_ctl_elem_value *ucontrol)
  897. {
  898. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  899. int i, change;
  900. unsigned int val;
  901. val = 0;
  902. spin_lock_irq(&chip->reg_lock);
  903. for (i = 0; i < 4; i++)
  904. val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  905. change = val != chip->dig_status;
  906. chip->dig_status = val;
  907. spin_unlock_irq(&chip->reg_lock);
  908. return change;
  909. }
  910. static struct snd_kcontrol_new snd_cmipci_spdif_default __devinitdata =
  911. {
  912. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  913. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  914. .info = snd_cmipci_spdif_default_info,
  915. .get = snd_cmipci_spdif_default_get,
  916. .put = snd_cmipci_spdif_default_put
  917. };
  918. static int snd_cmipci_spdif_mask_info(struct snd_kcontrol *kcontrol,
  919. struct snd_ctl_elem_info *uinfo)
  920. {
  921. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  922. uinfo->count = 1;
  923. return 0;
  924. }
  925. static int snd_cmipci_spdif_mask_get(struct snd_kcontrol *kcontrol,
  926. struct snd_ctl_elem_value *ucontrol)
  927. {
  928. ucontrol->value.iec958.status[0] = 0xff;
  929. ucontrol->value.iec958.status[1] = 0xff;
  930. ucontrol->value.iec958.status[2] = 0xff;
  931. ucontrol->value.iec958.status[3] = 0xff;
  932. return 0;
  933. }
  934. static struct snd_kcontrol_new snd_cmipci_spdif_mask __devinitdata =
  935. {
  936. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  937. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  938. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  939. .info = snd_cmipci_spdif_mask_info,
  940. .get = snd_cmipci_spdif_mask_get,
  941. };
  942. static int snd_cmipci_spdif_stream_info(struct snd_kcontrol *kcontrol,
  943. struct snd_ctl_elem_info *uinfo)
  944. {
  945. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  946. uinfo->count = 1;
  947. return 0;
  948. }
  949. static int snd_cmipci_spdif_stream_get(struct snd_kcontrol *kcontrol,
  950. struct snd_ctl_elem_value *ucontrol)
  951. {
  952. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  953. int i;
  954. spin_lock_irq(&chip->reg_lock);
  955. for (i = 0; i < 4; i++)
  956. ucontrol->value.iec958.status[i] = (chip->dig_pcm_status >> (i * 8)) & 0xff;
  957. spin_unlock_irq(&chip->reg_lock);
  958. return 0;
  959. }
  960. static int snd_cmipci_spdif_stream_put(struct snd_kcontrol *kcontrol,
  961. struct snd_ctl_elem_value *ucontrol)
  962. {
  963. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  964. int i, change;
  965. unsigned int val;
  966. val = 0;
  967. spin_lock_irq(&chip->reg_lock);
  968. for (i = 0; i < 4; i++)
  969. val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  970. change = val != chip->dig_pcm_status;
  971. chip->dig_pcm_status = val;
  972. spin_unlock_irq(&chip->reg_lock);
  973. return change;
  974. }
  975. static struct snd_kcontrol_new snd_cmipci_spdif_stream __devinitdata =
  976. {
  977. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  978. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  979. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  980. .info = snd_cmipci_spdif_stream_info,
  981. .get = snd_cmipci_spdif_stream_get,
  982. .put = snd_cmipci_spdif_stream_put
  983. };
  984. /*
  985. */
  986. /* save mixer setting and mute for AC3 playback */
  987. static int save_mixer_state(struct cmipci *cm)
  988. {
  989. if (! cm->mixer_insensitive) {
  990. struct snd_ctl_elem_value *val;
  991. unsigned int i;
  992. val = kmalloc(sizeof(*val), GFP_ATOMIC);
  993. if (!val)
  994. return -ENOMEM;
  995. for (i = 0; i < CM_SAVED_MIXERS; i++) {
  996. struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
  997. if (ctl) {
  998. int event;
  999. memset(val, 0, sizeof(*val));
  1000. ctl->get(ctl, val);
  1001. cm->mixer_res_status[i] = val->value.integer.value[0];
  1002. val->value.integer.value[0] = cm_saved_mixer[i].toggle_on;
  1003. event = SNDRV_CTL_EVENT_MASK_INFO;
  1004. if (cm->mixer_res_status[i] != val->value.integer.value[0]) {
  1005. ctl->put(ctl, val); /* toggle */
  1006. event |= SNDRV_CTL_EVENT_MASK_VALUE;
  1007. }
  1008. ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1009. snd_ctl_notify(cm->card, event, &ctl->id);
  1010. }
  1011. }
  1012. kfree(val);
  1013. cm->mixer_insensitive = 1;
  1014. }
  1015. return 0;
  1016. }
  1017. /* restore the previously saved mixer status */
  1018. static void restore_mixer_state(struct cmipci *cm)
  1019. {
  1020. if (cm->mixer_insensitive) {
  1021. struct snd_ctl_elem_value *val;
  1022. unsigned int i;
  1023. val = kmalloc(sizeof(*val), GFP_KERNEL);
  1024. if (!val)
  1025. return;
  1026. cm->mixer_insensitive = 0; /* at first clear this;
  1027. otherwise the changes will be ignored */
  1028. for (i = 0; i < CM_SAVED_MIXERS; i++) {
  1029. struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
  1030. if (ctl) {
  1031. int event;
  1032. memset(val, 0, sizeof(*val));
  1033. ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1034. ctl->get(ctl, val);
  1035. event = SNDRV_CTL_EVENT_MASK_INFO;
  1036. if (val->value.integer.value[0] != cm->mixer_res_status[i]) {
  1037. val->value.integer.value[0] = cm->mixer_res_status[i];
  1038. ctl->put(ctl, val);
  1039. event |= SNDRV_CTL_EVENT_MASK_VALUE;
  1040. }
  1041. snd_ctl_notify(cm->card, event, &ctl->id);
  1042. }
  1043. }
  1044. kfree(val);
  1045. }
  1046. }
  1047. /* spinlock held! */
  1048. static void setup_ac3(struct cmipci *cm, struct snd_pcm_substream *subs, int do_ac3, int rate)
  1049. {
  1050. if (do_ac3) {
  1051. /* AC3EN for 037 */
  1052. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
  1053. /* AC3EN for 039 */
  1054. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
  1055. if (cm->can_ac3_hw) {
  1056. /* SPD24SEL for 037, 0x02 */
  1057. /* SPD24SEL for 039, 0x20, but cannot be set */
  1058. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1059. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1060. } else { /* can_ac3_sw */
  1061. /* SPD32SEL for 037 & 039, 0x20 */
  1062. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1063. /* set 176K sample rate to fix 033 HW bug */
  1064. if (cm->chip_version == 33) {
  1065. if (rate >= 48000) {
  1066. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  1067. } else {
  1068. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  1069. }
  1070. }
  1071. }
  1072. } else {
  1073. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
  1074. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
  1075. if (cm->can_ac3_hw) {
  1076. /* chip model >= 37 */
  1077. if (snd_pcm_format_width(subs->runtime->format) > 16) {
  1078. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1079. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1080. } else {
  1081. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1082. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1083. }
  1084. } else {
  1085. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1086. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1087. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  1088. }
  1089. }
  1090. }
  1091. static int setup_spdif_playback(struct cmipci *cm, struct snd_pcm_substream *subs, int up, int do_ac3)
  1092. {
  1093. int rate, err;
  1094. rate = subs->runtime->rate;
  1095. if (up && do_ac3)
  1096. if ((err = save_mixer_state(cm)) < 0)
  1097. return err;
  1098. spin_lock_irq(&cm->reg_lock);
  1099. cm->spdif_playback_avail = up;
  1100. if (up) {
  1101. /* they are controlled via "IEC958 Output Switch" */
  1102. /* snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
  1103. /* snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
  1104. if (cm->spdif_playback_enabled)
  1105. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  1106. setup_ac3(cm, subs, do_ac3, rate);
  1107. if (rate == 48000 || rate == 96000)
  1108. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
  1109. else
  1110. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
  1111. if (rate > 48000)
  1112. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  1113. else
  1114. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  1115. } else {
  1116. /* they are controlled via "IEC958 Output Switch" */
  1117. /* snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
  1118. /* snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
  1119. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  1120. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  1121. setup_ac3(cm, subs, 0, 0);
  1122. }
  1123. spin_unlock_irq(&cm->reg_lock);
  1124. return 0;
  1125. }
  1126. /*
  1127. * preparation
  1128. */
  1129. /* playback - enable spdif only on the certain condition */
  1130. static int snd_cmipci_playback_prepare(struct snd_pcm_substream *substream)
  1131. {
  1132. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1133. int rate = substream->runtime->rate;
  1134. int err, do_spdif, do_ac3 = 0;
  1135. do_spdif = (rate >= 44100 && rate <= 96000 &&
  1136. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE &&
  1137. substream->runtime->channels == 2);
  1138. if (do_spdif && cm->can_ac3_hw)
  1139. do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
  1140. if ((err = setup_spdif_playback(cm, substream, do_spdif, do_ac3)) < 0)
  1141. return err;
  1142. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
  1143. }
  1144. /* playback (via device #2) - enable spdif always */
  1145. static int snd_cmipci_playback_spdif_prepare(struct snd_pcm_substream *substream)
  1146. {
  1147. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1148. int err, do_ac3;
  1149. if (cm->can_ac3_hw)
  1150. do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
  1151. else
  1152. do_ac3 = 1; /* doesn't matter */
  1153. if ((err = setup_spdif_playback(cm, substream, 1, do_ac3)) < 0)
  1154. return err;
  1155. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
  1156. }
  1157. /*
  1158. * Apparently, the samples last played on channel A stay in some buffer, even
  1159. * after the channel is reset, and get added to the data for the rear DACs when
  1160. * playing a multichannel stream on channel B. This is likely to generate
  1161. * wraparounds and thus distortions.
  1162. * To avoid this, we play at least one zero sample after the actual stream has
  1163. * stopped.
  1164. */
  1165. static void snd_cmipci_silence_hack(struct cmipci *cm, struct cmipci_pcm *rec)
  1166. {
  1167. struct snd_pcm_runtime *runtime = rec->substream->runtime;
  1168. unsigned int reg, val;
  1169. if (rec->needs_silencing && runtime && runtime->dma_area) {
  1170. /* set up a small silence buffer */
  1171. memset(runtime->dma_area, 0, PAGE_SIZE);
  1172. reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
  1173. val = ((PAGE_SIZE / 4) - 1) | (((PAGE_SIZE / 4) / 2 - 1) << 16);
  1174. snd_cmipci_write(cm, reg, val);
  1175. /* configure for 16 bits, 2 channels, 8 kHz */
  1176. if (runtime->channels > 2)
  1177. set_dac_channels(cm, rec, 2);
  1178. spin_lock_irq(&cm->reg_lock);
  1179. val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
  1180. val &= ~(CM_ASFC_MASK << (rec->ch * 3));
  1181. val |= (4 << CM_ASFC_SHIFT) << (rec->ch * 3);
  1182. snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
  1183. val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
  1184. val &= ~(CM_CH0FMT_MASK << (rec->ch * 2));
  1185. val |= (3 << CM_CH0FMT_SHIFT) << (rec->ch * 2);
  1186. if (cm->can_96k)
  1187. val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
  1188. snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
  1189. /* start stream (we don't need interrupts) */
  1190. cm->ctrl |= CM_CHEN0 << rec->ch;
  1191. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  1192. spin_unlock_irq(&cm->reg_lock);
  1193. msleep(1);
  1194. /* stop and reset stream */
  1195. spin_lock_irq(&cm->reg_lock);
  1196. cm->ctrl &= ~(CM_CHEN0 << rec->ch);
  1197. val = CM_RST_CH0 << rec->ch;
  1198. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | val);
  1199. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~val);
  1200. spin_unlock_irq(&cm->reg_lock);
  1201. rec->needs_silencing = 0;
  1202. }
  1203. }
  1204. static int snd_cmipci_playback_hw_free(struct snd_pcm_substream *substream)
  1205. {
  1206. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1207. setup_spdif_playback(cm, substream, 0, 0);
  1208. restore_mixer_state(cm);
  1209. snd_cmipci_silence_hack(cm, &cm->channel[0]);
  1210. return snd_cmipci_hw_free(substream);
  1211. }
  1212. static int snd_cmipci_playback2_hw_free(struct snd_pcm_substream *substream)
  1213. {
  1214. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1215. snd_cmipci_silence_hack(cm, &cm->channel[1]);
  1216. return snd_cmipci_hw_free(substream);
  1217. }
  1218. /* capture */
  1219. static int snd_cmipci_capture_prepare(struct snd_pcm_substream *substream)
  1220. {
  1221. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1222. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
  1223. }
  1224. /* capture with spdif (via device #2) */
  1225. static int snd_cmipci_capture_spdif_prepare(struct snd_pcm_substream *substream)
  1226. {
  1227. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1228. spin_lock_irq(&cm->reg_lock);
  1229. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
  1230. if (cm->can_96k) {
  1231. if (substream->runtime->rate > 48000)
  1232. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  1233. else
  1234. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  1235. }
  1236. if (snd_pcm_format_width(substream->runtime->format) > 16)
  1237. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1238. else
  1239. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1240. spin_unlock_irq(&cm->reg_lock);
  1241. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
  1242. }
  1243. static int snd_cmipci_capture_spdif_hw_free(struct snd_pcm_substream *subs)
  1244. {
  1245. struct cmipci *cm = snd_pcm_substream_chip(subs);
  1246. spin_lock_irq(&cm->reg_lock);
  1247. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
  1248. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1249. spin_unlock_irq(&cm->reg_lock);
  1250. return snd_cmipci_hw_free(subs);
  1251. }
  1252. /*
  1253. * interrupt handler
  1254. */
  1255. static irqreturn_t snd_cmipci_interrupt(int irq, void *dev_id)
  1256. {
  1257. struct cmipci *cm = dev_id;
  1258. unsigned int status, mask = 0;
  1259. /* fastpath out, to ease interrupt sharing */
  1260. status = snd_cmipci_read(cm, CM_REG_INT_STATUS);
  1261. if (!(status & CM_INTR))
  1262. return IRQ_NONE;
  1263. /* acknowledge interrupt */
  1264. spin_lock(&cm->reg_lock);
  1265. if (status & CM_CHINT0)
  1266. mask |= CM_CH0_INT_EN;
  1267. if (status & CM_CHINT1)
  1268. mask |= CM_CH1_INT_EN;
  1269. snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, mask);
  1270. snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, mask);
  1271. spin_unlock(&cm->reg_lock);
  1272. if (cm->rmidi && (status & CM_UARTINT))
  1273. snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data);
  1274. if (cm->pcm) {
  1275. if ((status & CM_CHINT0) && cm->channel[0].running)
  1276. snd_pcm_period_elapsed(cm->channel[0].substream);
  1277. if ((status & CM_CHINT1) && cm->channel[1].running)
  1278. snd_pcm_period_elapsed(cm->channel[1].substream);
  1279. }
  1280. return IRQ_HANDLED;
  1281. }
  1282. /*
  1283. * h/w infos
  1284. */
  1285. /* playback on channel A */
  1286. static struct snd_pcm_hardware snd_cmipci_playback =
  1287. {
  1288. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1289. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1290. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1291. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1292. .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  1293. .rate_min = 5512,
  1294. .rate_max = 48000,
  1295. .channels_min = 1,
  1296. .channels_max = 2,
  1297. .buffer_bytes_max = (128*1024),
  1298. .period_bytes_min = 64,
  1299. .period_bytes_max = (128*1024),
  1300. .periods_min = 2,
  1301. .periods_max = 1024,
  1302. .fifo_size = 0,
  1303. };
  1304. /* capture on channel B */
  1305. static struct snd_pcm_hardware snd_cmipci_capture =
  1306. {
  1307. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1308. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1309. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1310. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1311. .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  1312. .rate_min = 5512,
  1313. .rate_max = 48000,
  1314. .channels_min = 1,
  1315. .channels_max = 2,
  1316. .buffer_bytes_max = (128*1024),
  1317. .period_bytes_min = 64,
  1318. .period_bytes_max = (128*1024),
  1319. .periods_min = 2,
  1320. .periods_max = 1024,
  1321. .fifo_size = 0,
  1322. };
  1323. /* playback on channel B - stereo 16bit only? */
  1324. static struct snd_pcm_hardware snd_cmipci_playback2 =
  1325. {
  1326. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1327. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1328. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1329. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1330. .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  1331. .rate_min = 5512,
  1332. .rate_max = 48000,
  1333. .channels_min = 2,
  1334. .channels_max = 2,
  1335. .buffer_bytes_max = (128*1024),
  1336. .period_bytes_min = 64,
  1337. .period_bytes_max = (128*1024),
  1338. .periods_min = 2,
  1339. .periods_max = 1024,
  1340. .fifo_size = 0,
  1341. };
  1342. /* spdif playback on channel A */
  1343. static struct snd_pcm_hardware snd_cmipci_playback_spdif =
  1344. {
  1345. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1346. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1347. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1348. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1349. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  1350. .rate_min = 44100,
  1351. .rate_max = 48000,
  1352. .channels_min = 2,
  1353. .channels_max = 2,
  1354. .buffer_bytes_max = (128*1024),
  1355. .period_bytes_min = 64,
  1356. .period_bytes_max = (128*1024),
  1357. .periods_min = 2,
  1358. .periods_max = 1024,
  1359. .fifo_size = 0,
  1360. };
  1361. /* spdif playback on channel A (32bit, IEC958 subframes) */
  1362. static struct snd_pcm_hardware snd_cmipci_playback_iec958_subframe =
  1363. {
  1364. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1365. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1366. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1367. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  1368. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  1369. .rate_min = 44100,
  1370. .rate_max = 48000,
  1371. .channels_min = 2,
  1372. .channels_max = 2,
  1373. .buffer_bytes_max = (128*1024),
  1374. .period_bytes_min = 64,
  1375. .period_bytes_max = (128*1024),
  1376. .periods_min = 2,
  1377. .periods_max = 1024,
  1378. .fifo_size = 0,
  1379. };
  1380. /* spdif capture on channel B */
  1381. static struct snd_pcm_hardware snd_cmipci_capture_spdif =
  1382. {
  1383. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1384. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1385. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1386. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  1387. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  1388. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  1389. .rate_min = 44100,
  1390. .rate_max = 48000,
  1391. .channels_min = 2,
  1392. .channels_max = 2,
  1393. .buffer_bytes_max = (128*1024),
  1394. .period_bytes_min = 64,
  1395. .period_bytes_max = (128*1024),
  1396. .periods_min = 2,
  1397. .periods_max = 1024,
  1398. .fifo_size = 0,
  1399. };
  1400. static unsigned int rate_constraints[] = { 5512, 8000, 11025, 16000, 22050,
  1401. 32000, 44100, 48000, 88200, 96000, 128000 };
  1402. static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  1403. .count = ARRAY_SIZE(rate_constraints),
  1404. .list = rate_constraints,
  1405. .mask = 0,
  1406. };
  1407. /*
  1408. * check device open/close
  1409. */
  1410. static int open_device_check(struct cmipci *cm, int mode, struct snd_pcm_substream *subs)
  1411. {
  1412. int ch = mode & CM_OPEN_CH_MASK;
  1413. /* FIXME: a file should wait until the device becomes free
  1414. * when it's opened on blocking mode. however, since the current
  1415. * pcm framework doesn't pass file pointer before actually opened,
  1416. * we can't know whether blocking mode or not in open callback..
  1417. */
  1418. mutex_lock(&cm->open_mutex);
  1419. if (cm->opened[ch]) {
  1420. mutex_unlock(&cm->open_mutex);
  1421. return -EBUSY;
  1422. }
  1423. cm->opened[ch] = mode;
  1424. cm->channel[ch].substream = subs;
  1425. if (! (mode & CM_OPEN_DAC)) {
  1426. /* disable dual DAC mode */
  1427. cm->channel[ch].is_dac = 0;
  1428. spin_lock_irq(&cm->reg_lock);
  1429. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
  1430. spin_unlock_irq(&cm->reg_lock);
  1431. }
  1432. mutex_unlock(&cm->open_mutex);
  1433. return 0;
  1434. }
  1435. static void close_device_check(struct cmipci *cm, int mode)
  1436. {
  1437. int ch = mode & CM_OPEN_CH_MASK;
  1438. mutex_lock(&cm->open_mutex);
  1439. if (cm->opened[ch] == mode) {
  1440. if (cm->channel[ch].substream) {
  1441. snd_cmipci_ch_reset(cm, ch);
  1442. cm->channel[ch].running = 0;
  1443. cm->channel[ch].substream = NULL;
  1444. }
  1445. cm->opened[ch] = 0;
  1446. if (! cm->channel[ch].is_dac) {
  1447. /* enable dual DAC mode again */
  1448. cm->channel[ch].is_dac = 1;
  1449. spin_lock_irq(&cm->reg_lock);
  1450. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
  1451. spin_unlock_irq(&cm->reg_lock);
  1452. }
  1453. }
  1454. mutex_unlock(&cm->open_mutex);
  1455. }
  1456. /*
  1457. */
  1458. static int snd_cmipci_playback_open(struct snd_pcm_substream *substream)
  1459. {
  1460. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1461. struct snd_pcm_runtime *runtime = substream->runtime;
  1462. int err;
  1463. if ((err = open_device_check(cm, CM_OPEN_PLAYBACK, substream)) < 0)
  1464. return err;
  1465. runtime->hw = snd_cmipci_playback;
  1466. if (cm->chip_version == 68) {
  1467. runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
  1468. SNDRV_PCM_RATE_96000;
  1469. runtime->hw.rate_max = 96000;
  1470. } else if (cm->chip_version == 55) {
  1471. err = snd_pcm_hw_constraint_list(runtime, 0,
  1472. SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  1473. if (err < 0)
  1474. return err;
  1475. runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
  1476. runtime->hw.rate_max = 128000;
  1477. }
  1478. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  1479. cm->dig_pcm_status = cm->dig_status;
  1480. return 0;
  1481. }
  1482. static int snd_cmipci_capture_open(struct snd_pcm_substream *substream)
  1483. {
  1484. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1485. struct snd_pcm_runtime *runtime = substream->runtime;
  1486. int err;
  1487. if ((err = open_device_check(cm, CM_OPEN_CAPTURE, substream)) < 0)
  1488. return err;
  1489. runtime->hw = snd_cmipci_capture;
  1490. if (cm->chip_version == 68) { // 8768 only supports 44k/48k recording
  1491. runtime->hw.rate_min = 41000;
  1492. runtime->hw.rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
  1493. } else if (cm->chip_version == 55) {
  1494. err = snd_pcm_hw_constraint_list(runtime, 0,
  1495. SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  1496. if (err < 0)
  1497. return err;
  1498. runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
  1499. runtime->hw.rate_max = 128000;
  1500. }
  1501. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  1502. return 0;
  1503. }
  1504. static int snd_cmipci_playback2_open(struct snd_pcm_substream *substream)
  1505. {
  1506. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1507. struct snd_pcm_runtime *runtime = substream->runtime;
  1508. int err;
  1509. if ((err = open_device_check(cm, CM_OPEN_PLAYBACK2, substream)) < 0) /* use channel B */
  1510. return err;
  1511. runtime->hw = snd_cmipci_playback2;
  1512. mutex_lock(&cm->open_mutex);
  1513. if (! cm->opened[CM_CH_PLAY]) {
  1514. if (cm->can_multi_ch) {
  1515. runtime->hw.channels_max = cm->max_channels;
  1516. if (cm->max_channels == 4)
  1517. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_4);
  1518. else if (cm->max_channels == 6)
  1519. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_6);
  1520. else if (cm->max_channels == 8)
  1521. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_8);
  1522. }
  1523. }
  1524. mutex_unlock(&cm->open_mutex);
  1525. if (cm->chip_version == 68) {
  1526. runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
  1527. SNDRV_PCM_RATE_96000;
  1528. runtime->hw.rate_max = 96000;
  1529. } else if (cm->chip_version == 55) {
  1530. err = snd_pcm_hw_constraint_list(runtime, 0,
  1531. SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  1532. if (err < 0)
  1533. return err;
  1534. runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
  1535. runtime->hw.rate_max = 128000;
  1536. }
  1537. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  1538. return 0;
  1539. }
  1540. static int snd_cmipci_playback_spdif_open(struct snd_pcm_substream *substream)
  1541. {
  1542. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1543. struct snd_pcm_runtime *runtime = substream->runtime;
  1544. int err;
  1545. if ((err = open_device_check(cm, CM_OPEN_SPDIF_PLAYBACK, substream)) < 0) /* use channel A */
  1546. return err;
  1547. if (cm->can_ac3_hw) {
  1548. runtime->hw = snd_cmipci_playback_spdif;
  1549. if (cm->chip_version >= 37) {
  1550. runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1551. snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  1552. }
  1553. if (cm->can_96k) {
  1554. runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
  1555. SNDRV_PCM_RATE_96000;
  1556. runtime->hw.rate_max = 96000;
  1557. }
  1558. } else {
  1559. runtime->hw = snd_cmipci_playback_iec958_subframe;
  1560. }
  1561. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
  1562. cm->dig_pcm_status = cm->dig_status;
  1563. return 0;
  1564. }
  1565. static int snd_cmipci_capture_spdif_open(struct snd_pcm_substream *substream)
  1566. {
  1567. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1568. struct snd_pcm_runtime *runtime = substream->runtime;
  1569. int err;
  1570. if ((err = open_device_check(cm, CM_OPEN_SPDIF_CAPTURE, substream)) < 0) /* use channel B */
  1571. return err;
  1572. runtime->hw = snd_cmipci_capture_spdif;
  1573. if (cm->can_96k && !(cm->chip_version == 68)) {
  1574. runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
  1575. SNDRV_PCM_RATE_96000;
  1576. runtime->hw.rate_max = 96000;
  1577. }
  1578. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
  1579. return 0;
  1580. }
  1581. /*
  1582. */
  1583. static int snd_cmipci_playback_close(struct snd_pcm_substream *substream)
  1584. {
  1585. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1586. close_device_check(cm, CM_OPEN_PLAYBACK);
  1587. return 0;
  1588. }
  1589. static int snd_cmipci_capture_close(struct snd_pcm_substream *substream)
  1590. {
  1591. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1592. close_device_check(cm, CM_OPEN_CAPTURE);
  1593. return 0;
  1594. }
  1595. static int snd_cmipci_playback2_close(struct snd_pcm_substream *substream)
  1596. {
  1597. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1598. close_device_check(cm, CM_OPEN_PLAYBACK2);
  1599. close_device_check(cm, CM_OPEN_PLAYBACK_MULTI);
  1600. return 0;
  1601. }
  1602. static int snd_cmipci_playback_spdif_close(struct snd_pcm_substream *substream)
  1603. {
  1604. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1605. close_device_check(cm, CM_OPEN_SPDIF_PLAYBACK);
  1606. return 0;
  1607. }
  1608. static int snd_cmipci_capture_spdif_close(struct snd_pcm_substream *substream)
  1609. {
  1610. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1611. close_device_check(cm, CM_OPEN_SPDIF_CAPTURE);
  1612. return 0;
  1613. }
  1614. /*
  1615. */
  1616. static struct snd_pcm_ops snd_cmipci_playback_ops = {
  1617. .open = snd_cmipci_playback_open,
  1618. .close = snd_cmipci_playback_close,
  1619. .ioctl = snd_pcm_lib_ioctl,
  1620. .hw_params = snd_cmipci_hw_params,
  1621. .hw_free = snd_cmipci_playback_hw_free,
  1622. .prepare = snd_cmipci_playback_prepare,
  1623. .trigger = snd_cmipci_playback_trigger,
  1624. .pointer = snd_cmipci_playback_pointer,
  1625. };
  1626. static struct snd_pcm_ops snd_cmipci_capture_ops = {
  1627. .open = snd_cmipci_capture_open,
  1628. .close = snd_cmipci_capture_close,
  1629. .ioctl = snd_pcm_lib_ioctl,
  1630. .hw_params = snd_cmipci_hw_params,
  1631. .hw_free = snd_cmipci_hw_free,
  1632. .prepare = snd_cmipci_capture_prepare,
  1633. .trigger = snd_cmipci_capture_trigger,
  1634. .pointer = snd_cmipci_capture_pointer,
  1635. };
  1636. static struct snd_pcm_ops snd_cmipci_playback2_ops = {
  1637. .open = snd_cmipci_playback2_open,
  1638. .close = snd_cmipci_playback2_close,
  1639. .ioctl = snd_pcm_lib_ioctl,
  1640. .hw_params = snd_cmipci_playback2_hw_params,
  1641. .hw_free = snd_cmipci_playback2_hw_free,
  1642. .prepare = snd_cmipci_capture_prepare, /* channel B */
  1643. .trigger = snd_cmipci_capture_trigger, /* channel B */
  1644. .pointer = snd_cmipci_capture_pointer, /* channel B */
  1645. };
  1646. static struct snd_pcm_ops snd_cmipci_playback_spdif_ops = {
  1647. .open = snd_cmipci_playback_spdif_open,
  1648. .close = snd_cmipci_playback_spdif_close,
  1649. .ioctl = snd_pcm_lib_ioctl,
  1650. .hw_params = snd_cmipci_hw_params,
  1651. .hw_free = snd_cmipci_playback_hw_free,
  1652. .prepare = snd_cmipci_playback_spdif_prepare, /* set up rate */
  1653. .trigger = snd_cmipci_playback_trigger,
  1654. .pointer = snd_cmipci_playback_pointer,
  1655. };
  1656. static struct snd_pcm_ops snd_cmipci_capture_spdif_ops = {
  1657. .open = snd_cmipci_capture_spdif_open,
  1658. .close = snd_cmipci_capture_spdif_close,
  1659. .ioctl = snd_pcm_lib_ioctl,
  1660. .hw_params = snd_cmipci_hw_params,
  1661. .hw_free = snd_cmipci_capture_spdif_hw_free,
  1662. .prepare = snd_cmipci_capture_spdif_prepare,
  1663. .trigger = snd_cmipci_capture_trigger,
  1664. .pointer = snd_cmipci_capture_pointer,
  1665. };
  1666. /*
  1667. */
  1668. static int __devinit snd_cmipci_pcm_new(struct cmipci *cm, int device)
  1669. {
  1670. struct snd_pcm *pcm;
  1671. int err;
  1672. err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
  1673. if (err < 0)
  1674. return err;
  1675. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_ops);
  1676. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_ops);
  1677. pcm->private_data = cm;
  1678. pcm->info_flags = 0;
  1679. strcpy(pcm->name, "C-Media PCI DAC/ADC");
  1680. cm->pcm = pcm;
  1681. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1682. snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  1683. return 0;
  1684. }
  1685. static int __devinit snd_cmipci_pcm2_new(struct cmipci *cm, int device)
  1686. {
  1687. struct snd_pcm *pcm;
  1688. int err;
  1689. err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 0, &pcm);
  1690. if (err < 0)
  1691. return err;
  1692. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback2_ops);
  1693. pcm->private_data = cm;
  1694. pcm->info_flags = 0;
  1695. strcpy(pcm->name, "C-Media PCI 2nd DAC");
  1696. cm->pcm2 = pcm;
  1697. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1698. snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  1699. return 0;
  1700. }
  1701. static int __devinit snd_cmipci_pcm_spdif_new(struct cmipci *cm, int device)
  1702. {
  1703. struct snd_pcm *pcm;
  1704. int err;
  1705. err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
  1706. if (err < 0)
  1707. return err;
  1708. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_spdif_ops);
  1709. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_spdif_ops);
  1710. pcm->private_data = cm;
  1711. pcm->info_flags = 0;
  1712. strcpy(pcm->name, "C-Media PCI IEC958");
  1713. cm->pcm_spdif = pcm;
  1714. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1715. snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  1716. return 0;
  1717. }
  1718. /*
  1719. * mixer interface:
  1720. * - CM8338/8738 has a compatible mixer interface with SB16, but
  1721. * lack of some elements like tone control, i/o gain and AGC.
  1722. * - Access to native registers:
  1723. * - A 3D switch
  1724. * - Output mute switches
  1725. */
  1726. static void snd_cmipci_mixer_write(struct cmipci *s, unsigned char idx, unsigned char data)
  1727. {
  1728. outb(idx, s->iobase + CM_REG_SB16_ADDR);
  1729. outb(data, s->iobase + CM_REG_SB16_DATA);
  1730. }
  1731. static unsigned char snd_cmipci_mixer_read(struct cmipci *s, unsigned char idx)
  1732. {
  1733. unsigned char v;
  1734. outb(idx, s->iobase + CM_REG_SB16_ADDR);
  1735. v = inb(s->iobase + CM_REG_SB16_DATA);
  1736. return v;
  1737. }
  1738. /*
  1739. * general mixer element
  1740. */
  1741. struct cmipci_sb_reg {
  1742. unsigned int left_reg, right_reg;
  1743. unsigned int left_shift, right_shift;
  1744. unsigned int mask;
  1745. unsigned int invert: 1;
  1746. unsigned int stereo: 1;
  1747. };
  1748. #define COMPOSE_SB_REG(lreg,rreg,lshift,rshift,mask,invert,stereo) \
  1749. ((lreg) | ((rreg) << 8) | (lshift << 16) | (rshift << 19) | (mask << 24) | (invert << 22) | (stereo << 23))
  1750. #define CMIPCI_DOUBLE(xname, left_reg, right_reg, left_shift, right_shift, mask, invert, stereo) \
  1751. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1752. .info = snd_cmipci_info_volume, \
  1753. .get = snd_cmipci_get_volume, .put = snd_cmipci_put_volume, \
  1754. .private_value = COMPOSE_SB_REG(left_reg, right_reg, left_shift, right_shift, mask, invert, stereo), \
  1755. }
  1756. #define CMIPCI_SB_VOL_STEREO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg+1, shift, shift, mask, 0, 1)
  1757. #define CMIPCI_SB_VOL_MONO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg, shift, shift, mask, 0, 0)
  1758. #define CMIPCI_SB_SW_STEREO(xname,lshift,rshift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, lshift, rshift, 1, 0, 1)
  1759. #define CMIPCI_SB_SW_MONO(xname,shift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, shift, shift, 1, 0, 0)
  1760. static void cmipci_sb_reg_decode(struct cmipci_sb_reg *r, unsigned long val)
  1761. {
  1762. r->left_reg = val & 0xff;
  1763. r->right_reg = (val >> 8) & 0xff;
  1764. r->left_shift = (val >> 16) & 0x07;
  1765. r->right_shift = (val >> 19) & 0x07;
  1766. r->invert = (val >> 22) & 1;
  1767. r->stereo = (val >> 23) & 1;
  1768. r->mask = (val >> 24) & 0xff;
  1769. }
  1770. static int snd_cmipci_info_volume(struct snd_kcontrol *kcontrol,
  1771. struct snd_ctl_elem_info *uinfo)
  1772. {
  1773. struct cmipci_sb_reg reg;
  1774. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1775. uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1776. uinfo->count = reg.stereo + 1;
  1777. uinfo->value.integer.min = 0;
  1778. uinfo->value.integer.max = reg.mask;
  1779. return 0;
  1780. }
  1781. static int snd_cmipci_get_volume(struct snd_kcontrol *kcontrol,
  1782. struct snd_ctl_elem_value *ucontrol)
  1783. {
  1784. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1785. struct cmipci_sb_reg reg;
  1786. int val;
  1787. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1788. spin_lock_irq(&cm->reg_lock);
  1789. val = (snd_cmipci_mixer_read(cm, reg.left_reg) >> reg.left_shift) & reg.mask;
  1790. if (reg.invert)
  1791. val = reg.mask - val;
  1792. ucontrol->value.integer.value[0] = val;
  1793. if (reg.stereo) {
  1794. val = (snd_cmipci_mixer_read(cm, reg.right_reg) >> reg.right_shift) & reg.mask;
  1795. if (reg.invert)
  1796. val = reg.mask - val;
  1797. ucontrol->value.integer.value[1] = val;
  1798. }
  1799. spin_unlock_irq(&cm->reg_lock);
  1800. return 0;
  1801. }
  1802. static int snd_cmipci_put_volume(struct snd_kcontrol *kcontrol,
  1803. struct snd_ctl_elem_value *ucontrol)
  1804. {
  1805. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1806. struct cmipci_sb_reg reg;
  1807. int change;
  1808. int left, right, oleft, oright;
  1809. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1810. left = ucontrol->value.integer.value[0] & reg.mask;
  1811. if (reg.invert)
  1812. left = reg.mask - left;
  1813. left <<= reg.left_shift;
  1814. if (reg.stereo) {
  1815. right = ucontrol->value.integer.value[1] & reg.mask;
  1816. if (reg.invert)
  1817. right = reg.mask - right;
  1818. right <<= reg.right_shift;
  1819. } else
  1820. right = 0;
  1821. spin_lock_irq(&cm->reg_lock);
  1822. oleft = snd_cmipci_mixer_read(cm, reg.left_reg);
  1823. left |= oleft & ~(reg.mask << reg.left_shift);
  1824. change = left != oleft;
  1825. if (reg.stereo) {
  1826. if (reg.left_reg != reg.right_reg) {
  1827. snd_cmipci_mixer_write(cm, reg.left_reg, left);
  1828. oright = snd_cmipci_mixer_read(cm, reg.right_reg);
  1829. } else
  1830. oright = left;
  1831. right |= oright & ~(reg.mask << reg.right_shift);
  1832. change |= right != oright;
  1833. snd_cmipci_mixer_write(cm, reg.right_reg, right);
  1834. } else
  1835. snd_cmipci_mixer_write(cm, reg.left_reg, left);
  1836. spin_unlock_irq(&cm->reg_lock);
  1837. return change;
  1838. }
  1839. /*
  1840. * input route (left,right) -> (left,right)
  1841. */
  1842. #define CMIPCI_SB_INPUT_SW(xname, left_shift, right_shift) \
  1843. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1844. .info = snd_cmipci_info_input_sw, \
  1845. .get = snd_cmipci_get_input_sw, .put = snd_cmipci_put_input_sw, \
  1846. .private_value = COMPOSE_SB_REG(SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, left_shift, right_shift, 1, 0, 1), \
  1847. }
  1848. static int snd_cmipci_info_input_sw(struct snd_kcontrol *kcontrol,
  1849. struct snd_ctl_elem_info *uinfo)
  1850. {
  1851. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1852. uinfo->count = 4;
  1853. uinfo->value.integer.min = 0;
  1854. uinfo->value.integer.max = 1;
  1855. return 0;
  1856. }
  1857. static int snd_cmipci_get_input_sw(struct snd_kcontrol *kcontrol,
  1858. struct snd_ctl_elem_value *ucontrol)
  1859. {
  1860. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1861. struct cmipci_sb_reg reg;
  1862. int val1, val2;
  1863. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1864. spin_lock_irq(&cm->reg_lock);
  1865. val1 = snd_cmipci_mixer_read(cm, reg.left_reg);
  1866. val2 = snd_cmipci_mixer_read(cm, reg.right_reg);
  1867. spin_unlock_irq(&cm->reg_lock);
  1868. ucontrol->value.integer.value[0] = (val1 >> reg.left_shift) & 1;
  1869. ucontrol->value.integer.value[1] = (val2 >> reg.left_shift) & 1;
  1870. ucontrol->value.integer.value[2] = (val1 >> reg.right_shift) & 1;
  1871. ucontrol->value.integer.value[3] = (val2 >> reg.right_shift) & 1;
  1872. return 0;
  1873. }
  1874. static int snd_cmipci_put_input_sw(struct snd_kcontrol *kcontrol,
  1875. struct snd_ctl_elem_value *ucontrol)
  1876. {
  1877. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1878. struct cmipci_sb_reg reg;
  1879. int change;
  1880. int val1, val2, oval1, oval2;
  1881. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1882. spin_lock_irq(&cm->reg_lock);
  1883. oval1 = snd_cmipci_mixer_read(cm, reg.left_reg);
  1884. oval2 = snd_cmipci_mixer_read(cm, reg.right_reg);
  1885. val1 = oval1 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
  1886. val2 = oval2 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
  1887. val1 |= (ucontrol->value.integer.value[0] & 1) << reg.left_shift;
  1888. val2 |= (ucontrol->value.integer.value[1] & 1) << reg.left_shift;
  1889. val1 |= (ucontrol->value.integer.value[2] & 1) << reg.right_shift;
  1890. val2 |= (ucontrol->value.integer.value[3] & 1) << reg.right_shift;
  1891. change = val1 != oval1 || val2 != oval2;
  1892. snd_cmipci_mixer_write(cm, reg.left_reg, val1);
  1893. snd_cmipci_mixer_write(cm, reg.right_reg, val2);
  1894. spin_unlock_irq(&cm->reg_lock);
  1895. return change;
  1896. }
  1897. /*
  1898. * native mixer switches/volumes
  1899. */
  1900. #define CMIPCI_MIXER_SW_STEREO(xname, reg, lshift, rshift, invert) \
  1901. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1902. .info = snd_cmipci_info_native_mixer, \
  1903. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1904. .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, 1, invert, 1), \
  1905. }
  1906. #define CMIPCI_MIXER_SW_MONO(xname, reg, shift, invert) \
  1907. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1908. .info = snd_cmipci_info_native_mixer, \
  1909. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1910. .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, 1, invert, 0), \
  1911. }
  1912. #define CMIPCI_MIXER_VOL_STEREO(xname, reg, lshift, rshift, mask) \
  1913. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1914. .info = snd_cmipci_info_native_mixer, \
  1915. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1916. .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, mask, 0, 1), \
  1917. }
  1918. #define CMIPCI_MIXER_VOL_MONO(xname, reg, shift, mask) \
  1919. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1920. .info = snd_cmipci_info_native_mixer, \
  1921. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1922. .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, mask, 0, 0), \
  1923. }
  1924. static int snd_cmipci_info_native_mixer(struct snd_kcontrol *kcontrol,
  1925. struct snd_ctl_elem_info *uinfo)
  1926. {
  1927. struct cmipci_sb_reg reg;
  1928. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1929. uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1930. uinfo->count = reg.stereo + 1;
  1931. uinfo->value.integer.min = 0;
  1932. uinfo->value.integer.max = reg.mask;
  1933. return 0;
  1934. }
  1935. static int snd_cmipci_get_native_mixer(struct snd_kcontrol *kcontrol,
  1936. struct snd_ctl_elem_value *ucontrol)
  1937. {
  1938. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1939. struct cmipci_sb_reg reg;
  1940. unsigned char oreg, val;
  1941. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1942. spin_lock_irq(&cm->reg_lock);
  1943. oreg = inb(cm->iobase + reg.left_reg);
  1944. val = (oreg >> reg.left_shift) & reg.mask;
  1945. if (reg.invert)
  1946. val = reg.mask - val;
  1947. ucontrol->value.integer.value[0] = val;
  1948. if (reg.stereo) {
  1949. val = (oreg >> reg.right_shift) & reg.mask;
  1950. if (reg.invert)
  1951. val = reg.mask - val;
  1952. ucontrol->value.integer.value[1] = val;
  1953. }
  1954. spin_unlock_irq(&cm->reg_lock);
  1955. return 0;
  1956. }
  1957. static int snd_cmipci_put_native_mixer(struct snd_kcontrol *kcontrol,
  1958. struct snd_ctl_elem_value *ucontrol)
  1959. {
  1960. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1961. struct cmipci_sb_reg reg;
  1962. unsigned char oreg, nreg, val;
  1963. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1964. spin_lock_irq(&cm->reg_lock);
  1965. oreg = inb(cm->iobase + reg.left_reg);
  1966. val = ucontrol->value.integer.value[0] & reg.mask;
  1967. if (reg.invert)
  1968. val = reg.mask - val;
  1969. nreg = oreg & ~(reg.mask << reg.left_shift);
  1970. nreg |= (val << reg.left_shift);
  1971. if (reg.stereo) {
  1972. val = ucontrol->value.integer.value[1] & reg.mask;
  1973. if (reg.invert)
  1974. val = reg.mask - val;
  1975. nreg &= ~(reg.mask << reg.right_shift);
  1976. nreg |= (val << reg.right_shift);
  1977. }
  1978. outb(nreg, cm->iobase + reg.left_reg);
  1979. spin_unlock_irq(&cm->reg_lock);
  1980. return (nreg != oreg);
  1981. }
  1982. /*
  1983. * special case - check mixer sensitivity
  1984. */
  1985. static int snd_cmipci_get_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
  1986. struct snd_ctl_elem_value *ucontrol)
  1987. {
  1988. //struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1989. return snd_cmipci_get_native_mixer(kcontrol, ucontrol);
  1990. }
  1991. static int snd_cmipci_put_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
  1992. struct snd_ctl_elem_value *ucontrol)
  1993. {
  1994. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1995. if (cm->mixer_insensitive) {
  1996. /* ignored */
  1997. return 0;
  1998. }
  1999. return snd_cmipci_put_native_mixer(kcontrol, ucontrol);
  2000. }
  2001. static struct snd_kcontrol_new snd_cmipci_mixers[] __devinitdata = {
  2002. CMIPCI_SB_VOL_STEREO("Master Playback Volume", SB_DSP4_MASTER_DEV, 3, 31),
  2003. CMIPCI_MIXER_SW_MONO("3D Control - Switch", CM_REG_MIXER1, CM_X3DEN_SHIFT, 0),
  2004. CMIPCI_SB_VOL_STEREO("PCM Playback Volume", SB_DSP4_PCM_DEV, 3, 31),
  2005. //CMIPCI_MIXER_SW_MONO("PCM Playback Switch", CM_REG_MIXER1, CM_WSMUTE_SHIFT, 1),
  2006. { /* switch with sensitivity */
  2007. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2008. .name = "PCM Playback Switch",
  2009. .info = snd_cmipci_info_native_mixer,
  2010. .get = snd_cmipci_get_native_mixer_sensitive,
  2011. .put = snd_cmipci_put_native_mixer_sensitive,
  2012. .private_value = COMPOSE_SB_REG(CM_REG_MIXER1, CM_REG_MIXER1, CM_WSMUTE_SHIFT, CM_WSMUTE_SHIFT, 1, 1, 0),
  2013. },
  2014. CMIPCI_MIXER_SW_STEREO("PCM Capture Switch", CM_REG_MIXER1, CM_WAVEINL_SHIFT, CM_WAVEINR_SHIFT, 0),
  2015. CMIPCI_SB_VOL_STEREO("Synth Playback Volume", SB_DSP4_SYNTH_DEV, 3, 31),
  2016. CMIPCI_MIXER_SW_MONO("Synth Playback Switch", CM_REG_MIXER1, CM_FMMUTE_SHIFT, 1),
  2017. CMIPCI_SB_INPUT_SW("Synth Capture Route", 6, 5),
  2018. CMIPCI_SB_VOL_STEREO("CD Playback Volume", SB_DSP4_CD_DEV, 3, 31),
  2019. CMIPCI_SB_SW_STEREO("CD Playback Switch", 2, 1),
  2020. CMIPCI_SB_INPUT_SW("CD Capture Route", 2, 1),
  2021. CMIPCI_SB_VOL_STEREO("Line Playback Volume", SB_DSP4_LINE_DEV, 3, 31),
  2022. CMIPCI_SB_SW_STEREO("Line Playback Switch", 4, 3),
  2023. CMIPCI_SB_INPUT_SW("Line Capture Route", 4, 3),
  2024. CMIPCI_SB_VOL_MONO("Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
  2025. CMIPCI_SB_SW_MONO("Mic Playback Switch", 0),
  2026. CMIPCI_DOUBLE("Mic Capture Switch", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 0, 0, 1, 0, 0),
  2027. CMIPCI_SB_VOL_MONO("PC Speaker Playback Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
  2028. CMIPCI_MIXER_VOL_STEREO("Aux Playback Volume", CM_REG_AUX_VOL, 4, 0, 15),
  2029. CMIPCI_MIXER_SW_STEREO("Aux Playback Switch", CM_REG_MIXER2, CM_VAUXLM_SHIFT, CM_VAUXRM_SHIFT, 0),
  2030. CMIPCI_MIXER_SW_STEREO("Aux Capture Switch", CM_REG_MIXER2, CM_RAUXLEN_SHIFT, CM_RAUXREN_SHIFT, 0),
  2031. CMIPCI_MIXER_SW_MONO("Mic Boost Playback Switch", CM_REG_MIXER2, CM_MICGAINZ_SHIFT, 1),
  2032. CMIPCI_MIXER_VOL_MONO("Mic Capture Volume", CM_REG_MIXER2, CM_VADMIC_SHIFT, 7),
  2033. CMIPCI_SB_VOL_MONO("Phone Playback Volume", CM_REG_EXTENT_IND, 5, 7),
  2034. CMIPCI_DOUBLE("Phone Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 4, 4, 1, 0, 0),
  2035. CMIPCI_DOUBLE("PC Speaker Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 3, 3, 1, 0, 0),
  2036. CMIPCI_DOUBLE("Mic Boost Capture Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 0, 0, 1, 0, 0),
  2037. };
  2038. /*
  2039. * other switches
  2040. */
  2041. struct cmipci_switch_args {
  2042. int reg; /* register index */
  2043. unsigned int mask; /* mask bits */
  2044. unsigned int mask_on; /* mask bits to turn on */
  2045. unsigned int is_byte: 1; /* byte access? */
  2046. unsigned int ac3_sensitive: 1; /* access forbidden during
  2047. * non-audio operation?
  2048. */
  2049. };
  2050. #define snd_cmipci_uswitch_info snd_ctl_boolean_mono_info
  2051. static int _snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
  2052. struct snd_ctl_elem_value *ucontrol,
  2053. struct cmipci_switch_args *args)
  2054. {
  2055. unsigned int val;
  2056. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2057. spin_lock_irq(&cm->reg_lock);
  2058. if (args->ac3_sensitive && cm->mixer_insensitive) {
  2059. ucontrol->value.integer.value[0] = 0;
  2060. spin_unlock_irq(&cm->reg_lock);
  2061. return 0;
  2062. }
  2063. if (args->is_byte)
  2064. val = inb(cm->iobase + args->reg);
  2065. else
  2066. val = snd_cmipci_read(cm, args->reg);
  2067. ucontrol->value.integer.value[0] = ((val & args->mask) == args->mask_on) ? 1 : 0;
  2068. spin_unlock_irq(&cm->reg_lock);
  2069. return 0;
  2070. }
  2071. static int snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
  2072. struct snd_ctl_elem_value *ucontrol)
  2073. {
  2074. struct cmipci_switch_args *args;
  2075. args = (struct cmipci_switch_args *)kcontrol->private_value;
  2076. if (snd_BUG_ON(!args))
  2077. return -EINVAL;
  2078. return _snd_cmipci_uswitch_get(kcontrol, ucontrol, args);
  2079. }
  2080. static int _snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
  2081. struct snd_ctl_elem_value *ucontrol,
  2082. struct cmipci_switch_args *args)
  2083. {
  2084. unsigned int val;
  2085. int change;
  2086. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2087. spin_lock_irq(&cm->reg_lock);
  2088. if (args->ac3_sensitive && cm->mixer_insensitive) {
  2089. /* ignored */
  2090. spin_unlock_irq(&cm->reg_lock);
  2091. return 0;
  2092. }
  2093. if (args->is_byte)
  2094. val = inb(cm->iobase + args->reg);
  2095. else
  2096. val = snd_cmipci_read(cm, args->reg);
  2097. change = (val & args->mask) != (ucontrol->value.integer.value[0] ?
  2098. args->mask_on : (args->mask & ~args->mask_on));
  2099. if (change) {
  2100. val &= ~args->mask;
  2101. if (ucontrol->value.integer.value[0])
  2102. val |= args->mask_on;
  2103. else
  2104. val |= (args->mask & ~args->mask_on);
  2105. if (args->is_byte)
  2106. outb((unsigned char)val, cm->iobase + args->reg);
  2107. else
  2108. snd_cmipci_write(cm, args->reg, val);
  2109. }
  2110. spin_unlock_irq(&cm->reg_lock);
  2111. return change;
  2112. }
  2113. static int snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
  2114. struct snd_ctl_elem_value *ucontrol)
  2115. {
  2116. struct cmipci_switch_args *args;
  2117. args = (struct cmipci_switch_args *)kcontrol->private_value;
  2118. if (snd_BUG_ON(!args))
  2119. return -EINVAL;
  2120. return _snd_cmipci_uswitch_put(kcontrol, ucontrol, args);
  2121. }
  2122. #define DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask_on, xis_byte, xac3) \
  2123. static struct cmipci_switch_args cmipci_switch_arg_##sname = { \
  2124. .reg = xreg, \
  2125. .mask = xmask, \
  2126. .mask_on = xmask_on, \
  2127. .is_byte = xis_byte, \
  2128. .ac3_sensitive = xac3, \
  2129. }
  2130. #define DEFINE_BIT_SWITCH_ARG(sname, xreg, xmask, xis_byte, xac3) \
  2131. DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask, xis_byte, xac3)
  2132. #if 0 /* these will be controlled in pcm device */
  2133. DEFINE_BIT_SWITCH_ARG(spdif_in, CM_REG_FUNCTRL1, CM_SPDF_1, 0, 0);
  2134. DEFINE_BIT_SWITCH_ARG(spdif_out, CM_REG_FUNCTRL1, CM_SPDF_0, 0, 0);
  2135. #endif
  2136. DEFINE_BIT_SWITCH_ARG(spdif_in_sel1, CM_REG_CHFORMAT, CM_SPDIF_SELECT1, 0, 0);
  2137. DEFINE_BIT_SWITCH_ARG(spdif_in_sel2, CM_REG_MISC_CTRL, CM_SPDIF_SELECT2, 0, 0);
  2138. DEFINE_BIT_SWITCH_ARG(spdif_enable, CM_REG_LEGACY_CTRL, CM_ENSPDOUT, 0, 0);
  2139. DEFINE_BIT_SWITCH_ARG(spdo2dac, CM_REG_FUNCTRL1, CM_SPDO2DAC, 0, 1);
  2140. DEFINE_BIT_SWITCH_ARG(spdi_valid, CM_REG_MISC, CM_SPDVALID, 1, 0);
  2141. DEFINE_BIT_SWITCH_ARG(spdif_copyright, CM_REG_LEGACY_CTRL, CM_SPDCOPYRHT, 0, 0);
  2142. DEFINE_BIT_SWITCH_ARG(spdif_dac_out, CM_REG_LEGACY_CTRL, CM_DAC2SPDO, 0, 1);
  2143. DEFINE_SWITCH_ARG(spdo_5v, CM_REG_MISC_CTRL, CM_SPDO5V, 0, 0, 0); /* inverse: 0 = 5V */
  2144. // DEFINE_BIT_SWITCH_ARG(spdo_48k, CM_REG_MISC_CTRL, CM_SPDF_AC97|CM_SPDIF48K, 0, 1);
  2145. DEFINE_BIT_SWITCH_ARG(spdif_loop, CM_REG_FUNCTRL1, CM_SPDFLOOP, 0, 1);
  2146. DEFINE_BIT_SWITCH_ARG(spdi_monitor, CM_REG_MIXER1, CM_CDPLAY, 1, 0);
  2147. /* DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_CHFORMAT, CM_SPDIF_INVERSE, 0, 0); */
  2148. DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_MISC, CM_SPDIF_INVERSE, 1, 0);
  2149. DEFINE_BIT_SWITCH_ARG(spdi_phase2, CM_REG_CHFORMAT, CM_SPDIF_INVERSE2, 0, 0);
  2150. #if CM_CH_PLAY == 1
  2151. DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, 0, 0, 0); /* reversed */
  2152. #else
  2153. DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, CM_XCHGDAC, 0, 0);
  2154. #endif
  2155. DEFINE_BIT_SWITCH_ARG(fourch, CM_REG_MISC_CTRL, CM_N4SPK3D, 0, 0);
  2156. // DEFINE_BIT_SWITCH_ARG(line_rear, CM_REG_MIXER1, CM_REAR2LIN, 1, 0);
  2157. // DEFINE_BIT_SWITCH_ARG(line_bass, CM_REG_LEGACY_CTRL, CM_CENTR2LIN|CM_BASE2LIN, 0, 0);
  2158. // DEFINE_BIT_SWITCH_ARG(joystick, CM_REG_FUNCTRL1, CM_JYSTK_EN, 0, 0); /* now module option */
  2159. DEFINE_SWITCH_ARG(modem, CM_REG_MISC_CTRL, CM_FLINKON|CM_FLINKOFF, CM_FLINKON, 0, 0);
  2160. #define DEFINE_SWITCH(sname, stype, sarg) \
  2161. { .name = sname, \
  2162. .iface = stype, \
  2163. .info = snd_cmipci_uswitch_info, \
  2164. .get = snd_cmipci_uswitch_get, \
  2165. .put = snd_cmipci_uswitch_put, \
  2166. .private_value = (unsigned long)&cmipci_switch_arg_##sarg,\
  2167. }
  2168. #define DEFINE_CARD_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_CARD, sarg)
  2169. #define DEFINE_MIXER_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_MIXER, sarg)
  2170. /*
  2171. * callbacks for spdif output switch
  2172. * needs toggle two registers..
  2173. */
  2174. static int snd_cmipci_spdout_enable_get(struct snd_kcontrol *kcontrol,
  2175. struct snd_ctl_elem_value *ucontrol)
  2176. {
  2177. int changed;
  2178. changed = _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
  2179. changed |= _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
  2180. return changed;
  2181. }
  2182. static int snd_cmipci_spdout_enable_put(struct snd_kcontrol *kcontrol,
  2183. struct snd_ctl_elem_value *ucontrol)
  2184. {
  2185. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  2186. int changed;
  2187. changed = _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
  2188. changed |= _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
  2189. if (changed) {
  2190. if (ucontrol->value.integer.value[0]) {
  2191. if (chip->spdif_playback_avail)
  2192. snd_cmipci_set_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  2193. } else {
  2194. if (chip->spdif_playback_avail)
  2195. snd_cmipci_clear_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  2196. }
  2197. }
  2198. chip->spdif_playback_enabled = ucontrol->value.integer.value[0];
  2199. return changed;
  2200. }
  2201. static int snd_cmipci_line_in_mode_info(struct snd_kcontrol *kcontrol,
  2202. struct snd_ctl_elem_info *uinfo)
  2203. {
  2204. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2205. static char *texts[3] = { "Line-In", "Rear Output", "Bass Output" };
  2206. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2207. uinfo->count = 1;
  2208. uinfo->value.enumerated.items = cm->chip_version >= 39 ? 3 : 2;
  2209. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  2210. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  2211. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  2212. return 0;
  2213. }
  2214. static inline unsigned int get_line_in_mode(struct cmipci *cm)
  2215. {
  2216. unsigned int val;
  2217. if (cm->chip_version >= 39) {
  2218. val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL);
  2219. if (val & (CM_CENTR2LIN | CM_BASE2LIN))
  2220. return 2;
  2221. }
  2222. val = snd_cmipci_read_b(cm, CM_REG_MIXER1);
  2223. if (val & CM_REAR2LIN)
  2224. return 1;
  2225. return 0;
  2226. }
  2227. static int snd_cmipci_line_in_mode_get(struct snd_kcontrol *kcontrol,
  2228. struct snd_ctl_elem_value *ucontrol)
  2229. {
  2230. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2231. spin_lock_irq(&cm->reg_lock);
  2232. ucontrol->value.enumerated.item[0] = get_line_in_mode(cm);
  2233. spin_unlock_irq(&cm->reg_lock);
  2234. return 0;
  2235. }
  2236. static int snd_cmipci_line_in_mode_put(struct snd_kcontrol *kcontrol,
  2237. struct snd_ctl_elem_value *ucontrol)
  2238. {
  2239. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2240. int change;
  2241. spin_lock_irq(&cm->reg_lock);
  2242. if (ucontrol->value.enumerated.item[0] == 2)
  2243. change = snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
  2244. else
  2245. change = snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
  2246. if (ucontrol->value.enumerated.item[0] == 1)
  2247. change |= snd_cmipci_set_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
  2248. else
  2249. change |= snd_cmipci_clear_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
  2250. spin_unlock_irq(&cm->reg_lock);
  2251. return change;
  2252. }
  2253. static int snd_cmipci_mic_in_mode_info(struct snd_kcontrol *kcontrol,
  2254. struct snd_ctl_elem_info *uinfo)
  2255. {
  2256. static char *texts[2] = { "Mic-In", "Center/LFE Output" };
  2257. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2258. uinfo->count = 1;
  2259. uinfo->value.enumerated.items = 2;
  2260. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  2261. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  2262. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  2263. return 0;
  2264. }
  2265. static int snd_cmipci_mic_in_mode_get(struct snd_kcontrol *kcontrol,
  2266. struct snd_ctl_elem_value *ucontrol)
  2267. {
  2268. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2269. /* same bit as spdi_phase */
  2270. spin_lock_irq(&cm->reg_lock);
  2271. ucontrol->value.enumerated.item[0] =
  2272. (snd_cmipci_read_b(cm, CM_REG_MISC) & CM_SPDIF_INVERSE) ? 1 : 0;
  2273. spin_unlock_irq(&cm->reg_lock);
  2274. return 0;
  2275. }
  2276. static int snd_cmipci_mic_in_mode_put(struct snd_kcontrol *kcontrol,
  2277. struct snd_ctl_elem_value *ucontrol)
  2278. {
  2279. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2280. int change;
  2281. spin_lock_irq(&cm->reg_lock);
  2282. if (ucontrol->value.enumerated.item[0])
  2283. change = snd_cmipci_set_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
  2284. else
  2285. change = snd_cmipci_clear_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
  2286. spin_unlock_irq(&cm->reg_lock);
  2287. return change;
  2288. }
  2289. /* both for CM8338/8738 */
  2290. static struct snd_kcontrol_new snd_cmipci_mixer_switches[] __devinitdata = {
  2291. DEFINE_MIXER_SWITCH("Four Channel Mode", fourch),
  2292. {
  2293. .name = "Line-In Mode",
  2294. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2295. .info = snd_cmipci_line_in_mode_info,
  2296. .get = snd_cmipci_line_in_mode_get,
  2297. .put = snd_cmipci_line_in_mode_put,
  2298. },
  2299. };
  2300. /* for non-multichannel chips */
  2301. static struct snd_kcontrol_new snd_cmipci_nomulti_switch __devinitdata =
  2302. DEFINE_MIXER_SWITCH("Exchange DAC", exchange_dac);
  2303. /* only for CM8738 */
  2304. static struct snd_kcontrol_new snd_cmipci_8738_mixer_switches[] __devinitdata = {
  2305. #if 0 /* controlled in pcm device */
  2306. DEFINE_MIXER_SWITCH("IEC958 In Record", spdif_in),
  2307. DEFINE_MIXER_SWITCH("IEC958 Out", spdif_out),
  2308. DEFINE_MIXER_SWITCH("IEC958 Out To DAC", spdo2dac),
  2309. #endif
  2310. // DEFINE_MIXER_SWITCH("IEC958 Output Switch", spdif_enable),
  2311. { .name = "IEC958 Output Switch",
  2312. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2313. .info = snd_cmipci_uswitch_info,
  2314. .get = snd_cmipci_spdout_enable_get,
  2315. .put = snd_cmipci_spdout_enable_put,
  2316. },
  2317. DEFINE_MIXER_SWITCH("IEC958 In Valid", spdi_valid),
  2318. DEFINE_MIXER_SWITCH("IEC958 Copyright", spdif_copyright),
  2319. DEFINE_MIXER_SWITCH("IEC958 5V", spdo_5v),
  2320. // DEFINE_MIXER_SWITCH("IEC958 In/Out 48KHz", spdo_48k),
  2321. DEFINE_MIXER_SWITCH("IEC958 Loop", spdif_loop),
  2322. DEFINE_MIXER_SWITCH("IEC958 In Monitor", spdi_monitor),
  2323. };
  2324. /* only for model 033/037 */
  2325. static struct snd_kcontrol_new snd_cmipci_old_mixer_switches[] __devinitdata = {
  2326. DEFINE_MIXER_SWITCH("IEC958 Mix Analog", spdif_dac_out),
  2327. DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase),
  2328. DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel1),
  2329. };
  2330. /* only for model 039 or later */
  2331. static struct snd_kcontrol_new snd_cmipci_extra_mixer_switches[] __devinitdata = {
  2332. DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel2),
  2333. DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase2),
  2334. {
  2335. .name = "Mic-In Mode",
  2336. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2337. .info = snd_cmipci_mic_in_mode_info,
  2338. .get = snd_cmipci_mic_in_mode_get,
  2339. .put = snd_cmipci_mic_in_mode_put,
  2340. }
  2341. };
  2342. /* card control switches */
  2343. static struct snd_kcontrol_new snd_cmipci_modem_switch __devinitdata =
  2344. DEFINE_CARD_SWITCH("Modem", modem);
  2345. static int __devinit snd_cmipci_mixer_new(struct cmipci *cm, int pcm_spdif_device)
  2346. {
  2347. struct snd_card *card;
  2348. struct snd_kcontrol_new *sw;
  2349. struct snd_kcontrol *kctl;
  2350. unsigned int idx;
  2351. int err;
  2352. if (snd_BUG_ON(!cm || !cm->card))
  2353. return -EINVAL;
  2354. card = cm->card;
  2355. strcpy(card->mixername, "CMedia PCI");
  2356. spin_lock_irq(&cm->reg_lock);
  2357. snd_cmipci_mixer_write(cm, 0x00, 0x00); /* mixer reset */
  2358. spin_unlock_irq(&cm->reg_lock);
  2359. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixers); idx++) {
  2360. if (cm->chip_version == 68) { // 8768 has no PCM volume
  2361. if (!strcmp(snd_cmipci_mixers[idx].name,
  2362. "PCM Playback Volume"))
  2363. continue;
  2364. }
  2365. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cmipci_mixers[idx], cm))) < 0)
  2366. return err;
  2367. }
  2368. /* mixer switches */
  2369. sw = snd_cmipci_mixer_switches;
  2370. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixer_switches); idx++, sw++) {
  2371. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2372. if (err < 0)
  2373. return err;
  2374. }
  2375. if (! cm->can_multi_ch) {
  2376. err = snd_ctl_add(cm->card, snd_ctl_new1(&snd_cmipci_nomulti_switch, cm));
  2377. if (err < 0)
  2378. return err;
  2379. }
  2380. if (cm->device == PCI_DEVICE_ID_CMEDIA_CM8738 ||
  2381. cm->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
  2382. sw = snd_cmipci_8738_mixer_switches;
  2383. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_8738_mixer_switches); idx++, sw++) {
  2384. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2385. if (err < 0)
  2386. return err;
  2387. }
  2388. if (cm->can_ac3_hw) {
  2389. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_default, cm))) < 0)
  2390. return err;
  2391. kctl->id.device = pcm_spdif_device;
  2392. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_mask, cm))) < 0)
  2393. return err;
  2394. kctl->id.device = pcm_spdif_device;
  2395. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_stream, cm))) < 0)
  2396. return err;
  2397. kctl->id.device = pcm_spdif_device;
  2398. }
  2399. if (cm->chip_version <= 37) {
  2400. sw = snd_cmipci_old_mixer_switches;
  2401. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_old_mixer_switches); idx++, sw++) {
  2402. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2403. if (err < 0)
  2404. return err;
  2405. }
  2406. }
  2407. }
  2408. if (cm->chip_version >= 39) {
  2409. sw = snd_cmipci_extra_mixer_switches;
  2410. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_extra_mixer_switches); idx++, sw++) {
  2411. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2412. if (err < 0)
  2413. return err;
  2414. }
  2415. }
  2416. /* card switches */
  2417. /*
  2418. * newer chips don't have the register bits to force modem link
  2419. * detection; the bit that was FLINKON now mutes CH1
  2420. */
  2421. if (cm->chip_version < 39) {
  2422. err = snd_ctl_add(cm->card,
  2423. snd_ctl_new1(&snd_cmipci_modem_switch, cm));
  2424. if (err < 0)
  2425. return err;
  2426. }
  2427. for (idx = 0; idx < CM_SAVED_MIXERS; idx++) {
  2428. struct snd_ctl_elem_id elem_id;
  2429. struct snd_kcontrol *ctl;
  2430. memset(&elem_id, 0, sizeof(elem_id));
  2431. elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2432. strcpy(elem_id.name, cm_saved_mixer[idx].name);
  2433. ctl = snd_ctl_find_id(cm->card, &elem_id);
  2434. if (ctl)
  2435. cm->mixer_res_ctl[idx] = ctl;
  2436. }
  2437. return 0;
  2438. }
  2439. /*
  2440. * proc interface
  2441. */
  2442. #ifdef CONFIG_PROC_FS
  2443. static void snd_cmipci_proc_read(struct snd_info_entry *entry,
  2444. struct snd_info_buffer *buffer)
  2445. {
  2446. struct cmipci *cm = entry->private_data;
  2447. int i, v;
  2448. snd_iprintf(buffer, "%s\n", cm->card->longname);
  2449. for (i = 0; i < 0x94; i++) {
  2450. if (i == 0x28)
  2451. i = 0x90;
  2452. v = inb(cm->iobase + i);
  2453. if (i % 4 == 0)
  2454. snd_iprintf(buffer, "\n%02x:", i);
  2455. snd_iprintf(buffer, " %02x", v);
  2456. }
  2457. snd_iprintf(buffer, "\n");
  2458. }
  2459. static void __devinit snd_cmipci_proc_init(struct cmipci *cm)
  2460. {
  2461. struct snd_info_entry *entry;
  2462. if (! snd_card_proc_new(cm->card, "cmipci", &entry))
  2463. snd_info_set_text_ops(entry, cm, snd_cmipci_proc_read);
  2464. }
  2465. #else /* !CONFIG_PROC_FS */
  2466. static inline void snd_cmipci_proc_init(struct cmipci *cm) {}
  2467. #endif
  2468. static struct pci_device_id snd_cmipci_ids[] = {
  2469. {PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A), 0},
  2470. {PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B), 0},
  2471. {PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738), 0},
  2472. {PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738B), 0},
  2473. {PCI_VDEVICE(AL, PCI_DEVICE_ID_CMEDIA_CM8738), 0},
  2474. {0,},
  2475. };
  2476. /*
  2477. * check chip version and capabilities
  2478. * driver name is modified according to the chip model
  2479. */
  2480. static void __devinit query_chip(struct cmipci *cm)
  2481. {
  2482. unsigned int detect;
  2483. /* check reg 0Ch, bit 24-31 */
  2484. detect = snd_cmipci_read(cm, CM_REG_INT_HLDCLR) & CM_CHIP_MASK2;
  2485. if (! detect) {
  2486. /* check reg 08h, bit 24-28 */
  2487. detect = snd_cmipci_read(cm, CM_REG_CHFORMAT) & CM_CHIP_MASK1;
  2488. switch (detect) {
  2489. case 0:
  2490. cm->chip_version = 33;
  2491. if (cm->do_soft_ac3)
  2492. cm->can_ac3_sw = 1;
  2493. else
  2494. cm->can_ac3_hw = 1;
  2495. break;
  2496. case CM_CHIP_037:
  2497. cm->chip_version = 37;
  2498. cm->can_ac3_hw = 1;
  2499. break;
  2500. default:
  2501. cm->chip_version = 39;
  2502. cm->can_ac3_hw = 1;
  2503. break;
  2504. }
  2505. cm->max_channels = 2;
  2506. } else {
  2507. if (detect & CM_CHIP_039) {
  2508. cm->chip_version = 39;
  2509. if (detect & CM_CHIP_039_6CH) /* 4 or 6 channels */
  2510. cm->max_channels = 6;
  2511. else
  2512. cm->max_channels = 4;
  2513. } else if (detect & CM_CHIP_8768) {
  2514. cm->chip_version = 68;
  2515. cm->max_channels = 8;
  2516. cm->can_96k = 1;
  2517. } else {
  2518. cm->chip_version = 55;
  2519. cm->max_channels = 6;
  2520. cm->can_96k = 1;
  2521. }
  2522. cm->can_ac3_hw = 1;
  2523. cm->can_multi_ch = 1;
  2524. }
  2525. }
  2526. #ifdef SUPPORT_JOYSTICK
  2527. static int __devinit snd_cmipci_create_gameport(struct cmipci *cm, int dev)
  2528. {
  2529. static int ports[] = { 0x201, 0x200, 0 }; /* FIXME: majority is 0x201? */
  2530. struct gameport *gp;
  2531. struct resource *r = NULL;
  2532. int i, io_port = 0;
  2533. if (joystick_port[dev] == 0)
  2534. return -ENODEV;
  2535. if (joystick_port[dev] == 1) { /* auto-detect */
  2536. for (i = 0; ports[i]; i++) {
  2537. io_port = ports[i];
  2538. r = request_region(io_port, 1, "CMIPCI gameport");
  2539. if (r)
  2540. break;
  2541. }
  2542. } else {
  2543. io_port = joystick_port[dev];
  2544. r = request_region(io_port, 1, "CMIPCI gameport");
  2545. }
  2546. if (!r) {
  2547. printk(KERN_WARNING "cmipci: cannot reserve joystick ports\n");
  2548. return -EBUSY;
  2549. }
  2550. cm->gameport = gp = gameport_allocate_port();
  2551. if (!gp) {
  2552. printk(KERN_ERR "cmipci: cannot allocate memory for gameport\n");
  2553. release_and_free_resource(r);
  2554. return -ENOMEM;
  2555. }
  2556. gameport_set_name(gp, "C-Media Gameport");
  2557. gameport_set_phys(gp, "pci%s/gameport0", pci_name(cm->pci));
  2558. gameport_set_dev_parent(gp, &cm->pci->dev);
  2559. gp->io = io_port;
  2560. gameport_set_port_data(gp, r);
  2561. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  2562. gameport_register_port(cm->gameport);
  2563. return 0;
  2564. }
  2565. static void snd_cmipci_free_gameport(struct cmipci *cm)
  2566. {
  2567. if (cm->gameport) {
  2568. struct resource *r = gameport_get_port_data(cm->gameport);
  2569. gameport_unregister_port(cm->gameport);
  2570. cm->gameport = NULL;
  2571. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  2572. release_and_free_resource(r);
  2573. }
  2574. }
  2575. #else
  2576. static inline int snd_cmipci_create_gameport(struct cmipci *cm, int dev) { return -ENOSYS; }
  2577. static inline void snd_cmipci_free_gameport(struct cmipci *cm) { }
  2578. #endif
  2579. static int snd_cmipci_free(struct cmipci *cm)
  2580. {
  2581. if (cm->irq >= 0) {
  2582. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  2583. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT);
  2584. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
  2585. snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  2586. snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  2587. snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
  2588. snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
  2589. /* reset mixer */
  2590. snd_cmipci_mixer_write(cm, 0, 0);
  2591. free_irq(cm->irq, cm);
  2592. }
  2593. snd_cmipci_free_gameport(cm);
  2594. pci_release_regions(cm->pci);
  2595. pci_disable_device(cm->pci);
  2596. kfree(cm);
  2597. return 0;
  2598. }
  2599. static int snd_cmipci_dev_free(struct snd_device *device)
  2600. {
  2601. struct cmipci *cm = device->device_data;
  2602. return snd_cmipci_free(cm);
  2603. }
  2604. static int __devinit snd_cmipci_create_fm(struct cmipci *cm, long fm_port)
  2605. {
  2606. long iosynth;
  2607. unsigned int val;
  2608. struct snd_opl3 *opl3;
  2609. int err;
  2610. if (!fm_port)
  2611. goto disable_fm;
  2612. if (cm->chip_version >= 39) {
  2613. /* first try FM regs in PCI port range */
  2614. iosynth = cm->iobase + CM_REG_FM_PCI;
  2615. err = snd_opl3_create(cm->card, iosynth, iosynth + 2,
  2616. OPL3_HW_OPL3, 1, &opl3);
  2617. } else {
  2618. err = -EIO;
  2619. }
  2620. if (err < 0) {
  2621. /* then try legacy ports */
  2622. val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL) & ~CM_FMSEL_MASK;
  2623. iosynth = fm_port;
  2624. switch (iosynth) {
  2625. case 0x3E8: val |= CM_FMSEL_3E8; break;
  2626. case 0x3E0: val |= CM_FMSEL_3E0; break;
  2627. case 0x3C8: val |= CM_FMSEL_3C8; break;
  2628. case 0x388: val |= CM_FMSEL_388; break;
  2629. default:
  2630. goto disable_fm;
  2631. }
  2632. snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
  2633. /* enable FM */
  2634. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  2635. if (snd_opl3_create(cm->card, iosynth, iosynth + 2,
  2636. OPL3_HW_OPL3, 0, &opl3) < 0) {
  2637. printk(KERN_ERR "cmipci: no OPL device at %#lx, "
  2638. "skipping...\n", iosynth);
  2639. goto disable_fm;
  2640. }
  2641. }
  2642. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  2643. printk(KERN_ERR "cmipci: cannot create OPL3 hwdep\n");
  2644. return err;
  2645. }
  2646. return 0;
  2647. disable_fm:
  2648. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_FMSEL_MASK);
  2649. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  2650. return 0;
  2651. }
  2652. static int __devinit snd_cmipci_create(struct snd_card *card, struct pci_dev *pci,
  2653. int dev, struct cmipci **rcmipci)
  2654. {
  2655. struct cmipci *cm;
  2656. int err;
  2657. static struct snd_device_ops ops = {
  2658. .dev_free = snd_cmipci_dev_free,
  2659. };
  2660. unsigned int val;
  2661. long iomidi = 0;
  2662. int integrated_midi = 0;
  2663. char modelstr[16];
  2664. int pcm_index, pcm_spdif_index;
  2665. static struct pci_device_id intel_82437vx[] = {
  2666. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437VX) },
  2667. { },
  2668. };
  2669. *rcmipci = NULL;
  2670. if ((err = pci_enable_device(pci)) < 0)
  2671. return err;
  2672. cm = kzalloc(sizeof(*cm), GFP_KERNEL);
  2673. if (cm == NULL) {
  2674. pci_disable_device(pci);
  2675. return -ENOMEM;
  2676. }
  2677. spin_lock_init(&cm->reg_lock);
  2678. mutex_init(&cm->open_mutex);
  2679. cm->device = pci->device;
  2680. cm->card = card;
  2681. cm->pci = pci;
  2682. cm->irq = -1;
  2683. cm->channel[0].ch = 0;
  2684. cm->channel[1].ch = 1;
  2685. cm->channel[0].is_dac = cm->channel[1].is_dac = 1; /* dual DAC mode */
  2686. if ((err = pci_request_regions(pci, card->driver)) < 0) {
  2687. kfree(cm);
  2688. pci_disable_device(pci);
  2689. return err;
  2690. }
  2691. cm->iobase = pci_resource_start(pci, 0);
  2692. if (request_irq(pci->irq, snd_cmipci_interrupt,
  2693. IRQF_SHARED, card->driver, cm)) {
  2694. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  2695. snd_cmipci_free(cm);
  2696. return -EBUSY;
  2697. }
  2698. cm->irq = pci->irq;
  2699. pci_set_master(cm->pci);
  2700. /*
  2701. * check chip version, max channels and capabilities
  2702. */
  2703. cm->chip_version = 0;
  2704. cm->max_channels = 2;
  2705. cm->do_soft_ac3 = soft_ac3[dev];
  2706. if (pci->device != PCI_DEVICE_ID_CMEDIA_CM8338A &&
  2707. pci->device != PCI_DEVICE_ID_CMEDIA_CM8338B)
  2708. query_chip(cm);
  2709. /* added -MCx suffix for chip supporting multi-channels */
  2710. if (cm->can_multi_ch)
  2711. sprintf(cm->card->driver + strlen(cm->card->driver),
  2712. "-MC%d", cm->max_channels);
  2713. else if (cm->can_ac3_sw)
  2714. strcpy(cm->card->driver + strlen(cm->card->driver), "-SWIEC");
  2715. cm->dig_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  2716. cm->dig_pcm_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  2717. #if CM_CH_PLAY == 1
  2718. cm->ctrl = CM_CHADC0; /* default FUNCNTRL0 */
  2719. #else
  2720. cm->ctrl = CM_CHADC1; /* default FUNCNTRL0 */
  2721. #endif
  2722. /* initialize codec registers */
  2723. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
  2724. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
  2725. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
  2726. snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  2727. snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  2728. snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
  2729. snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
  2730. snd_cmipci_write(cm, CM_REG_CHFORMAT, 0);
  2731. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC|CM_N4SPK3D);
  2732. #if CM_CH_PLAY == 1
  2733. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  2734. #else
  2735. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  2736. #endif
  2737. if (cm->chip_version) {
  2738. snd_cmipci_write_b(cm, CM_REG_EXT_MISC, 0x20); /* magic */
  2739. snd_cmipci_write_b(cm, CM_REG_EXT_MISC + 1, 0x09); /* more magic */
  2740. }
  2741. /* Set Bus Master Request */
  2742. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_BREQ);
  2743. /* Assume TX and compatible chip set (Autodetection required for VX chip sets) */
  2744. switch (pci->device) {
  2745. case PCI_DEVICE_ID_CMEDIA_CM8738:
  2746. case PCI_DEVICE_ID_CMEDIA_CM8738B:
  2747. if (!pci_dev_present(intel_82437vx))
  2748. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_TXVX);
  2749. break;
  2750. default:
  2751. break;
  2752. }
  2753. if (cm->chip_version < 68) {
  2754. val = pci->device < 0x110 ? 8338 : 8738;
  2755. } else {
  2756. switch (snd_cmipci_read_b(cm, CM_REG_INT_HLDCLR + 3) & 0x03) {
  2757. case 0:
  2758. val = 8769;
  2759. break;
  2760. case 2:
  2761. val = 8762;
  2762. break;
  2763. default:
  2764. switch ((pci->subsystem_vendor << 16) |
  2765. pci->subsystem_device) {
  2766. case 0x13f69761:
  2767. case 0x584d3741:
  2768. case 0x584d3751:
  2769. case 0x584d3761:
  2770. case 0x584d3771:
  2771. case 0x72848384:
  2772. val = 8770;
  2773. break;
  2774. default:
  2775. val = 8768;
  2776. break;
  2777. }
  2778. }
  2779. }
  2780. sprintf(card->shortname, "C-Media CMI%d", val);
  2781. if (cm->chip_version < 68)
  2782. sprintf(modelstr, " (model %d)", cm->chip_version);
  2783. else
  2784. modelstr[0] = '\0';
  2785. sprintf(card->longname, "%s%s at %#lx, irq %i",
  2786. card->shortname, modelstr, cm->iobase, cm->irq);
  2787. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, cm, &ops)) < 0) {
  2788. snd_cmipci_free(cm);
  2789. return err;
  2790. }
  2791. if (cm->chip_version >= 39) {
  2792. val = snd_cmipci_read_b(cm, CM_REG_MPU_PCI + 1);
  2793. if (val != 0x00 && val != 0xff) {
  2794. iomidi = cm->iobase + CM_REG_MPU_PCI;
  2795. integrated_midi = 1;
  2796. }
  2797. }
  2798. if (!integrated_midi) {
  2799. val = 0;
  2800. iomidi = mpu_port[dev];
  2801. switch (iomidi) {
  2802. case 0x320: val = CM_VMPU_320; break;
  2803. case 0x310: val = CM_VMPU_310; break;
  2804. case 0x300: val = CM_VMPU_300; break;
  2805. case 0x330: val = CM_VMPU_330; break;
  2806. default:
  2807. iomidi = 0; break;
  2808. }
  2809. if (iomidi > 0) {
  2810. snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
  2811. /* enable UART */
  2812. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_UART_EN);
  2813. if (inb(iomidi + 1) == 0xff) {
  2814. snd_printk(KERN_ERR "cannot enable MPU-401 port"
  2815. " at %#lx\n", iomidi);
  2816. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1,
  2817. CM_UART_EN);
  2818. iomidi = 0;
  2819. }
  2820. }
  2821. }
  2822. if (cm->chip_version < 68) {
  2823. err = snd_cmipci_create_fm(cm, fm_port[dev]);
  2824. if (err < 0)
  2825. return err;
  2826. }
  2827. /* reset mixer */
  2828. snd_cmipci_mixer_write(cm, 0, 0);
  2829. snd_cmipci_proc_init(cm);
  2830. /* create pcm devices */
  2831. pcm_index = pcm_spdif_index = 0;
  2832. if ((err = snd_cmipci_pcm_new(cm, pcm_index)) < 0)
  2833. return err;
  2834. pcm_index++;
  2835. if ((err = snd_cmipci_pcm2_new(cm, pcm_index)) < 0)
  2836. return err;
  2837. pcm_index++;
  2838. if (cm->can_ac3_hw || cm->can_ac3_sw) {
  2839. pcm_spdif_index = pcm_index;
  2840. if ((err = snd_cmipci_pcm_spdif_new(cm, pcm_index)) < 0)
  2841. return err;
  2842. }
  2843. /* create mixer interface & switches */
  2844. if ((err = snd_cmipci_mixer_new(cm, pcm_spdif_index)) < 0)
  2845. return err;
  2846. if (iomidi > 0) {
  2847. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
  2848. iomidi,
  2849. (integrated_midi ?
  2850. MPU401_INFO_INTEGRATED : 0),
  2851. cm->irq, 0, &cm->rmidi)) < 0) {
  2852. printk(KERN_ERR "cmipci: no UART401 device at 0x%lx\n", iomidi);
  2853. }
  2854. }
  2855. #ifdef USE_VAR48KRATE
  2856. for (val = 0; val < ARRAY_SIZE(rates); val++)
  2857. snd_cmipci_set_pll(cm, rates[val], val);
  2858. /*
  2859. * (Re-)Enable external switch spdo_48k
  2860. */
  2861. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K|CM_SPDF_AC97);
  2862. #endif /* USE_VAR48KRATE */
  2863. if (snd_cmipci_create_gameport(cm, dev) < 0)
  2864. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  2865. snd_card_set_dev(card, &pci->dev);
  2866. *rcmipci = cm;
  2867. return 0;
  2868. }
  2869. /*
  2870. */
  2871. MODULE_DEVICE_TABLE(pci, snd_cmipci_ids);
  2872. static int __devinit snd_cmipci_probe(struct pci_dev *pci,
  2873. const struct pci_device_id *pci_id)
  2874. {
  2875. static int dev;
  2876. struct snd_card *card;
  2877. struct cmipci *cm;
  2878. int err;
  2879. if (dev >= SNDRV_CARDS)
  2880. return -ENODEV;
  2881. if (! enable[dev]) {
  2882. dev++;
  2883. return -ENOENT;
  2884. }
  2885. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  2886. if (err < 0)
  2887. return err;
  2888. switch (pci->device) {
  2889. case PCI_DEVICE_ID_CMEDIA_CM8738:
  2890. case PCI_DEVICE_ID_CMEDIA_CM8738B:
  2891. strcpy(card->driver, "CMI8738");
  2892. break;
  2893. case PCI_DEVICE_ID_CMEDIA_CM8338A:
  2894. case PCI_DEVICE_ID_CMEDIA_CM8338B:
  2895. strcpy(card->driver, "CMI8338");
  2896. break;
  2897. default:
  2898. strcpy(card->driver, "CMIPCI");
  2899. break;
  2900. }
  2901. if ((err = snd_cmipci_create(card, pci, dev, &cm)) < 0) {
  2902. snd_card_free(card);
  2903. return err;
  2904. }
  2905. card->private_data = cm;
  2906. if ((err = snd_card_register(card)) < 0) {
  2907. snd_card_free(card);
  2908. return err;
  2909. }
  2910. pci_set_drvdata(pci, card);
  2911. dev++;
  2912. return 0;
  2913. }
  2914. static void __devexit snd_cmipci_remove(struct pci_dev *pci)
  2915. {
  2916. snd_card_free(pci_get_drvdata(pci));
  2917. pci_set_drvdata(pci, NULL);
  2918. }
  2919. #ifdef CONFIG_PM
  2920. /*
  2921. * power management
  2922. */
  2923. static unsigned char saved_regs[] = {
  2924. CM_REG_FUNCTRL1, CM_REG_CHFORMAT, CM_REG_LEGACY_CTRL, CM_REG_MISC_CTRL,
  2925. CM_REG_MIXER0, CM_REG_MIXER1, CM_REG_MIXER2, CM_REG_MIXER3, CM_REG_PLL,
  2926. CM_REG_CH0_FRAME1, CM_REG_CH0_FRAME2,
  2927. CM_REG_CH1_FRAME1, CM_REG_CH1_FRAME2, CM_REG_EXT_MISC,
  2928. CM_REG_INT_STATUS, CM_REG_INT_HLDCLR, CM_REG_FUNCTRL0,
  2929. };
  2930. static unsigned char saved_mixers[] = {
  2931. SB_DSP4_MASTER_DEV, SB_DSP4_MASTER_DEV + 1,
  2932. SB_DSP4_PCM_DEV, SB_DSP4_PCM_DEV + 1,
  2933. SB_DSP4_SYNTH_DEV, SB_DSP4_SYNTH_DEV + 1,
  2934. SB_DSP4_CD_DEV, SB_DSP4_CD_DEV + 1,
  2935. SB_DSP4_LINE_DEV, SB_DSP4_LINE_DEV + 1,
  2936. SB_DSP4_MIC_DEV, SB_DSP4_SPEAKER_DEV,
  2937. CM_REG_EXTENT_IND, SB_DSP4_OUTPUT_SW,
  2938. SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT,
  2939. };
  2940. static int snd_cmipci_suspend(struct pci_dev *pci, pm_message_t state)
  2941. {
  2942. struct snd_card *card = pci_get_drvdata(pci);
  2943. struct cmipci *cm = card->private_data;
  2944. int i;
  2945. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  2946. snd_pcm_suspend_all(cm->pcm);
  2947. snd_pcm_suspend_all(cm->pcm2);
  2948. snd_pcm_suspend_all(cm->pcm_spdif);
  2949. /* save registers */
  2950. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  2951. cm->saved_regs[i] = snd_cmipci_read(cm, saved_regs[i]);
  2952. for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
  2953. cm->saved_mixers[i] = snd_cmipci_mixer_read(cm, saved_mixers[i]);
  2954. /* disable ints */
  2955. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
  2956. pci_disable_device(pci);
  2957. pci_save_state(pci);
  2958. pci_set_power_state(pci, pci_choose_state(pci, state));
  2959. return 0;
  2960. }
  2961. static int snd_cmipci_resume(struct pci_dev *pci)
  2962. {
  2963. struct snd_card *card = pci_get_drvdata(pci);
  2964. struct cmipci *cm = card->private_data;
  2965. int i;
  2966. pci_set_power_state(pci, PCI_D0);
  2967. pci_restore_state(pci);
  2968. if (pci_enable_device(pci) < 0) {
  2969. printk(KERN_ERR "cmipci: pci_enable_device failed, "
  2970. "disabling device\n");
  2971. snd_card_disconnect(card);
  2972. return -EIO;
  2973. }
  2974. pci_set_master(pci);
  2975. /* reset / initialize to a sane state */
  2976. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
  2977. snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  2978. snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  2979. snd_cmipci_mixer_write(cm, 0, 0);
  2980. /* restore registers */
  2981. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  2982. snd_cmipci_write(cm, saved_regs[i], cm->saved_regs[i]);
  2983. for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
  2984. snd_cmipci_mixer_write(cm, saved_mixers[i], cm->saved_mixers[i]);
  2985. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  2986. return 0;
  2987. }
  2988. #endif /* CONFIG_PM */
  2989. static struct pci_driver driver = {
  2990. .name = "C-Media PCI",
  2991. .id_table = snd_cmipci_ids,
  2992. .probe = snd_cmipci_probe,
  2993. .remove = __devexit_p(snd_cmipci_remove),
  2994. #ifdef CONFIG_PM
  2995. .suspend = snd_cmipci_suspend,
  2996. .resume = snd_cmipci_resume,
  2997. #endif
  2998. };
  2999. static int __init alsa_card_cmipci_init(void)
  3000. {
  3001. return pci_register_driver(&driver);
  3002. }
  3003. static void __exit alsa_card_cmipci_exit(void)
  3004. {
  3005. pci_unregister_driver(&driver);
  3006. }
  3007. module_init(alsa_card_cmipci_init)
  3008. module_exit(alsa_card_cmipci_exit)