cmipci.c 102 KB

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