via82xx.c 78 KB

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