via82xx.c 77 KB

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