via82xx.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556
  1. /*
  2. * ALSA driver for VIA VT82xx (South Bridge)
  3. *
  4. * VT82C686A/B/C, VT8233A/C, VT8235
  5. *
  6. * Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
  7. * Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
  8. * 2002 Takashi Iwai <tiwai@suse.de>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. /*
  26. * Changes:
  27. *
  28. * Dec. 19, 2002 Takashi Iwai <tiwai@suse.de>
  29. * - use the DSX channels for the first pcm playback.
  30. * (on VIA8233, 8233C and 8235 only)
  31. * this will allow you play simultaneously up to 4 streams.
  32. * multi-channel playback is assigned to the second device
  33. * on these chips.
  34. * - support the secondary capture (on VIA8233/C,8235)
  35. * - SPDIF support
  36. * the DSX3 channel can be used for SPDIF output.
  37. * on VIA8233A, this channel is assigned to the second pcm
  38. * playback.
  39. * the card config of alsa-lib will assign the correct
  40. * device for applications.
  41. * - clean up the code, separate low-level initialization
  42. * routines for each chipset.
  43. *
  44. * Sep. 26, 2005 Karsten Wiese <annabellesgarden@yahoo.de>
  45. * - Optimize position calculation for the 823x chips.
  46. */
  47. #include <sound/driver.h>
  48. #include <asm/io.h>
  49. #include <linux/delay.h>
  50. #include <linux/interrupt.h>
  51. #include <linux/init.h>
  52. #include <linux/pci.h>
  53. #include <linux/slab.h>
  54. #include <linux/gameport.h>
  55. #include <linux/moduleparam.h>
  56. #include <sound/core.h>
  57. #include <sound/pcm.h>
  58. #include <sound/pcm_params.h>
  59. #include <sound/info.h>
  60. #include <sound/ac97_codec.h>
  61. #include <sound/mpu401.h>
  62. #include <sound/initval.h>
  63. #if 0
  64. #define POINTER_DEBUG
  65. #endif
  66. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  67. MODULE_DESCRIPTION("VIA VT82xx audio");
  68. MODULE_LICENSE("GPL");
  69. MODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C,pci},{VIA,VT8233A/C,8235}}");
  70. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  71. #define SUPPORT_JOYSTICK 1
  72. #endif
  73. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  74. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  75. static long mpu_port;
  76. #ifdef SUPPORT_JOYSTICK
  77. static int joystick;
  78. #endif
  79. static int ac97_clock = 48000;
  80. static char *ac97_quirk;
  81. static int dxs_support;
  82. module_param(index, int, 0444);
  83. MODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
  84. module_param(id, charp, 0444);
  85. MODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
  86. module_param(mpu_port, long, 0444);
  87. MODULE_PARM_DESC(mpu_port, "MPU-401 port. (VT82C686x only)");
  88. #ifdef SUPPORT_JOYSTICK
  89. module_param(joystick, bool, 0444);
  90. MODULE_PARM_DESC(joystick, "Enable joystick. (VT82C686x only)");
  91. #endif
  92. module_param(ac97_clock, int, 0444);
  93. MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
  94. module_param(ac97_quirk, charp, 0444);
  95. MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
  96. module_param(dxs_support, int, 0444);
  97. MODULE_PARM_DESC(dxs_support, "Support for DXS channels (0 = auto, 1 = enable, 2 = disable, 3 = 48k only, 4 = no VRA, 5 = enable any sample rate)");
  98. /* just for backward compatibility */
  99. static int enable;
  100. module_param(enable, bool, 0444);
  101. /* revision numbers for via686 */
  102. #define VIA_REV_686_A 0x10
  103. #define VIA_REV_686_B 0x11
  104. #define VIA_REV_686_C 0x12
  105. #define VIA_REV_686_D 0x13
  106. #define VIA_REV_686_E 0x14
  107. #define VIA_REV_686_H 0x20
  108. /* revision numbers for via8233 */
  109. #define VIA_REV_PRE_8233 0x10 /* not in market */
  110. #define VIA_REV_8233C 0x20 /* 2 rec, 4 pb, 1 multi-pb */
  111. #define VIA_REV_8233 0x30 /* 2 rec, 4 pb, 1 multi-pb, spdif */
  112. #define VIA_REV_8233A 0x40 /* 1 rec, 1 multi-pb, spdf */
  113. #define VIA_REV_8235 0x50 /* 2 rec, 4 pb, 1 multi-pb, spdif */
  114. #define VIA_REV_8237 0x60
  115. /*
  116. * Direct registers
  117. */
  118. #define VIAREG(via, x) ((via)->port + VIA_REG_##x)
  119. #define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
  120. /* common offsets */
  121. #define VIA_REG_OFFSET_STATUS 0x00 /* byte - channel status */
  122. #define VIA_REG_STAT_ACTIVE 0x80 /* RO */
  123. #define VIA8233_SHADOW_STAT_ACTIVE 0x08 /* RO */
  124. #define VIA_REG_STAT_PAUSED 0x40 /* RO */
  125. #define VIA_REG_STAT_TRIGGER_QUEUED 0x08 /* RO */
  126. #define VIA_REG_STAT_STOPPED 0x04 /* RWC */
  127. #define VIA_REG_STAT_EOL 0x02 /* RWC */
  128. #define VIA_REG_STAT_FLAG 0x01 /* RWC */
  129. #define VIA_REG_OFFSET_CONTROL 0x01 /* byte - channel control */
  130. #define VIA_REG_CTRL_START 0x80 /* WO */
  131. #define VIA_REG_CTRL_TERMINATE 0x40 /* WO */
  132. #define VIA_REG_CTRL_AUTOSTART 0x20
  133. #define VIA_REG_CTRL_PAUSE 0x08 /* RW */
  134. #define VIA_REG_CTRL_INT_STOP 0x04
  135. #define VIA_REG_CTRL_INT_EOL 0x02
  136. #define VIA_REG_CTRL_INT_FLAG 0x01
  137. #define VIA_REG_CTRL_RESET 0x01 /* RW - probably reset? undocumented */
  138. #define VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
  139. #define VIA_REG_OFFSET_TYPE 0x02 /* byte - channel type (686 only) */
  140. #define VIA_REG_TYPE_AUTOSTART 0x80 /* RW - autostart at EOL */
  141. #define VIA_REG_TYPE_16BIT 0x20 /* RW */
  142. #define VIA_REG_TYPE_STEREO 0x10 /* RW */
  143. #define VIA_REG_TYPE_INT_LLINE 0x00
  144. #define VIA_REG_TYPE_INT_LSAMPLE 0x04
  145. #define VIA_REG_TYPE_INT_LESSONE 0x08
  146. #define VIA_REG_TYPE_INT_MASK 0x0c
  147. #define VIA_REG_TYPE_INT_EOL 0x02
  148. #define VIA_REG_TYPE_INT_FLAG 0x01
  149. #define VIA_REG_OFFSET_TABLE_PTR 0x04 /* dword - channel table pointer */
  150. #define VIA_REG_OFFSET_CURR_PTR 0x04 /* dword - channel current pointer */
  151. #define VIA_REG_OFFSET_STOP_IDX 0x08 /* dword - stop index, channel type, sample rate */
  152. #define VIA8233_REG_TYPE_16BIT 0x00200000 /* RW */
  153. #define VIA8233_REG_TYPE_STEREO 0x00100000 /* RW */
  154. #define VIA_REG_OFFSET_CURR_COUNT 0x0c /* dword - channel current count (24 bit) */
  155. #define VIA_REG_OFFSET_CURR_INDEX 0x0f /* byte - channel current index (for via8233 only) */
  156. #define DEFINE_VIA_REGSET(name,val) \
  157. enum {\
  158. VIA_REG_##name##_STATUS = (val),\
  159. VIA_REG_##name##_CONTROL = (val) + 0x01,\
  160. VIA_REG_##name##_TYPE = (val) + 0x02,\
  161. VIA_REG_##name##_TABLE_PTR = (val) + 0x04,\
  162. VIA_REG_##name##_CURR_PTR = (val) + 0x04,\
  163. VIA_REG_##name##_STOP_IDX = (val) + 0x08,\
  164. VIA_REG_##name##_CURR_COUNT = (val) + 0x0c,\
  165. }
  166. /* playback block */
  167. DEFINE_VIA_REGSET(PLAYBACK, 0x00);
  168. DEFINE_VIA_REGSET(CAPTURE, 0x10);
  169. DEFINE_VIA_REGSET(FM, 0x20);
  170. /* AC'97 */
  171. #define VIA_REG_AC97 0x80 /* dword */
  172. #define VIA_REG_AC97_CODEC_ID_MASK (3<<30)
  173. #define VIA_REG_AC97_CODEC_ID_SHIFT 30
  174. #define VIA_REG_AC97_CODEC_ID_PRIMARY 0x00
  175. #define VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
  176. #define VIA_REG_AC97_SECONDARY_VALID (1<<27)
  177. #define VIA_REG_AC97_PRIMARY_VALID (1<<25)
  178. #define VIA_REG_AC97_BUSY (1<<24)
  179. #define VIA_REG_AC97_READ (1<<23)
  180. #define VIA_REG_AC97_CMD_SHIFT 16
  181. #define VIA_REG_AC97_CMD_MASK 0x7e
  182. #define VIA_REG_AC97_DATA_SHIFT 0
  183. #define VIA_REG_AC97_DATA_MASK 0xffff
  184. #define VIA_REG_SGD_SHADOW 0x84 /* dword */
  185. /* via686 */
  186. #define VIA_REG_SGD_STAT_PB_FLAG (1<<0)
  187. #define VIA_REG_SGD_STAT_CP_FLAG (1<<1)
  188. #define VIA_REG_SGD_STAT_FM_FLAG (1<<2)
  189. #define VIA_REG_SGD_STAT_PB_EOL (1<<4)
  190. #define VIA_REG_SGD_STAT_CP_EOL (1<<5)
  191. #define VIA_REG_SGD_STAT_FM_EOL (1<<6)
  192. #define VIA_REG_SGD_STAT_PB_STOP (1<<8)
  193. #define VIA_REG_SGD_STAT_CP_STOP (1<<9)
  194. #define VIA_REG_SGD_STAT_FM_STOP (1<<10)
  195. #define VIA_REG_SGD_STAT_PB_ACTIVE (1<<12)
  196. #define VIA_REG_SGD_STAT_CP_ACTIVE (1<<13)
  197. #define VIA_REG_SGD_STAT_FM_ACTIVE (1<<14)
  198. /* via8233 */
  199. #define VIA8233_REG_SGD_STAT_FLAG (1<<0)
  200. #define VIA8233_REG_SGD_STAT_EOL (1<<1)
  201. #define VIA8233_REG_SGD_STAT_STOP (1<<2)
  202. #define VIA8233_REG_SGD_STAT_ACTIVE (1<<3)
  203. #define VIA8233_INTR_MASK(chan) ((VIA8233_REG_SGD_STAT_FLAG|VIA8233_REG_SGD_STAT_EOL) << ((chan) * 4))
  204. #define VIA8233_REG_SGD_CHAN_SDX 0
  205. #define VIA8233_REG_SGD_CHAN_MULTI 4
  206. #define VIA8233_REG_SGD_CHAN_REC 6
  207. #define VIA8233_REG_SGD_CHAN_REC1 7
  208. #define VIA_REG_GPI_STATUS 0x88
  209. #define VIA_REG_GPI_INTR 0x8c
  210. /* multi-channel and capture registers for via8233 */
  211. DEFINE_VIA_REGSET(MULTPLAY, 0x40);
  212. DEFINE_VIA_REGSET(CAPTURE_8233, 0x60);
  213. /* via8233-specific registers */
  214. #define VIA_REG_OFS_PLAYBACK_VOLUME_L 0x02 /* byte */
  215. #define VIA_REG_OFS_PLAYBACK_VOLUME_R 0x03 /* byte */
  216. #define VIA_REG_OFS_MULTPLAY_FORMAT 0x02 /* byte - format and channels */
  217. #define VIA_REG_MULTPLAY_FMT_8BIT 0x00
  218. #define VIA_REG_MULTPLAY_FMT_16BIT 0x80
  219. #define VIA_REG_MULTPLAY_FMT_CH_MASK 0x70 /* # channels << 4 (valid = 1,2,4,6) */
  220. #define VIA_REG_OFS_CAPTURE_FIFO 0x02 /* byte - bit 6 = fifo enable */
  221. #define VIA_REG_CAPTURE_FIFO_ENABLE 0x40
  222. #define VIA_DXS_MAX_VOLUME 31 /* max. volume (attenuation) of reg 0x32/33 */
  223. #define VIA_REG_CAPTURE_CHANNEL 0x63 /* byte - input select */
  224. #define VIA_REG_CAPTURE_CHANNEL_MIC 0x4
  225. #define VIA_REG_CAPTURE_CHANNEL_LINE 0
  226. #define VIA_REG_CAPTURE_SELECT_CODEC 0x03 /* recording source codec (0 = primary) */
  227. #define VIA_TBL_BIT_FLAG 0x40000000
  228. #define VIA_TBL_BIT_EOL 0x80000000
  229. /* pci space */
  230. #define VIA_ACLINK_STAT 0x40
  231. #define VIA_ACLINK_C11_READY 0x20
  232. #define VIA_ACLINK_C10_READY 0x10
  233. #define VIA_ACLINK_C01_READY 0x04 /* secondary codec ready */
  234. #define VIA_ACLINK_LOWPOWER 0x02 /* low-power state */
  235. #define VIA_ACLINK_C00_READY 0x01 /* primary codec ready */
  236. #define VIA_ACLINK_CTRL 0x41
  237. #define VIA_ACLINK_CTRL_ENABLE 0x80 /* 0: disable, 1: enable */
  238. #define VIA_ACLINK_CTRL_RESET 0x40 /* 0: assert, 1: de-assert */
  239. #define VIA_ACLINK_CTRL_SYNC 0x20 /* 0: release SYNC, 1: force SYNC hi */
  240. #define VIA_ACLINK_CTRL_SDO 0x10 /* 0: release SDO, 1: force SDO hi */
  241. #define VIA_ACLINK_CTRL_VRA 0x08 /* 0: disable VRA, 1: enable VRA */
  242. #define VIA_ACLINK_CTRL_PCM 0x04 /* 0: disable PCM, 1: enable PCM */
  243. #define VIA_ACLINK_CTRL_FM 0x02 /* via686 only */
  244. #define VIA_ACLINK_CTRL_SB 0x01 /* via686 only */
  245. #define VIA_ACLINK_CTRL_INIT (VIA_ACLINK_CTRL_ENABLE|\
  246. VIA_ACLINK_CTRL_RESET|\
  247. VIA_ACLINK_CTRL_PCM|\
  248. VIA_ACLINK_CTRL_VRA)
  249. #define VIA_FUNC_ENABLE 0x42
  250. #define VIA_FUNC_MIDI_PNP 0x80 /* FIXME: it's 0x40 in the datasheet! */
  251. #define VIA_FUNC_MIDI_IRQMASK 0x40 /* FIXME: not documented! */
  252. #define VIA_FUNC_RX2C_WRITE 0x20
  253. #define VIA_FUNC_SB_FIFO_EMPTY 0x10
  254. #define VIA_FUNC_ENABLE_GAME 0x08
  255. #define VIA_FUNC_ENABLE_FM 0x04
  256. #define VIA_FUNC_ENABLE_MIDI 0x02
  257. #define VIA_FUNC_ENABLE_SB 0x01
  258. #define VIA_PNP_CONTROL 0x43
  259. #define VIA_FM_NMI_CTRL 0x48
  260. #define VIA8233_VOLCHG_CTRL 0x48
  261. #define VIA8233_SPDIF_CTRL 0x49
  262. #define VIA8233_SPDIF_DX3 0x08
  263. #define VIA8233_SPDIF_SLOT_MASK 0x03
  264. #define VIA8233_SPDIF_SLOT_1011 0x00
  265. #define VIA8233_SPDIF_SLOT_34 0x01
  266. #define VIA8233_SPDIF_SLOT_78 0x02
  267. #define VIA8233_SPDIF_SLOT_69 0x03
  268. /*
  269. */
  270. #define VIA_DXS_AUTO 0
  271. #define VIA_DXS_ENABLE 1
  272. #define VIA_DXS_DISABLE 2
  273. #define VIA_DXS_48K 3
  274. #define VIA_DXS_NO_VRA 4
  275. #define VIA_DXS_SRC 5
  276. /*
  277. * pcm stream
  278. */
  279. struct snd_via_sg_table {
  280. unsigned int offset;
  281. unsigned int size;
  282. } ;
  283. #define VIA_TABLE_SIZE 255
  284. struct viadev {
  285. unsigned int reg_offset;
  286. unsigned long port;
  287. int direction; /* playback = 0, capture = 1 */
  288. struct snd_pcm_substream *substream;
  289. int running;
  290. unsigned int tbl_entries; /* # descriptors */
  291. struct snd_dma_buffer table;
  292. struct snd_via_sg_table *idx_table;
  293. /* for recovery from the unexpected pointer */
  294. unsigned int lastpos;
  295. unsigned int fragsize;
  296. unsigned int bufsize;
  297. unsigned int bufsize2;
  298. int hwptr_done; /* processed frame position in the buffer */
  299. int in_interrupt;
  300. int shadow_shift;
  301. };
  302. enum { TYPE_CARD_VIA686 = 1, TYPE_CARD_VIA8233 };
  303. enum { TYPE_VIA686, TYPE_VIA8233, TYPE_VIA8233A };
  304. #define VIA_MAX_DEVS 7 /* 4 playback, 1 multi, 2 capture */
  305. struct via_rate_lock {
  306. spinlock_t lock;
  307. int rate;
  308. int used;
  309. };
  310. struct via82xx {
  311. int irq;
  312. unsigned long port;
  313. struct resource *mpu_res;
  314. int chip_type;
  315. unsigned char revision;
  316. unsigned char old_legacy;
  317. unsigned char old_legacy_cfg;
  318. #ifdef CONFIG_PM
  319. unsigned char legacy_saved;
  320. unsigned char legacy_cfg_saved;
  321. unsigned char spdif_ctrl_saved;
  322. unsigned char capture_src_saved[2];
  323. unsigned int mpu_port_saved;
  324. #endif
  325. unsigned char playback_volume[4][2]; /* for VIA8233/C/8235; default = 0 */
  326. unsigned char playback_volume_c[2]; /* for VIA8233/C/8235; default = 0 */
  327. unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
  328. struct pci_dev *pci;
  329. struct snd_card *card;
  330. unsigned int num_devs;
  331. unsigned int playback_devno, multi_devno, capture_devno;
  332. struct viadev devs[VIA_MAX_DEVS];
  333. struct via_rate_lock rates[2]; /* playback and capture */
  334. unsigned int dxs_fixed: 1; /* DXS channel accepts only 48kHz */
  335. unsigned int no_vra: 1; /* no need to set VRA on DXS channels */
  336. unsigned int dxs_src: 1; /* use full SRC capabilities of DXS */
  337. unsigned int spdif_on: 1; /* only spdif rates work to external DACs */
  338. struct snd_pcm *pcms[2];
  339. struct snd_rawmidi *rmidi;
  340. struct snd_ac97_bus *ac97_bus;
  341. struct snd_ac97 *ac97;
  342. unsigned int ac97_clock;
  343. unsigned int ac97_secondary; /* secondary AC'97 codec is present */
  344. spinlock_t reg_lock;
  345. struct snd_info_entry *proc_entry;
  346. #ifdef SUPPORT_JOYSTICK
  347. struct gameport *gameport;
  348. #endif
  349. };
  350. static struct pci_device_id snd_via82xx_ids[] = {
  351. /* 0x1106, 0x3058 */
  352. { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA686, }, /* 686A */
  353. /* 0x1106, 0x3059 */
  354. { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPE_CARD_VIA8233, }, /* VT8233 */
  355. { 0, }
  356. };
  357. MODULE_DEVICE_TABLE(pci, snd_via82xx_ids);
  358. /*
  359. */
  360. /*
  361. * allocate and initialize the descriptor buffers
  362. * periods = number of periods
  363. * fragsize = period size in bytes
  364. */
  365. static int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
  366. struct pci_dev *pci,
  367. unsigned int periods, unsigned int fragsize)
  368. {
  369. unsigned int i, idx, ofs, rest;
  370. struct via82xx *chip = snd_pcm_substream_chip(substream);
  371. struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
  372. if (dev->table.area == NULL) {
  373. /* the start of each lists must be aligned to 8 bytes,
  374. * but the kernel pages are much bigger, so we don't care
  375. */
  376. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  377. PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
  378. &dev->table) < 0)
  379. return -ENOMEM;
  380. }
  381. if (! dev->idx_table) {
  382. dev->idx_table = kmalloc(sizeof(*dev->idx_table) * VIA_TABLE_SIZE, GFP_KERNEL);
  383. if (! dev->idx_table)
  384. return -ENOMEM;
  385. }
  386. /* fill the entries */
  387. idx = 0;
  388. ofs = 0;
  389. for (i = 0; i < periods; i++) {
  390. rest = fragsize;
  391. /* fill descriptors for a period.
  392. * a period can be split to several descriptors if it's
  393. * over page boundary.
  394. */
  395. do {
  396. unsigned int r;
  397. unsigned int flag;
  398. if (idx >= VIA_TABLE_SIZE) {
  399. snd_printk(KERN_ERR "via82xx: too much table size!\n");
  400. return -EINVAL;
  401. }
  402. ((u32 *)dev->table.area)[idx << 1] = cpu_to_le32((u32)snd_pcm_sgbuf_get_addr(sgbuf, ofs));
  403. r = PAGE_SIZE - (ofs % PAGE_SIZE);
  404. if (rest < r)
  405. r = rest;
  406. rest -= r;
  407. if (! rest) {
  408. if (i == periods - 1)
  409. flag = VIA_TBL_BIT_EOL; /* buffer boundary */
  410. else
  411. flag = VIA_TBL_BIT_FLAG; /* period boundary */
  412. } else
  413. flag = 0; /* period continues to the next */
  414. // printk("via: tbl %d: at %d size %d (rest %d)\n", idx, ofs, r, rest);
  415. ((u32 *)dev->table.area)[(idx<<1) + 1] = cpu_to_le32(r | flag);
  416. dev->idx_table[idx].offset = ofs;
  417. dev->idx_table[idx].size = r;
  418. ofs += r;
  419. idx++;
  420. } while (rest > 0);
  421. }
  422. dev->tbl_entries = idx;
  423. dev->bufsize = periods * fragsize;
  424. dev->bufsize2 = dev->bufsize / 2;
  425. dev->fragsize = fragsize;
  426. return 0;
  427. }
  428. static int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
  429. struct pci_dev *pci)
  430. {
  431. if (dev->table.area) {
  432. snd_dma_free_pages(&dev->table);
  433. dev->table.area = NULL;
  434. }
  435. kfree(dev->idx_table);
  436. dev->idx_table = NULL;
  437. return 0;
  438. }
  439. /*
  440. * Basic I/O
  441. */
  442. static inline unsigned int snd_via82xx_codec_xread(struct via82xx *chip)
  443. {
  444. return inl(VIAREG(chip, AC97));
  445. }
  446. static inline void snd_via82xx_codec_xwrite(struct via82xx *chip, unsigned int val)
  447. {
  448. outl(val, VIAREG(chip, AC97));
  449. }
  450. static int snd_via82xx_codec_ready(struct via82xx *chip, int secondary)
  451. {
  452. unsigned int timeout = 1000; /* 1ms */
  453. unsigned int val;
  454. while (timeout-- > 0) {
  455. udelay(1);
  456. if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
  457. return val & 0xffff;
  458. }
  459. snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n",
  460. secondary, snd_via82xx_codec_xread(chip));
  461. return -EIO;
  462. }
  463. static int snd_via82xx_codec_valid(struct via82xx *chip, int secondary)
  464. {
  465. unsigned int timeout = 1000; /* 1ms */
  466. unsigned int val, val1;
  467. unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
  468. VIA_REG_AC97_SECONDARY_VALID;
  469. while (timeout-- > 0) {
  470. val = snd_via82xx_codec_xread(chip);
  471. val1 = val & (VIA_REG_AC97_BUSY | stat);
  472. if (val1 == stat)
  473. return val & 0xffff;
  474. udelay(1);
  475. }
  476. return -EIO;
  477. }
  478. static void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
  479. {
  480. struct via82xx *chip = ac97->private_data;
  481. int err;
  482. err = snd_via82xx_codec_ready(chip, ac97->num);
  483. /* here we need to wait fairly for long time.. */
  484. msleep(500);
  485. }
  486. static void snd_via82xx_codec_write(struct snd_ac97 *ac97,
  487. unsigned short reg,
  488. unsigned short val)
  489. {
  490. struct via82xx *chip = ac97->private_data;
  491. unsigned int xval;
  492. xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
  493. xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
  494. xval |= reg << VIA_REG_AC97_CMD_SHIFT;
  495. xval |= val << VIA_REG_AC97_DATA_SHIFT;
  496. snd_via82xx_codec_xwrite(chip, xval);
  497. snd_via82xx_codec_ready(chip, ac97->num);
  498. }
  499. static unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
  500. {
  501. struct via82xx *chip = ac97->private_data;
  502. unsigned int xval, val = 0xffff;
  503. int again = 0;
  504. xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
  505. xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
  506. xval |= VIA_REG_AC97_READ;
  507. xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
  508. while (1) {
  509. if (again++ > 3) {
  510. snd_printk(KERN_ERR "codec_read: codec %i is not valid [0x%x]\n",
  511. ac97->num, snd_via82xx_codec_xread(chip));
  512. return 0xffff;
  513. }
  514. snd_via82xx_codec_xwrite(chip, xval);
  515. udelay (20);
  516. if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
  517. udelay(25);
  518. val = snd_via82xx_codec_xread(chip);
  519. break;
  520. }
  521. }
  522. return val & 0xffff;
  523. }
  524. static void snd_via82xx_channel_reset(struct via82xx *chip, struct viadev *viadev)
  525. {
  526. outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
  527. VIADEV_REG(viadev, OFFSET_CONTROL));
  528. inb(VIADEV_REG(viadev, OFFSET_CONTROL));
  529. udelay(50);
  530. /* disable interrupts */
  531. outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
  532. /* clear interrupts */
  533. outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
  534. outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
  535. // outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
  536. viadev->lastpos = 0;
  537. viadev->hwptr_done = 0;
  538. }
  539. /*
  540. * Interrupt handler
  541. * Used for 686 and 8233A
  542. */
  543. static irqreturn_t snd_via686_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  544. {
  545. struct via82xx *chip = dev_id;
  546. unsigned int status;
  547. unsigned int i;
  548. status = inl(VIAREG(chip, SGD_SHADOW));
  549. if (! (status & chip->intr_mask)) {
  550. if (chip->rmidi)
  551. /* check mpu401 interrupt */
  552. return snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
  553. return IRQ_NONE;
  554. }
  555. /* check status for each stream */
  556. spin_lock(&chip->reg_lock);
  557. for (i = 0; i < chip->num_devs; i++) {
  558. struct viadev *viadev = &chip->devs[i];
  559. unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
  560. if (! (c_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED)))
  561. continue;
  562. if (viadev->substream && viadev->running) {
  563. /*
  564. * Update hwptr_done based on 'period elapsed'
  565. * interrupts. We'll use it, when the chip returns 0
  566. * for OFFSET_CURR_COUNT.
  567. */
  568. if (c_status & VIA_REG_STAT_EOL)
  569. viadev->hwptr_done = 0;
  570. else
  571. viadev->hwptr_done += viadev->fragsize;
  572. viadev->in_interrupt = c_status;
  573. spin_unlock(&chip->reg_lock);
  574. snd_pcm_period_elapsed(viadev->substream);
  575. spin_lock(&chip->reg_lock);
  576. viadev->in_interrupt = 0;
  577. }
  578. outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
  579. }
  580. spin_unlock(&chip->reg_lock);
  581. return IRQ_HANDLED;
  582. }
  583. /*
  584. * Interrupt handler
  585. */
  586. static irqreturn_t snd_via8233_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  587. {
  588. struct via82xx *chip = dev_id;
  589. unsigned int status;
  590. unsigned int i;
  591. int irqreturn = 0;
  592. /* check status for each stream */
  593. spin_lock(&chip->reg_lock);
  594. status = inl(VIAREG(chip, SGD_SHADOW));
  595. for (i = 0; i < chip->num_devs; i++) {
  596. struct viadev *viadev = &chip->devs[i];
  597. struct snd_pcm_substream *substream;
  598. unsigned char c_status, shadow_status;
  599. shadow_status = (status >> viadev->shadow_shift) &
  600. (VIA8233_SHADOW_STAT_ACTIVE|VIA_REG_STAT_EOL|
  601. VIA_REG_STAT_FLAG);
  602. c_status = shadow_status & (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG);
  603. if (!c_status)
  604. continue;
  605. substream = viadev->substream;
  606. if (substream && viadev->running) {
  607. /*
  608. * Update hwptr_done based on 'period elapsed'
  609. * interrupts. We'll use it, when the chip returns 0
  610. * for OFFSET_CURR_COUNT.
  611. */
  612. if (c_status & VIA_REG_STAT_EOL)
  613. viadev->hwptr_done = 0;
  614. else
  615. viadev->hwptr_done += viadev->fragsize;
  616. viadev->in_interrupt = c_status;
  617. if (shadow_status & VIA8233_SHADOW_STAT_ACTIVE)
  618. viadev->in_interrupt |= VIA_REG_STAT_ACTIVE;
  619. spin_unlock(&chip->reg_lock);
  620. snd_pcm_period_elapsed(substream);
  621. spin_lock(&chip->reg_lock);
  622. viadev->in_interrupt = 0;
  623. }
  624. outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
  625. irqreturn = 1;
  626. }
  627. spin_unlock(&chip->reg_lock);
  628. return IRQ_RETVAL(irqreturn);
  629. }
  630. /*
  631. * PCM callbacks
  632. */
  633. /*
  634. * trigger callback
  635. */
  636. static int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  637. {
  638. struct via82xx *chip = snd_pcm_substream_chip(substream);
  639. struct viadev *viadev = substream->runtime->private_data;
  640. unsigned char val;
  641. if (chip->chip_type != TYPE_VIA686)
  642. val = VIA_REG_CTRL_INT;
  643. else
  644. val = 0;
  645. switch (cmd) {
  646. case SNDRV_PCM_TRIGGER_START:
  647. case SNDRV_PCM_TRIGGER_RESUME:
  648. val |= VIA_REG_CTRL_START;
  649. viadev->running = 1;
  650. break;
  651. case SNDRV_PCM_TRIGGER_STOP:
  652. case SNDRV_PCM_TRIGGER_SUSPEND:
  653. val = VIA_REG_CTRL_TERMINATE;
  654. viadev->running = 0;
  655. break;
  656. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  657. val |= VIA_REG_CTRL_PAUSE;
  658. viadev->running = 0;
  659. break;
  660. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  661. viadev->running = 1;
  662. break;
  663. default:
  664. return -EINVAL;
  665. }
  666. outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
  667. if (cmd == SNDRV_PCM_TRIGGER_STOP)
  668. snd_via82xx_channel_reset(chip, viadev);
  669. return 0;
  670. }
  671. /*
  672. * pointer callbacks
  673. */
  674. /*
  675. * calculate the linear position at the given sg-buffer index and the rest count
  676. */
  677. #define check_invalid_pos(viadev,pos) \
  678. ((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
  679. viadev->lastpos < viadev->bufsize2))
  680. static inline unsigned int calc_linear_pos(struct viadev *viadev, unsigned int idx,
  681. unsigned int count)
  682. {
  683. unsigned int size, base, res;
  684. size = viadev->idx_table[idx].size;
  685. base = viadev->idx_table[idx].offset;
  686. res = base + size - count;
  687. if (res >= viadev->bufsize)
  688. res -= viadev->bufsize;
  689. /* check the validity of the calculated position */
  690. if (size < count) {
  691. snd_printd(KERN_ERR "invalid via82xx_cur_ptr (size = %d, count = %d)\n",
  692. (int)size, (int)count);
  693. res = viadev->lastpos;
  694. } else {
  695. if (! count) {
  696. /* Some mobos report count = 0 on the DMA boundary,
  697. * i.e. count = size indeed.
  698. * Let's check whether this step is above the expected size.
  699. */
  700. int delta = res - viadev->lastpos;
  701. if (delta < 0)
  702. delta += viadev->bufsize;
  703. if ((unsigned int)delta > viadev->fragsize)
  704. res = base;
  705. }
  706. if (check_invalid_pos(viadev, res)) {
  707. #ifdef POINTER_DEBUG
  708. printk(KERN_DEBUG "fail: idx = %i/%i, lastpos = 0x%x, "
  709. "bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, "
  710. "count = 0x%x\n", idx, viadev->tbl_entries,
  711. viadev->lastpos, viadev->bufsize2,
  712. viadev->idx_table[idx].offset,
  713. viadev->idx_table[idx].size, count);
  714. #endif
  715. /* count register returns full size when end of buffer is reached */
  716. res = base + size;
  717. if (check_invalid_pos(viadev, res)) {
  718. snd_printd(KERN_ERR "invalid via82xx_cur_ptr (2), "
  719. "using last valid pointer\n");
  720. res = viadev->lastpos;
  721. }
  722. }
  723. }
  724. return res;
  725. }
  726. /*
  727. * get the current pointer on via686
  728. */
  729. static snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
  730. {
  731. struct via82xx *chip = snd_pcm_substream_chip(substream);
  732. struct viadev *viadev = substream->runtime->private_data;
  733. unsigned int idx, ptr, count, res;
  734. snd_assert(viadev->tbl_entries, return 0);
  735. if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
  736. return 0;
  737. spin_lock(&chip->reg_lock);
  738. count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
  739. /* The via686a does not have the current index register,
  740. * so we need to calculate the index from CURR_PTR.
  741. */
  742. ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
  743. if (ptr <= (unsigned int)viadev->table.addr)
  744. idx = 0;
  745. else /* CURR_PTR holds the address + 8 */
  746. idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) % viadev->tbl_entries;
  747. res = calc_linear_pos(viadev, idx, count);
  748. viadev->lastpos = res; /* remember the last position */
  749. spin_unlock(&chip->reg_lock);
  750. return bytes_to_frames(substream->runtime, res);
  751. }
  752. /*
  753. * get the current pointer on via823x
  754. */
  755. static snd_pcm_uframes_t snd_via8233_pcm_pointer(struct snd_pcm_substream *substream)
  756. {
  757. struct via82xx *chip = snd_pcm_substream_chip(substream);
  758. struct viadev *viadev = substream->runtime->private_data;
  759. unsigned int idx, count, res;
  760. int status;
  761. snd_assert(viadev->tbl_entries, return 0);
  762. spin_lock(&chip->reg_lock);
  763. count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT));
  764. status = viadev->in_interrupt;
  765. if (!status)
  766. status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
  767. if (!(status & VIA_REG_STAT_ACTIVE)) {
  768. res = 0;
  769. goto unlock;
  770. }
  771. if (count & 0xffffff) {
  772. idx = count >> 24;
  773. if (idx >= viadev->tbl_entries) {
  774. #ifdef POINTER_DEBUG
  775. printk(KERN_DEBUG "fail: invalid idx = %i/%i\n", idx,
  776. viadev->tbl_entries);
  777. #endif
  778. res = viadev->lastpos;
  779. } else {
  780. count &= 0xffffff;
  781. res = calc_linear_pos(viadev, idx, count);
  782. }
  783. } else {
  784. res = viadev->hwptr_done;
  785. if (!viadev->in_interrupt) {
  786. if (status & VIA_REG_STAT_EOL) {
  787. res = 0;
  788. } else
  789. if (status & VIA_REG_STAT_FLAG) {
  790. res += viadev->fragsize;
  791. }
  792. }
  793. }
  794. unlock:
  795. viadev->lastpos = res;
  796. spin_unlock(&chip->reg_lock);
  797. return bytes_to_frames(substream->runtime, res);
  798. }
  799. /*
  800. * hw_params callback:
  801. * allocate the buffer and build up the buffer description table
  802. */
  803. static int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
  804. struct snd_pcm_hw_params *hw_params)
  805. {
  806. struct via82xx *chip = snd_pcm_substream_chip(substream);
  807. struct viadev *viadev = substream->runtime->private_data;
  808. int err;
  809. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  810. if (err < 0)
  811. return err;
  812. err = build_via_table(viadev, substream, chip->pci,
  813. params_periods(hw_params),
  814. params_period_bytes(hw_params));
  815. if (err < 0)
  816. return err;
  817. return 0;
  818. }
  819. /*
  820. * hw_free callback:
  821. * clean up the buffer description table and release the buffer
  822. */
  823. static int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
  824. {
  825. struct via82xx *chip = snd_pcm_substream_chip(substream);
  826. struct viadev *viadev = substream->runtime->private_data;
  827. clean_via_table(viadev, substream, chip->pci);
  828. snd_pcm_lib_free_pages(substream);
  829. return 0;
  830. }
  831. /*
  832. * set up the table pointer
  833. */
  834. static void snd_via82xx_set_table_ptr(struct via82xx *chip, struct viadev *viadev)
  835. {
  836. snd_via82xx_codec_ready(chip, 0);
  837. outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
  838. udelay(20);
  839. snd_via82xx_codec_ready(chip, 0);
  840. }
  841. /*
  842. * prepare callback for playback and capture on via686
  843. */
  844. static void via686_setup_format(struct via82xx *chip, struct viadev *viadev,
  845. struct snd_pcm_runtime *runtime)
  846. {
  847. snd_via82xx_channel_reset(chip, viadev);
  848. /* this must be set after channel_reset */
  849. snd_via82xx_set_table_ptr(chip, viadev);
  850. outb(VIA_REG_TYPE_AUTOSTART |
  851. (runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA_REG_TYPE_16BIT : 0) |
  852. (runtime->channels > 1 ? VIA_REG_TYPE_STEREO : 0) |
  853. ((viadev->reg_offset & 0x10) == 0 ? VIA_REG_TYPE_INT_LSAMPLE : 0) |
  854. VIA_REG_TYPE_INT_EOL |
  855. VIA_REG_TYPE_INT_FLAG, VIADEV_REG(viadev, OFFSET_TYPE));
  856. }
  857. static int snd_via686_playback_prepare(struct snd_pcm_substream *substream)
  858. {
  859. struct via82xx *chip = snd_pcm_substream_chip(substream);
  860. struct viadev *viadev = substream->runtime->private_data;
  861. struct snd_pcm_runtime *runtime = substream->runtime;
  862. snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
  863. snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
  864. via686_setup_format(chip, viadev, runtime);
  865. return 0;
  866. }
  867. static int snd_via686_capture_prepare(struct snd_pcm_substream *substream)
  868. {
  869. struct via82xx *chip = snd_pcm_substream_chip(substream);
  870. struct viadev *viadev = substream->runtime->private_data;
  871. struct snd_pcm_runtime *runtime = substream->runtime;
  872. snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  873. via686_setup_format(chip, viadev, runtime);
  874. return 0;
  875. }
  876. /*
  877. * lock the current rate
  878. */
  879. static int via_lock_rate(struct via_rate_lock *rec, int rate)
  880. {
  881. int changed = 0;
  882. spin_lock_irq(&rec->lock);
  883. if (rec->rate != rate) {
  884. if (rec->rate && rec->used > 1) /* already set */
  885. changed = -EINVAL;
  886. else {
  887. rec->rate = rate;
  888. changed = 1;
  889. }
  890. }
  891. spin_unlock_irq(&rec->lock);
  892. return changed;
  893. }
  894. /*
  895. * prepare callback for DSX playback on via823x
  896. */
  897. static int snd_via8233_playback_prepare(struct snd_pcm_substream *substream)
  898. {
  899. struct via82xx *chip = snd_pcm_substream_chip(substream);
  900. struct viadev *viadev = substream->runtime->private_data;
  901. struct snd_pcm_runtime *runtime = substream->runtime;
  902. int ac97_rate = chip->dxs_src ? 48000 : runtime->rate;
  903. int rate_changed;
  904. u32 rbits;
  905. if ((rate_changed = via_lock_rate(&chip->rates[0], ac97_rate)) < 0)
  906. return rate_changed;
  907. if (rate_changed)
  908. snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
  909. chip->no_vra ? 48000 : runtime->rate);
  910. if (chip->spdif_on && viadev->reg_offset == 0x30)
  911. snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
  912. if (runtime->rate == 48000)
  913. rbits = 0xfffff;
  914. else
  915. rbits = (0x100000 / 48000) * runtime->rate +
  916. ((0x100000 % 48000) * runtime->rate) / 48000;
  917. snd_assert((rbits & ~0xfffff) == 0, return -EINVAL);
  918. snd_via82xx_channel_reset(chip, viadev);
  919. snd_via82xx_set_table_ptr(chip, viadev);
  920. outb(chip->playback_volume[viadev->reg_offset / 0x10][0],
  921. VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_L));
  922. outb(chip->playback_volume[viadev->reg_offset / 0x10][1],
  923. VIADEV_REG(viadev, OFS_PLAYBACK_VOLUME_R));
  924. outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) | /* format */
  925. (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) | /* stereo */
  926. rbits | /* rate */
  927. 0xff000000, /* STOP index is never reached */
  928. VIADEV_REG(viadev, OFFSET_STOP_IDX));
  929. udelay(20);
  930. snd_via82xx_codec_ready(chip, 0);
  931. return 0;
  932. }
  933. /*
  934. * prepare callback for multi-channel playback on via823x
  935. */
  936. static int snd_via8233_multi_prepare(struct snd_pcm_substream *substream)
  937. {
  938. struct via82xx *chip = snd_pcm_substream_chip(substream);
  939. struct viadev *viadev = substream->runtime->private_data;
  940. struct snd_pcm_runtime *runtime = substream->runtime;
  941. unsigned int slots;
  942. int fmt;
  943. if (via_lock_rate(&chip->rates[0], runtime->rate) < 0)
  944. return -EINVAL;
  945. snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, runtime->rate);
  946. snd_ac97_set_rate(chip->ac97, AC97_PCM_SURR_DAC_RATE, runtime->rate);
  947. snd_ac97_set_rate(chip->ac97, AC97_PCM_LFE_DAC_RATE, runtime->rate);
  948. snd_ac97_set_rate(chip->ac97, AC97_SPDIF, runtime->rate);
  949. snd_via82xx_channel_reset(chip, viadev);
  950. snd_via82xx_set_table_ptr(chip, viadev);
  951. fmt = (runtime->format == SNDRV_PCM_FORMAT_S16_LE) ?
  952. VIA_REG_MULTPLAY_FMT_16BIT : VIA_REG_MULTPLAY_FMT_8BIT;
  953. fmt |= runtime->channels << 4;
  954. outb(fmt, VIADEV_REG(viadev, OFS_MULTPLAY_FORMAT));
  955. #if 0
  956. if (chip->revision == VIA_REV_8233A)
  957. slots = 0;
  958. else
  959. #endif
  960. {
  961. /* set sample number to slot 3, 4, 7, 8, 6, 9 (for VIA8233/C,8235) */
  962. /* corresponding to FL, FR, RL, RR, C, LFE ?? */
  963. switch (runtime->channels) {
  964. case 1: slots = (1<<0) | (1<<4); break;
  965. case 2: slots = (1<<0) | (2<<4); break;
  966. case 3: slots = (1<<0) | (2<<4) | (5<<8); break;
  967. case 4: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12); break;
  968. case 5: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16); break;
  969. case 6: slots = (1<<0) | (2<<4) | (3<<8) | (4<<12) | (5<<16) | (6<<20); break;
  970. default: slots = 0; break;
  971. }
  972. }
  973. /* STOP index is never reached */
  974. outl(0xff000000 | slots, VIADEV_REG(viadev, OFFSET_STOP_IDX));
  975. udelay(20);
  976. snd_via82xx_codec_ready(chip, 0);
  977. return 0;
  978. }
  979. /*
  980. * prepare callback for capture on via823x
  981. */
  982. static int snd_via8233_capture_prepare(struct snd_pcm_substream *substream)
  983. {
  984. struct via82xx *chip = snd_pcm_substream_chip(substream);
  985. struct viadev *viadev = substream->runtime->private_data;
  986. struct snd_pcm_runtime *runtime = substream->runtime;
  987. if (via_lock_rate(&chip->rates[1], runtime->rate) < 0)
  988. return -EINVAL;
  989. snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
  990. snd_via82xx_channel_reset(chip, viadev);
  991. snd_via82xx_set_table_ptr(chip, viadev);
  992. outb(VIA_REG_CAPTURE_FIFO_ENABLE, VIADEV_REG(viadev, OFS_CAPTURE_FIFO));
  993. outl((runtime->format == SNDRV_PCM_FORMAT_S16_LE ? VIA8233_REG_TYPE_16BIT : 0) |
  994. (runtime->channels > 1 ? VIA8233_REG_TYPE_STEREO : 0) |
  995. 0xff000000, /* STOP index is never reached */
  996. VIADEV_REG(viadev, OFFSET_STOP_IDX));
  997. udelay(20);
  998. snd_via82xx_codec_ready(chip, 0);
  999. return 0;
  1000. }
  1001. /*
  1002. * pcm hardware definition, identical for both playback and capture
  1003. */
  1004. static struct snd_pcm_hardware snd_via82xx_hw =
  1005. {
  1006. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1007. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1008. SNDRV_PCM_INFO_MMAP_VALID |
  1009. /* SNDRV_PCM_INFO_RESUME | */
  1010. SNDRV_PCM_INFO_PAUSE),
  1011. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1012. .rates = SNDRV_PCM_RATE_48000,
  1013. .rate_min = 48000,
  1014. .rate_max = 48000,
  1015. .channels_min = 1,
  1016. .channels_max = 2,
  1017. .buffer_bytes_max = 128 * 1024,
  1018. .period_bytes_min = 32,
  1019. .period_bytes_max = 128 * 1024,
  1020. .periods_min = 2,
  1021. .periods_max = VIA_TABLE_SIZE / 2,
  1022. .fifo_size = 0,
  1023. };
  1024. /*
  1025. * open callback skeleton
  1026. */
  1027. static int snd_via82xx_pcm_open(struct via82xx *chip, struct viadev *viadev,
  1028. struct snd_pcm_substream *substream)
  1029. {
  1030. struct snd_pcm_runtime *runtime = substream->runtime;
  1031. int err;
  1032. struct via_rate_lock *ratep;
  1033. runtime->hw = snd_via82xx_hw;
  1034. /* set the hw rate condition */
  1035. ratep = &chip->rates[viadev->direction];
  1036. spin_lock_irq(&ratep->lock);
  1037. ratep->used++;
  1038. if (chip->spdif_on && viadev->reg_offset == 0x30) {
  1039. /* DXS#3 and spdif is on */
  1040. runtime->hw.rates = chip->ac97->rates[AC97_RATES_SPDIF];
  1041. snd_pcm_limit_hw_rates(runtime);
  1042. } else if (chip->dxs_fixed && viadev->reg_offset < 0x40) {
  1043. /* fixed DXS playback rate */
  1044. runtime->hw.rates = SNDRV_PCM_RATE_48000;
  1045. runtime->hw.rate_min = runtime->hw.rate_max = 48000;
  1046. } else if (chip->dxs_src && viadev->reg_offset < 0x40) {
  1047. /* use full SRC capabilities of DXS */
  1048. runtime->hw.rates = (SNDRV_PCM_RATE_CONTINUOUS |
  1049. SNDRV_PCM_RATE_8000_48000);
  1050. runtime->hw.rate_min = 8000;
  1051. runtime->hw.rate_max = 48000;
  1052. } else if (! ratep->rate) {
  1053. int idx = viadev->direction ? AC97_RATES_ADC : AC97_RATES_FRONT_DAC;
  1054. runtime->hw.rates = chip->ac97->rates[idx];
  1055. snd_pcm_limit_hw_rates(runtime);
  1056. } else {
  1057. /* a fixed rate */
  1058. runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
  1059. runtime->hw.rate_max = runtime->hw.rate_min = ratep->rate;
  1060. }
  1061. spin_unlock_irq(&ratep->lock);
  1062. /* we may remove following constaint when we modify table entries
  1063. in interrupt */
  1064. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  1065. return err;
  1066. runtime->private_data = viadev;
  1067. viadev->substream = substream;
  1068. return 0;
  1069. }
  1070. /*
  1071. * open callback for playback on via686 and via823x DSX
  1072. */
  1073. static int snd_via82xx_playback_open(struct snd_pcm_substream *substream)
  1074. {
  1075. struct via82xx *chip = snd_pcm_substream_chip(substream);
  1076. struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
  1077. int err;
  1078. if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
  1079. return err;
  1080. return 0;
  1081. }
  1082. /*
  1083. * open callback for playback on via823x multi-channel
  1084. */
  1085. static int snd_via8233_multi_open(struct snd_pcm_substream *substream)
  1086. {
  1087. struct via82xx *chip = snd_pcm_substream_chip(substream);
  1088. struct viadev *viadev = &chip->devs[chip->multi_devno];
  1089. int err;
  1090. /* channels constraint for VIA8233A
  1091. * 3 and 5 channels are not supported
  1092. */
  1093. static unsigned int channels[] = {
  1094. 1, 2, 4, 6
  1095. };
  1096. static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
  1097. .count = ARRAY_SIZE(channels),
  1098. .list = channels,
  1099. .mask = 0,
  1100. };
  1101. if ((err = snd_via82xx_pcm_open(chip, viadev, substream)) < 0)
  1102. return err;
  1103. substream->runtime->hw.channels_max = 6;
  1104. if (chip->revision == VIA_REV_8233A)
  1105. snd_pcm_hw_constraint_list(substream->runtime, 0,
  1106. SNDRV_PCM_HW_PARAM_CHANNELS,
  1107. &hw_constraints_channels);
  1108. return 0;
  1109. }
  1110. /*
  1111. * open callback for capture on via686 and via823x
  1112. */
  1113. static int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
  1114. {
  1115. struct via82xx *chip = snd_pcm_substream_chip(substream);
  1116. struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
  1117. return snd_via82xx_pcm_open(chip, viadev, substream);
  1118. }
  1119. /*
  1120. * close callback
  1121. */
  1122. static int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
  1123. {
  1124. struct via82xx *chip = snd_pcm_substream_chip(substream);
  1125. struct viadev *viadev = substream->runtime->private_data;
  1126. struct via_rate_lock *ratep;
  1127. /* release the rate lock */
  1128. ratep = &chip->rates[viadev->direction];
  1129. spin_lock_irq(&ratep->lock);
  1130. ratep->used--;
  1131. if (! ratep->used)
  1132. ratep->rate = 0;
  1133. spin_unlock_irq(&ratep->lock);
  1134. viadev->substream = NULL;
  1135. return 0;
  1136. }
  1137. /* via686 playback callbacks */
  1138. static struct snd_pcm_ops snd_via686_playback_ops = {
  1139. .open = snd_via82xx_playback_open,
  1140. .close = snd_via82xx_pcm_close,
  1141. .ioctl = snd_pcm_lib_ioctl,
  1142. .hw_params = snd_via82xx_hw_params,
  1143. .hw_free = snd_via82xx_hw_free,
  1144. .prepare = snd_via686_playback_prepare,
  1145. .trigger = snd_via82xx_pcm_trigger,
  1146. .pointer = snd_via686_pcm_pointer,
  1147. .page = snd_pcm_sgbuf_ops_page,
  1148. };
  1149. /* via686 capture callbacks */
  1150. static struct snd_pcm_ops snd_via686_capture_ops = {
  1151. .open = snd_via82xx_capture_open,
  1152. .close = snd_via82xx_pcm_close,
  1153. .ioctl = snd_pcm_lib_ioctl,
  1154. .hw_params = snd_via82xx_hw_params,
  1155. .hw_free = snd_via82xx_hw_free,
  1156. .prepare = snd_via686_capture_prepare,
  1157. .trigger = snd_via82xx_pcm_trigger,
  1158. .pointer = snd_via686_pcm_pointer,
  1159. .page = snd_pcm_sgbuf_ops_page,
  1160. };
  1161. /* via823x DSX playback callbacks */
  1162. static struct snd_pcm_ops snd_via8233_playback_ops = {
  1163. .open = snd_via82xx_playback_open,
  1164. .close = snd_via82xx_pcm_close,
  1165. .ioctl = snd_pcm_lib_ioctl,
  1166. .hw_params = snd_via82xx_hw_params,
  1167. .hw_free = snd_via82xx_hw_free,
  1168. .prepare = snd_via8233_playback_prepare,
  1169. .trigger = snd_via82xx_pcm_trigger,
  1170. .pointer = snd_via8233_pcm_pointer,
  1171. .page = snd_pcm_sgbuf_ops_page,
  1172. };
  1173. /* via823x multi-channel playback callbacks */
  1174. static struct snd_pcm_ops snd_via8233_multi_ops = {
  1175. .open = snd_via8233_multi_open,
  1176. .close = snd_via82xx_pcm_close,
  1177. .ioctl = snd_pcm_lib_ioctl,
  1178. .hw_params = snd_via82xx_hw_params,
  1179. .hw_free = snd_via82xx_hw_free,
  1180. .prepare = snd_via8233_multi_prepare,
  1181. .trigger = snd_via82xx_pcm_trigger,
  1182. .pointer = snd_via8233_pcm_pointer,
  1183. .page = snd_pcm_sgbuf_ops_page,
  1184. };
  1185. /* via823x capture callbacks */
  1186. static struct snd_pcm_ops snd_via8233_capture_ops = {
  1187. .open = snd_via82xx_capture_open,
  1188. .close = snd_via82xx_pcm_close,
  1189. .ioctl = snd_pcm_lib_ioctl,
  1190. .hw_params = snd_via82xx_hw_params,
  1191. .hw_free = snd_via82xx_hw_free,
  1192. .prepare = snd_via8233_capture_prepare,
  1193. .trigger = snd_via82xx_pcm_trigger,
  1194. .pointer = snd_via8233_pcm_pointer,
  1195. .page = snd_pcm_sgbuf_ops_page,
  1196. };
  1197. static void init_viadev(struct via82xx *chip, int idx, unsigned int reg_offset,
  1198. int shadow_pos, int direction)
  1199. {
  1200. chip->devs[idx].reg_offset = reg_offset;
  1201. chip->devs[idx].shadow_shift = shadow_pos * 4;
  1202. chip->devs[idx].direction = direction;
  1203. chip->devs[idx].port = chip->port + reg_offset;
  1204. }
  1205. /*
  1206. * create pcm instances for VIA8233, 8233C and 8235 (not 8233A)
  1207. */
  1208. static int __devinit snd_via8233_pcm_new(struct via82xx *chip)
  1209. {
  1210. struct snd_pcm *pcm;
  1211. int i, err;
  1212. chip->playback_devno = 0; /* x 4 */
  1213. chip->multi_devno = 4; /* x 1 */
  1214. chip->capture_devno = 5; /* x 2 */
  1215. chip->num_devs = 7;
  1216. chip->intr_mask = 0x33033333; /* FLAG|EOL for rec0-1, mc, sdx0-3 */
  1217. /* PCM #0: 4 DSX playbacks and 1 capture */
  1218. err = snd_pcm_new(chip->card, chip->card->shortname, 0, 4, 1, &pcm);
  1219. if (err < 0)
  1220. return err;
  1221. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
  1222. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
  1223. pcm->private_data = chip;
  1224. strcpy(pcm->name, chip->card->shortname);
  1225. chip->pcms[0] = pcm;
  1226. /* set up playbacks */
  1227. for (i = 0; i < 4; i++)
  1228. init_viadev(chip, i, 0x10 * i, i, 0);
  1229. /* capture */
  1230. init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
  1231. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1232. snd_dma_pci_data(chip->pci),
  1233. 64*1024, 128*1024)) < 0)
  1234. return err;
  1235. /* PCM #1: multi-channel playback and 2nd capture */
  1236. err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 1, &pcm);
  1237. if (err < 0)
  1238. return err;
  1239. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
  1240. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
  1241. pcm->private_data = chip;
  1242. strcpy(pcm->name, chip->card->shortname);
  1243. chip->pcms[1] = pcm;
  1244. /* set up playback */
  1245. init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
  1246. /* set up capture */
  1247. init_viadev(chip, chip->capture_devno + 1, VIA_REG_CAPTURE_8233_STATUS + 0x10, 7, 1);
  1248. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1249. snd_dma_pci_data(chip->pci),
  1250. 64*1024, 128*1024)) < 0)
  1251. return err;
  1252. return 0;
  1253. }
  1254. /*
  1255. * create pcm instances for VIA8233A
  1256. */
  1257. static int __devinit snd_via8233a_pcm_new(struct via82xx *chip)
  1258. {
  1259. struct snd_pcm *pcm;
  1260. int err;
  1261. chip->multi_devno = 0;
  1262. chip->playback_devno = 1;
  1263. chip->capture_devno = 2;
  1264. chip->num_devs = 3;
  1265. chip->intr_mask = 0x03033000; /* FLAG|EOL for rec0, mc, sdx3 */
  1266. /* PCM #0: multi-channel playback and capture */
  1267. err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
  1268. if (err < 0)
  1269. return err;
  1270. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_multi_ops);
  1271. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via8233_capture_ops);
  1272. pcm->private_data = chip;
  1273. strcpy(pcm->name, chip->card->shortname);
  1274. chip->pcms[0] = pcm;
  1275. /* set up playback */
  1276. init_viadev(chip, chip->multi_devno, VIA_REG_MULTPLAY_STATUS, 4, 0);
  1277. /* capture */
  1278. init_viadev(chip, chip->capture_devno, VIA_REG_CAPTURE_8233_STATUS, 6, 1);
  1279. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1280. snd_dma_pci_data(chip->pci),
  1281. 64*1024, 128*1024)) < 0)
  1282. return err;
  1283. /* SPDIF supported? */
  1284. if (! ac97_can_spdif(chip->ac97))
  1285. return 0;
  1286. /* PCM #1: DXS3 playback (for spdif) */
  1287. err = snd_pcm_new(chip->card, chip->card->shortname, 1, 1, 0, &pcm);
  1288. if (err < 0)
  1289. return err;
  1290. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via8233_playback_ops);
  1291. pcm->private_data = chip;
  1292. strcpy(pcm->name, chip->card->shortname);
  1293. chip->pcms[1] = pcm;
  1294. /* set up playback */
  1295. init_viadev(chip, chip->playback_devno, 0x30, 3, 0);
  1296. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1297. snd_dma_pci_data(chip->pci),
  1298. 64*1024, 128*1024)) < 0)
  1299. return err;
  1300. return 0;
  1301. }
  1302. /*
  1303. * create a pcm instance for via686a/b
  1304. */
  1305. static int __devinit snd_via686_pcm_new(struct via82xx *chip)
  1306. {
  1307. struct snd_pcm *pcm;
  1308. int err;
  1309. chip->playback_devno = 0;
  1310. chip->capture_devno = 1;
  1311. chip->num_devs = 2;
  1312. chip->intr_mask = 0x77; /* FLAG | EOL for PB, CP, FM */
  1313. err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
  1314. if (err < 0)
  1315. return err;
  1316. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
  1317. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
  1318. pcm->private_data = chip;
  1319. strcpy(pcm->name, chip->card->shortname);
  1320. chip->pcms[0] = pcm;
  1321. init_viadev(chip, 0, VIA_REG_PLAYBACK_STATUS, 0, 0);
  1322. init_viadev(chip, 1, VIA_REG_CAPTURE_STATUS, 0, 1);
  1323. if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1324. snd_dma_pci_data(chip->pci),
  1325. 64*1024, 128*1024)) < 0)
  1326. return err;
  1327. return 0;
  1328. }
  1329. /*
  1330. * Mixer part
  1331. */
  1332. static int snd_via8233_capture_source_info(struct snd_kcontrol *kcontrol,
  1333. struct snd_ctl_elem_info *uinfo)
  1334. {
  1335. /* formerly they were "Line" and "Mic", but it looks like that they
  1336. * have nothing to do with the actual physical connections...
  1337. */
  1338. static char *texts[2] = {
  1339. "Input1", "Input2"
  1340. };
  1341. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1342. uinfo->count = 1;
  1343. uinfo->value.enumerated.items = 2;
  1344. if (uinfo->value.enumerated.item >= 2)
  1345. uinfo->value.enumerated.item = 1;
  1346. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  1347. return 0;
  1348. }
  1349. static int snd_via8233_capture_source_get(struct snd_kcontrol *kcontrol,
  1350. struct snd_ctl_elem_value *ucontrol)
  1351. {
  1352. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1353. unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
  1354. ucontrol->value.enumerated.item[0] = inb(port) & VIA_REG_CAPTURE_CHANNEL_MIC ? 1 : 0;
  1355. return 0;
  1356. }
  1357. static int snd_via8233_capture_source_put(struct snd_kcontrol *kcontrol,
  1358. struct snd_ctl_elem_value *ucontrol)
  1359. {
  1360. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1361. unsigned long port = chip->port + (kcontrol->id.index ? (VIA_REG_CAPTURE_CHANNEL + 0x10) : VIA_REG_CAPTURE_CHANNEL);
  1362. u8 val, oval;
  1363. spin_lock_irq(&chip->reg_lock);
  1364. oval = inb(port);
  1365. val = oval & ~VIA_REG_CAPTURE_CHANNEL_MIC;
  1366. if (ucontrol->value.enumerated.item[0])
  1367. val |= VIA_REG_CAPTURE_CHANNEL_MIC;
  1368. if (val != oval)
  1369. outb(val, port);
  1370. spin_unlock_irq(&chip->reg_lock);
  1371. return val != oval;
  1372. }
  1373. static struct snd_kcontrol_new snd_via8233_capture_source __devinitdata = {
  1374. .name = "Input Source Select",
  1375. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1376. .info = snd_via8233_capture_source_info,
  1377. .get = snd_via8233_capture_source_get,
  1378. .put = snd_via8233_capture_source_put,
  1379. };
  1380. static int snd_via8233_dxs3_spdif_info(struct snd_kcontrol *kcontrol,
  1381. struct snd_ctl_elem_info *uinfo)
  1382. {
  1383. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1384. uinfo->count = 1;
  1385. uinfo->value.integer.min = 0;
  1386. uinfo->value.integer.max = 1;
  1387. return 0;
  1388. }
  1389. static int snd_via8233_dxs3_spdif_get(struct snd_kcontrol *kcontrol,
  1390. struct snd_ctl_elem_value *ucontrol)
  1391. {
  1392. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1393. u8 val;
  1394. pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
  1395. ucontrol->value.integer.value[0] = (val & VIA8233_SPDIF_DX3) ? 1 : 0;
  1396. return 0;
  1397. }
  1398. static int snd_via8233_dxs3_spdif_put(struct snd_kcontrol *kcontrol,
  1399. struct snd_ctl_elem_value *ucontrol)
  1400. {
  1401. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1402. u8 val, oval;
  1403. pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &oval);
  1404. val = oval & ~VIA8233_SPDIF_DX3;
  1405. if (ucontrol->value.integer.value[0])
  1406. val |= VIA8233_SPDIF_DX3;
  1407. /* save the spdif flag for rate filtering */
  1408. chip->spdif_on = ucontrol->value.integer.value[0] ? 1 : 0;
  1409. if (val != oval) {
  1410. pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
  1411. return 1;
  1412. }
  1413. return 0;
  1414. }
  1415. static struct snd_kcontrol_new snd_via8233_dxs3_spdif_control __devinitdata = {
  1416. .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
  1417. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1418. .info = snd_via8233_dxs3_spdif_info,
  1419. .get = snd_via8233_dxs3_spdif_get,
  1420. .put = snd_via8233_dxs3_spdif_put,
  1421. };
  1422. static int snd_via8233_dxs_volume_info(struct snd_kcontrol *kcontrol,
  1423. struct snd_ctl_elem_info *uinfo)
  1424. {
  1425. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1426. uinfo->count = 2;
  1427. uinfo->value.integer.min = 0;
  1428. uinfo->value.integer.max = VIA_DXS_MAX_VOLUME;
  1429. return 0;
  1430. }
  1431. static int snd_via8233_dxs_volume_get(struct snd_kcontrol *kcontrol,
  1432. struct snd_ctl_elem_value *ucontrol)
  1433. {
  1434. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1435. unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
  1436. ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][0];
  1437. ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume[idx][1];
  1438. return 0;
  1439. }
  1440. static int snd_via8233_pcmdxs_volume_get(struct snd_kcontrol *kcontrol,
  1441. struct snd_ctl_elem_value *ucontrol)
  1442. {
  1443. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1444. ucontrol->value.integer.value[0] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[0];
  1445. ucontrol->value.integer.value[1] = VIA_DXS_MAX_VOLUME - chip->playback_volume_c[1];
  1446. return 0;
  1447. }
  1448. static int snd_via8233_dxs_volume_put(struct snd_kcontrol *kcontrol,
  1449. struct snd_ctl_elem_value *ucontrol)
  1450. {
  1451. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1452. unsigned int idx = snd_ctl_get_ioff(kcontrol, &ucontrol->id);
  1453. unsigned long port = chip->port + 0x10 * idx;
  1454. unsigned char val;
  1455. int i, change = 0;
  1456. for (i = 0; i < 2; i++) {
  1457. val = ucontrol->value.integer.value[i];
  1458. if (val > VIA_DXS_MAX_VOLUME)
  1459. val = VIA_DXS_MAX_VOLUME;
  1460. val = VIA_DXS_MAX_VOLUME - val;
  1461. change |= val != chip->playback_volume[idx][i];
  1462. if (change) {
  1463. chip->playback_volume[idx][i] = val;
  1464. outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
  1465. }
  1466. }
  1467. return change;
  1468. }
  1469. static int snd_via8233_pcmdxs_volume_put(struct snd_kcontrol *kcontrol,
  1470. struct snd_ctl_elem_value *ucontrol)
  1471. {
  1472. struct via82xx *chip = snd_kcontrol_chip(kcontrol);
  1473. unsigned int idx;
  1474. unsigned char val;
  1475. int i, change = 0;
  1476. for (i = 0; i < 2; i++) {
  1477. val = ucontrol->value.integer.value[i];
  1478. if (val > VIA_DXS_MAX_VOLUME)
  1479. val = VIA_DXS_MAX_VOLUME;
  1480. val = VIA_DXS_MAX_VOLUME - val;
  1481. if (val != chip->playback_volume_c[i]) {
  1482. change = 1;
  1483. chip->playback_volume_c[i] = val;
  1484. for (idx = 0; idx < 4; idx++) {
  1485. unsigned long port = chip->port + 0x10 * idx;
  1486. chip->playback_volume[idx][i] = val;
  1487. outb(val, port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
  1488. }
  1489. }
  1490. }
  1491. return change;
  1492. }
  1493. static struct snd_kcontrol_new snd_via8233_pcmdxs_volume_control __devinitdata = {
  1494. .name = "PCM Playback Volume",
  1495. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1496. .info = snd_via8233_dxs_volume_info,
  1497. .get = snd_via8233_pcmdxs_volume_get,
  1498. .put = snd_via8233_pcmdxs_volume_put,
  1499. };
  1500. static struct snd_kcontrol_new snd_via8233_dxs_volume_control __devinitdata = {
  1501. .name = "VIA DXS Playback Volume",
  1502. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1503. .count = 4,
  1504. .info = snd_via8233_dxs_volume_info,
  1505. .get = snd_via8233_dxs_volume_get,
  1506. .put = snd_via8233_dxs_volume_put,
  1507. };
  1508. /*
  1509. */
  1510. static void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
  1511. {
  1512. struct via82xx *chip = bus->private_data;
  1513. chip->ac97_bus = NULL;
  1514. }
  1515. static void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
  1516. {
  1517. struct via82xx *chip = ac97->private_data;
  1518. chip->ac97 = NULL;
  1519. }
  1520. static struct ac97_quirk ac97_quirks[] = {
  1521. {
  1522. .subvendor = 0x1106,
  1523. .subdevice = 0x4161,
  1524. .codec_id = 0x56494161, /* VT1612A */
  1525. .name = "Soltek SL-75DRV5",
  1526. .type = AC97_TUNE_NONE
  1527. },
  1528. { /* FIXME: which codec? */
  1529. .subvendor = 0x1106,
  1530. .subdevice = 0x4161,
  1531. .name = "ASRock K7VT2",
  1532. .type = AC97_TUNE_HP_ONLY
  1533. },
  1534. {
  1535. .subvendor = 0x1019,
  1536. .subdevice = 0x0a81,
  1537. .name = "ECS K7VTA3",
  1538. .type = AC97_TUNE_HP_ONLY
  1539. },
  1540. {
  1541. .subvendor = 0x1019,
  1542. .subdevice = 0x0a85,
  1543. .name = "ECS L7VMM2",
  1544. .type = AC97_TUNE_HP_ONLY
  1545. },
  1546. {
  1547. .subvendor = 0x1849,
  1548. .subdevice = 0x3059,
  1549. .name = "ASRock K7VM2",
  1550. .type = AC97_TUNE_HP_ONLY /* VT1616 */
  1551. },
  1552. {
  1553. .subvendor = 0x14cd,
  1554. .subdevice = 0x7002,
  1555. .name = "Unknown",
  1556. .type = AC97_TUNE_ALC_JACK
  1557. },
  1558. {
  1559. .subvendor = 0x1071,
  1560. .subdevice = 0x8590,
  1561. .name = "Mitac Mobo",
  1562. .type = AC97_TUNE_ALC_JACK
  1563. },
  1564. {
  1565. .subvendor = 0x161f,
  1566. .subdevice = 0x202b,
  1567. .name = "Arima Notebook",
  1568. .type = AC97_TUNE_HP_ONLY,
  1569. },
  1570. {
  1571. .subvendor = 0x161f,
  1572. .subdevice = 0x2032,
  1573. .name = "Targa Traveller 811",
  1574. .type = AC97_TUNE_HP_ONLY,
  1575. },
  1576. { } /* terminator */
  1577. };
  1578. static int __devinit snd_via82xx_mixer_new(struct via82xx *chip, const char *quirk_override)
  1579. {
  1580. struct snd_ac97_template ac97;
  1581. int err;
  1582. static struct snd_ac97_bus_ops ops = {
  1583. .write = snd_via82xx_codec_write,
  1584. .read = snd_via82xx_codec_read,
  1585. .wait = snd_via82xx_codec_wait,
  1586. };
  1587. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
  1588. return err;
  1589. chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
  1590. chip->ac97_bus->clock = chip->ac97_clock;
  1591. memset(&ac97, 0, sizeof(ac97));
  1592. ac97.private_data = chip;
  1593. ac97.private_free = snd_via82xx_mixer_free_ac97;
  1594. ac97.pci = chip->pci;
  1595. ac97.scaps = AC97_SCAP_SKIP_MODEM;
  1596. if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
  1597. return err;
  1598. snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
  1599. if (chip->chip_type != TYPE_VIA686) {
  1600. /* use slot 10/11 */
  1601. snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
  1602. }
  1603. return 0;
  1604. }
  1605. #ifdef SUPPORT_JOYSTICK
  1606. #define JOYSTICK_ADDR 0x200
  1607. static int __devinit snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
  1608. {
  1609. struct gameport *gp;
  1610. struct resource *r;
  1611. if (!joystick)
  1612. return -ENODEV;
  1613. r = request_region(JOYSTICK_ADDR, 8, "VIA686 gameport");
  1614. if (!r) {
  1615. printk(KERN_WARNING "via82xx: cannot reserve joystick port 0x%#x\n",
  1616. JOYSTICK_ADDR);
  1617. return -EBUSY;
  1618. }
  1619. chip->gameport = gp = gameport_allocate_port();
  1620. if (!gp) {
  1621. printk(KERN_ERR "via82xx: cannot allocate memory for gameport\n");
  1622. release_and_free_resource(r);
  1623. return -ENOMEM;
  1624. }
  1625. gameport_set_name(gp, "VIA686 Gameport");
  1626. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  1627. gameport_set_dev_parent(gp, &chip->pci->dev);
  1628. gp->io = JOYSTICK_ADDR;
  1629. gameport_set_port_data(gp, r);
  1630. /* Enable legacy joystick port */
  1631. *legacy |= VIA_FUNC_ENABLE_GAME;
  1632. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, *legacy);
  1633. gameport_register_port(chip->gameport);
  1634. return 0;
  1635. }
  1636. static void snd_via686_free_gameport(struct via82xx *chip)
  1637. {
  1638. if (chip->gameport) {
  1639. struct resource *r = gameport_get_port_data(chip->gameport);
  1640. gameport_unregister_port(chip->gameport);
  1641. chip->gameport = NULL;
  1642. release_and_free_resource(r);
  1643. }
  1644. }
  1645. #else
  1646. static inline int snd_via686_create_gameport(struct via82xx *chip, unsigned char *legacy)
  1647. {
  1648. return -ENOSYS;
  1649. }
  1650. static inline void snd_via686_free_gameport(struct via82xx *chip) { }
  1651. #endif
  1652. /*
  1653. *
  1654. */
  1655. static int __devinit snd_via8233_init_misc(struct via82xx *chip)
  1656. {
  1657. int i, err, caps;
  1658. unsigned char val;
  1659. caps = chip->chip_type == TYPE_VIA8233A ? 1 : 2;
  1660. for (i = 0; i < caps; i++) {
  1661. snd_via8233_capture_source.index = i;
  1662. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_capture_source, chip));
  1663. if (err < 0)
  1664. return err;
  1665. }
  1666. if (ac97_can_spdif(chip->ac97)) {
  1667. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs3_spdif_control, chip));
  1668. if (err < 0)
  1669. return err;
  1670. }
  1671. if (chip->chip_type != TYPE_VIA8233A) {
  1672. /* when no h/w PCM volume control is found, use DXS volume control
  1673. * as the PCM vol control
  1674. */
  1675. struct snd_ctl_elem_id sid;
  1676. memset(&sid, 0, sizeof(sid));
  1677. strcpy(sid.name, "PCM Playback Volume");
  1678. sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1679. if (! snd_ctl_find_id(chip->card, &sid)) {
  1680. snd_printd(KERN_INFO "Using DXS as PCM Playback\n");
  1681. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_pcmdxs_volume_control, chip));
  1682. if (err < 0)
  1683. return err;
  1684. }
  1685. else /* Using DXS when PCM emulation is enabled is really weird */
  1686. {
  1687. /* Standalone DXS controls */
  1688. err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_via8233_dxs_volume_control, chip));
  1689. if (err < 0)
  1690. return err;
  1691. }
  1692. }
  1693. /* select spdif data slot 10/11 */
  1694. pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &val);
  1695. val = (val & ~VIA8233_SPDIF_SLOT_MASK) | VIA8233_SPDIF_SLOT_1011;
  1696. val &= ~VIA8233_SPDIF_DX3; /* SPDIF off as default */
  1697. pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, val);
  1698. return 0;
  1699. }
  1700. static int __devinit snd_via686_init_misc(struct via82xx *chip)
  1701. {
  1702. unsigned char legacy, legacy_cfg;
  1703. int rev_h = 0;
  1704. legacy = chip->old_legacy;
  1705. legacy_cfg = chip->old_legacy_cfg;
  1706. legacy |= VIA_FUNC_MIDI_IRQMASK; /* FIXME: correct? (disable MIDI) */
  1707. legacy &= ~VIA_FUNC_ENABLE_GAME; /* disable joystick */
  1708. if (chip->revision >= VIA_REV_686_H) {
  1709. rev_h = 1;
  1710. if (mpu_port >= 0x200) { /* force MIDI */
  1711. mpu_port &= 0xfffc;
  1712. pci_write_config_dword(chip->pci, 0x18, mpu_port | 0x01);
  1713. #ifdef CONFIG_PM
  1714. chip->mpu_port_saved = mpu_port;
  1715. #endif
  1716. } else {
  1717. mpu_port = pci_resource_start(chip->pci, 2);
  1718. }
  1719. } else {
  1720. switch (mpu_port) { /* force MIDI */
  1721. case 0x300:
  1722. case 0x310:
  1723. case 0x320:
  1724. case 0x330:
  1725. legacy_cfg &= ~(3 << 2);
  1726. legacy_cfg |= (mpu_port & 0x0030) >> 2;
  1727. break;
  1728. default: /* no, use BIOS settings */
  1729. if (legacy & VIA_FUNC_ENABLE_MIDI)
  1730. mpu_port = 0x300 + ((legacy_cfg & 0x000c) << 2);
  1731. break;
  1732. }
  1733. }
  1734. if (mpu_port >= 0x200 &&
  1735. (chip->mpu_res = request_region(mpu_port, 2, "VIA82xx MPU401"))
  1736. != NULL) {
  1737. if (rev_h)
  1738. legacy |= VIA_FUNC_MIDI_PNP; /* enable PCI I/O 2 */
  1739. legacy |= VIA_FUNC_ENABLE_MIDI;
  1740. } else {
  1741. if (rev_h)
  1742. legacy &= ~VIA_FUNC_MIDI_PNP; /* disable PCI I/O 2 */
  1743. legacy &= ~VIA_FUNC_ENABLE_MIDI;
  1744. mpu_port = 0;
  1745. }
  1746. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
  1747. pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, legacy_cfg);
  1748. if (chip->mpu_res) {
  1749. if (snd_mpu401_uart_new(chip->card, 0, MPU401_HW_VIA686A,
  1750. mpu_port, 1,
  1751. chip->irq, 0, &chip->rmidi) < 0) {
  1752. printk(KERN_WARNING "unable to initialize MPU-401"
  1753. " at 0x%lx, skipping\n", mpu_port);
  1754. legacy &= ~VIA_FUNC_ENABLE_MIDI;
  1755. } else {
  1756. legacy &= ~VIA_FUNC_MIDI_IRQMASK; /* enable MIDI interrupt */
  1757. }
  1758. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, legacy);
  1759. }
  1760. snd_via686_create_gameport(chip, &legacy);
  1761. #ifdef CONFIG_PM
  1762. chip->legacy_saved = legacy;
  1763. chip->legacy_cfg_saved = legacy_cfg;
  1764. #endif
  1765. return 0;
  1766. }
  1767. /*
  1768. * proc interface
  1769. */
  1770. static void snd_via82xx_proc_read(struct snd_info_entry *entry,
  1771. struct snd_info_buffer *buffer)
  1772. {
  1773. struct via82xx *chip = entry->private_data;
  1774. int i;
  1775. snd_iprintf(buffer, "%s\n\n", chip->card->longname);
  1776. for (i = 0; i < 0xa0; i += 4) {
  1777. snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
  1778. }
  1779. }
  1780. static void __devinit snd_via82xx_proc_init(struct via82xx *chip)
  1781. {
  1782. struct snd_info_entry *entry;
  1783. if (! snd_card_proc_new(chip->card, "via82xx", &entry))
  1784. snd_info_set_text_ops(entry, chip, 1024, snd_via82xx_proc_read);
  1785. }
  1786. /*
  1787. *
  1788. */
  1789. static int snd_via82xx_chip_init(struct via82xx *chip)
  1790. {
  1791. unsigned int val;
  1792. unsigned long end_time;
  1793. unsigned char pval;
  1794. #if 0 /* broken on K7M? */
  1795. if (chip->chip_type == TYPE_VIA686)
  1796. /* disable all legacy ports */
  1797. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, 0);
  1798. #endif
  1799. pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
  1800. if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
  1801. /* deassert ACLink reset, force SYNC */
  1802. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
  1803. VIA_ACLINK_CTRL_ENABLE |
  1804. VIA_ACLINK_CTRL_RESET |
  1805. VIA_ACLINK_CTRL_SYNC);
  1806. udelay(100);
  1807. #if 1 /* FIXME: should we do full reset here for all chip models? */
  1808. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
  1809. udelay(100);
  1810. #else
  1811. /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
  1812. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
  1813. VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
  1814. udelay(2);
  1815. #endif
  1816. /* ACLink on, deassert ACLink reset, VSR, SGD data out */
  1817. /* note - FM data out has trouble with non VRA codecs !! */
  1818. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
  1819. udelay(100);
  1820. }
  1821. /* Make sure VRA is enabled, in case we didn't do a
  1822. * complete codec reset, above */
  1823. pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
  1824. if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
  1825. /* ACLink on, deassert ACLink reset, VSR, SGD data out */
  1826. /* note - FM data out has trouble with non VRA codecs !! */
  1827. pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
  1828. udelay(100);
  1829. }
  1830. /* wait until codec ready */
  1831. end_time = jiffies + msecs_to_jiffies(750);
  1832. do {
  1833. pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
  1834. if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
  1835. break;
  1836. schedule_timeout_uninterruptible(1);
  1837. } while (time_before(jiffies, end_time));
  1838. if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
  1839. snd_printk(KERN_ERR "AC'97 codec is not ready [0x%x]\n", val);
  1840. #if 0 /* FIXME: we don't support the second codec yet so skip the detection now.. */
  1841. snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
  1842. VIA_REG_AC97_SECONDARY_VALID |
  1843. (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
  1844. end_time = jiffies + msecs_to_jiffies(750);
  1845. snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
  1846. VIA_REG_AC97_SECONDARY_VALID |
  1847. (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
  1848. do {
  1849. if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
  1850. chip->ac97_secondary = 1;
  1851. goto __ac97_ok2;
  1852. }
  1853. schedule_timeout_interruptible(1);
  1854. } while (time_before(jiffies, end_time));
  1855. /* This is ok, the most of motherboards have only one codec */
  1856. __ac97_ok2:
  1857. #endif
  1858. if (chip->chip_type == TYPE_VIA686) {
  1859. /* route FM trap to IRQ, disable FM trap */
  1860. pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
  1861. /* disable all GPI interrupts */
  1862. outl(0, VIAREG(chip, GPI_INTR));
  1863. }
  1864. if (chip->chip_type != TYPE_VIA686) {
  1865. /* Workaround for Award BIOS bug:
  1866. * DXS channels don't work properly with VRA if MC97 is disabled.
  1867. */
  1868. struct pci_dev *pci;
  1869. pci = pci_get_device(0x1106, 0x3068, NULL); /* MC97 */
  1870. if (pci) {
  1871. unsigned char data;
  1872. pci_read_config_byte(pci, 0x44, &data);
  1873. pci_write_config_byte(pci, 0x44, data | 0x40);
  1874. pci_dev_put(pci);
  1875. }
  1876. }
  1877. if (chip->chip_type != TYPE_VIA8233A) {
  1878. int i, idx;
  1879. for (idx = 0; idx < 4; idx++) {
  1880. unsigned long port = chip->port + 0x10 * idx;
  1881. for (i = 0; i < 2; i++) {
  1882. chip->playback_volume[idx][i]=chip->playback_volume_c[i];
  1883. outb(chip->playback_volume_c[i],
  1884. port + VIA_REG_OFS_PLAYBACK_VOLUME_L + i);
  1885. }
  1886. }
  1887. }
  1888. return 0;
  1889. }
  1890. #ifdef CONFIG_PM
  1891. /*
  1892. * power management
  1893. */
  1894. static int snd_via82xx_suspend(struct pci_dev *pci, pm_message_t state)
  1895. {
  1896. struct snd_card *card = pci_get_drvdata(pci);
  1897. struct via82xx *chip = card->private_data;
  1898. int i;
  1899. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1900. for (i = 0; i < 2; i++)
  1901. snd_pcm_suspend_all(chip->pcms[i]);
  1902. for (i = 0; i < chip->num_devs; i++)
  1903. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  1904. synchronize_irq(chip->irq);
  1905. snd_ac97_suspend(chip->ac97);
  1906. /* save misc values */
  1907. if (chip->chip_type != TYPE_VIA686) {
  1908. pci_read_config_byte(chip->pci, VIA8233_SPDIF_CTRL, &chip->spdif_ctrl_saved);
  1909. chip->capture_src_saved[0] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL);
  1910. chip->capture_src_saved[1] = inb(chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
  1911. }
  1912. pci_set_power_state(pci, PCI_D3hot);
  1913. pci_disable_device(pci);
  1914. pci_save_state(pci);
  1915. return 0;
  1916. }
  1917. static int snd_via82xx_resume(struct pci_dev *pci)
  1918. {
  1919. struct snd_card *card = pci_get_drvdata(pci);
  1920. struct via82xx *chip = card->private_data;
  1921. int i;
  1922. pci_restore_state(pci);
  1923. pci_enable_device(pci);
  1924. pci_set_power_state(pci, PCI_D0);
  1925. snd_via82xx_chip_init(chip);
  1926. if (chip->chip_type == TYPE_VIA686) {
  1927. if (chip->mpu_port_saved)
  1928. pci_write_config_dword(chip->pci, 0x18, chip->mpu_port_saved | 0x01);
  1929. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->legacy_saved);
  1930. pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->legacy_cfg_saved);
  1931. } else {
  1932. pci_write_config_byte(chip->pci, VIA8233_SPDIF_CTRL, chip->spdif_ctrl_saved);
  1933. outb(chip->capture_src_saved[0], chip->port + VIA_REG_CAPTURE_CHANNEL);
  1934. outb(chip->capture_src_saved[1], chip->port + VIA_REG_CAPTURE_CHANNEL + 0x10);
  1935. }
  1936. snd_ac97_resume(chip->ac97);
  1937. for (i = 0; i < chip->num_devs; i++)
  1938. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  1939. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1940. return 0;
  1941. }
  1942. #endif /* CONFIG_PM */
  1943. static int snd_via82xx_free(struct via82xx *chip)
  1944. {
  1945. unsigned int i;
  1946. if (chip->irq < 0)
  1947. goto __end_hw;
  1948. /* disable interrupts */
  1949. for (i = 0; i < chip->num_devs; i++)
  1950. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  1951. synchronize_irq(chip->irq);
  1952. __end_hw:
  1953. if (chip->irq >= 0)
  1954. free_irq(chip->irq, chip);
  1955. release_and_free_resource(chip->mpu_res);
  1956. pci_release_regions(chip->pci);
  1957. if (chip->chip_type == TYPE_VIA686) {
  1958. snd_via686_free_gameport(chip);
  1959. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE, chip->old_legacy);
  1960. pci_write_config_byte(chip->pci, VIA_PNP_CONTROL, chip->old_legacy_cfg);
  1961. }
  1962. pci_disable_device(chip->pci);
  1963. kfree(chip);
  1964. return 0;
  1965. }
  1966. static int snd_via82xx_dev_free(struct snd_device *device)
  1967. {
  1968. struct via82xx *chip = device->device_data;
  1969. return snd_via82xx_free(chip);
  1970. }
  1971. static int __devinit snd_via82xx_create(struct snd_card *card,
  1972. struct pci_dev *pci,
  1973. int chip_type,
  1974. int revision,
  1975. unsigned int ac97_clock,
  1976. struct via82xx ** r_via)
  1977. {
  1978. struct via82xx *chip;
  1979. int err;
  1980. static struct snd_device_ops ops = {
  1981. .dev_free = snd_via82xx_dev_free,
  1982. };
  1983. if ((err = pci_enable_device(pci)) < 0)
  1984. return err;
  1985. if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
  1986. pci_disable_device(pci);
  1987. return -ENOMEM;
  1988. }
  1989. chip->chip_type = chip_type;
  1990. chip->revision = revision;
  1991. spin_lock_init(&chip->reg_lock);
  1992. spin_lock_init(&chip->rates[0].lock);
  1993. spin_lock_init(&chip->rates[1].lock);
  1994. chip->card = card;
  1995. chip->pci = pci;
  1996. chip->irq = -1;
  1997. pci_read_config_byte(pci, VIA_FUNC_ENABLE, &chip->old_legacy);
  1998. pci_read_config_byte(pci, VIA_PNP_CONTROL, &chip->old_legacy_cfg);
  1999. pci_write_config_byte(chip->pci, VIA_FUNC_ENABLE,
  2000. chip->old_legacy & ~(VIA_FUNC_ENABLE_SB|VIA_FUNC_ENABLE_FM));
  2001. if ((err = pci_request_regions(pci, card->driver)) < 0) {
  2002. kfree(chip);
  2003. pci_disable_device(pci);
  2004. return err;
  2005. }
  2006. chip->port = pci_resource_start(pci, 0);
  2007. if (request_irq(pci->irq,
  2008. chip_type == TYPE_VIA8233 ?
  2009. snd_via8233_interrupt : snd_via686_interrupt,
  2010. SA_INTERRUPT|SA_SHIRQ,
  2011. card->driver, chip)) {
  2012. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  2013. snd_via82xx_free(chip);
  2014. return -EBUSY;
  2015. }
  2016. chip->irq = pci->irq;
  2017. if (ac97_clock >= 8000 && ac97_clock <= 48000)
  2018. chip->ac97_clock = ac97_clock;
  2019. synchronize_irq(chip->irq);
  2020. if ((err = snd_via82xx_chip_init(chip)) < 0) {
  2021. snd_via82xx_free(chip);
  2022. return err;
  2023. }
  2024. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  2025. snd_via82xx_free(chip);
  2026. return err;
  2027. }
  2028. /* The 8233 ac97 controller does not implement the master bit
  2029. * in the pci command register. IMHO this is a violation of the PCI spec.
  2030. * We call pci_set_master here because it does not hurt. */
  2031. pci_set_master(pci);
  2032. snd_card_set_dev(card, &pci->dev);
  2033. *r_via = chip;
  2034. return 0;
  2035. }
  2036. struct via823x_info {
  2037. int revision;
  2038. char *name;
  2039. int type;
  2040. };
  2041. static struct via823x_info via823x_cards[] __devinitdata = {
  2042. { VIA_REV_PRE_8233, "VIA 8233-Pre", TYPE_VIA8233 },
  2043. { VIA_REV_8233C, "VIA 8233C", TYPE_VIA8233 },
  2044. { VIA_REV_8233, "VIA 8233", TYPE_VIA8233 },
  2045. { VIA_REV_8233A, "VIA 8233A", TYPE_VIA8233A },
  2046. { VIA_REV_8235, "VIA 8235", TYPE_VIA8233 },
  2047. { VIA_REV_8237, "VIA 8237", TYPE_VIA8233 },
  2048. };
  2049. /*
  2050. * auto detection of DXS channel supports.
  2051. */
  2052. struct dxs_whitelist {
  2053. unsigned short subvendor;
  2054. unsigned short subdevice;
  2055. unsigned short mask;
  2056. short action; /* new dxs_support value */
  2057. };
  2058. static int __devinit check_dxs_list(struct pci_dev *pci)
  2059. {
  2060. static struct dxs_whitelist whitelist[] = {
  2061. { .subvendor = 0x1005, .subdevice = 0x4710, .action = VIA_DXS_ENABLE }, /* Avance Logic Mobo */
  2062. { .subvendor = 0x1019, .subdevice = 0x0996, .action = VIA_DXS_48K },
  2063. { .subvendor = 0x1019, .subdevice = 0x0a81, .action = VIA_DXS_NO_VRA }, /* ECS K7VTA3 v8.0 */
  2064. { .subvendor = 0x1019, .subdevice = 0x0a85, .action = VIA_DXS_NO_VRA }, /* ECS L7VMM2 */
  2065. { .subvendor = 0x1019, .subdevice = 0xa101, .action = VIA_DXS_SRC },
  2066. { .subvendor = 0x1019, .subdevice = 0xaa01, .action = VIA_DXS_SRC }, /* ECS K8T890-A */
  2067. { .subvendor = 0x1025, .subdevice = 0x0033, .action = VIA_DXS_NO_VRA }, /* Acer Inspire 1353LM */
  2068. { .subvendor = 0x1025, .subdevice = 0x0046, .action = VIA_DXS_SRC }, /* Acer Aspire 1524 WLMi */
  2069. { .subvendor = 0x1043, .subdevice = 0x8095, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8X (FIXME: possibly VIA_DXS_ENABLE?)*/
  2070. { .subvendor = 0x1043, .subdevice = 0x80a1, .action = VIA_DXS_NO_VRA }, /* ASUS A7V8-X */
  2071. { .subvendor = 0x1043, .subdevice = 0x80b0, .action = VIA_DXS_NO_VRA }, /* ASUS A7V600 & K8V*/
  2072. { .subvendor = 0x1043, .subdevice = 0x810d, .action = VIA_DXS_SRC }, /* ASUS */
  2073. { .subvendor = 0x1043, .subdevice = 0x812a, .action = VIA_DXS_SRC }, /* ASUS A8V Deluxe */
  2074. { .subvendor = 0x1043, .subdevice = 0x8174, .action = VIA_DXS_SRC }, /* ASUS */
  2075. { .subvendor = 0x1071, .subdevice = 0x8375, .action = VIA_DXS_NO_VRA }, /* Vobis/Yakumo/Mitac notebook */
  2076. { .subvendor = 0x1071, .subdevice = 0x8399, .action = VIA_DXS_NO_VRA }, /* Umax AB 595T (VIA K8N800A - VT8237) */
  2077. { .subvendor = 0x10cf, .subdevice = 0x118e, .action = VIA_DXS_ENABLE }, /* FSC laptop */
  2078. { .subvendor = 0x1106, .subdevice = 0x4161, .action = VIA_DXS_NO_VRA }, /* ASRock K7VT2 */
  2079. { .subvendor = 0x1106, .subdevice = 0x4552, .action = VIA_DXS_NO_VRA }, /* QDI Kudoz 7X/600-6AL */
  2080. { .subvendor = 0x1106, .subdevice = 0xaa01, .action = VIA_DXS_NO_VRA }, /* EPIA MII */
  2081. { .subvendor = 0x1106, .subdevice = 0xc001, .action = VIA_DXS_SRC }, /* Insight P4-ITX */
  2082. { .subvendor = 0x1297, .subdevice = 0xa232, .action = VIA_DXS_ENABLE }, /* Shuttle ?? */
  2083. { .subvendor = 0x1297, .subdevice = 0xc160, .action = VIA_DXS_ENABLE }, /* Shuttle SK41G */
  2084. { .subvendor = 0x1458, .subdevice = 0xa002, .action = VIA_DXS_ENABLE }, /* Gigabyte GA-7VAXP */
  2085. { .subvendor = 0x1462, .subdevice = 0x0080, .action = VIA_DXS_SRC }, /* MSI K8T Neo-FIS2R */
  2086. { .subvendor = 0x1462, .subdevice = 0x0430, .action = VIA_DXS_SRC }, /* MSI 7142 (K8MM-V) */
  2087. { .subvendor = 0x1462, .subdevice = 0x0470, .action = VIA_DXS_SRC }, /* MSI KT880 Delta-FSR */
  2088. { .subvendor = 0x1462, .subdevice = 0x3800, .action = VIA_DXS_ENABLE }, /* MSI KT266 */
  2089. { .subvendor = 0x1462, .subdevice = 0x5901, .action = VIA_DXS_NO_VRA }, /* MSI KT6 Delta-SR */
  2090. { .subvendor = 0x1462, .subdevice = 0x7023, .action = VIA_DXS_NO_VRA }, /* MSI K8T Neo2-FI */
  2091. { .subvendor = 0x1462, .subdevice = 0x7120, .action = VIA_DXS_ENABLE }, /* MSI KT4V */
  2092. { .subvendor = 0x1462, .subdevice = 0x7142, .action = VIA_DXS_ENABLE }, /* MSI K8MM-V */
  2093. { .subvendor = 0x1462, .subdevice = 0xb012, .action = VIA_DXS_SRC }, /* P4M800/VIA8237R */
  2094. { .subvendor = 0x147b, .subdevice = 0x1401, .action = VIA_DXS_ENABLE }, /* ABIT KD7(-RAID) */
  2095. { .subvendor = 0x147b, .subdevice = 0x1411, .action = VIA_DXS_ENABLE }, /* ABIT VA-20 */
  2096. { .subvendor = 0x147b, .subdevice = 0x1413, .action = VIA_DXS_ENABLE }, /* ABIT KV8 Pro */
  2097. { .subvendor = 0x147b, .subdevice = 0x1415, .action = VIA_DXS_NO_VRA }, /* Abit AV8 */
  2098. { .subvendor = 0x14ff, .subdevice = 0x0403, .action = VIA_DXS_ENABLE }, /* Twinhead mobo */
  2099. { .subvendor = 0x14ff, .subdevice = 0x0408, .action = VIA_DXS_SRC }, /* Twinhead laptop */
  2100. { .subvendor = 0x1558, .subdevice = 0x4701, .action = VIA_DXS_SRC }, /* Clevo D470 */
  2101. { .subvendor = 0x1584, .subdevice = 0x8120, .action = VIA_DXS_ENABLE }, /* Gericom/Targa/Vobis/Uniwill laptop */
  2102. { .subvendor = 0x1584, .subdevice = 0x8123, .action = VIA_DXS_NO_VRA }, /* Uniwill (Targa Visionary XP-210) */
  2103. { .subvendor = 0x161f, .subdevice = 0x202b, .action = VIA_DXS_NO_VRA }, /* Amira Note book */
  2104. { .subvendor = 0x161f, .subdevice = 0x2032, .action = VIA_DXS_48K }, /* m680x machines */
  2105. { .subvendor = 0x1631, .subdevice = 0xe004, .action = VIA_DXS_ENABLE }, /* Easy Note 3174, Packard Bell */
  2106. { .subvendor = 0x1695, .subdevice = 0x3005, .action = VIA_DXS_ENABLE }, /* EPoX EP-8K9A */
  2107. { .subvendor = 0x1695, .subdevice = 0x300c, .action = VIA_DXS_SRC }, /* EPoX EP-8KRAI */
  2108. { .subvendor = 0x1695, .subdevice = 0x300e, .action = VIA_DXS_SRC }, /* EPoX 9HEAI */
  2109. { .subvendor = 0x16f3, .subdevice = 0x6405, .action = VIA_DXS_SRC }, /* Jetway K8M8MS */
  2110. { .subvendor = 0x1734, .subdevice = 0x1078, .action = VIA_DXS_SRC }, /* FSC Amilo L7300 */
  2111. { .subvendor = 0x1734, .subdevice = 0x1093, .action = VIA_DXS_SRC }, /* FSC */
  2112. { .subvendor = 0x1849, .subdevice = 0x3059, .action = VIA_DXS_NO_VRA }, /* ASRock K7VM2 */
  2113. { .subvendor = 0x1849, .subdevice = 0x9739, .action = VIA_DXS_SRC }, /* ASRock mobo(?) */
  2114. { .subvendor = 0x1849, .subdevice = 0x9761, .action = VIA_DXS_SRC }, /* ASRock mobo(?) */
  2115. { .subvendor = 0x1919, .subdevice = 0x200a, .action = VIA_DXS_NO_VRA }, /* Soltek SL-K8Tpro-939 */
  2116. { .subvendor = 0x4005, .subdevice = 0x4710, .action = VIA_DXS_SRC }, /* MSI K7T266 Pro2 (MS-6380 V2.0) BIOS 3.7 */
  2117. { } /* terminator */
  2118. };
  2119. struct dxs_whitelist *w;
  2120. unsigned short subsystem_vendor;
  2121. unsigned short subsystem_device;
  2122. pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
  2123. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
  2124. for (w = whitelist; w->subvendor; w++) {
  2125. if (w->subvendor != subsystem_vendor)
  2126. continue;
  2127. if (w->mask) {
  2128. if ((w->mask & subsystem_device) == w->subdevice)
  2129. return w->action;
  2130. } else {
  2131. if (subsystem_device == w->subdevice)
  2132. return w->action;
  2133. }
  2134. }
  2135. /*
  2136. * not detected, try 48k rate only to be sure.
  2137. */
  2138. printk(KERN_INFO "via82xx: Assuming DXS channels with 48k fixed sample rate.\n");
  2139. printk(KERN_INFO " Please try dxs_support=5 option\n");
  2140. printk(KERN_INFO " and report if it works on your machine.\n");
  2141. printk(KERN_INFO " For more details, read ALSA-Configuration.txt.\n");
  2142. return VIA_DXS_48K;
  2143. };
  2144. static int __devinit snd_via82xx_probe(struct pci_dev *pci,
  2145. const struct pci_device_id *pci_id)
  2146. {
  2147. struct snd_card *card;
  2148. struct via82xx *chip;
  2149. unsigned char revision;
  2150. int chip_type = 0, card_type;
  2151. unsigned int i;
  2152. int err;
  2153. card = snd_card_new(index, id, THIS_MODULE, 0);
  2154. if (card == NULL)
  2155. return -ENOMEM;
  2156. card_type = pci_id->driver_data;
  2157. pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
  2158. switch (card_type) {
  2159. case TYPE_CARD_VIA686:
  2160. strcpy(card->driver, "VIA686A");
  2161. sprintf(card->shortname, "VIA 82C686A/B rev%x", revision);
  2162. chip_type = TYPE_VIA686;
  2163. break;
  2164. case TYPE_CARD_VIA8233:
  2165. chip_type = TYPE_VIA8233;
  2166. sprintf(card->shortname, "VIA 823x rev%x", revision);
  2167. for (i = 0; i < ARRAY_SIZE(via823x_cards); i++) {
  2168. if (revision == via823x_cards[i].revision) {
  2169. chip_type = via823x_cards[i].type;
  2170. strcpy(card->shortname, via823x_cards[i].name);
  2171. break;
  2172. }
  2173. }
  2174. if (chip_type != TYPE_VIA8233A) {
  2175. if (dxs_support == VIA_DXS_AUTO)
  2176. dxs_support = check_dxs_list(pci);
  2177. /* force to use VIA8233 or 8233A model according to
  2178. * dxs_support module option
  2179. */
  2180. if (dxs_support == VIA_DXS_DISABLE)
  2181. chip_type = TYPE_VIA8233A;
  2182. else
  2183. chip_type = TYPE_VIA8233;
  2184. }
  2185. if (chip_type == TYPE_VIA8233A)
  2186. strcpy(card->driver, "VIA8233A");
  2187. else if (revision >= VIA_REV_8237)
  2188. strcpy(card->driver, "VIA8237"); /* no slog assignment */
  2189. else
  2190. strcpy(card->driver, "VIA8233");
  2191. break;
  2192. default:
  2193. snd_printk(KERN_ERR "invalid card type %d\n", card_type);
  2194. err = -EINVAL;
  2195. goto __error;
  2196. }
  2197. if ((err = snd_via82xx_create(card, pci, chip_type, revision,
  2198. ac97_clock, &chip)) < 0)
  2199. goto __error;
  2200. card->private_data = chip;
  2201. if ((err = snd_via82xx_mixer_new(chip, ac97_quirk)) < 0)
  2202. goto __error;
  2203. if (chip_type == TYPE_VIA686) {
  2204. if ((err = snd_via686_pcm_new(chip)) < 0 ||
  2205. (err = snd_via686_init_misc(chip)) < 0)
  2206. goto __error;
  2207. } else {
  2208. if (chip_type == TYPE_VIA8233A) {
  2209. if ((err = snd_via8233a_pcm_new(chip)) < 0)
  2210. goto __error;
  2211. // chip->dxs_fixed = 1; /* FIXME: use 48k for DXS #3? */
  2212. } else {
  2213. if ((err = snd_via8233_pcm_new(chip)) < 0)
  2214. goto __error;
  2215. if (dxs_support == VIA_DXS_48K)
  2216. chip->dxs_fixed = 1;
  2217. else if (dxs_support == VIA_DXS_NO_VRA)
  2218. chip->no_vra = 1;
  2219. else if (dxs_support == VIA_DXS_SRC) {
  2220. chip->no_vra = 1;
  2221. chip->dxs_src = 1;
  2222. }
  2223. }
  2224. if ((err = snd_via8233_init_misc(chip)) < 0)
  2225. goto __error;
  2226. }
  2227. /* disable interrupts */
  2228. for (i = 0; i < chip->num_devs; i++)
  2229. snd_via82xx_channel_reset(chip, &chip->devs[i]);
  2230. snprintf(card->longname, sizeof(card->longname),
  2231. "%s with %s at %#lx, irq %d", card->shortname,
  2232. snd_ac97_get_short_name(chip->ac97), chip->port, chip->irq);
  2233. snd_via82xx_proc_init(chip);
  2234. if ((err = snd_card_register(card)) < 0) {
  2235. snd_card_free(card);
  2236. return err;
  2237. }
  2238. pci_set_drvdata(pci, card);
  2239. return 0;
  2240. __error:
  2241. snd_card_free(card);
  2242. return err;
  2243. }
  2244. static void __devexit snd_via82xx_remove(struct pci_dev *pci)
  2245. {
  2246. snd_card_free(pci_get_drvdata(pci));
  2247. pci_set_drvdata(pci, NULL);
  2248. }
  2249. static struct pci_driver driver = {
  2250. .name = "VIA 82xx Audio",
  2251. .id_table = snd_via82xx_ids,
  2252. .probe = snd_via82xx_probe,
  2253. .remove = __devexit_p(snd_via82xx_remove),
  2254. #ifdef CONFIG_PM
  2255. .suspend = snd_via82xx_suspend,
  2256. .resume = snd_via82xx_resume,
  2257. #endif
  2258. };
  2259. static int __init alsa_card_via82xx_init(void)
  2260. {
  2261. return pci_register_driver(&driver);
  2262. }
  2263. static void __exit alsa_card_via82xx_exit(void)
  2264. {
  2265. pci_unregister_driver(&driver);
  2266. }
  2267. module_init(alsa_card_via82xx_init)
  2268. module_exit(alsa_card_via82xx_exit)