cmipci.c 90 KB

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