cmipci.c 99 KB

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