cmipci.c 102 KB

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