cmipci.c 101 KB

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