cmipci.c 90 KB

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