cmipci.c 86 KB

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