cmipci.c 100 KB

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