cmipci.c 102 KB

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