cmipci.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424
  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/module.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 bool 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 bool 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, "Software-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 up to 0xff
  574. * are mapped onto the 8 ADC/DAC sampling frequency which can be chosen
  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 up to 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, rem, tries;
  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. for (tries = 0; tries < 3; tries++) {
  836. rem = snd_cmipci_read_w(cm, reg);
  837. if (rem < rec->dma_size)
  838. goto ok;
  839. }
  840. printk(KERN_ERR "cmipci: invalid PCM pointer: %#x\n", rem);
  841. return SNDRV_PCM_POS_XRUN;
  842. ok:
  843. ptr = (rec->dma_size - (rem + 1)) >> rec->shift;
  844. #else
  845. reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
  846. ptr = snd_cmipci_read(cm, reg) - rec->offset;
  847. ptr = bytes_to_frames(substream->runtime, ptr);
  848. #endif
  849. if (substream->runtime->channels > 2)
  850. ptr = (ptr * 2) / substream->runtime->channels;
  851. return ptr;
  852. }
  853. /*
  854. * playback
  855. */
  856. static int snd_cmipci_playback_trigger(struct snd_pcm_substream *substream,
  857. int cmd)
  858. {
  859. struct cmipci *cm = snd_pcm_substream_chip(substream);
  860. return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], cmd);
  861. }
  862. static snd_pcm_uframes_t snd_cmipci_playback_pointer(struct snd_pcm_substream *substream)
  863. {
  864. struct cmipci *cm = snd_pcm_substream_chip(substream);
  865. return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_PLAY], substream);
  866. }
  867. /*
  868. * capture
  869. */
  870. static int snd_cmipci_capture_trigger(struct snd_pcm_substream *substream,
  871. int cmd)
  872. {
  873. struct cmipci *cm = snd_pcm_substream_chip(substream);
  874. return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], cmd);
  875. }
  876. static snd_pcm_uframes_t snd_cmipci_capture_pointer(struct snd_pcm_substream *substream)
  877. {
  878. struct cmipci *cm = snd_pcm_substream_chip(substream);
  879. return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_CAPT], substream);
  880. }
  881. /*
  882. * hw preparation for spdif
  883. */
  884. static int snd_cmipci_spdif_default_info(struct snd_kcontrol *kcontrol,
  885. struct snd_ctl_elem_info *uinfo)
  886. {
  887. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  888. uinfo->count = 1;
  889. return 0;
  890. }
  891. static int snd_cmipci_spdif_default_get(struct snd_kcontrol *kcontrol,
  892. struct snd_ctl_elem_value *ucontrol)
  893. {
  894. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  895. int i;
  896. spin_lock_irq(&chip->reg_lock);
  897. for (i = 0; i < 4; i++)
  898. ucontrol->value.iec958.status[i] = (chip->dig_status >> (i * 8)) & 0xff;
  899. spin_unlock_irq(&chip->reg_lock);
  900. return 0;
  901. }
  902. static int snd_cmipci_spdif_default_put(struct snd_kcontrol *kcontrol,
  903. struct snd_ctl_elem_value *ucontrol)
  904. {
  905. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  906. int i, change;
  907. unsigned int val;
  908. val = 0;
  909. spin_lock_irq(&chip->reg_lock);
  910. for (i = 0; i < 4; i++)
  911. val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  912. change = val != chip->dig_status;
  913. chip->dig_status = val;
  914. spin_unlock_irq(&chip->reg_lock);
  915. return change;
  916. }
  917. static struct snd_kcontrol_new snd_cmipci_spdif_default __devinitdata =
  918. {
  919. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  920. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  921. .info = snd_cmipci_spdif_default_info,
  922. .get = snd_cmipci_spdif_default_get,
  923. .put = snd_cmipci_spdif_default_put
  924. };
  925. static int snd_cmipci_spdif_mask_info(struct snd_kcontrol *kcontrol,
  926. struct snd_ctl_elem_info *uinfo)
  927. {
  928. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  929. uinfo->count = 1;
  930. return 0;
  931. }
  932. static int snd_cmipci_spdif_mask_get(struct snd_kcontrol *kcontrol,
  933. struct snd_ctl_elem_value *ucontrol)
  934. {
  935. ucontrol->value.iec958.status[0] = 0xff;
  936. ucontrol->value.iec958.status[1] = 0xff;
  937. ucontrol->value.iec958.status[2] = 0xff;
  938. ucontrol->value.iec958.status[3] = 0xff;
  939. return 0;
  940. }
  941. static struct snd_kcontrol_new snd_cmipci_spdif_mask __devinitdata =
  942. {
  943. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  944. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  945. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  946. .info = snd_cmipci_spdif_mask_info,
  947. .get = snd_cmipci_spdif_mask_get,
  948. };
  949. static int snd_cmipci_spdif_stream_info(struct snd_kcontrol *kcontrol,
  950. struct snd_ctl_elem_info *uinfo)
  951. {
  952. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  953. uinfo->count = 1;
  954. return 0;
  955. }
  956. static int snd_cmipci_spdif_stream_get(struct snd_kcontrol *kcontrol,
  957. struct snd_ctl_elem_value *ucontrol)
  958. {
  959. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  960. int i;
  961. spin_lock_irq(&chip->reg_lock);
  962. for (i = 0; i < 4; i++)
  963. ucontrol->value.iec958.status[i] = (chip->dig_pcm_status >> (i * 8)) & 0xff;
  964. spin_unlock_irq(&chip->reg_lock);
  965. return 0;
  966. }
  967. static int snd_cmipci_spdif_stream_put(struct snd_kcontrol *kcontrol,
  968. struct snd_ctl_elem_value *ucontrol)
  969. {
  970. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  971. int i, change;
  972. unsigned int val;
  973. val = 0;
  974. spin_lock_irq(&chip->reg_lock);
  975. for (i = 0; i < 4; i++)
  976. val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  977. change = val != chip->dig_pcm_status;
  978. chip->dig_pcm_status = val;
  979. spin_unlock_irq(&chip->reg_lock);
  980. return change;
  981. }
  982. static struct snd_kcontrol_new snd_cmipci_spdif_stream __devinitdata =
  983. {
  984. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  985. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  986. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  987. .info = snd_cmipci_spdif_stream_info,
  988. .get = snd_cmipci_spdif_stream_get,
  989. .put = snd_cmipci_spdif_stream_put
  990. };
  991. /*
  992. */
  993. /* save mixer setting and mute for AC3 playback */
  994. static int save_mixer_state(struct cmipci *cm)
  995. {
  996. if (! cm->mixer_insensitive) {
  997. struct snd_ctl_elem_value *val;
  998. unsigned int i;
  999. val = kmalloc(sizeof(*val), GFP_ATOMIC);
  1000. if (!val)
  1001. return -ENOMEM;
  1002. for (i = 0; i < CM_SAVED_MIXERS; i++) {
  1003. struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
  1004. if (ctl) {
  1005. int event;
  1006. memset(val, 0, sizeof(*val));
  1007. ctl->get(ctl, val);
  1008. cm->mixer_res_status[i] = val->value.integer.value[0];
  1009. val->value.integer.value[0] = cm_saved_mixer[i].toggle_on;
  1010. event = SNDRV_CTL_EVENT_MASK_INFO;
  1011. if (cm->mixer_res_status[i] != val->value.integer.value[0]) {
  1012. ctl->put(ctl, val); /* toggle */
  1013. event |= SNDRV_CTL_EVENT_MASK_VALUE;
  1014. }
  1015. ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1016. snd_ctl_notify(cm->card, event, &ctl->id);
  1017. }
  1018. }
  1019. kfree(val);
  1020. cm->mixer_insensitive = 1;
  1021. }
  1022. return 0;
  1023. }
  1024. /* restore the previously saved mixer status */
  1025. static void restore_mixer_state(struct cmipci *cm)
  1026. {
  1027. if (cm->mixer_insensitive) {
  1028. struct snd_ctl_elem_value *val;
  1029. unsigned int i;
  1030. val = kmalloc(sizeof(*val), GFP_KERNEL);
  1031. if (!val)
  1032. return;
  1033. cm->mixer_insensitive = 0; /* at first clear this;
  1034. otherwise the changes will be ignored */
  1035. for (i = 0; i < CM_SAVED_MIXERS; i++) {
  1036. struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
  1037. if (ctl) {
  1038. int event;
  1039. memset(val, 0, sizeof(*val));
  1040. ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1041. ctl->get(ctl, val);
  1042. event = SNDRV_CTL_EVENT_MASK_INFO;
  1043. if (val->value.integer.value[0] != cm->mixer_res_status[i]) {
  1044. val->value.integer.value[0] = cm->mixer_res_status[i];
  1045. ctl->put(ctl, val);
  1046. event |= SNDRV_CTL_EVENT_MASK_VALUE;
  1047. }
  1048. snd_ctl_notify(cm->card, event, &ctl->id);
  1049. }
  1050. }
  1051. kfree(val);
  1052. }
  1053. }
  1054. /* spinlock held! */
  1055. static void setup_ac3(struct cmipci *cm, struct snd_pcm_substream *subs, int do_ac3, int rate)
  1056. {
  1057. if (do_ac3) {
  1058. /* AC3EN for 037 */
  1059. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
  1060. /* AC3EN for 039 */
  1061. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
  1062. if (cm->can_ac3_hw) {
  1063. /* SPD24SEL for 037, 0x02 */
  1064. /* SPD24SEL for 039, 0x20, but cannot be set */
  1065. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1066. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1067. } else { /* can_ac3_sw */
  1068. /* SPD32SEL for 037 & 039, 0x20 */
  1069. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1070. /* set 176K sample rate to fix 033 HW bug */
  1071. if (cm->chip_version == 33) {
  1072. if (rate >= 48000) {
  1073. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  1074. } else {
  1075. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  1076. }
  1077. }
  1078. }
  1079. } else {
  1080. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
  1081. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
  1082. if (cm->can_ac3_hw) {
  1083. /* chip model >= 37 */
  1084. if (snd_pcm_format_width(subs->runtime->format) > 16) {
  1085. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1086. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1087. } else {
  1088. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1089. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1090. }
  1091. } else {
  1092. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1093. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1094. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  1095. }
  1096. }
  1097. }
  1098. static int setup_spdif_playback(struct cmipci *cm, struct snd_pcm_substream *subs, int up, int do_ac3)
  1099. {
  1100. int rate, err;
  1101. rate = subs->runtime->rate;
  1102. if (up && do_ac3)
  1103. if ((err = save_mixer_state(cm)) < 0)
  1104. return err;
  1105. spin_lock_irq(&cm->reg_lock);
  1106. cm->spdif_playback_avail = up;
  1107. if (up) {
  1108. /* they are controlled via "IEC958 Output Switch" */
  1109. /* snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
  1110. /* snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
  1111. if (cm->spdif_playback_enabled)
  1112. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  1113. setup_ac3(cm, subs, do_ac3, rate);
  1114. if (rate == 48000 || rate == 96000)
  1115. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
  1116. else
  1117. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
  1118. if (rate > 48000)
  1119. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  1120. else
  1121. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  1122. } else {
  1123. /* they are controlled via "IEC958 Output Switch" */
  1124. /* snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
  1125. /* snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
  1126. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  1127. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  1128. setup_ac3(cm, subs, 0, 0);
  1129. }
  1130. spin_unlock_irq(&cm->reg_lock);
  1131. return 0;
  1132. }
  1133. /*
  1134. * preparation
  1135. */
  1136. /* playback - enable spdif only on the certain condition */
  1137. static int snd_cmipci_playback_prepare(struct snd_pcm_substream *substream)
  1138. {
  1139. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1140. int rate = substream->runtime->rate;
  1141. int err, do_spdif, do_ac3 = 0;
  1142. do_spdif = (rate >= 44100 && rate <= 96000 &&
  1143. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE &&
  1144. substream->runtime->channels == 2);
  1145. if (do_spdif && cm->can_ac3_hw)
  1146. do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
  1147. if ((err = setup_spdif_playback(cm, substream, do_spdif, do_ac3)) < 0)
  1148. return err;
  1149. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
  1150. }
  1151. /* playback (via device #2) - enable spdif always */
  1152. static int snd_cmipci_playback_spdif_prepare(struct snd_pcm_substream *substream)
  1153. {
  1154. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1155. int err, do_ac3;
  1156. if (cm->can_ac3_hw)
  1157. do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
  1158. else
  1159. do_ac3 = 1; /* doesn't matter */
  1160. if ((err = setup_spdif_playback(cm, substream, 1, do_ac3)) < 0)
  1161. return err;
  1162. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
  1163. }
  1164. /*
  1165. * Apparently, the samples last played on channel A stay in some buffer, even
  1166. * after the channel is reset, and get added to the data for the rear DACs when
  1167. * playing a multichannel stream on channel B. This is likely to generate
  1168. * wraparounds and thus distortions.
  1169. * To avoid this, we play at least one zero sample after the actual stream has
  1170. * stopped.
  1171. */
  1172. static void snd_cmipci_silence_hack(struct cmipci *cm, struct cmipci_pcm *rec)
  1173. {
  1174. struct snd_pcm_runtime *runtime = rec->substream->runtime;
  1175. unsigned int reg, val;
  1176. if (rec->needs_silencing && runtime && runtime->dma_area) {
  1177. /* set up a small silence buffer */
  1178. memset(runtime->dma_area, 0, PAGE_SIZE);
  1179. reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
  1180. val = ((PAGE_SIZE / 4) - 1) | (((PAGE_SIZE / 4) / 2 - 1) << 16);
  1181. snd_cmipci_write(cm, reg, val);
  1182. /* configure for 16 bits, 2 channels, 8 kHz */
  1183. if (runtime->channels > 2)
  1184. set_dac_channels(cm, rec, 2);
  1185. spin_lock_irq(&cm->reg_lock);
  1186. val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
  1187. val &= ~(CM_ASFC_MASK << (rec->ch * 3));
  1188. val |= (4 << CM_ASFC_SHIFT) << (rec->ch * 3);
  1189. snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
  1190. val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
  1191. val &= ~(CM_CH0FMT_MASK << (rec->ch * 2));
  1192. val |= (3 << CM_CH0FMT_SHIFT) << (rec->ch * 2);
  1193. if (cm->can_96k)
  1194. val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
  1195. snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
  1196. /* start stream (we don't need interrupts) */
  1197. cm->ctrl |= CM_CHEN0 << rec->ch;
  1198. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  1199. spin_unlock_irq(&cm->reg_lock);
  1200. msleep(1);
  1201. /* stop and reset stream */
  1202. spin_lock_irq(&cm->reg_lock);
  1203. cm->ctrl &= ~(CM_CHEN0 << rec->ch);
  1204. val = CM_RST_CH0 << rec->ch;
  1205. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | val);
  1206. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~val);
  1207. spin_unlock_irq(&cm->reg_lock);
  1208. rec->needs_silencing = 0;
  1209. }
  1210. }
  1211. static int snd_cmipci_playback_hw_free(struct snd_pcm_substream *substream)
  1212. {
  1213. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1214. setup_spdif_playback(cm, substream, 0, 0);
  1215. restore_mixer_state(cm);
  1216. snd_cmipci_silence_hack(cm, &cm->channel[0]);
  1217. return snd_cmipci_hw_free(substream);
  1218. }
  1219. static int snd_cmipci_playback2_hw_free(struct snd_pcm_substream *substream)
  1220. {
  1221. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1222. snd_cmipci_silence_hack(cm, &cm->channel[1]);
  1223. return snd_cmipci_hw_free(substream);
  1224. }
  1225. /* capture */
  1226. static int snd_cmipci_capture_prepare(struct snd_pcm_substream *substream)
  1227. {
  1228. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1229. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
  1230. }
  1231. /* capture with spdif (via device #2) */
  1232. static int snd_cmipci_capture_spdif_prepare(struct snd_pcm_substream *substream)
  1233. {
  1234. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1235. spin_lock_irq(&cm->reg_lock);
  1236. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
  1237. if (cm->can_96k) {
  1238. if (substream->runtime->rate > 48000)
  1239. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  1240. else
  1241. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
  1242. }
  1243. if (snd_pcm_format_width(substream->runtime->format) > 16)
  1244. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1245. else
  1246. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1247. spin_unlock_irq(&cm->reg_lock);
  1248. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
  1249. }
  1250. static int snd_cmipci_capture_spdif_hw_free(struct snd_pcm_substream *subs)
  1251. {
  1252. struct cmipci *cm = snd_pcm_substream_chip(subs);
  1253. spin_lock_irq(&cm->reg_lock);
  1254. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
  1255. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1256. spin_unlock_irq(&cm->reg_lock);
  1257. return snd_cmipci_hw_free(subs);
  1258. }
  1259. /*
  1260. * interrupt handler
  1261. */
  1262. static irqreturn_t snd_cmipci_interrupt(int irq, void *dev_id)
  1263. {
  1264. struct cmipci *cm = dev_id;
  1265. unsigned int status, mask = 0;
  1266. /* fastpath out, to ease interrupt sharing */
  1267. status = snd_cmipci_read(cm, CM_REG_INT_STATUS);
  1268. if (!(status & CM_INTR))
  1269. return IRQ_NONE;
  1270. /* acknowledge interrupt */
  1271. spin_lock(&cm->reg_lock);
  1272. if (status & CM_CHINT0)
  1273. mask |= CM_CH0_INT_EN;
  1274. if (status & CM_CHINT1)
  1275. mask |= CM_CH1_INT_EN;
  1276. snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, mask);
  1277. snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, mask);
  1278. spin_unlock(&cm->reg_lock);
  1279. if (cm->rmidi && (status & CM_UARTINT))
  1280. snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data);
  1281. if (cm->pcm) {
  1282. if ((status & CM_CHINT0) && cm->channel[0].running)
  1283. snd_pcm_period_elapsed(cm->channel[0].substream);
  1284. if ((status & CM_CHINT1) && cm->channel[1].running)
  1285. snd_pcm_period_elapsed(cm->channel[1].substream);
  1286. }
  1287. return IRQ_HANDLED;
  1288. }
  1289. /*
  1290. * h/w infos
  1291. */
  1292. /* playback on channel A */
  1293. static struct snd_pcm_hardware snd_cmipci_playback =
  1294. {
  1295. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1296. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1297. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1298. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1299. .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  1300. .rate_min = 5512,
  1301. .rate_max = 48000,
  1302. .channels_min = 1,
  1303. .channels_max = 2,
  1304. .buffer_bytes_max = (128*1024),
  1305. .period_bytes_min = 64,
  1306. .period_bytes_max = (128*1024),
  1307. .periods_min = 2,
  1308. .periods_max = 1024,
  1309. .fifo_size = 0,
  1310. };
  1311. /* capture on channel B */
  1312. static struct snd_pcm_hardware snd_cmipci_capture =
  1313. {
  1314. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1315. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1316. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1317. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1318. .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  1319. .rate_min = 5512,
  1320. .rate_max = 48000,
  1321. .channels_min = 1,
  1322. .channels_max = 2,
  1323. .buffer_bytes_max = (128*1024),
  1324. .period_bytes_min = 64,
  1325. .period_bytes_max = (128*1024),
  1326. .periods_min = 2,
  1327. .periods_max = 1024,
  1328. .fifo_size = 0,
  1329. };
  1330. /* playback on channel B - stereo 16bit only? */
  1331. static struct snd_pcm_hardware snd_cmipci_playback2 =
  1332. {
  1333. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1334. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1335. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1336. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1337. .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  1338. .rate_min = 5512,
  1339. .rate_max = 48000,
  1340. .channels_min = 2,
  1341. .channels_max = 2,
  1342. .buffer_bytes_max = (128*1024),
  1343. .period_bytes_min = 64,
  1344. .period_bytes_max = (128*1024),
  1345. .periods_min = 2,
  1346. .periods_max = 1024,
  1347. .fifo_size = 0,
  1348. };
  1349. /* spdif playback on channel A */
  1350. static struct snd_pcm_hardware snd_cmipci_playback_spdif =
  1351. {
  1352. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1353. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1354. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1355. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1356. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  1357. .rate_min = 44100,
  1358. .rate_max = 48000,
  1359. .channels_min = 2,
  1360. .channels_max = 2,
  1361. .buffer_bytes_max = (128*1024),
  1362. .period_bytes_min = 64,
  1363. .period_bytes_max = (128*1024),
  1364. .periods_min = 2,
  1365. .periods_max = 1024,
  1366. .fifo_size = 0,
  1367. };
  1368. /* spdif playback on channel A (32bit, IEC958 subframes) */
  1369. static struct snd_pcm_hardware snd_cmipci_playback_iec958_subframe =
  1370. {
  1371. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1372. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1373. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1374. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  1375. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  1376. .rate_min = 44100,
  1377. .rate_max = 48000,
  1378. .channels_min = 2,
  1379. .channels_max = 2,
  1380. .buffer_bytes_max = (128*1024),
  1381. .period_bytes_min = 64,
  1382. .period_bytes_max = (128*1024),
  1383. .periods_min = 2,
  1384. .periods_max = 1024,
  1385. .fifo_size = 0,
  1386. };
  1387. /* spdif capture on channel B */
  1388. static struct snd_pcm_hardware snd_cmipci_capture_spdif =
  1389. {
  1390. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1391. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1392. SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
  1393. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  1394. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  1395. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  1396. .rate_min = 44100,
  1397. .rate_max = 48000,
  1398. .channels_min = 2,
  1399. .channels_max = 2,
  1400. .buffer_bytes_max = (128*1024),
  1401. .period_bytes_min = 64,
  1402. .period_bytes_max = (128*1024),
  1403. .periods_min = 2,
  1404. .periods_max = 1024,
  1405. .fifo_size = 0,
  1406. };
  1407. static unsigned int rate_constraints[] = { 5512, 8000, 11025, 16000, 22050,
  1408. 32000, 44100, 48000, 88200, 96000, 128000 };
  1409. static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  1410. .count = ARRAY_SIZE(rate_constraints),
  1411. .list = rate_constraints,
  1412. .mask = 0,
  1413. };
  1414. /*
  1415. * check device open/close
  1416. */
  1417. static int open_device_check(struct cmipci *cm, int mode, struct snd_pcm_substream *subs)
  1418. {
  1419. int ch = mode & CM_OPEN_CH_MASK;
  1420. /* FIXME: a file should wait until the device becomes free
  1421. * when it's opened on blocking mode. however, since the current
  1422. * pcm framework doesn't pass file pointer before actually opened,
  1423. * we can't know whether blocking mode or not in open callback..
  1424. */
  1425. mutex_lock(&cm->open_mutex);
  1426. if (cm->opened[ch]) {
  1427. mutex_unlock(&cm->open_mutex);
  1428. return -EBUSY;
  1429. }
  1430. cm->opened[ch] = mode;
  1431. cm->channel[ch].substream = subs;
  1432. if (! (mode & CM_OPEN_DAC)) {
  1433. /* disable dual DAC mode */
  1434. cm->channel[ch].is_dac = 0;
  1435. spin_lock_irq(&cm->reg_lock);
  1436. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
  1437. spin_unlock_irq(&cm->reg_lock);
  1438. }
  1439. mutex_unlock(&cm->open_mutex);
  1440. return 0;
  1441. }
  1442. static void close_device_check(struct cmipci *cm, int mode)
  1443. {
  1444. int ch = mode & CM_OPEN_CH_MASK;
  1445. mutex_lock(&cm->open_mutex);
  1446. if (cm->opened[ch] == mode) {
  1447. if (cm->channel[ch].substream) {
  1448. snd_cmipci_ch_reset(cm, ch);
  1449. cm->channel[ch].running = 0;
  1450. cm->channel[ch].substream = NULL;
  1451. }
  1452. cm->opened[ch] = 0;
  1453. if (! cm->channel[ch].is_dac) {
  1454. /* enable dual DAC mode again */
  1455. cm->channel[ch].is_dac = 1;
  1456. spin_lock_irq(&cm->reg_lock);
  1457. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
  1458. spin_unlock_irq(&cm->reg_lock);
  1459. }
  1460. }
  1461. mutex_unlock(&cm->open_mutex);
  1462. }
  1463. /*
  1464. */
  1465. static int snd_cmipci_playback_open(struct snd_pcm_substream *substream)
  1466. {
  1467. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1468. struct snd_pcm_runtime *runtime = substream->runtime;
  1469. int err;
  1470. if ((err = open_device_check(cm, CM_OPEN_PLAYBACK, substream)) < 0)
  1471. return err;
  1472. runtime->hw = snd_cmipci_playback;
  1473. if (cm->chip_version == 68) {
  1474. runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
  1475. SNDRV_PCM_RATE_96000;
  1476. runtime->hw.rate_max = 96000;
  1477. } else if (cm->chip_version == 55) {
  1478. err = snd_pcm_hw_constraint_list(runtime, 0,
  1479. SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  1480. if (err < 0)
  1481. return err;
  1482. runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
  1483. runtime->hw.rate_max = 128000;
  1484. }
  1485. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  1486. cm->dig_pcm_status = cm->dig_status;
  1487. return 0;
  1488. }
  1489. static int snd_cmipci_capture_open(struct snd_pcm_substream *substream)
  1490. {
  1491. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1492. struct snd_pcm_runtime *runtime = substream->runtime;
  1493. int err;
  1494. if ((err = open_device_check(cm, CM_OPEN_CAPTURE, substream)) < 0)
  1495. return err;
  1496. runtime->hw = snd_cmipci_capture;
  1497. if (cm->chip_version == 68) { // 8768 only supports 44k/48k recording
  1498. runtime->hw.rate_min = 41000;
  1499. runtime->hw.rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
  1500. } else if (cm->chip_version == 55) {
  1501. err = snd_pcm_hw_constraint_list(runtime, 0,
  1502. SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  1503. if (err < 0)
  1504. return err;
  1505. runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
  1506. runtime->hw.rate_max = 128000;
  1507. }
  1508. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  1509. return 0;
  1510. }
  1511. static int snd_cmipci_playback2_open(struct snd_pcm_substream *substream)
  1512. {
  1513. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1514. struct snd_pcm_runtime *runtime = substream->runtime;
  1515. int err;
  1516. if ((err = open_device_check(cm, CM_OPEN_PLAYBACK2, substream)) < 0) /* use channel B */
  1517. return err;
  1518. runtime->hw = snd_cmipci_playback2;
  1519. mutex_lock(&cm->open_mutex);
  1520. if (! cm->opened[CM_CH_PLAY]) {
  1521. if (cm->can_multi_ch) {
  1522. runtime->hw.channels_max = cm->max_channels;
  1523. if (cm->max_channels == 4)
  1524. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_4);
  1525. else if (cm->max_channels == 6)
  1526. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_6);
  1527. else if (cm->max_channels == 8)
  1528. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_8);
  1529. }
  1530. }
  1531. mutex_unlock(&cm->open_mutex);
  1532. if (cm->chip_version == 68) {
  1533. runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
  1534. SNDRV_PCM_RATE_96000;
  1535. runtime->hw.rate_max = 96000;
  1536. } else if (cm->chip_version == 55) {
  1537. err = snd_pcm_hw_constraint_list(runtime, 0,
  1538. SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  1539. if (err < 0)
  1540. return err;
  1541. runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
  1542. runtime->hw.rate_max = 128000;
  1543. }
  1544. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  1545. return 0;
  1546. }
  1547. static int snd_cmipci_playback_spdif_open(struct snd_pcm_substream *substream)
  1548. {
  1549. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1550. struct snd_pcm_runtime *runtime = substream->runtime;
  1551. int err;
  1552. if ((err = open_device_check(cm, CM_OPEN_SPDIF_PLAYBACK, substream)) < 0) /* use channel A */
  1553. return err;
  1554. if (cm->can_ac3_hw) {
  1555. runtime->hw = snd_cmipci_playback_spdif;
  1556. if (cm->chip_version >= 37) {
  1557. runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1558. snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  1559. }
  1560. if (cm->can_96k) {
  1561. runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
  1562. SNDRV_PCM_RATE_96000;
  1563. runtime->hw.rate_max = 96000;
  1564. }
  1565. } else {
  1566. runtime->hw = snd_cmipci_playback_iec958_subframe;
  1567. }
  1568. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
  1569. cm->dig_pcm_status = cm->dig_status;
  1570. return 0;
  1571. }
  1572. static int snd_cmipci_capture_spdif_open(struct snd_pcm_substream *substream)
  1573. {
  1574. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1575. struct snd_pcm_runtime *runtime = substream->runtime;
  1576. int err;
  1577. if ((err = open_device_check(cm, CM_OPEN_SPDIF_CAPTURE, substream)) < 0) /* use channel B */
  1578. return err;
  1579. runtime->hw = snd_cmipci_capture_spdif;
  1580. if (cm->can_96k && !(cm->chip_version == 68)) {
  1581. runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
  1582. SNDRV_PCM_RATE_96000;
  1583. runtime->hw.rate_max = 96000;
  1584. }
  1585. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
  1586. return 0;
  1587. }
  1588. /*
  1589. */
  1590. static int snd_cmipci_playback_close(struct snd_pcm_substream *substream)
  1591. {
  1592. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1593. close_device_check(cm, CM_OPEN_PLAYBACK);
  1594. return 0;
  1595. }
  1596. static int snd_cmipci_capture_close(struct snd_pcm_substream *substream)
  1597. {
  1598. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1599. close_device_check(cm, CM_OPEN_CAPTURE);
  1600. return 0;
  1601. }
  1602. static int snd_cmipci_playback2_close(struct snd_pcm_substream *substream)
  1603. {
  1604. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1605. close_device_check(cm, CM_OPEN_PLAYBACK2);
  1606. close_device_check(cm, CM_OPEN_PLAYBACK_MULTI);
  1607. return 0;
  1608. }
  1609. static int snd_cmipci_playback_spdif_close(struct snd_pcm_substream *substream)
  1610. {
  1611. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1612. close_device_check(cm, CM_OPEN_SPDIF_PLAYBACK);
  1613. return 0;
  1614. }
  1615. static int snd_cmipci_capture_spdif_close(struct snd_pcm_substream *substream)
  1616. {
  1617. struct cmipci *cm = snd_pcm_substream_chip(substream);
  1618. close_device_check(cm, CM_OPEN_SPDIF_CAPTURE);
  1619. return 0;
  1620. }
  1621. /*
  1622. */
  1623. static struct snd_pcm_ops snd_cmipci_playback_ops = {
  1624. .open = snd_cmipci_playback_open,
  1625. .close = snd_cmipci_playback_close,
  1626. .ioctl = snd_pcm_lib_ioctl,
  1627. .hw_params = snd_cmipci_hw_params,
  1628. .hw_free = snd_cmipci_playback_hw_free,
  1629. .prepare = snd_cmipci_playback_prepare,
  1630. .trigger = snd_cmipci_playback_trigger,
  1631. .pointer = snd_cmipci_playback_pointer,
  1632. };
  1633. static struct snd_pcm_ops snd_cmipci_capture_ops = {
  1634. .open = snd_cmipci_capture_open,
  1635. .close = snd_cmipci_capture_close,
  1636. .ioctl = snd_pcm_lib_ioctl,
  1637. .hw_params = snd_cmipci_hw_params,
  1638. .hw_free = snd_cmipci_hw_free,
  1639. .prepare = snd_cmipci_capture_prepare,
  1640. .trigger = snd_cmipci_capture_trigger,
  1641. .pointer = snd_cmipci_capture_pointer,
  1642. };
  1643. static struct snd_pcm_ops snd_cmipci_playback2_ops = {
  1644. .open = snd_cmipci_playback2_open,
  1645. .close = snd_cmipci_playback2_close,
  1646. .ioctl = snd_pcm_lib_ioctl,
  1647. .hw_params = snd_cmipci_playback2_hw_params,
  1648. .hw_free = snd_cmipci_playback2_hw_free,
  1649. .prepare = snd_cmipci_capture_prepare, /* channel B */
  1650. .trigger = snd_cmipci_capture_trigger, /* channel B */
  1651. .pointer = snd_cmipci_capture_pointer, /* channel B */
  1652. };
  1653. static struct snd_pcm_ops snd_cmipci_playback_spdif_ops = {
  1654. .open = snd_cmipci_playback_spdif_open,
  1655. .close = snd_cmipci_playback_spdif_close,
  1656. .ioctl = snd_pcm_lib_ioctl,
  1657. .hw_params = snd_cmipci_hw_params,
  1658. .hw_free = snd_cmipci_playback_hw_free,
  1659. .prepare = snd_cmipci_playback_spdif_prepare, /* set up rate */
  1660. .trigger = snd_cmipci_playback_trigger,
  1661. .pointer = snd_cmipci_playback_pointer,
  1662. };
  1663. static struct snd_pcm_ops snd_cmipci_capture_spdif_ops = {
  1664. .open = snd_cmipci_capture_spdif_open,
  1665. .close = snd_cmipci_capture_spdif_close,
  1666. .ioctl = snd_pcm_lib_ioctl,
  1667. .hw_params = snd_cmipci_hw_params,
  1668. .hw_free = snd_cmipci_capture_spdif_hw_free,
  1669. .prepare = snd_cmipci_capture_spdif_prepare,
  1670. .trigger = snd_cmipci_capture_trigger,
  1671. .pointer = snd_cmipci_capture_pointer,
  1672. };
  1673. /*
  1674. */
  1675. static int __devinit snd_cmipci_pcm_new(struct cmipci *cm, int device)
  1676. {
  1677. struct snd_pcm *pcm;
  1678. int err;
  1679. err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
  1680. if (err < 0)
  1681. return err;
  1682. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_ops);
  1683. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_ops);
  1684. pcm->private_data = cm;
  1685. pcm->info_flags = 0;
  1686. strcpy(pcm->name, "C-Media PCI DAC/ADC");
  1687. cm->pcm = pcm;
  1688. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1689. snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  1690. return 0;
  1691. }
  1692. static int __devinit snd_cmipci_pcm2_new(struct cmipci *cm, int device)
  1693. {
  1694. struct snd_pcm *pcm;
  1695. int err;
  1696. err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 0, &pcm);
  1697. if (err < 0)
  1698. return err;
  1699. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback2_ops);
  1700. pcm->private_data = cm;
  1701. pcm->info_flags = 0;
  1702. strcpy(pcm->name, "C-Media PCI 2nd DAC");
  1703. cm->pcm2 = pcm;
  1704. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1705. snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  1706. return 0;
  1707. }
  1708. static int __devinit snd_cmipci_pcm_spdif_new(struct cmipci *cm, int device)
  1709. {
  1710. struct snd_pcm *pcm;
  1711. int err;
  1712. err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
  1713. if (err < 0)
  1714. return err;
  1715. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_spdif_ops);
  1716. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_spdif_ops);
  1717. pcm->private_data = cm;
  1718. pcm->info_flags = 0;
  1719. strcpy(pcm->name, "C-Media PCI IEC958");
  1720. cm->pcm_spdif = pcm;
  1721. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1722. snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  1723. return 0;
  1724. }
  1725. /*
  1726. * mixer interface:
  1727. * - CM8338/8738 has a compatible mixer interface with SB16, but
  1728. * lack of some elements like tone control, i/o gain and AGC.
  1729. * - Access to native registers:
  1730. * - A 3D switch
  1731. * - Output mute switches
  1732. */
  1733. static void snd_cmipci_mixer_write(struct cmipci *s, unsigned char idx, unsigned char data)
  1734. {
  1735. outb(idx, s->iobase + CM_REG_SB16_ADDR);
  1736. outb(data, s->iobase + CM_REG_SB16_DATA);
  1737. }
  1738. static unsigned char snd_cmipci_mixer_read(struct cmipci *s, unsigned char idx)
  1739. {
  1740. unsigned char v;
  1741. outb(idx, s->iobase + CM_REG_SB16_ADDR);
  1742. v = inb(s->iobase + CM_REG_SB16_DATA);
  1743. return v;
  1744. }
  1745. /*
  1746. * general mixer element
  1747. */
  1748. struct cmipci_sb_reg {
  1749. unsigned int left_reg, right_reg;
  1750. unsigned int left_shift, right_shift;
  1751. unsigned int mask;
  1752. unsigned int invert: 1;
  1753. unsigned int stereo: 1;
  1754. };
  1755. #define COMPOSE_SB_REG(lreg,rreg,lshift,rshift,mask,invert,stereo) \
  1756. ((lreg) | ((rreg) << 8) | (lshift << 16) | (rshift << 19) | (mask << 24) | (invert << 22) | (stereo << 23))
  1757. #define CMIPCI_DOUBLE(xname, left_reg, right_reg, left_shift, right_shift, mask, invert, stereo) \
  1758. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1759. .info = snd_cmipci_info_volume, \
  1760. .get = snd_cmipci_get_volume, .put = snd_cmipci_put_volume, \
  1761. .private_value = COMPOSE_SB_REG(left_reg, right_reg, left_shift, right_shift, mask, invert, stereo), \
  1762. }
  1763. #define CMIPCI_SB_VOL_STEREO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg+1, shift, shift, mask, 0, 1)
  1764. #define CMIPCI_SB_VOL_MONO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg, shift, shift, mask, 0, 0)
  1765. #define CMIPCI_SB_SW_STEREO(xname,lshift,rshift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, lshift, rshift, 1, 0, 1)
  1766. #define CMIPCI_SB_SW_MONO(xname,shift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, shift, shift, 1, 0, 0)
  1767. static void cmipci_sb_reg_decode(struct cmipci_sb_reg *r, unsigned long val)
  1768. {
  1769. r->left_reg = val & 0xff;
  1770. r->right_reg = (val >> 8) & 0xff;
  1771. r->left_shift = (val >> 16) & 0x07;
  1772. r->right_shift = (val >> 19) & 0x07;
  1773. r->invert = (val >> 22) & 1;
  1774. r->stereo = (val >> 23) & 1;
  1775. r->mask = (val >> 24) & 0xff;
  1776. }
  1777. static int snd_cmipci_info_volume(struct snd_kcontrol *kcontrol,
  1778. struct snd_ctl_elem_info *uinfo)
  1779. {
  1780. struct cmipci_sb_reg reg;
  1781. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1782. uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1783. uinfo->count = reg.stereo + 1;
  1784. uinfo->value.integer.min = 0;
  1785. uinfo->value.integer.max = reg.mask;
  1786. return 0;
  1787. }
  1788. static int snd_cmipci_get_volume(struct snd_kcontrol *kcontrol,
  1789. struct snd_ctl_elem_value *ucontrol)
  1790. {
  1791. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1792. struct cmipci_sb_reg reg;
  1793. int val;
  1794. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1795. spin_lock_irq(&cm->reg_lock);
  1796. val = (snd_cmipci_mixer_read(cm, reg.left_reg) >> reg.left_shift) & reg.mask;
  1797. if (reg.invert)
  1798. val = reg.mask - val;
  1799. ucontrol->value.integer.value[0] = val;
  1800. if (reg.stereo) {
  1801. val = (snd_cmipci_mixer_read(cm, reg.right_reg) >> reg.right_shift) & reg.mask;
  1802. if (reg.invert)
  1803. val = reg.mask - val;
  1804. ucontrol->value.integer.value[1] = val;
  1805. }
  1806. spin_unlock_irq(&cm->reg_lock);
  1807. return 0;
  1808. }
  1809. static int snd_cmipci_put_volume(struct snd_kcontrol *kcontrol,
  1810. struct snd_ctl_elem_value *ucontrol)
  1811. {
  1812. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1813. struct cmipci_sb_reg reg;
  1814. int change;
  1815. int left, right, oleft, oright;
  1816. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1817. left = ucontrol->value.integer.value[0] & reg.mask;
  1818. if (reg.invert)
  1819. left = reg.mask - left;
  1820. left <<= reg.left_shift;
  1821. if (reg.stereo) {
  1822. right = ucontrol->value.integer.value[1] & reg.mask;
  1823. if (reg.invert)
  1824. right = reg.mask - right;
  1825. right <<= reg.right_shift;
  1826. } else
  1827. right = 0;
  1828. spin_lock_irq(&cm->reg_lock);
  1829. oleft = snd_cmipci_mixer_read(cm, reg.left_reg);
  1830. left |= oleft & ~(reg.mask << reg.left_shift);
  1831. change = left != oleft;
  1832. if (reg.stereo) {
  1833. if (reg.left_reg != reg.right_reg) {
  1834. snd_cmipci_mixer_write(cm, reg.left_reg, left);
  1835. oright = snd_cmipci_mixer_read(cm, reg.right_reg);
  1836. } else
  1837. oright = left;
  1838. right |= oright & ~(reg.mask << reg.right_shift);
  1839. change |= right != oright;
  1840. snd_cmipci_mixer_write(cm, reg.right_reg, right);
  1841. } else
  1842. snd_cmipci_mixer_write(cm, reg.left_reg, left);
  1843. spin_unlock_irq(&cm->reg_lock);
  1844. return change;
  1845. }
  1846. /*
  1847. * input route (left,right) -> (left,right)
  1848. */
  1849. #define CMIPCI_SB_INPUT_SW(xname, left_shift, right_shift) \
  1850. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1851. .info = snd_cmipci_info_input_sw, \
  1852. .get = snd_cmipci_get_input_sw, .put = snd_cmipci_put_input_sw, \
  1853. .private_value = COMPOSE_SB_REG(SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, left_shift, right_shift, 1, 0, 1), \
  1854. }
  1855. static int snd_cmipci_info_input_sw(struct snd_kcontrol *kcontrol,
  1856. struct snd_ctl_elem_info *uinfo)
  1857. {
  1858. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1859. uinfo->count = 4;
  1860. uinfo->value.integer.min = 0;
  1861. uinfo->value.integer.max = 1;
  1862. return 0;
  1863. }
  1864. static int snd_cmipci_get_input_sw(struct snd_kcontrol *kcontrol,
  1865. struct snd_ctl_elem_value *ucontrol)
  1866. {
  1867. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1868. struct cmipci_sb_reg reg;
  1869. int val1, val2;
  1870. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1871. spin_lock_irq(&cm->reg_lock);
  1872. val1 = snd_cmipci_mixer_read(cm, reg.left_reg);
  1873. val2 = snd_cmipci_mixer_read(cm, reg.right_reg);
  1874. spin_unlock_irq(&cm->reg_lock);
  1875. ucontrol->value.integer.value[0] = (val1 >> reg.left_shift) & 1;
  1876. ucontrol->value.integer.value[1] = (val2 >> reg.left_shift) & 1;
  1877. ucontrol->value.integer.value[2] = (val1 >> reg.right_shift) & 1;
  1878. ucontrol->value.integer.value[3] = (val2 >> reg.right_shift) & 1;
  1879. return 0;
  1880. }
  1881. static int snd_cmipci_put_input_sw(struct snd_kcontrol *kcontrol,
  1882. struct snd_ctl_elem_value *ucontrol)
  1883. {
  1884. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1885. struct cmipci_sb_reg reg;
  1886. int change;
  1887. int val1, val2, oval1, oval2;
  1888. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1889. spin_lock_irq(&cm->reg_lock);
  1890. oval1 = snd_cmipci_mixer_read(cm, reg.left_reg);
  1891. oval2 = snd_cmipci_mixer_read(cm, reg.right_reg);
  1892. val1 = oval1 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
  1893. val2 = oval2 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
  1894. val1 |= (ucontrol->value.integer.value[0] & 1) << reg.left_shift;
  1895. val2 |= (ucontrol->value.integer.value[1] & 1) << reg.left_shift;
  1896. val1 |= (ucontrol->value.integer.value[2] & 1) << reg.right_shift;
  1897. val2 |= (ucontrol->value.integer.value[3] & 1) << reg.right_shift;
  1898. change = val1 != oval1 || val2 != oval2;
  1899. snd_cmipci_mixer_write(cm, reg.left_reg, val1);
  1900. snd_cmipci_mixer_write(cm, reg.right_reg, val2);
  1901. spin_unlock_irq(&cm->reg_lock);
  1902. return change;
  1903. }
  1904. /*
  1905. * native mixer switches/volumes
  1906. */
  1907. #define CMIPCI_MIXER_SW_STEREO(xname, reg, lshift, rshift, invert) \
  1908. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1909. .info = snd_cmipci_info_native_mixer, \
  1910. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1911. .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, 1, invert, 1), \
  1912. }
  1913. #define CMIPCI_MIXER_SW_MONO(xname, reg, shift, invert) \
  1914. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1915. .info = snd_cmipci_info_native_mixer, \
  1916. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1917. .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, 1, invert, 0), \
  1918. }
  1919. #define CMIPCI_MIXER_VOL_STEREO(xname, reg, lshift, rshift, mask) \
  1920. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1921. .info = snd_cmipci_info_native_mixer, \
  1922. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1923. .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, mask, 0, 1), \
  1924. }
  1925. #define CMIPCI_MIXER_VOL_MONO(xname, reg, shift, mask) \
  1926. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1927. .info = snd_cmipci_info_native_mixer, \
  1928. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1929. .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, mask, 0, 0), \
  1930. }
  1931. static int snd_cmipci_info_native_mixer(struct snd_kcontrol *kcontrol,
  1932. struct snd_ctl_elem_info *uinfo)
  1933. {
  1934. struct cmipci_sb_reg reg;
  1935. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1936. uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1937. uinfo->count = reg.stereo + 1;
  1938. uinfo->value.integer.min = 0;
  1939. uinfo->value.integer.max = reg.mask;
  1940. return 0;
  1941. }
  1942. static int snd_cmipci_get_native_mixer(struct snd_kcontrol *kcontrol,
  1943. struct snd_ctl_elem_value *ucontrol)
  1944. {
  1945. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1946. struct cmipci_sb_reg reg;
  1947. unsigned char oreg, val;
  1948. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1949. spin_lock_irq(&cm->reg_lock);
  1950. oreg = inb(cm->iobase + reg.left_reg);
  1951. val = (oreg >> reg.left_shift) & reg.mask;
  1952. if (reg.invert)
  1953. val = reg.mask - val;
  1954. ucontrol->value.integer.value[0] = val;
  1955. if (reg.stereo) {
  1956. val = (oreg >> reg.right_shift) & reg.mask;
  1957. if (reg.invert)
  1958. val = reg.mask - val;
  1959. ucontrol->value.integer.value[1] = val;
  1960. }
  1961. spin_unlock_irq(&cm->reg_lock);
  1962. return 0;
  1963. }
  1964. static int snd_cmipci_put_native_mixer(struct snd_kcontrol *kcontrol,
  1965. struct snd_ctl_elem_value *ucontrol)
  1966. {
  1967. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1968. struct cmipci_sb_reg reg;
  1969. unsigned char oreg, nreg, val;
  1970. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1971. spin_lock_irq(&cm->reg_lock);
  1972. oreg = inb(cm->iobase + reg.left_reg);
  1973. val = ucontrol->value.integer.value[0] & reg.mask;
  1974. if (reg.invert)
  1975. val = reg.mask - val;
  1976. nreg = oreg & ~(reg.mask << reg.left_shift);
  1977. nreg |= (val << reg.left_shift);
  1978. if (reg.stereo) {
  1979. val = ucontrol->value.integer.value[1] & reg.mask;
  1980. if (reg.invert)
  1981. val = reg.mask - val;
  1982. nreg &= ~(reg.mask << reg.right_shift);
  1983. nreg |= (val << reg.right_shift);
  1984. }
  1985. outb(nreg, cm->iobase + reg.left_reg);
  1986. spin_unlock_irq(&cm->reg_lock);
  1987. return (nreg != oreg);
  1988. }
  1989. /*
  1990. * special case - check mixer sensitivity
  1991. */
  1992. static int snd_cmipci_get_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
  1993. struct snd_ctl_elem_value *ucontrol)
  1994. {
  1995. //struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  1996. return snd_cmipci_get_native_mixer(kcontrol, ucontrol);
  1997. }
  1998. static int snd_cmipci_put_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
  1999. struct snd_ctl_elem_value *ucontrol)
  2000. {
  2001. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2002. if (cm->mixer_insensitive) {
  2003. /* ignored */
  2004. return 0;
  2005. }
  2006. return snd_cmipci_put_native_mixer(kcontrol, ucontrol);
  2007. }
  2008. static struct snd_kcontrol_new snd_cmipci_mixers[] __devinitdata = {
  2009. CMIPCI_SB_VOL_STEREO("Master Playback Volume", SB_DSP4_MASTER_DEV, 3, 31),
  2010. CMIPCI_MIXER_SW_MONO("3D Control - Switch", CM_REG_MIXER1, CM_X3DEN_SHIFT, 0),
  2011. CMIPCI_SB_VOL_STEREO("PCM Playback Volume", SB_DSP4_PCM_DEV, 3, 31),
  2012. //CMIPCI_MIXER_SW_MONO("PCM Playback Switch", CM_REG_MIXER1, CM_WSMUTE_SHIFT, 1),
  2013. { /* switch with sensitivity */
  2014. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2015. .name = "PCM Playback Switch",
  2016. .info = snd_cmipci_info_native_mixer,
  2017. .get = snd_cmipci_get_native_mixer_sensitive,
  2018. .put = snd_cmipci_put_native_mixer_sensitive,
  2019. .private_value = COMPOSE_SB_REG(CM_REG_MIXER1, CM_REG_MIXER1, CM_WSMUTE_SHIFT, CM_WSMUTE_SHIFT, 1, 1, 0),
  2020. },
  2021. CMIPCI_MIXER_SW_STEREO("PCM Capture Switch", CM_REG_MIXER1, CM_WAVEINL_SHIFT, CM_WAVEINR_SHIFT, 0),
  2022. CMIPCI_SB_VOL_STEREO("Synth Playback Volume", SB_DSP4_SYNTH_DEV, 3, 31),
  2023. CMIPCI_MIXER_SW_MONO("Synth Playback Switch", CM_REG_MIXER1, CM_FMMUTE_SHIFT, 1),
  2024. CMIPCI_SB_INPUT_SW("Synth Capture Route", 6, 5),
  2025. CMIPCI_SB_VOL_STEREO("CD Playback Volume", SB_DSP4_CD_DEV, 3, 31),
  2026. CMIPCI_SB_SW_STEREO("CD Playback Switch", 2, 1),
  2027. CMIPCI_SB_INPUT_SW("CD Capture Route", 2, 1),
  2028. CMIPCI_SB_VOL_STEREO("Line Playback Volume", SB_DSP4_LINE_DEV, 3, 31),
  2029. CMIPCI_SB_SW_STEREO("Line Playback Switch", 4, 3),
  2030. CMIPCI_SB_INPUT_SW("Line Capture Route", 4, 3),
  2031. CMIPCI_SB_VOL_MONO("Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
  2032. CMIPCI_SB_SW_MONO("Mic Playback Switch", 0),
  2033. CMIPCI_DOUBLE("Mic Capture Switch", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 0, 0, 1, 0, 0),
  2034. CMIPCI_SB_VOL_MONO("Beep Playback Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
  2035. CMIPCI_MIXER_VOL_STEREO("Aux Playback Volume", CM_REG_AUX_VOL, 4, 0, 15),
  2036. CMIPCI_MIXER_SW_STEREO("Aux Playback Switch", CM_REG_MIXER2, CM_VAUXLM_SHIFT, CM_VAUXRM_SHIFT, 0),
  2037. CMIPCI_MIXER_SW_STEREO("Aux Capture Switch", CM_REG_MIXER2, CM_RAUXLEN_SHIFT, CM_RAUXREN_SHIFT, 0),
  2038. CMIPCI_MIXER_SW_MONO("Mic Boost Playback Switch", CM_REG_MIXER2, CM_MICGAINZ_SHIFT, 1),
  2039. CMIPCI_MIXER_VOL_MONO("Mic Capture Volume", CM_REG_MIXER2, CM_VADMIC_SHIFT, 7),
  2040. CMIPCI_SB_VOL_MONO("Phone Playback Volume", CM_REG_EXTENT_IND, 5, 7),
  2041. CMIPCI_DOUBLE("Phone Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 4, 4, 1, 0, 0),
  2042. CMIPCI_DOUBLE("Beep Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 3, 3, 1, 0, 0),
  2043. CMIPCI_DOUBLE("Mic Boost Capture Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 0, 0, 1, 0, 0),
  2044. };
  2045. /*
  2046. * other switches
  2047. */
  2048. struct cmipci_switch_args {
  2049. int reg; /* register index */
  2050. unsigned int mask; /* mask bits */
  2051. unsigned int mask_on; /* mask bits to turn on */
  2052. unsigned int is_byte: 1; /* byte access? */
  2053. unsigned int ac3_sensitive: 1; /* access forbidden during
  2054. * non-audio operation?
  2055. */
  2056. };
  2057. #define snd_cmipci_uswitch_info snd_ctl_boolean_mono_info
  2058. static int _snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
  2059. struct snd_ctl_elem_value *ucontrol,
  2060. struct cmipci_switch_args *args)
  2061. {
  2062. unsigned int val;
  2063. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2064. spin_lock_irq(&cm->reg_lock);
  2065. if (args->ac3_sensitive && cm->mixer_insensitive) {
  2066. ucontrol->value.integer.value[0] = 0;
  2067. spin_unlock_irq(&cm->reg_lock);
  2068. return 0;
  2069. }
  2070. if (args->is_byte)
  2071. val = inb(cm->iobase + args->reg);
  2072. else
  2073. val = snd_cmipci_read(cm, args->reg);
  2074. ucontrol->value.integer.value[0] = ((val & args->mask) == args->mask_on) ? 1 : 0;
  2075. spin_unlock_irq(&cm->reg_lock);
  2076. return 0;
  2077. }
  2078. static int snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
  2079. struct snd_ctl_elem_value *ucontrol)
  2080. {
  2081. struct cmipci_switch_args *args;
  2082. args = (struct cmipci_switch_args *)kcontrol->private_value;
  2083. if (snd_BUG_ON(!args))
  2084. return -EINVAL;
  2085. return _snd_cmipci_uswitch_get(kcontrol, ucontrol, args);
  2086. }
  2087. static int _snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
  2088. struct snd_ctl_elem_value *ucontrol,
  2089. struct cmipci_switch_args *args)
  2090. {
  2091. unsigned int val;
  2092. int change;
  2093. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2094. spin_lock_irq(&cm->reg_lock);
  2095. if (args->ac3_sensitive && cm->mixer_insensitive) {
  2096. /* ignored */
  2097. spin_unlock_irq(&cm->reg_lock);
  2098. return 0;
  2099. }
  2100. if (args->is_byte)
  2101. val = inb(cm->iobase + args->reg);
  2102. else
  2103. val = snd_cmipci_read(cm, args->reg);
  2104. change = (val & args->mask) != (ucontrol->value.integer.value[0] ?
  2105. args->mask_on : (args->mask & ~args->mask_on));
  2106. if (change) {
  2107. val &= ~args->mask;
  2108. if (ucontrol->value.integer.value[0])
  2109. val |= args->mask_on;
  2110. else
  2111. val |= (args->mask & ~args->mask_on);
  2112. if (args->is_byte)
  2113. outb((unsigned char)val, cm->iobase + args->reg);
  2114. else
  2115. snd_cmipci_write(cm, args->reg, val);
  2116. }
  2117. spin_unlock_irq(&cm->reg_lock);
  2118. return change;
  2119. }
  2120. static int snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
  2121. struct snd_ctl_elem_value *ucontrol)
  2122. {
  2123. struct cmipci_switch_args *args;
  2124. args = (struct cmipci_switch_args *)kcontrol->private_value;
  2125. if (snd_BUG_ON(!args))
  2126. return -EINVAL;
  2127. return _snd_cmipci_uswitch_put(kcontrol, ucontrol, args);
  2128. }
  2129. #define DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask_on, xis_byte, xac3) \
  2130. static struct cmipci_switch_args cmipci_switch_arg_##sname = { \
  2131. .reg = xreg, \
  2132. .mask = xmask, \
  2133. .mask_on = xmask_on, \
  2134. .is_byte = xis_byte, \
  2135. .ac3_sensitive = xac3, \
  2136. }
  2137. #define DEFINE_BIT_SWITCH_ARG(sname, xreg, xmask, xis_byte, xac3) \
  2138. DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask, xis_byte, xac3)
  2139. #if 0 /* these will be controlled in pcm device */
  2140. DEFINE_BIT_SWITCH_ARG(spdif_in, CM_REG_FUNCTRL1, CM_SPDF_1, 0, 0);
  2141. DEFINE_BIT_SWITCH_ARG(spdif_out, CM_REG_FUNCTRL1, CM_SPDF_0, 0, 0);
  2142. #endif
  2143. DEFINE_BIT_SWITCH_ARG(spdif_in_sel1, CM_REG_CHFORMAT, CM_SPDIF_SELECT1, 0, 0);
  2144. DEFINE_BIT_SWITCH_ARG(spdif_in_sel2, CM_REG_MISC_CTRL, CM_SPDIF_SELECT2, 0, 0);
  2145. DEFINE_BIT_SWITCH_ARG(spdif_enable, CM_REG_LEGACY_CTRL, CM_ENSPDOUT, 0, 0);
  2146. DEFINE_BIT_SWITCH_ARG(spdo2dac, CM_REG_FUNCTRL1, CM_SPDO2DAC, 0, 1);
  2147. DEFINE_BIT_SWITCH_ARG(spdi_valid, CM_REG_MISC, CM_SPDVALID, 1, 0);
  2148. DEFINE_BIT_SWITCH_ARG(spdif_copyright, CM_REG_LEGACY_CTRL, CM_SPDCOPYRHT, 0, 0);
  2149. DEFINE_BIT_SWITCH_ARG(spdif_dac_out, CM_REG_LEGACY_CTRL, CM_DAC2SPDO, 0, 1);
  2150. DEFINE_SWITCH_ARG(spdo_5v, CM_REG_MISC_CTRL, CM_SPDO5V, 0, 0, 0); /* inverse: 0 = 5V */
  2151. // DEFINE_BIT_SWITCH_ARG(spdo_48k, CM_REG_MISC_CTRL, CM_SPDF_AC97|CM_SPDIF48K, 0, 1);
  2152. DEFINE_BIT_SWITCH_ARG(spdif_loop, CM_REG_FUNCTRL1, CM_SPDFLOOP, 0, 1);
  2153. DEFINE_BIT_SWITCH_ARG(spdi_monitor, CM_REG_MIXER1, CM_CDPLAY, 1, 0);
  2154. /* DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_CHFORMAT, CM_SPDIF_INVERSE, 0, 0); */
  2155. DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_MISC, CM_SPDIF_INVERSE, 1, 0);
  2156. DEFINE_BIT_SWITCH_ARG(spdi_phase2, CM_REG_CHFORMAT, CM_SPDIF_INVERSE2, 0, 0);
  2157. #if CM_CH_PLAY == 1
  2158. DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, 0, 0, 0); /* reversed */
  2159. #else
  2160. DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, CM_XCHGDAC, 0, 0);
  2161. #endif
  2162. DEFINE_BIT_SWITCH_ARG(fourch, CM_REG_MISC_CTRL, CM_N4SPK3D, 0, 0);
  2163. // DEFINE_BIT_SWITCH_ARG(line_rear, CM_REG_MIXER1, CM_REAR2LIN, 1, 0);
  2164. // DEFINE_BIT_SWITCH_ARG(line_bass, CM_REG_LEGACY_CTRL, CM_CENTR2LIN|CM_BASE2LIN, 0, 0);
  2165. // DEFINE_BIT_SWITCH_ARG(joystick, CM_REG_FUNCTRL1, CM_JYSTK_EN, 0, 0); /* now module option */
  2166. DEFINE_SWITCH_ARG(modem, CM_REG_MISC_CTRL, CM_FLINKON|CM_FLINKOFF, CM_FLINKON, 0, 0);
  2167. #define DEFINE_SWITCH(sname, stype, sarg) \
  2168. { .name = sname, \
  2169. .iface = stype, \
  2170. .info = snd_cmipci_uswitch_info, \
  2171. .get = snd_cmipci_uswitch_get, \
  2172. .put = snd_cmipci_uswitch_put, \
  2173. .private_value = (unsigned long)&cmipci_switch_arg_##sarg,\
  2174. }
  2175. #define DEFINE_CARD_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_CARD, sarg)
  2176. #define DEFINE_MIXER_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_MIXER, sarg)
  2177. /*
  2178. * callbacks for spdif output switch
  2179. * needs toggle two registers..
  2180. */
  2181. static int snd_cmipci_spdout_enable_get(struct snd_kcontrol *kcontrol,
  2182. struct snd_ctl_elem_value *ucontrol)
  2183. {
  2184. int changed;
  2185. changed = _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
  2186. changed |= _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
  2187. return changed;
  2188. }
  2189. static int snd_cmipci_spdout_enable_put(struct snd_kcontrol *kcontrol,
  2190. struct snd_ctl_elem_value *ucontrol)
  2191. {
  2192. struct cmipci *chip = snd_kcontrol_chip(kcontrol);
  2193. int changed;
  2194. changed = _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
  2195. changed |= _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
  2196. if (changed) {
  2197. if (ucontrol->value.integer.value[0]) {
  2198. if (chip->spdif_playback_avail)
  2199. snd_cmipci_set_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  2200. } else {
  2201. if (chip->spdif_playback_avail)
  2202. snd_cmipci_clear_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  2203. }
  2204. }
  2205. chip->spdif_playback_enabled = ucontrol->value.integer.value[0];
  2206. return changed;
  2207. }
  2208. static int snd_cmipci_line_in_mode_info(struct snd_kcontrol *kcontrol,
  2209. struct snd_ctl_elem_info *uinfo)
  2210. {
  2211. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2212. static const char *const texts[3] = {
  2213. "Line-In", "Rear Output", "Bass Output"
  2214. };
  2215. return snd_ctl_enum_info(uinfo, 1,
  2216. cm->chip_version >= 39 ? 3 : 2, texts);
  2217. }
  2218. static inline unsigned int get_line_in_mode(struct cmipci *cm)
  2219. {
  2220. unsigned int val;
  2221. if (cm->chip_version >= 39) {
  2222. val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL);
  2223. if (val & (CM_CENTR2LIN | CM_BASE2LIN))
  2224. return 2;
  2225. }
  2226. val = snd_cmipci_read_b(cm, CM_REG_MIXER1);
  2227. if (val & CM_REAR2LIN)
  2228. return 1;
  2229. return 0;
  2230. }
  2231. static int snd_cmipci_line_in_mode_get(struct snd_kcontrol *kcontrol,
  2232. struct snd_ctl_elem_value *ucontrol)
  2233. {
  2234. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2235. spin_lock_irq(&cm->reg_lock);
  2236. ucontrol->value.enumerated.item[0] = get_line_in_mode(cm);
  2237. spin_unlock_irq(&cm->reg_lock);
  2238. return 0;
  2239. }
  2240. static int snd_cmipci_line_in_mode_put(struct snd_kcontrol *kcontrol,
  2241. struct snd_ctl_elem_value *ucontrol)
  2242. {
  2243. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2244. int change;
  2245. spin_lock_irq(&cm->reg_lock);
  2246. if (ucontrol->value.enumerated.item[0] == 2)
  2247. change = snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
  2248. else
  2249. change = snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
  2250. if (ucontrol->value.enumerated.item[0] == 1)
  2251. change |= snd_cmipci_set_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
  2252. else
  2253. change |= snd_cmipci_clear_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
  2254. spin_unlock_irq(&cm->reg_lock);
  2255. return change;
  2256. }
  2257. static int snd_cmipci_mic_in_mode_info(struct snd_kcontrol *kcontrol,
  2258. struct snd_ctl_elem_info *uinfo)
  2259. {
  2260. static const char *const texts[2] = { "Mic-In", "Center/LFE Output" };
  2261. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  2262. }
  2263. static int snd_cmipci_mic_in_mode_get(struct snd_kcontrol *kcontrol,
  2264. struct snd_ctl_elem_value *ucontrol)
  2265. {
  2266. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2267. /* same bit as spdi_phase */
  2268. spin_lock_irq(&cm->reg_lock);
  2269. ucontrol->value.enumerated.item[0] =
  2270. (snd_cmipci_read_b(cm, CM_REG_MISC) & CM_SPDIF_INVERSE) ? 1 : 0;
  2271. spin_unlock_irq(&cm->reg_lock);
  2272. return 0;
  2273. }
  2274. static int snd_cmipci_mic_in_mode_put(struct snd_kcontrol *kcontrol,
  2275. struct snd_ctl_elem_value *ucontrol)
  2276. {
  2277. struct cmipci *cm = snd_kcontrol_chip(kcontrol);
  2278. int change;
  2279. spin_lock_irq(&cm->reg_lock);
  2280. if (ucontrol->value.enumerated.item[0])
  2281. change = snd_cmipci_set_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
  2282. else
  2283. change = snd_cmipci_clear_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
  2284. spin_unlock_irq(&cm->reg_lock);
  2285. return change;
  2286. }
  2287. /* both for CM8338/8738 */
  2288. static struct snd_kcontrol_new snd_cmipci_mixer_switches[] __devinitdata = {
  2289. DEFINE_MIXER_SWITCH("Four Channel Mode", fourch),
  2290. {
  2291. .name = "Line-In Mode",
  2292. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2293. .info = snd_cmipci_line_in_mode_info,
  2294. .get = snd_cmipci_line_in_mode_get,
  2295. .put = snd_cmipci_line_in_mode_put,
  2296. },
  2297. };
  2298. /* for non-multichannel chips */
  2299. static struct snd_kcontrol_new snd_cmipci_nomulti_switch __devinitdata =
  2300. DEFINE_MIXER_SWITCH("Exchange DAC", exchange_dac);
  2301. /* only for CM8738 */
  2302. static struct snd_kcontrol_new snd_cmipci_8738_mixer_switches[] __devinitdata = {
  2303. #if 0 /* controlled in pcm device */
  2304. DEFINE_MIXER_SWITCH("IEC958 In Record", spdif_in),
  2305. DEFINE_MIXER_SWITCH("IEC958 Out", spdif_out),
  2306. DEFINE_MIXER_SWITCH("IEC958 Out To DAC", spdo2dac),
  2307. #endif
  2308. // DEFINE_MIXER_SWITCH("IEC958 Output Switch", spdif_enable),
  2309. { .name = "IEC958 Output Switch",
  2310. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2311. .info = snd_cmipci_uswitch_info,
  2312. .get = snd_cmipci_spdout_enable_get,
  2313. .put = snd_cmipci_spdout_enable_put,
  2314. },
  2315. DEFINE_MIXER_SWITCH("IEC958 In Valid", spdi_valid),
  2316. DEFINE_MIXER_SWITCH("IEC958 Copyright", spdif_copyright),
  2317. DEFINE_MIXER_SWITCH("IEC958 5V", spdo_5v),
  2318. // DEFINE_MIXER_SWITCH("IEC958 In/Out 48KHz", spdo_48k),
  2319. DEFINE_MIXER_SWITCH("IEC958 Loop", spdif_loop),
  2320. DEFINE_MIXER_SWITCH("IEC958 In Monitor", spdi_monitor),
  2321. };
  2322. /* only for model 033/037 */
  2323. static struct snd_kcontrol_new snd_cmipci_old_mixer_switches[] __devinitdata = {
  2324. DEFINE_MIXER_SWITCH("IEC958 Mix Analog", spdif_dac_out),
  2325. DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase),
  2326. DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel1),
  2327. };
  2328. /* only for model 039 or later */
  2329. static struct snd_kcontrol_new snd_cmipci_extra_mixer_switches[] __devinitdata = {
  2330. DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel2),
  2331. DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase2),
  2332. {
  2333. .name = "Mic-In Mode",
  2334. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2335. .info = snd_cmipci_mic_in_mode_info,
  2336. .get = snd_cmipci_mic_in_mode_get,
  2337. .put = snd_cmipci_mic_in_mode_put,
  2338. }
  2339. };
  2340. /* card control switches */
  2341. static struct snd_kcontrol_new snd_cmipci_modem_switch __devinitdata =
  2342. DEFINE_CARD_SWITCH("Modem", modem);
  2343. static int __devinit snd_cmipci_mixer_new(struct cmipci *cm, int pcm_spdif_device)
  2344. {
  2345. struct snd_card *card;
  2346. struct snd_kcontrol_new *sw;
  2347. struct snd_kcontrol *kctl;
  2348. unsigned int idx;
  2349. int err;
  2350. if (snd_BUG_ON(!cm || !cm->card))
  2351. return -EINVAL;
  2352. card = cm->card;
  2353. strcpy(card->mixername, "CMedia PCI");
  2354. spin_lock_irq(&cm->reg_lock);
  2355. snd_cmipci_mixer_write(cm, 0x00, 0x00); /* mixer reset */
  2356. spin_unlock_irq(&cm->reg_lock);
  2357. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixers); idx++) {
  2358. if (cm->chip_version == 68) { // 8768 has no PCM volume
  2359. if (!strcmp(snd_cmipci_mixers[idx].name,
  2360. "PCM Playback Volume"))
  2361. continue;
  2362. }
  2363. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cmipci_mixers[idx], cm))) < 0)
  2364. return err;
  2365. }
  2366. /* mixer switches */
  2367. sw = snd_cmipci_mixer_switches;
  2368. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixer_switches); idx++, sw++) {
  2369. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2370. if (err < 0)
  2371. return err;
  2372. }
  2373. if (! cm->can_multi_ch) {
  2374. err = snd_ctl_add(cm->card, snd_ctl_new1(&snd_cmipci_nomulti_switch, cm));
  2375. if (err < 0)
  2376. return err;
  2377. }
  2378. if (cm->device == PCI_DEVICE_ID_CMEDIA_CM8738 ||
  2379. cm->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
  2380. sw = snd_cmipci_8738_mixer_switches;
  2381. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_8738_mixer_switches); idx++, sw++) {
  2382. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2383. if (err < 0)
  2384. return err;
  2385. }
  2386. if (cm->can_ac3_hw) {
  2387. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_default, cm))) < 0)
  2388. return err;
  2389. kctl->id.device = pcm_spdif_device;
  2390. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_mask, cm))) < 0)
  2391. return err;
  2392. kctl->id.device = pcm_spdif_device;
  2393. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_stream, cm))) < 0)
  2394. return err;
  2395. kctl->id.device = pcm_spdif_device;
  2396. }
  2397. if (cm->chip_version <= 37) {
  2398. sw = snd_cmipci_old_mixer_switches;
  2399. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_old_mixer_switches); idx++, sw++) {
  2400. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2401. if (err < 0)
  2402. return err;
  2403. }
  2404. }
  2405. }
  2406. if (cm->chip_version >= 39) {
  2407. sw = snd_cmipci_extra_mixer_switches;
  2408. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_extra_mixer_switches); idx++, sw++) {
  2409. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2410. if (err < 0)
  2411. return err;
  2412. }
  2413. }
  2414. /* card switches */
  2415. /*
  2416. * newer chips don't have the register bits to force modem link
  2417. * detection; the bit that was FLINKON now mutes CH1
  2418. */
  2419. if (cm->chip_version < 39) {
  2420. err = snd_ctl_add(cm->card,
  2421. snd_ctl_new1(&snd_cmipci_modem_switch, cm));
  2422. if (err < 0)
  2423. return err;
  2424. }
  2425. for (idx = 0; idx < CM_SAVED_MIXERS; idx++) {
  2426. struct snd_ctl_elem_id elem_id;
  2427. struct snd_kcontrol *ctl;
  2428. memset(&elem_id, 0, sizeof(elem_id));
  2429. elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2430. strcpy(elem_id.name, cm_saved_mixer[idx].name);
  2431. ctl = snd_ctl_find_id(cm->card, &elem_id);
  2432. if (ctl)
  2433. cm->mixer_res_ctl[idx] = ctl;
  2434. }
  2435. return 0;
  2436. }
  2437. /*
  2438. * proc interface
  2439. */
  2440. #ifdef CONFIG_PROC_FS
  2441. static void snd_cmipci_proc_read(struct snd_info_entry *entry,
  2442. struct snd_info_buffer *buffer)
  2443. {
  2444. struct cmipci *cm = entry->private_data;
  2445. int i, v;
  2446. snd_iprintf(buffer, "%s\n", cm->card->longname);
  2447. for (i = 0; i < 0x94; i++) {
  2448. if (i == 0x28)
  2449. i = 0x90;
  2450. v = inb(cm->iobase + i);
  2451. if (i % 4 == 0)
  2452. snd_iprintf(buffer, "\n%02x:", i);
  2453. snd_iprintf(buffer, " %02x", v);
  2454. }
  2455. snd_iprintf(buffer, "\n");
  2456. }
  2457. static void __devinit snd_cmipci_proc_init(struct cmipci *cm)
  2458. {
  2459. struct snd_info_entry *entry;
  2460. if (! snd_card_proc_new(cm->card, "cmipci", &entry))
  2461. snd_info_set_text_ops(entry, cm, snd_cmipci_proc_read);
  2462. }
  2463. #else /* !CONFIG_PROC_FS */
  2464. static inline void snd_cmipci_proc_init(struct cmipci *cm) {}
  2465. #endif
  2466. static DEFINE_PCI_DEVICE_TABLE(snd_cmipci_ids) = {
  2467. {PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A), 0},
  2468. {PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B), 0},
  2469. {PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738), 0},
  2470. {PCI_VDEVICE(CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738B), 0},
  2471. {PCI_VDEVICE(AL, PCI_DEVICE_ID_CMEDIA_CM8738), 0},
  2472. {0,},
  2473. };
  2474. /*
  2475. * check chip version and capabilities
  2476. * driver name is modified according to the chip model
  2477. */
  2478. static void __devinit query_chip(struct cmipci *cm)
  2479. {
  2480. unsigned int detect;
  2481. /* check reg 0Ch, bit 24-31 */
  2482. detect = snd_cmipci_read(cm, CM_REG_INT_HLDCLR) & CM_CHIP_MASK2;
  2483. if (! detect) {
  2484. /* check reg 08h, bit 24-28 */
  2485. detect = snd_cmipci_read(cm, CM_REG_CHFORMAT) & CM_CHIP_MASK1;
  2486. switch (detect) {
  2487. case 0:
  2488. cm->chip_version = 33;
  2489. if (cm->do_soft_ac3)
  2490. cm->can_ac3_sw = 1;
  2491. else
  2492. cm->can_ac3_hw = 1;
  2493. break;
  2494. case CM_CHIP_037:
  2495. cm->chip_version = 37;
  2496. cm->can_ac3_hw = 1;
  2497. break;
  2498. default:
  2499. cm->chip_version = 39;
  2500. cm->can_ac3_hw = 1;
  2501. break;
  2502. }
  2503. cm->max_channels = 2;
  2504. } else {
  2505. if (detect & CM_CHIP_039) {
  2506. cm->chip_version = 39;
  2507. if (detect & CM_CHIP_039_6CH) /* 4 or 6 channels */
  2508. cm->max_channels = 6;
  2509. else
  2510. cm->max_channels = 4;
  2511. } else if (detect & CM_CHIP_8768) {
  2512. cm->chip_version = 68;
  2513. cm->max_channels = 8;
  2514. cm->can_96k = 1;
  2515. } else {
  2516. cm->chip_version = 55;
  2517. cm->max_channels = 6;
  2518. cm->can_96k = 1;
  2519. }
  2520. cm->can_ac3_hw = 1;
  2521. cm->can_multi_ch = 1;
  2522. }
  2523. }
  2524. #ifdef SUPPORT_JOYSTICK
  2525. static int __devinit snd_cmipci_create_gameport(struct cmipci *cm, int dev)
  2526. {
  2527. static int ports[] = { 0x201, 0x200, 0 }; /* FIXME: majority is 0x201? */
  2528. struct gameport *gp;
  2529. struct resource *r = NULL;
  2530. int i, io_port = 0;
  2531. if (joystick_port[dev] == 0)
  2532. return -ENODEV;
  2533. if (joystick_port[dev] == 1) { /* auto-detect */
  2534. for (i = 0; ports[i]; i++) {
  2535. io_port = ports[i];
  2536. r = request_region(io_port, 1, "CMIPCI gameport");
  2537. if (r)
  2538. break;
  2539. }
  2540. } else {
  2541. io_port = joystick_port[dev];
  2542. r = request_region(io_port, 1, "CMIPCI gameport");
  2543. }
  2544. if (!r) {
  2545. printk(KERN_WARNING "cmipci: cannot reserve joystick ports\n");
  2546. return -EBUSY;
  2547. }
  2548. cm->gameport = gp = gameport_allocate_port();
  2549. if (!gp) {
  2550. printk(KERN_ERR "cmipci: cannot allocate memory for gameport\n");
  2551. release_and_free_resource(r);
  2552. return -ENOMEM;
  2553. }
  2554. gameport_set_name(gp, "C-Media Gameport");
  2555. gameport_set_phys(gp, "pci%s/gameport0", pci_name(cm->pci));
  2556. gameport_set_dev_parent(gp, &cm->pci->dev);
  2557. gp->io = io_port;
  2558. gameport_set_port_data(gp, r);
  2559. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  2560. gameport_register_port(cm->gameport);
  2561. return 0;
  2562. }
  2563. static void snd_cmipci_free_gameport(struct cmipci *cm)
  2564. {
  2565. if (cm->gameport) {
  2566. struct resource *r = gameport_get_port_data(cm->gameport);
  2567. gameport_unregister_port(cm->gameport);
  2568. cm->gameport = NULL;
  2569. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  2570. release_and_free_resource(r);
  2571. }
  2572. }
  2573. #else
  2574. static inline int snd_cmipci_create_gameport(struct cmipci *cm, int dev) { return -ENOSYS; }
  2575. static inline void snd_cmipci_free_gameport(struct cmipci *cm) { }
  2576. #endif
  2577. static int snd_cmipci_free(struct cmipci *cm)
  2578. {
  2579. if (cm->irq >= 0) {
  2580. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  2581. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT);
  2582. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
  2583. snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  2584. snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  2585. snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
  2586. snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
  2587. /* reset mixer */
  2588. snd_cmipci_mixer_write(cm, 0, 0);
  2589. free_irq(cm->irq, cm);
  2590. }
  2591. snd_cmipci_free_gameport(cm);
  2592. pci_release_regions(cm->pci);
  2593. pci_disable_device(cm->pci);
  2594. kfree(cm);
  2595. return 0;
  2596. }
  2597. static int snd_cmipci_dev_free(struct snd_device *device)
  2598. {
  2599. struct cmipci *cm = device->device_data;
  2600. return snd_cmipci_free(cm);
  2601. }
  2602. static int __devinit snd_cmipci_create_fm(struct cmipci *cm, long fm_port)
  2603. {
  2604. long iosynth;
  2605. unsigned int val;
  2606. struct snd_opl3 *opl3;
  2607. int err;
  2608. if (!fm_port)
  2609. goto disable_fm;
  2610. if (cm->chip_version >= 39) {
  2611. /* first try FM regs in PCI port range */
  2612. iosynth = cm->iobase + CM_REG_FM_PCI;
  2613. err = snd_opl3_create(cm->card, iosynth, iosynth + 2,
  2614. OPL3_HW_OPL3, 1, &opl3);
  2615. } else {
  2616. err = -EIO;
  2617. }
  2618. if (err < 0) {
  2619. /* then try legacy ports */
  2620. val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL) & ~CM_FMSEL_MASK;
  2621. iosynth = fm_port;
  2622. switch (iosynth) {
  2623. case 0x3E8: val |= CM_FMSEL_3E8; break;
  2624. case 0x3E0: val |= CM_FMSEL_3E0; break;
  2625. case 0x3C8: val |= CM_FMSEL_3C8; break;
  2626. case 0x388: val |= CM_FMSEL_388; break;
  2627. default:
  2628. goto disable_fm;
  2629. }
  2630. snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
  2631. /* enable FM */
  2632. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  2633. if (snd_opl3_create(cm->card, iosynth, iosynth + 2,
  2634. OPL3_HW_OPL3, 0, &opl3) < 0) {
  2635. printk(KERN_ERR "cmipci: no OPL device at %#lx, "
  2636. "skipping...\n", iosynth);
  2637. goto disable_fm;
  2638. }
  2639. }
  2640. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  2641. printk(KERN_ERR "cmipci: cannot create OPL3 hwdep\n");
  2642. return err;
  2643. }
  2644. return 0;
  2645. disable_fm:
  2646. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_FMSEL_MASK);
  2647. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  2648. return 0;
  2649. }
  2650. static int __devinit snd_cmipci_create(struct snd_card *card, struct pci_dev *pci,
  2651. int dev, struct cmipci **rcmipci)
  2652. {
  2653. struct cmipci *cm;
  2654. int err;
  2655. static struct snd_device_ops ops = {
  2656. .dev_free = snd_cmipci_dev_free,
  2657. };
  2658. unsigned int val;
  2659. long iomidi = 0;
  2660. int integrated_midi = 0;
  2661. char modelstr[16];
  2662. int pcm_index, pcm_spdif_index;
  2663. static DEFINE_PCI_DEVICE_TABLE(intel_82437vx) = {
  2664. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437VX) },
  2665. { },
  2666. };
  2667. *rcmipci = NULL;
  2668. if ((err = pci_enable_device(pci)) < 0)
  2669. return err;
  2670. cm = kzalloc(sizeof(*cm), GFP_KERNEL);
  2671. if (cm == NULL) {
  2672. pci_disable_device(pci);
  2673. return -ENOMEM;
  2674. }
  2675. spin_lock_init(&cm->reg_lock);
  2676. mutex_init(&cm->open_mutex);
  2677. cm->device = pci->device;
  2678. cm->card = card;
  2679. cm->pci = pci;
  2680. cm->irq = -1;
  2681. cm->channel[0].ch = 0;
  2682. cm->channel[1].ch = 1;
  2683. cm->channel[0].is_dac = cm->channel[1].is_dac = 1; /* dual DAC mode */
  2684. if ((err = pci_request_regions(pci, card->driver)) < 0) {
  2685. kfree(cm);
  2686. pci_disable_device(pci);
  2687. return err;
  2688. }
  2689. cm->iobase = pci_resource_start(pci, 0);
  2690. if (request_irq(pci->irq, snd_cmipci_interrupt,
  2691. IRQF_SHARED, KBUILD_MODNAME, cm)) {
  2692. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  2693. snd_cmipci_free(cm);
  2694. return -EBUSY;
  2695. }
  2696. cm->irq = pci->irq;
  2697. pci_set_master(cm->pci);
  2698. /*
  2699. * check chip version, max channels and capabilities
  2700. */
  2701. cm->chip_version = 0;
  2702. cm->max_channels = 2;
  2703. cm->do_soft_ac3 = soft_ac3[dev];
  2704. if (pci->device != PCI_DEVICE_ID_CMEDIA_CM8338A &&
  2705. pci->device != PCI_DEVICE_ID_CMEDIA_CM8338B)
  2706. query_chip(cm);
  2707. /* added -MCx suffix for chip supporting multi-channels */
  2708. if (cm->can_multi_ch)
  2709. sprintf(cm->card->driver + strlen(cm->card->driver),
  2710. "-MC%d", cm->max_channels);
  2711. else if (cm->can_ac3_sw)
  2712. strcpy(cm->card->driver + strlen(cm->card->driver), "-SWIEC");
  2713. cm->dig_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  2714. cm->dig_pcm_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  2715. #if CM_CH_PLAY == 1
  2716. cm->ctrl = CM_CHADC0; /* default FUNCNTRL0 */
  2717. #else
  2718. cm->ctrl = CM_CHADC1; /* default FUNCNTRL0 */
  2719. #endif
  2720. /* initialize codec registers */
  2721. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
  2722. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
  2723. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
  2724. snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  2725. snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  2726. snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
  2727. snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
  2728. snd_cmipci_write(cm, CM_REG_CHFORMAT, 0);
  2729. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC|CM_N4SPK3D);
  2730. #if CM_CH_PLAY == 1
  2731. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  2732. #else
  2733. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  2734. #endif
  2735. if (cm->chip_version) {
  2736. snd_cmipci_write_b(cm, CM_REG_EXT_MISC, 0x20); /* magic */
  2737. snd_cmipci_write_b(cm, CM_REG_EXT_MISC + 1, 0x09); /* more magic */
  2738. }
  2739. /* Set Bus Master Request */
  2740. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_BREQ);
  2741. /* Assume TX and compatible chip set (Autodetection required for VX chip sets) */
  2742. switch (pci->device) {
  2743. case PCI_DEVICE_ID_CMEDIA_CM8738:
  2744. case PCI_DEVICE_ID_CMEDIA_CM8738B:
  2745. if (!pci_dev_present(intel_82437vx))
  2746. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_TXVX);
  2747. break;
  2748. default:
  2749. break;
  2750. }
  2751. if (cm->chip_version < 68) {
  2752. val = pci->device < 0x110 ? 8338 : 8738;
  2753. } else {
  2754. switch (snd_cmipci_read_b(cm, CM_REG_INT_HLDCLR + 3) & 0x03) {
  2755. case 0:
  2756. val = 8769;
  2757. break;
  2758. case 2:
  2759. val = 8762;
  2760. break;
  2761. default:
  2762. switch ((pci->subsystem_vendor << 16) |
  2763. pci->subsystem_device) {
  2764. case 0x13f69761:
  2765. case 0x584d3741:
  2766. case 0x584d3751:
  2767. case 0x584d3761:
  2768. case 0x584d3771:
  2769. case 0x72848384:
  2770. val = 8770;
  2771. break;
  2772. default:
  2773. val = 8768;
  2774. break;
  2775. }
  2776. }
  2777. }
  2778. sprintf(card->shortname, "C-Media CMI%d", val);
  2779. if (cm->chip_version < 68)
  2780. sprintf(modelstr, " (model %d)", cm->chip_version);
  2781. else
  2782. modelstr[0] = '\0';
  2783. sprintf(card->longname, "%s%s at %#lx, irq %i",
  2784. card->shortname, modelstr, cm->iobase, cm->irq);
  2785. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, cm, &ops)) < 0) {
  2786. snd_cmipci_free(cm);
  2787. return err;
  2788. }
  2789. if (cm->chip_version >= 39) {
  2790. val = snd_cmipci_read_b(cm, CM_REG_MPU_PCI + 1);
  2791. if (val != 0x00 && val != 0xff) {
  2792. iomidi = cm->iobase + CM_REG_MPU_PCI;
  2793. integrated_midi = 1;
  2794. }
  2795. }
  2796. if (!integrated_midi) {
  2797. val = 0;
  2798. iomidi = mpu_port[dev];
  2799. switch (iomidi) {
  2800. case 0x320: val = CM_VMPU_320; break;
  2801. case 0x310: val = CM_VMPU_310; break;
  2802. case 0x300: val = CM_VMPU_300; break;
  2803. case 0x330: val = CM_VMPU_330; break;
  2804. default:
  2805. iomidi = 0; break;
  2806. }
  2807. if (iomidi > 0) {
  2808. snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
  2809. /* enable UART */
  2810. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_UART_EN);
  2811. if (inb(iomidi + 1) == 0xff) {
  2812. snd_printk(KERN_ERR "cannot enable MPU-401 port"
  2813. " at %#lx\n", iomidi);
  2814. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1,
  2815. CM_UART_EN);
  2816. iomidi = 0;
  2817. }
  2818. }
  2819. }
  2820. if (cm->chip_version < 68) {
  2821. err = snd_cmipci_create_fm(cm, fm_port[dev]);
  2822. if (err < 0)
  2823. return err;
  2824. }
  2825. /* reset mixer */
  2826. snd_cmipci_mixer_write(cm, 0, 0);
  2827. snd_cmipci_proc_init(cm);
  2828. /* create pcm devices */
  2829. pcm_index = pcm_spdif_index = 0;
  2830. if ((err = snd_cmipci_pcm_new(cm, pcm_index)) < 0)
  2831. return err;
  2832. pcm_index++;
  2833. if ((err = snd_cmipci_pcm2_new(cm, pcm_index)) < 0)
  2834. return err;
  2835. pcm_index++;
  2836. if (cm->can_ac3_hw || cm->can_ac3_sw) {
  2837. pcm_spdif_index = pcm_index;
  2838. if ((err = snd_cmipci_pcm_spdif_new(cm, pcm_index)) < 0)
  2839. return err;
  2840. }
  2841. /* create mixer interface & switches */
  2842. if ((err = snd_cmipci_mixer_new(cm, pcm_spdif_index)) < 0)
  2843. return err;
  2844. if (iomidi > 0) {
  2845. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
  2846. iomidi,
  2847. (integrated_midi ?
  2848. MPU401_INFO_INTEGRATED : 0) |
  2849. MPU401_INFO_IRQ_HOOK,
  2850. -1, &cm->rmidi)) < 0) {
  2851. printk(KERN_ERR "cmipci: no UART401 device at 0x%lx\n", iomidi);
  2852. }
  2853. }
  2854. #ifdef USE_VAR48KRATE
  2855. for (val = 0; val < ARRAY_SIZE(rates); val++)
  2856. snd_cmipci_set_pll(cm, rates[val], val);
  2857. /*
  2858. * (Re-)Enable external switch spdo_48k
  2859. */
  2860. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K|CM_SPDF_AC97);
  2861. #endif /* USE_VAR48KRATE */
  2862. if (snd_cmipci_create_gameport(cm, dev) < 0)
  2863. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  2864. snd_card_set_dev(card, &pci->dev);
  2865. *rcmipci = cm;
  2866. return 0;
  2867. }
  2868. /*
  2869. */
  2870. MODULE_DEVICE_TABLE(pci, snd_cmipci_ids);
  2871. static int __devinit snd_cmipci_probe(struct pci_dev *pci,
  2872. const struct pci_device_id *pci_id)
  2873. {
  2874. static int dev;
  2875. struct snd_card *card;
  2876. struct cmipci *cm;
  2877. int err;
  2878. if (dev >= SNDRV_CARDS)
  2879. return -ENODEV;
  2880. if (! enable[dev]) {
  2881. dev++;
  2882. return -ENOENT;
  2883. }
  2884. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  2885. if (err < 0)
  2886. return err;
  2887. switch (pci->device) {
  2888. case PCI_DEVICE_ID_CMEDIA_CM8738:
  2889. case PCI_DEVICE_ID_CMEDIA_CM8738B:
  2890. strcpy(card->driver, "CMI8738");
  2891. break;
  2892. case PCI_DEVICE_ID_CMEDIA_CM8338A:
  2893. case PCI_DEVICE_ID_CMEDIA_CM8338B:
  2894. strcpy(card->driver, "CMI8338");
  2895. break;
  2896. default:
  2897. strcpy(card->driver, "CMIPCI");
  2898. break;
  2899. }
  2900. if ((err = snd_cmipci_create(card, pci, dev, &cm)) < 0) {
  2901. snd_card_free(card);
  2902. return err;
  2903. }
  2904. card->private_data = cm;
  2905. if ((err = snd_card_register(card)) < 0) {
  2906. snd_card_free(card);
  2907. return err;
  2908. }
  2909. pci_set_drvdata(pci, card);
  2910. dev++;
  2911. return 0;
  2912. }
  2913. static void __devexit snd_cmipci_remove(struct pci_dev *pci)
  2914. {
  2915. snd_card_free(pci_get_drvdata(pci));
  2916. pci_set_drvdata(pci, NULL);
  2917. }
  2918. #ifdef CONFIG_PM
  2919. /*
  2920. * power management
  2921. */
  2922. static unsigned char saved_regs[] = {
  2923. CM_REG_FUNCTRL1, CM_REG_CHFORMAT, CM_REG_LEGACY_CTRL, CM_REG_MISC_CTRL,
  2924. CM_REG_MIXER0, CM_REG_MIXER1, CM_REG_MIXER2, CM_REG_MIXER3, CM_REG_PLL,
  2925. CM_REG_CH0_FRAME1, CM_REG_CH0_FRAME2,
  2926. CM_REG_CH1_FRAME1, CM_REG_CH1_FRAME2, CM_REG_EXT_MISC,
  2927. CM_REG_INT_STATUS, CM_REG_INT_HLDCLR, CM_REG_FUNCTRL0,
  2928. };
  2929. static unsigned char saved_mixers[] = {
  2930. SB_DSP4_MASTER_DEV, SB_DSP4_MASTER_DEV + 1,
  2931. SB_DSP4_PCM_DEV, SB_DSP4_PCM_DEV + 1,
  2932. SB_DSP4_SYNTH_DEV, SB_DSP4_SYNTH_DEV + 1,
  2933. SB_DSP4_CD_DEV, SB_DSP4_CD_DEV + 1,
  2934. SB_DSP4_LINE_DEV, SB_DSP4_LINE_DEV + 1,
  2935. SB_DSP4_MIC_DEV, SB_DSP4_SPEAKER_DEV,
  2936. CM_REG_EXTENT_IND, SB_DSP4_OUTPUT_SW,
  2937. SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT,
  2938. };
  2939. static int snd_cmipci_suspend(struct pci_dev *pci, pm_message_t state)
  2940. {
  2941. struct snd_card *card = pci_get_drvdata(pci);
  2942. struct cmipci *cm = card->private_data;
  2943. int i;
  2944. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  2945. snd_pcm_suspend_all(cm->pcm);
  2946. snd_pcm_suspend_all(cm->pcm2);
  2947. snd_pcm_suspend_all(cm->pcm_spdif);
  2948. /* save registers */
  2949. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  2950. cm->saved_regs[i] = snd_cmipci_read(cm, saved_regs[i]);
  2951. for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
  2952. cm->saved_mixers[i] = snd_cmipci_mixer_read(cm, saved_mixers[i]);
  2953. /* disable ints */
  2954. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
  2955. pci_disable_device(pci);
  2956. pci_save_state(pci);
  2957. pci_set_power_state(pci, pci_choose_state(pci, state));
  2958. return 0;
  2959. }
  2960. static int snd_cmipci_resume(struct pci_dev *pci)
  2961. {
  2962. struct snd_card *card = pci_get_drvdata(pci);
  2963. struct cmipci *cm = card->private_data;
  2964. int i;
  2965. pci_set_power_state(pci, PCI_D0);
  2966. pci_restore_state(pci);
  2967. if (pci_enable_device(pci) < 0) {
  2968. printk(KERN_ERR "cmipci: pci_enable_device failed, "
  2969. "disabling device\n");
  2970. snd_card_disconnect(card);
  2971. return -EIO;
  2972. }
  2973. pci_set_master(pci);
  2974. /* reset / initialize to a sane state */
  2975. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
  2976. snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  2977. snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  2978. snd_cmipci_mixer_write(cm, 0, 0);
  2979. /* restore registers */
  2980. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  2981. snd_cmipci_write(cm, saved_regs[i], cm->saved_regs[i]);
  2982. for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
  2983. snd_cmipci_mixer_write(cm, saved_mixers[i], cm->saved_mixers[i]);
  2984. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  2985. return 0;
  2986. }
  2987. #endif /* CONFIG_PM */
  2988. static struct pci_driver driver = {
  2989. .name = KBUILD_MODNAME,
  2990. .id_table = snd_cmipci_ids,
  2991. .probe = snd_cmipci_probe,
  2992. .remove = __devexit_p(snd_cmipci_remove),
  2993. #ifdef CONFIG_PM
  2994. .suspend = snd_cmipci_suspend,
  2995. .resume = snd_cmipci_resume,
  2996. #endif
  2997. };
  2998. static int __init alsa_card_cmipci_init(void)
  2999. {
  3000. return pci_register_driver(&driver);
  3001. }
  3002. static void __exit alsa_card_cmipci_exit(void)
  3003. {
  3004. pci_unregister_driver(&driver);
  3005. }
  3006. module_init(alsa_card_cmipci_init)
  3007. module_exit(alsa_card_cmipci_exit)