cmipci.c 90 KB

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