es18xx.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495
  1. /*
  2. * Driver for generic ESS AudioDrive ES18xx soundcards
  3. * Copyright (c) by Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>
  4. * Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* GENERAL NOTES:
  23. *
  24. * BUGS:
  25. * - There are pops (we can't delay in trigger function, cause midlevel
  26. * often need to trigger down and then up very quickly).
  27. * Any ideas?
  28. * - Support for 16 bit DMA seems to be broken. I've no hardware to tune it.
  29. */
  30. /*
  31. * ES1868 NOTES:
  32. * - The chip has one half duplex pcm (with very limited full duplex support).
  33. *
  34. * - Duplex stereophonic sound is impossible.
  35. * - Record and playback must share the same frequency rate.
  36. *
  37. * - The driver use dma2 for playback and dma1 for capture.
  38. */
  39. /*
  40. * ES1869 NOTES:
  41. *
  42. * - there are a first full duplex pcm and a second playback only pcm
  43. * (incompatible with first pcm capture)
  44. *
  45. * - there is support for the capture volume and ESS Spatializer 3D effect.
  46. *
  47. * - contrarily to some pages in DS_1869.PDF the rates can be set
  48. * independently.
  49. *
  50. * - Zoom Video is implemented by sharing the FM DAC, thus the user can
  51. * have either FM playback or Video playback but not both simultaneously.
  52. * The Video Playback Switch mixer control toggles this choice.
  53. *
  54. * BUGS:
  55. *
  56. * - There is a major trouble I noted:
  57. *
  58. * using both channel for playback stereo 16 bit samples at 44100 Hz
  59. * the second pcm (Audio1) DMA slows down irregularly and sound is garbled.
  60. *
  61. * The same happens using Audio1 for captureing.
  62. *
  63. * The Windows driver does not suffer of this (although it use Audio1
  64. * only for captureing). I'm unable to discover why.
  65. *
  66. */
  67. /*
  68. * ES1879 NOTES:
  69. * - When Zoom Video is enabled (reg 0x71 bit 6 toggled on) the PCM playback
  70. * seems to be effected (speaker_test plays a lower frequency). Can't find
  71. * anything in the datasheet to account for this, so a Video Playback Switch
  72. * control has been included to allow ZV to be enabled only when necessary.
  73. * Then again on at least one test system the 0x71 bit 6 enable bit is not
  74. * needed for ZV, so maybe the datasheet is entirely wrong here.
  75. */
  76. #include <sound/driver.h>
  77. #include <linux/init.h>
  78. #include <linux/err.h>
  79. #include <linux/isa.h>
  80. #include <linux/slab.h>
  81. #include <linux/pnp.h>
  82. #include <linux/isapnp.h>
  83. #include <linux/moduleparam.h>
  84. #include <linux/delay.h>
  85. #include <asm/io.h>
  86. #include <asm/dma.h>
  87. #include <sound/core.h>
  88. #include <sound/control.h>
  89. #include <sound/pcm.h>
  90. #include <sound/pcm_params.h>
  91. #include <sound/mpu401.h>
  92. #include <sound/opl3.h>
  93. #define SNDRV_LEGACY_FIND_FREE_IRQ
  94. #define SNDRV_LEGACY_FIND_FREE_DMA
  95. #include <sound/initval.h>
  96. #define PFX "es18xx: "
  97. struct snd_es18xx {
  98. unsigned long port; /* port of ESS chip */
  99. unsigned long mpu_port; /* MPU-401 port of ESS chip */
  100. unsigned long fm_port; /* FM port */
  101. unsigned long ctrl_port; /* Control port of ESS chip */
  102. struct resource *res_port;
  103. struct resource *res_mpu_port;
  104. struct resource *res_ctrl_port;
  105. int irq; /* IRQ number of ESS chip */
  106. int dma1; /* DMA1 */
  107. int dma2; /* DMA2 */
  108. unsigned short version; /* version of ESS chip */
  109. int caps; /* Chip capabilities */
  110. unsigned short audio2_vol; /* volume level of audio2 */
  111. unsigned short active; /* active channel mask */
  112. unsigned int dma1_size;
  113. unsigned int dma2_size;
  114. unsigned int dma1_shift;
  115. unsigned int dma2_shift;
  116. struct snd_card *card;
  117. struct snd_pcm *pcm;
  118. struct snd_pcm_substream *playback_a_substream;
  119. struct snd_pcm_substream *capture_a_substream;
  120. struct snd_pcm_substream *playback_b_substream;
  121. struct snd_rawmidi *rmidi;
  122. struct snd_kcontrol *hw_volume;
  123. struct snd_kcontrol *hw_switch;
  124. struct snd_kcontrol *master_volume;
  125. struct snd_kcontrol *master_switch;
  126. spinlock_t reg_lock;
  127. spinlock_t mixer_lock;
  128. spinlock_t ctrl_lock;
  129. #ifdef CONFIG_PM
  130. unsigned char pm_reg;
  131. #endif
  132. };
  133. struct snd_audiodrive {
  134. struct snd_es18xx *chip;
  135. #ifdef CONFIG_PNP
  136. struct pnp_dev *dev;
  137. struct pnp_dev *devc;
  138. #endif
  139. };
  140. #define AUDIO1_IRQ 0x01
  141. #define AUDIO2_IRQ 0x02
  142. #define HWV_IRQ 0x04
  143. #define MPU_IRQ 0x08
  144. #define ES18XX_PCM2 0x0001 /* Has two useable PCM */
  145. #define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
  146. #define ES18XX_RECMIX 0x0004 /* Has record mixer */
  147. #define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
  148. #define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
  149. #define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
  150. #define ES18XX_AUXB 0x0040 /* AuxB mixer control */
  151. #define ES18XX_HWV 0x0080 /* Has seperate hardware volume mixer controls*/
  152. #define ES18XX_MONO 0x0100 /* Mono_in mixer control */
  153. #define ES18XX_I2S 0x0200 /* I2S mixer control */
  154. #define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
  155. #define ES18XX_CONTROL 0x0800 /* Has control ports */
  156. /* Power Management */
  157. #define ES18XX_PM 0x07
  158. #define ES18XX_PM_GPO0 0x01
  159. #define ES18XX_PM_GPO1 0x02
  160. #define ES18XX_PM_PDR 0x04
  161. #define ES18XX_PM_ANA 0x08
  162. #define ES18XX_PM_FM 0x020
  163. #define ES18XX_PM_SUS 0x080
  164. /* Lowlevel */
  165. #define DAC1 0x01
  166. #define ADC1 0x02
  167. #define DAC2 0x04
  168. #define MILLISECOND 10000
  169. static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
  170. {
  171. int i;
  172. for(i = MILLISECOND; i; i--)
  173. if ((inb(chip->port + 0x0C) & 0x80) == 0) {
  174. outb(val, chip->port + 0x0C);
  175. return 0;
  176. }
  177. snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
  178. return -EINVAL;
  179. }
  180. static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
  181. {
  182. int i;
  183. for(i = MILLISECOND/10; i; i--)
  184. if (inb(chip->port + 0x0C) & 0x40)
  185. return inb(chip->port + 0x0A);
  186. snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
  187. chip->port + 0x0A, inb(chip->port + 0x0A));
  188. return -ENODEV;
  189. }
  190. #undef REG_DEBUG
  191. static int snd_es18xx_write(struct snd_es18xx *chip,
  192. unsigned char reg, unsigned char data)
  193. {
  194. unsigned long flags;
  195. int ret;
  196. spin_lock_irqsave(&chip->reg_lock, flags);
  197. ret = snd_es18xx_dsp_command(chip, reg);
  198. if (ret < 0)
  199. goto end;
  200. ret = snd_es18xx_dsp_command(chip, data);
  201. end:
  202. spin_unlock_irqrestore(&chip->reg_lock, flags);
  203. #ifdef REG_DEBUG
  204. snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
  205. #endif
  206. return ret;
  207. }
  208. static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
  209. {
  210. unsigned long flags;
  211. int ret, data;
  212. spin_lock_irqsave(&chip->reg_lock, flags);
  213. ret = snd_es18xx_dsp_command(chip, 0xC0);
  214. if (ret < 0)
  215. goto end;
  216. ret = snd_es18xx_dsp_command(chip, reg);
  217. if (ret < 0)
  218. goto end;
  219. data = snd_es18xx_dsp_get_byte(chip);
  220. ret = data;
  221. #ifdef REG_DEBUG
  222. snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
  223. #endif
  224. end:
  225. spin_unlock_irqrestore(&chip->reg_lock, flags);
  226. return ret;
  227. }
  228. /* Return old value */
  229. static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
  230. unsigned char mask, unsigned char val)
  231. {
  232. int ret;
  233. unsigned char old, new, oval;
  234. unsigned long flags;
  235. spin_lock_irqsave(&chip->reg_lock, flags);
  236. ret = snd_es18xx_dsp_command(chip, 0xC0);
  237. if (ret < 0)
  238. goto end;
  239. ret = snd_es18xx_dsp_command(chip, reg);
  240. if (ret < 0)
  241. goto end;
  242. ret = snd_es18xx_dsp_get_byte(chip);
  243. if (ret < 0) {
  244. goto end;
  245. }
  246. old = ret;
  247. oval = old & mask;
  248. if (val != oval) {
  249. ret = snd_es18xx_dsp_command(chip, reg);
  250. if (ret < 0)
  251. goto end;
  252. new = (old & ~mask) | (val & mask);
  253. ret = snd_es18xx_dsp_command(chip, new);
  254. if (ret < 0)
  255. goto end;
  256. #ifdef REG_DEBUG
  257. snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
  258. reg, old, new, ret);
  259. #endif
  260. }
  261. ret = oval;
  262. end:
  263. spin_unlock_irqrestore(&chip->reg_lock, flags);
  264. return ret;
  265. }
  266. static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
  267. unsigned char reg, unsigned char data)
  268. {
  269. unsigned long flags;
  270. spin_lock_irqsave(&chip->mixer_lock, flags);
  271. outb(reg, chip->port + 0x04);
  272. outb(data, chip->port + 0x05);
  273. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  274. #ifdef REG_DEBUG
  275. snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
  276. #endif
  277. }
  278. static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
  279. {
  280. unsigned long flags;
  281. int data;
  282. spin_lock_irqsave(&chip->mixer_lock, flags);
  283. outb(reg, chip->port + 0x04);
  284. data = inb(chip->port + 0x05);
  285. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  286. #ifdef REG_DEBUG
  287. snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
  288. #endif
  289. return data;
  290. }
  291. /* Return old value */
  292. static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
  293. unsigned char mask, unsigned char val)
  294. {
  295. unsigned char old, new, oval;
  296. unsigned long flags;
  297. spin_lock_irqsave(&chip->mixer_lock, flags);
  298. outb(reg, chip->port + 0x04);
  299. old = inb(chip->port + 0x05);
  300. oval = old & mask;
  301. if (val != oval) {
  302. new = (old & ~mask) | (val & mask);
  303. outb(new, chip->port + 0x05);
  304. #ifdef REG_DEBUG
  305. snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
  306. reg, old, new);
  307. #endif
  308. }
  309. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  310. return oval;
  311. }
  312. static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
  313. unsigned char mask)
  314. {
  315. int old, expected, new;
  316. unsigned long flags;
  317. spin_lock_irqsave(&chip->mixer_lock, flags);
  318. outb(reg, chip->port + 0x04);
  319. old = inb(chip->port + 0x05);
  320. expected = old ^ mask;
  321. outb(expected, chip->port + 0x05);
  322. new = inb(chip->port + 0x05);
  323. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  324. #ifdef REG_DEBUG
  325. snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
  326. reg, old, expected, new);
  327. #endif
  328. return expected == new;
  329. }
  330. static int snd_es18xx_reset(struct snd_es18xx *chip)
  331. {
  332. int i;
  333. outb(0x03, chip->port + 0x06);
  334. inb(chip->port + 0x06);
  335. outb(0x00, chip->port + 0x06);
  336. for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
  337. if (inb(chip->port + 0x0A) != 0xAA)
  338. return -1;
  339. return 0;
  340. }
  341. static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
  342. {
  343. outb(0x02, chip->port + 0x06);
  344. inb(chip->port + 0x06);
  345. outb(0x00, chip->port + 0x06);
  346. return 0;
  347. }
  348. static struct snd_ratnum new_clocks[2] = {
  349. {
  350. .num = 793800,
  351. .den_min = 1,
  352. .den_max = 128,
  353. .den_step = 1,
  354. },
  355. {
  356. .num = 768000,
  357. .den_min = 1,
  358. .den_max = 128,
  359. .den_step = 1,
  360. }
  361. };
  362. static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
  363. .nrats = 2,
  364. .rats = new_clocks,
  365. };
  366. static struct snd_ratnum old_clocks[2] = {
  367. {
  368. .num = 795444,
  369. .den_min = 1,
  370. .den_max = 128,
  371. .den_step = 1,
  372. },
  373. {
  374. .num = 397722,
  375. .den_min = 1,
  376. .den_max = 128,
  377. .den_step = 1,
  378. }
  379. };
  380. static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
  381. .nrats = 2,
  382. .rats = old_clocks,
  383. };
  384. static void snd_es18xx_rate_set(struct snd_es18xx *chip,
  385. struct snd_pcm_substream *substream,
  386. int mode)
  387. {
  388. unsigned int bits, div0;
  389. struct snd_pcm_runtime *runtime = substream->runtime;
  390. if (chip->caps & ES18XX_NEW_RATE) {
  391. if (runtime->rate_num == new_clocks[0].num)
  392. bits = 128 - runtime->rate_den;
  393. else
  394. bits = 256 - runtime->rate_den;
  395. } else {
  396. if (runtime->rate_num == old_clocks[0].num)
  397. bits = 256 - runtime->rate_den;
  398. else
  399. bits = 128 - runtime->rate_den;
  400. }
  401. /* set filter register */
  402. div0 = 256 - 7160000*20/(8*82*runtime->rate);
  403. if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
  404. snd_es18xx_mixer_write(chip, 0x70, bits);
  405. /*
  406. * Comment from kernel oss driver:
  407. * FKS: fascinating: 0x72 doesn't seem to work.
  408. */
  409. snd_es18xx_write(chip, 0xA2, div0);
  410. snd_es18xx_mixer_write(chip, 0x72, div0);
  411. } else {
  412. snd_es18xx_write(chip, 0xA1, bits);
  413. snd_es18xx_write(chip, 0xA2, div0);
  414. }
  415. }
  416. static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
  417. struct snd_pcm_hw_params *hw_params)
  418. {
  419. struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  420. int shift, err;
  421. shift = 0;
  422. if (params_channels(hw_params) == 2)
  423. shift++;
  424. if (snd_pcm_format_width(params_format(hw_params)) == 16)
  425. shift++;
  426. if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
  427. if ((chip->caps & ES18XX_DUPLEX_MONO) &&
  428. (chip->capture_a_substream) &&
  429. params_channels(hw_params) != 1) {
  430. _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
  431. return -EBUSY;
  432. }
  433. chip->dma2_shift = shift;
  434. } else {
  435. chip->dma1_shift = shift;
  436. }
  437. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  438. return err;
  439. return 0;
  440. }
  441. static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
  442. {
  443. return snd_pcm_lib_free_pages(substream);
  444. }
  445. static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
  446. struct snd_pcm_substream *substream)
  447. {
  448. struct snd_pcm_runtime *runtime = substream->runtime;
  449. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  450. unsigned int count = snd_pcm_lib_period_bytes(substream);
  451. chip->dma2_size = size;
  452. snd_es18xx_rate_set(chip, substream, DAC2);
  453. /* Transfer Count Reload */
  454. count = 0x10000 - count;
  455. snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
  456. snd_es18xx_mixer_write(chip, 0x76, count >> 8);
  457. /* Set format */
  458. snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
  459. ((runtime->channels == 1) ? 0x00 : 0x02) |
  460. (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
  461. (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
  462. /* Set DMA controller */
  463. snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
  464. return 0;
  465. }
  466. static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
  467. struct snd_pcm_substream *substream,
  468. int cmd)
  469. {
  470. switch (cmd) {
  471. case SNDRV_PCM_TRIGGER_START:
  472. case SNDRV_PCM_TRIGGER_RESUME:
  473. if (chip->active & DAC2)
  474. return 0;
  475. chip->active |= DAC2;
  476. /* Start DMA */
  477. if (chip->dma2 >= 4)
  478. snd_es18xx_mixer_write(chip, 0x78, 0xb3);
  479. else
  480. snd_es18xx_mixer_write(chip, 0x78, 0x93);
  481. #ifdef AVOID_POPS
  482. /* Avoid pops */
  483. udelay(100000);
  484. if (chip->caps & ES18XX_PCM2)
  485. /* Restore Audio 2 volume */
  486. snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
  487. else
  488. /* Enable PCM output */
  489. snd_es18xx_dsp_command(chip, 0xD1);
  490. #endif
  491. break;
  492. case SNDRV_PCM_TRIGGER_STOP:
  493. case SNDRV_PCM_TRIGGER_SUSPEND:
  494. if (!(chip->active & DAC2))
  495. return 0;
  496. chip->active &= ~DAC2;
  497. /* Stop DMA */
  498. snd_es18xx_mixer_write(chip, 0x78, 0x00);
  499. #ifdef AVOID_POPS
  500. udelay(25000);
  501. if (chip->caps & ES18XX_PCM2)
  502. /* Set Audio 2 volume to 0 */
  503. snd_es18xx_mixer_write(chip, 0x7C, 0);
  504. else
  505. /* Disable PCM output */
  506. snd_es18xx_dsp_command(chip, 0xD3);
  507. #endif
  508. break;
  509. default:
  510. return -EINVAL;
  511. }
  512. return 0;
  513. }
  514. static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
  515. struct snd_pcm_hw_params *hw_params)
  516. {
  517. struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  518. int shift, err;
  519. shift = 0;
  520. if ((chip->caps & ES18XX_DUPLEX_MONO) &&
  521. chip->playback_a_substream &&
  522. params_channels(hw_params) != 1) {
  523. _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
  524. return -EBUSY;
  525. }
  526. if (params_channels(hw_params) == 2)
  527. shift++;
  528. if (snd_pcm_format_width(params_format(hw_params)) == 16)
  529. shift++;
  530. chip->dma1_shift = shift;
  531. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  532. return err;
  533. return 0;
  534. }
  535. static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
  536. {
  537. struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  538. struct snd_pcm_runtime *runtime = substream->runtime;
  539. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  540. unsigned int count = snd_pcm_lib_period_bytes(substream);
  541. chip->dma1_size = size;
  542. snd_es18xx_reset_fifo(chip);
  543. /* Set stereo/mono */
  544. snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
  545. snd_es18xx_rate_set(chip, substream, ADC1);
  546. /* Transfer Count Reload */
  547. count = 0x10000 - count;
  548. snd_es18xx_write(chip, 0xA4, count & 0xff);
  549. snd_es18xx_write(chip, 0xA5, count >> 8);
  550. #ifdef AVOID_POPS
  551. udelay(100000);
  552. #endif
  553. /* Set format */
  554. snd_es18xx_write(chip, 0xB7,
  555. snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
  556. snd_es18xx_write(chip, 0xB7, 0x90 |
  557. ((runtime->channels == 1) ? 0x40 : 0x08) |
  558. (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
  559. (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
  560. /* Set DMA controler */
  561. snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
  562. return 0;
  563. }
  564. static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
  565. int cmd)
  566. {
  567. struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  568. switch (cmd) {
  569. case SNDRV_PCM_TRIGGER_START:
  570. case SNDRV_PCM_TRIGGER_RESUME:
  571. if (chip->active & ADC1)
  572. return 0;
  573. chip->active |= ADC1;
  574. /* Start DMA */
  575. snd_es18xx_write(chip, 0xB8, 0x0f);
  576. break;
  577. case SNDRV_PCM_TRIGGER_STOP:
  578. case SNDRV_PCM_TRIGGER_SUSPEND:
  579. if (!(chip->active & ADC1))
  580. return 0;
  581. chip->active &= ~ADC1;
  582. /* Stop DMA */
  583. snd_es18xx_write(chip, 0xB8, 0x00);
  584. break;
  585. default:
  586. return -EINVAL;
  587. }
  588. return 0;
  589. }
  590. static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
  591. struct snd_pcm_substream *substream)
  592. {
  593. struct snd_pcm_runtime *runtime = substream->runtime;
  594. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  595. unsigned int count = snd_pcm_lib_period_bytes(substream);
  596. chip->dma1_size = size;
  597. snd_es18xx_reset_fifo(chip);
  598. /* Set stereo/mono */
  599. snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
  600. snd_es18xx_rate_set(chip, substream, DAC1);
  601. /* Transfer Count Reload */
  602. count = 0x10000 - count;
  603. snd_es18xx_write(chip, 0xA4, count & 0xff);
  604. snd_es18xx_write(chip, 0xA5, count >> 8);
  605. /* Set format */
  606. snd_es18xx_write(chip, 0xB6,
  607. snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
  608. snd_es18xx_write(chip, 0xB7,
  609. snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
  610. snd_es18xx_write(chip, 0xB7, 0x90 |
  611. (runtime->channels == 1 ? 0x40 : 0x08) |
  612. (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
  613. (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
  614. /* Set DMA controler */
  615. snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
  616. return 0;
  617. }
  618. static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
  619. struct snd_pcm_substream *substream,
  620. int cmd)
  621. {
  622. switch (cmd) {
  623. case SNDRV_PCM_TRIGGER_START:
  624. case SNDRV_PCM_TRIGGER_RESUME:
  625. if (chip->active & DAC1)
  626. return 0;
  627. chip->active |= DAC1;
  628. /* Start DMA */
  629. snd_es18xx_write(chip, 0xB8, 0x05);
  630. #ifdef AVOID_POPS
  631. /* Avoid pops */
  632. udelay(100000);
  633. /* Enable Audio 1 */
  634. snd_es18xx_dsp_command(chip, 0xD1);
  635. #endif
  636. break;
  637. case SNDRV_PCM_TRIGGER_STOP:
  638. case SNDRV_PCM_TRIGGER_SUSPEND:
  639. if (!(chip->active & DAC1))
  640. return 0;
  641. chip->active &= ~DAC1;
  642. /* Stop DMA */
  643. snd_es18xx_write(chip, 0xB8, 0x00);
  644. #ifdef AVOID_POPS
  645. /* Avoid pops */
  646. udelay(25000);
  647. /* Disable Audio 1 */
  648. snd_es18xx_dsp_command(chip, 0xD3);
  649. #endif
  650. break;
  651. default:
  652. return -EINVAL;
  653. }
  654. return 0;
  655. }
  656. static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
  657. {
  658. struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  659. if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
  660. return snd_es18xx_playback1_prepare(chip, substream);
  661. else
  662. return snd_es18xx_playback2_prepare(chip, substream);
  663. }
  664. static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
  665. int cmd)
  666. {
  667. struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  668. if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
  669. return snd_es18xx_playback1_trigger(chip, substream, cmd);
  670. else
  671. return snd_es18xx_playback2_trigger(chip, substream, cmd);
  672. }
  673. static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id)
  674. {
  675. struct snd_es18xx *chip = dev_id;
  676. unsigned char status;
  677. if (chip->caps & ES18XX_CONTROL) {
  678. /* Read Interrupt status */
  679. status = inb(chip->ctrl_port + 6);
  680. } else {
  681. /* Read Interrupt status */
  682. status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
  683. }
  684. #if 0
  685. else {
  686. status = 0;
  687. if (inb(chip->port + 0x0C) & 0x01)
  688. status |= AUDIO1_IRQ;
  689. if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
  690. status |= AUDIO2_IRQ;
  691. if ((chip->caps & ES18XX_HWV) &&
  692. snd_es18xx_mixer_read(chip, 0x64) & 0x10)
  693. status |= HWV_IRQ;
  694. }
  695. #endif
  696. /* Audio 1 & Audio 2 */
  697. if (status & AUDIO2_IRQ) {
  698. if (chip->active & DAC2)
  699. snd_pcm_period_elapsed(chip->playback_a_substream);
  700. /* ack interrupt */
  701. snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
  702. }
  703. if (status & AUDIO1_IRQ) {
  704. /* ok.. capture is active */
  705. if (chip->active & ADC1)
  706. snd_pcm_period_elapsed(chip->capture_a_substream);
  707. /* ok.. playback2 is active */
  708. else if (chip->active & DAC1)
  709. snd_pcm_period_elapsed(chip->playback_b_substream);
  710. /* ack interrupt */
  711. inb(chip->port + 0x0E);
  712. }
  713. /* MPU */
  714. if ((status & MPU_IRQ) && chip->rmidi)
  715. snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
  716. /* Hardware volume */
  717. if (status & HWV_IRQ) {
  718. int split = 0;
  719. if (chip->caps & ES18XX_HWV) {
  720. split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
  721. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
  722. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
  723. }
  724. if (!split) {
  725. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
  726. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
  727. }
  728. /* ack interrupt */
  729. snd_es18xx_mixer_write(chip, 0x66, 0x00);
  730. }
  731. return IRQ_HANDLED;
  732. }
  733. static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
  734. {
  735. struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  736. int pos;
  737. if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
  738. if (!(chip->active & DAC2))
  739. return 0;
  740. pos = snd_dma_pointer(chip->dma2, chip->dma2_size);
  741. return pos >> chip->dma2_shift;
  742. } else {
  743. if (!(chip->active & DAC1))
  744. return 0;
  745. pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
  746. return pos >> chip->dma1_shift;
  747. }
  748. }
  749. static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
  750. {
  751. struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  752. int pos;
  753. if (!(chip->active & ADC1))
  754. return 0;
  755. pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
  756. return pos >> chip->dma1_shift;
  757. }
  758. static struct snd_pcm_hardware snd_es18xx_playback =
  759. {
  760. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  761. SNDRV_PCM_INFO_RESUME |
  762. SNDRV_PCM_INFO_MMAP_VALID),
  763. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
  764. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
  765. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  766. .rate_min = 4000,
  767. .rate_max = 48000,
  768. .channels_min = 1,
  769. .channels_max = 2,
  770. .buffer_bytes_max = 65536,
  771. .period_bytes_min = 64,
  772. .period_bytes_max = 65536,
  773. .periods_min = 1,
  774. .periods_max = 1024,
  775. .fifo_size = 0,
  776. };
  777. static struct snd_pcm_hardware snd_es18xx_capture =
  778. {
  779. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  780. SNDRV_PCM_INFO_RESUME |
  781. SNDRV_PCM_INFO_MMAP_VALID),
  782. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
  783. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
  784. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  785. .rate_min = 4000,
  786. .rate_max = 48000,
  787. .channels_min = 1,
  788. .channels_max = 2,
  789. .buffer_bytes_max = 65536,
  790. .period_bytes_min = 64,
  791. .period_bytes_max = 65536,
  792. .periods_min = 1,
  793. .periods_max = 1024,
  794. .fifo_size = 0,
  795. };
  796. static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
  797. {
  798. struct snd_pcm_runtime *runtime = substream->runtime;
  799. struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  800. if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
  801. if ((chip->caps & ES18XX_DUPLEX_MONO) &&
  802. chip->capture_a_substream &&
  803. chip->capture_a_substream->runtime->channels != 1)
  804. return -EAGAIN;
  805. chip->playback_a_substream = substream;
  806. } else if (substream->number <= 1) {
  807. if (chip->capture_a_substream)
  808. return -EAGAIN;
  809. chip->playback_b_substream = substream;
  810. } else {
  811. snd_BUG();
  812. return -EINVAL;
  813. }
  814. substream->runtime->hw = snd_es18xx_playback;
  815. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  816. (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
  817. return 0;
  818. }
  819. static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
  820. {
  821. struct snd_pcm_runtime *runtime = substream->runtime;
  822. struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  823. if (chip->playback_b_substream)
  824. return -EAGAIN;
  825. if ((chip->caps & ES18XX_DUPLEX_MONO) &&
  826. chip->playback_a_substream &&
  827. chip->playback_a_substream->runtime->channels != 1)
  828. return -EAGAIN;
  829. chip->capture_a_substream = substream;
  830. substream->runtime->hw = snd_es18xx_capture;
  831. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  832. (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
  833. return 0;
  834. }
  835. static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
  836. {
  837. struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  838. if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
  839. chip->playback_a_substream = NULL;
  840. else
  841. chip->playback_b_substream = NULL;
  842. snd_pcm_lib_free_pages(substream);
  843. return 0;
  844. }
  845. static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
  846. {
  847. struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  848. chip->capture_a_substream = NULL;
  849. snd_pcm_lib_free_pages(substream);
  850. return 0;
  851. }
  852. /*
  853. * MIXER part
  854. */
  855. /* Record source mux routines:
  856. * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
  857. * bit table for the 4/5 source mux:
  858. * reg 1C:
  859. * b2 b1 b0 muxSource
  860. * x 0 x microphone
  861. * 0 1 x CD
  862. * 1 1 0 line
  863. * 1 1 1 mixer
  864. * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
  865. * either the play mixer or the capture mixer.
  866. *
  867. * "map4Source" translates from source number to reg bit pattern
  868. * "invMap4Source" translates from reg bit pattern to source number
  869. */
  870. static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  871. {
  872. static char *texts4Source[4] = {
  873. "Mic", "CD", "Line", "Master"
  874. };
  875. static char *texts5Source[5] = {
  876. "Mic", "CD", "Line", "Master", "Mix"
  877. };
  878. static char *texts8Source[8] = {
  879. "Mic", "Mic Master", "CD", "AOUT",
  880. "Mic1", "Mix", "Line", "Master"
  881. };
  882. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  883. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  884. uinfo->count = 1;
  885. switch (chip->version) {
  886. case 0x1868:
  887. case 0x1878:
  888. uinfo->value.enumerated.items = 4;
  889. if (uinfo->value.enumerated.item > 3)
  890. uinfo->value.enumerated.item = 3;
  891. strcpy(uinfo->value.enumerated.name, texts4Source[uinfo->value.enumerated.item]);
  892. break;
  893. case 0x1887:
  894. case 0x1888:
  895. uinfo->value.enumerated.items = 5;
  896. if (uinfo->value.enumerated.item > 4)
  897. uinfo->value.enumerated.item = 4;
  898. strcpy(uinfo->value.enumerated.name, texts5Source[uinfo->value.enumerated.item]);
  899. break;
  900. case 0x1869: /* DS somewhat contradictory for 1869: could be be 5 or 8 */
  901. case 0x1879:
  902. uinfo->value.enumerated.items = 8;
  903. if (uinfo->value.enumerated.item > 7)
  904. uinfo->value.enumerated.item = 7;
  905. strcpy(uinfo->value.enumerated.name, texts8Source[uinfo->value.enumerated.item]);
  906. break;
  907. default:
  908. return -EINVAL;
  909. }
  910. return 0;
  911. }
  912. static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  913. {
  914. static unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
  915. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  916. int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
  917. if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
  918. muxSource = invMap4Source[muxSource];
  919. if (muxSource==3 &&
  920. (chip->version == 0x1887 || chip->version == 0x1888) &&
  921. (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
  922. )
  923. muxSource = 4;
  924. }
  925. ucontrol->value.enumerated.item[0] = muxSource;
  926. return 0;
  927. }
  928. static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  929. {
  930. static unsigned char map4Source[4] = {0, 2, 6, 7};
  931. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  932. unsigned char val = ucontrol->value.enumerated.item[0];
  933. unsigned char retVal = 0;
  934. switch (chip->version) {
  935. /* 5 source chips */
  936. case 0x1887:
  937. case 0x1888:
  938. if (val > 4)
  939. return -EINVAL;
  940. if (val == 4) {
  941. retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
  942. val = 3;
  943. } else
  944. retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
  945. /* 4 source chips */
  946. case 0x1868:
  947. case 0x1878:
  948. if (val > 3)
  949. return -EINVAL;
  950. val = map4Source[val];
  951. break;
  952. /* 8 source chips */
  953. case 0x1869:
  954. case 0x1879:
  955. if (val > 7)
  956. return -EINVAL;
  957. break;
  958. default:
  959. return -EINVAL;
  960. }
  961. return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
  962. }
  963. static int snd_es18xx_info_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  964. {
  965. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  966. uinfo->count = 1;
  967. uinfo->value.integer.min = 0;
  968. uinfo->value.integer.max = 1;
  969. return 0;
  970. }
  971. static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  972. {
  973. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  974. unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
  975. ucontrol->value.integer.value[0] = !!(val & 8);
  976. return 0;
  977. }
  978. static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  979. {
  980. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  981. unsigned char oval, nval;
  982. int change;
  983. nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
  984. oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
  985. change = nval != oval;
  986. if (change) {
  987. snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
  988. snd_es18xx_mixer_write(chip, 0x50, nval);
  989. }
  990. return change;
  991. }
  992. static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  993. {
  994. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  995. uinfo->count = 2;
  996. uinfo->value.integer.min = 0;
  997. uinfo->value.integer.max = 63;
  998. return 0;
  999. }
  1000. static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1001. {
  1002. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  1003. ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
  1004. ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
  1005. return 0;
  1006. }
  1007. static int snd_es18xx_info_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1008. {
  1009. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1010. uinfo->count = 2;
  1011. uinfo->value.integer.min = 0;
  1012. uinfo->value.integer.max = 1;
  1013. return 0;
  1014. }
  1015. static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1016. {
  1017. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  1018. ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
  1019. ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
  1020. return 0;
  1021. }
  1022. static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
  1023. {
  1024. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  1025. chip->master_volume = NULL;
  1026. chip->master_switch = NULL;
  1027. chip->hw_volume = NULL;
  1028. chip->hw_switch = NULL;
  1029. }
  1030. static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
  1031. unsigned char mask, unsigned char val)
  1032. {
  1033. if (reg < 0xa0)
  1034. return snd_es18xx_mixer_bits(chip, reg, mask, val);
  1035. else
  1036. return snd_es18xx_bits(chip, reg, mask, val);
  1037. }
  1038. static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
  1039. {
  1040. if (reg < 0xa0)
  1041. return snd_es18xx_mixer_read(chip, reg);
  1042. else
  1043. return snd_es18xx_read(chip, reg);
  1044. }
  1045. #define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
  1046. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1047. .info = snd_es18xx_info_single, \
  1048. .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
  1049. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  1050. static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1051. {
  1052. int mask = (kcontrol->private_value >> 16) & 0xff;
  1053. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1054. uinfo->count = 1;
  1055. uinfo->value.integer.min = 0;
  1056. uinfo->value.integer.max = mask;
  1057. return 0;
  1058. }
  1059. static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1060. {
  1061. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  1062. int reg = kcontrol->private_value & 0xff;
  1063. int shift = (kcontrol->private_value >> 8) & 0xff;
  1064. int mask = (kcontrol->private_value >> 16) & 0xff;
  1065. int invert = (kcontrol->private_value >> 24) & 0xff;
  1066. int val;
  1067. val = snd_es18xx_reg_read(chip, reg);
  1068. ucontrol->value.integer.value[0] = (val >> shift) & mask;
  1069. if (invert)
  1070. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1071. return 0;
  1072. }
  1073. static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1074. {
  1075. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  1076. int reg = kcontrol->private_value & 0xff;
  1077. int shift = (kcontrol->private_value >> 8) & 0xff;
  1078. int mask = (kcontrol->private_value >> 16) & 0xff;
  1079. int invert = (kcontrol->private_value >> 24) & 0xff;
  1080. unsigned char val;
  1081. val = (ucontrol->value.integer.value[0] & mask);
  1082. if (invert)
  1083. val = mask - val;
  1084. mask <<= shift;
  1085. val <<= shift;
  1086. return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
  1087. }
  1088. #define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  1089. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1090. .info = snd_es18xx_info_double, \
  1091. .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
  1092. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  1093. static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1094. {
  1095. int mask = (kcontrol->private_value >> 24) & 0xff;
  1096. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1097. uinfo->count = 2;
  1098. uinfo->value.integer.min = 0;
  1099. uinfo->value.integer.max = mask;
  1100. return 0;
  1101. }
  1102. static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1103. {
  1104. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  1105. int left_reg = kcontrol->private_value & 0xff;
  1106. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1107. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1108. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1109. int mask = (kcontrol->private_value >> 24) & 0xff;
  1110. int invert = (kcontrol->private_value >> 22) & 1;
  1111. unsigned char left, right;
  1112. left = snd_es18xx_reg_read(chip, left_reg);
  1113. if (left_reg != right_reg)
  1114. right = snd_es18xx_reg_read(chip, right_reg);
  1115. else
  1116. right = left;
  1117. ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
  1118. ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
  1119. if (invert) {
  1120. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1121. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  1122. }
  1123. return 0;
  1124. }
  1125. static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1126. {
  1127. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  1128. int left_reg = kcontrol->private_value & 0xff;
  1129. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1130. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1131. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1132. int mask = (kcontrol->private_value >> 24) & 0xff;
  1133. int invert = (kcontrol->private_value >> 22) & 1;
  1134. int change;
  1135. unsigned char val1, val2, mask1, mask2;
  1136. val1 = ucontrol->value.integer.value[0] & mask;
  1137. val2 = ucontrol->value.integer.value[1] & mask;
  1138. if (invert) {
  1139. val1 = mask - val1;
  1140. val2 = mask - val2;
  1141. }
  1142. val1 <<= shift_left;
  1143. val2 <<= shift_right;
  1144. mask1 = mask << shift_left;
  1145. mask2 = mask << shift_right;
  1146. if (left_reg != right_reg) {
  1147. change = 0;
  1148. if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
  1149. change = 1;
  1150. if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
  1151. change = 1;
  1152. } else {
  1153. change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
  1154. val1 | val2) != (val1 | val2));
  1155. }
  1156. return change;
  1157. }
  1158. /* Mixer controls
  1159. * These arrays contain setup data for mixer controls.
  1160. *
  1161. * The controls that are universal to all chipsets are fully initialized
  1162. * here.
  1163. */
  1164. static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
  1165. ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
  1166. ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
  1167. ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
  1168. ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
  1169. ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
  1170. ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
  1171. ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
  1172. ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
  1173. ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
  1174. {
  1175. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1176. .name = "Capture Source",
  1177. .info = snd_es18xx_info_mux,
  1178. .get = snd_es18xx_get_mux,
  1179. .put = snd_es18xx_put_mux,
  1180. }
  1181. };
  1182. static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
  1183. ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
  1184. ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
  1185. ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
  1186. ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
  1187. ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
  1188. ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
  1189. };
  1190. /*
  1191. * The chipset specific mixer controls
  1192. */
  1193. static struct snd_kcontrol_new snd_es18xx_opt_speaker =
  1194. ES18XX_SINGLE("PC Speaker Playback Volume", 0, 0x3c, 0, 7, 0);
  1195. static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
  1196. ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
  1197. ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
  1198. ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
  1199. ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
  1200. };
  1201. static struct snd_kcontrol_new snd_es18xx_opt_1878 =
  1202. ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
  1203. static struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
  1204. ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
  1205. ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
  1206. ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
  1207. };
  1208. static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
  1209. ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
  1210. };
  1211. static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
  1212. ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
  1213. ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
  1214. };
  1215. static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
  1216. ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
  1217. {
  1218. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1219. .name = "3D Control - Switch",
  1220. .info = snd_es18xx_info_spatializer_enable,
  1221. .get = snd_es18xx_get_spatializer_enable,
  1222. .put = snd_es18xx_put_spatializer_enable,
  1223. }
  1224. };
  1225. static struct snd_kcontrol_new snd_es18xx_micpre1_control =
  1226. ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
  1227. static struct snd_kcontrol_new snd_es18xx_micpre2_control =
  1228. ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
  1229. static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
  1230. {
  1231. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1232. .name = "Hardware Master Playback Volume",
  1233. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1234. .info = snd_es18xx_info_hw_volume,
  1235. .get = snd_es18xx_get_hw_volume,
  1236. },
  1237. {
  1238. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1239. .name = "Hardware Master Playback Switch",
  1240. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1241. .info = snd_es18xx_info_hw_switch,
  1242. .get = snd_es18xx_get_hw_switch,
  1243. },
  1244. ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
  1245. };
  1246. static int __devinit snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
  1247. {
  1248. int data;
  1249. unsigned long flags;
  1250. spin_lock_irqsave(&chip->ctrl_lock, flags);
  1251. outb(reg, chip->ctrl_port);
  1252. data = inb(chip->ctrl_port + 1);
  1253. spin_unlock_irqrestore(&chip->ctrl_lock, flags);
  1254. return data;
  1255. }
  1256. static void __devinit snd_es18xx_config_write(struct snd_es18xx *chip,
  1257. unsigned char reg, unsigned char data)
  1258. {
  1259. /* No need for spinlocks, this function is used only in
  1260. otherwise protected init code */
  1261. outb(reg, chip->ctrl_port);
  1262. outb(data, chip->ctrl_port + 1);
  1263. #ifdef REG_DEBUG
  1264. snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
  1265. #endif
  1266. }
  1267. static int __devinit snd_es18xx_initialize(struct snd_es18xx *chip)
  1268. {
  1269. int mask = 0;
  1270. /* enable extended mode */
  1271. snd_es18xx_dsp_command(chip, 0xC6);
  1272. /* Reset mixer registers */
  1273. snd_es18xx_mixer_write(chip, 0x00, 0x00);
  1274. /* Audio 1 DMA demand mode (4 bytes/request) */
  1275. snd_es18xx_write(chip, 0xB9, 2);
  1276. if (chip->caps & ES18XX_CONTROL) {
  1277. /* Hardware volume IRQ */
  1278. snd_es18xx_config_write(chip, 0x27, chip->irq);
  1279. if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
  1280. /* FM I/O */
  1281. snd_es18xx_config_write(chip, 0x62, chip->fm_port >> 8);
  1282. snd_es18xx_config_write(chip, 0x63, chip->fm_port & 0xff);
  1283. }
  1284. if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
  1285. /* MPU-401 I/O */
  1286. snd_es18xx_config_write(chip, 0x64, chip->mpu_port >> 8);
  1287. snd_es18xx_config_write(chip, 0x65, chip->mpu_port & 0xff);
  1288. /* MPU-401 IRQ */
  1289. snd_es18xx_config_write(chip, 0x28, chip->irq);
  1290. }
  1291. /* Audio1 IRQ */
  1292. snd_es18xx_config_write(chip, 0x70, chip->irq);
  1293. /* Audio2 IRQ */
  1294. snd_es18xx_config_write(chip, 0x72, chip->irq);
  1295. /* Audio1 DMA */
  1296. snd_es18xx_config_write(chip, 0x74, chip->dma1);
  1297. /* Audio2 DMA */
  1298. snd_es18xx_config_write(chip, 0x75, chip->dma2);
  1299. /* Enable Audio 1 IRQ */
  1300. snd_es18xx_write(chip, 0xB1, 0x50);
  1301. /* Enable Audio 2 IRQ */
  1302. snd_es18xx_mixer_write(chip, 0x7A, 0x40);
  1303. /* Enable Audio 1 DMA */
  1304. snd_es18xx_write(chip, 0xB2, 0x50);
  1305. /* Enable MPU and hardware volume interrupt */
  1306. snd_es18xx_mixer_write(chip, 0x64, 0x42);
  1307. }
  1308. else {
  1309. int irqmask, dma1mask, dma2mask;
  1310. switch (chip->irq) {
  1311. case 2:
  1312. case 9:
  1313. irqmask = 0;
  1314. break;
  1315. case 5:
  1316. irqmask = 1;
  1317. break;
  1318. case 7:
  1319. irqmask = 2;
  1320. break;
  1321. case 10:
  1322. irqmask = 3;
  1323. break;
  1324. default:
  1325. snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
  1326. return -ENODEV;
  1327. }
  1328. switch (chip->dma1) {
  1329. case 0:
  1330. dma1mask = 1;
  1331. break;
  1332. case 1:
  1333. dma1mask = 2;
  1334. break;
  1335. case 3:
  1336. dma1mask = 3;
  1337. break;
  1338. default:
  1339. snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
  1340. return -ENODEV;
  1341. }
  1342. switch (chip->dma2) {
  1343. case 0:
  1344. dma2mask = 0;
  1345. break;
  1346. case 1:
  1347. dma2mask = 1;
  1348. break;
  1349. case 3:
  1350. dma2mask = 2;
  1351. break;
  1352. case 5:
  1353. dma2mask = 3;
  1354. break;
  1355. default:
  1356. snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
  1357. return -ENODEV;
  1358. }
  1359. /* Enable and set Audio 1 IRQ */
  1360. snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
  1361. /* Enable and set Audio 1 DMA */
  1362. snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
  1363. /* Set Audio 2 DMA */
  1364. snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
  1365. /* Enable Audio 2 IRQ and DMA
  1366. Set capture mixer input */
  1367. snd_es18xx_mixer_write(chip, 0x7A, 0x68);
  1368. /* Enable and set hardware volume interrupt */
  1369. snd_es18xx_mixer_write(chip, 0x64, 0x06);
  1370. if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
  1371. /* MPU401 share irq with audio
  1372. Joystick enabled
  1373. FM enabled */
  1374. snd_es18xx_mixer_write(chip, 0x40, 0x43 | (chip->mpu_port & 0xf0) >> 1);
  1375. }
  1376. snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
  1377. }
  1378. if (chip->caps & ES18XX_NEW_RATE) {
  1379. /* Change behaviour of register A1
  1380. 4x oversampling
  1381. 2nd channel DAC asynchronous */
  1382. snd_es18xx_mixer_write(chip, 0x71, 0x32);
  1383. }
  1384. if (!(chip->caps & ES18XX_PCM2)) {
  1385. /* Enable DMA FIFO */
  1386. snd_es18xx_write(chip, 0xB7, 0x80);
  1387. }
  1388. if (chip->caps & ES18XX_SPATIALIZER) {
  1389. /* Set spatializer parameters to recommended values */
  1390. snd_es18xx_mixer_write(chip, 0x54, 0x8f);
  1391. snd_es18xx_mixer_write(chip, 0x56, 0x95);
  1392. snd_es18xx_mixer_write(chip, 0x58, 0x94);
  1393. snd_es18xx_mixer_write(chip, 0x5a, 0x80);
  1394. }
  1395. /* Flip the "enable I2S" bits for those chipsets that need it */
  1396. switch (chip->version) {
  1397. case 0x1879:
  1398. //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
  1399. //so a Switch control has been added to toggle this 0x71 bit on/off:
  1400. //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
  1401. /* Note: we fall through on purpose here. */
  1402. case 0x1878:
  1403. snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
  1404. break;
  1405. }
  1406. /* Mute input source */
  1407. if (chip->caps & ES18XX_MUTEREC)
  1408. mask = 0x10;
  1409. if (chip->caps & ES18XX_RECMIX)
  1410. snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
  1411. else {
  1412. snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
  1413. snd_es18xx_write(chip, 0xb4, 0x00);
  1414. }
  1415. #ifndef AVOID_POPS
  1416. /* Enable PCM output */
  1417. snd_es18xx_dsp_command(chip, 0xD1);
  1418. #endif
  1419. return 0;
  1420. }
  1421. static int __devinit snd_es18xx_identify(struct snd_es18xx *chip)
  1422. {
  1423. int hi,lo;
  1424. /* reset */
  1425. if (snd_es18xx_reset(chip) < 0) {
  1426. snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
  1427. return -ENODEV;
  1428. }
  1429. snd_es18xx_dsp_command(chip, 0xe7);
  1430. hi = snd_es18xx_dsp_get_byte(chip);
  1431. if (hi < 0) {
  1432. return hi;
  1433. }
  1434. lo = snd_es18xx_dsp_get_byte(chip);
  1435. if ((lo & 0xf0) != 0x80) {
  1436. return -ENODEV;
  1437. }
  1438. if (hi == 0x48) {
  1439. chip->version = 0x488;
  1440. return 0;
  1441. }
  1442. if (hi != 0x68) {
  1443. return -ENODEV;
  1444. }
  1445. if ((lo & 0x0f) < 8) {
  1446. chip->version = 0x688;
  1447. return 0;
  1448. }
  1449. outb(0x40, chip->port + 0x04);
  1450. udelay(10);
  1451. hi = inb(chip->port + 0x05);
  1452. udelay(10);
  1453. lo = inb(chip->port + 0x05);
  1454. if (hi != lo) {
  1455. chip->version = hi << 8 | lo;
  1456. chip->ctrl_port = inb(chip->port + 0x05) << 8;
  1457. udelay(10);
  1458. chip->ctrl_port += inb(chip->port + 0x05);
  1459. if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
  1460. snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
  1461. return -EBUSY;
  1462. }
  1463. return 0;
  1464. }
  1465. /* If has Hardware volume */
  1466. if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
  1467. /* If has Audio2 */
  1468. if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
  1469. /* If has volume count */
  1470. if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
  1471. chip->version = 0x1887;
  1472. } else {
  1473. chip->version = 0x1888;
  1474. }
  1475. } else {
  1476. chip->version = 0x1788;
  1477. }
  1478. }
  1479. else
  1480. chip->version = 0x1688;
  1481. return 0;
  1482. }
  1483. static int __devinit snd_es18xx_probe(struct snd_es18xx *chip)
  1484. {
  1485. if (snd_es18xx_identify(chip) < 0) {
  1486. snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
  1487. return -ENODEV;
  1488. }
  1489. switch (chip->version) {
  1490. case 0x1868:
  1491. chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL;
  1492. break;
  1493. case 0x1869:
  1494. chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
  1495. break;
  1496. case 0x1878:
  1497. chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
  1498. break;
  1499. case 0x1879:
  1500. chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
  1501. break;
  1502. case 0x1887:
  1503. chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
  1504. break;
  1505. case 0x1888:
  1506. chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
  1507. break;
  1508. default:
  1509. snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
  1510. chip->port, chip->version);
  1511. return -ENODEV;
  1512. }
  1513. snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
  1514. if (chip->dma1 == chip->dma2)
  1515. chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
  1516. return snd_es18xx_initialize(chip);
  1517. }
  1518. static struct snd_pcm_ops snd_es18xx_playback_ops = {
  1519. .open = snd_es18xx_playback_open,
  1520. .close = snd_es18xx_playback_close,
  1521. .ioctl = snd_pcm_lib_ioctl,
  1522. .hw_params = snd_es18xx_playback_hw_params,
  1523. .hw_free = snd_es18xx_pcm_hw_free,
  1524. .prepare = snd_es18xx_playback_prepare,
  1525. .trigger = snd_es18xx_playback_trigger,
  1526. .pointer = snd_es18xx_playback_pointer,
  1527. };
  1528. static struct snd_pcm_ops snd_es18xx_capture_ops = {
  1529. .open = snd_es18xx_capture_open,
  1530. .close = snd_es18xx_capture_close,
  1531. .ioctl = snd_pcm_lib_ioctl,
  1532. .hw_params = snd_es18xx_capture_hw_params,
  1533. .hw_free = snd_es18xx_pcm_hw_free,
  1534. .prepare = snd_es18xx_capture_prepare,
  1535. .trigger = snd_es18xx_capture_trigger,
  1536. .pointer = snd_es18xx_capture_pointer,
  1537. };
  1538. static int __devinit snd_es18xx_pcm(struct snd_es18xx *chip, int device, struct snd_pcm ** rpcm)
  1539. {
  1540. struct snd_pcm *pcm;
  1541. char str[16];
  1542. int err;
  1543. if (rpcm)
  1544. *rpcm = NULL;
  1545. sprintf(str, "ES%x", chip->version);
  1546. if (chip->caps & ES18XX_PCM2)
  1547. err = snd_pcm_new(chip->card, str, device, 2, 1, &pcm);
  1548. else
  1549. err = snd_pcm_new(chip->card, str, device, 1, 1, &pcm);
  1550. if (err < 0)
  1551. return err;
  1552. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
  1553. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
  1554. /* global setup */
  1555. pcm->private_data = chip;
  1556. pcm->info_flags = 0;
  1557. if (chip->caps & ES18XX_DUPLEX_SAME)
  1558. pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  1559. if (! (chip->caps & ES18XX_PCM2))
  1560. pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
  1561. sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
  1562. chip->pcm = pcm;
  1563. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1564. snd_dma_isa_data(),
  1565. 64*1024,
  1566. chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
  1567. if (rpcm)
  1568. *rpcm = pcm;
  1569. return 0;
  1570. }
  1571. /* Power Management support functions */
  1572. #ifdef CONFIG_PM
  1573. static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
  1574. {
  1575. struct snd_audiodrive *acard = card->private_data;
  1576. struct snd_es18xx *chip = acard->chip;
  1577. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
  1578. snd_pcm_suspend_all(chip->pcm);
  1579. /* power down */
  1580. chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
  1581. chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
  1582. snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
  1583. snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
  1584. return 0;
  1585. }
  1586. static int snd_es18xx_resume(struct snd_card *card)
  1587. {
  1588. struct snd_audiodrive *acard = card->private_data;
  1589. struct snd_es18xx *chip = acard->chip;
  1590. /* restore PM register, we won't wake till (not 0x07) i/o activity though */
  1591. snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
  1592. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
  1593. return 0;
  1594. }
  1595. #endif /* CONFIG_PM */
  1596. static int snd_es18xx_free(struct snd_es18xx *chip)
  1597. {
  1598. release_and_free_resource(chip->res_port);
  1599. release_and_free_resource(chip->res_ctrl_port);
  1600. release_and_free_resource(chip->res_mpu_port);
  1601. if (chip->irq >= 0)
  1602. free_irq(chip->irq, (void *) chip);
  1603. if (chip->dma1 >= 0) {
  1604. disable_dma(chip->dma1);
  1605. free_dma(chip->dma1);
  1606. }
  1607. if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
  1608. disable_dma(chip->dma2);
  1609. free_dma(chip->dma2);
  1610. }
  1611. kfree(chip);
  1612. return 0;
  1613. }
  1614. static int snd_es18xx_dev_free(struct snd_device *device)
  1615. {
  1616. struct snd_es18xx *chip = device->device_data;
  1617. return snd_es18xx_free(chip);
  1618. }
  1619. static int __devinit snd_es18xx_new_device(struct snd_card *card,
  1620. unsigned long port,
  1621. unsigned long mpu_port,
  1622. unsigned long fm_port,
  1623. int irq, int dma1, int dma2,
  1624. struct snd_es18xx ** rchip)
  1625. {
  1626. struct snd_es18xx *chip;
  1627. static struct snd_device_ops ops = {
  1628. .dev_free = snd_es18xx_dev_free,
  1629. };
  1630. int err;
  1631. *rchip = NULL;
  1632. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1633. if (chip == NULL)
  1634. return -ENOMEM;
  1635. spin_lock_init(&chip->reg_lock);
  1636. spin_lock_init(&chip->mixer_lock);
  1637. spin_lock_init(&chip->ctrl_lock);
  1638. chip->card = card;
  1639. chip->port = port;
  1640. chip->mpu_port = mpu_port;
  1641. chip->fm_port = fm_port;
  1642. chip->irq = -1;
  1643. chip->dma1 = -1;
  1644. chip->dma2 = -1;
  1645. chip->audio2_vol = 0x00;
  1646. chip->active = 0;
  1647. if ((chip->res_port = request_region(port, 16, "ES18xx")) == NULL) {
  1648. snd_es18xx_free(chip);
  1649. snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
  1650. return -EBUSY;
  1651. }
  1652. if (request_irq(irq, snd_es18xx_interrupt, IRQF_DISABLED, "ES18xx", (void *) chip)) {
  1653. snd_es18xx_free(chip);
  1654. snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
  1655. return -EBUSY;
  1656. }
  1657. chip->irq = irq;
  1658. if (request_dma(dma1, "ES18xx DMA 1")) {
  1659. snd_es18xx_free(chip);
  1660. snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
  1661. return -EBUSY;
  1662. }
  1663. chip->dma1 = dma1;
  1664. if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
  1665. snd_es18xx_free(chip);
  1666. snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
  1667. return -EBUSY;
  1668. }
  1669. chip->dma2 = dma2;
  1670. if (snd_es18xx_probe(chip) < 0) {
  1671. snd_es18xx_free(chip);
  1672. return -ENODEV;
  1673. }
  1674. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1675. snd_es18xx_free(chip);
  1676. return err;
  1677. }
  1678. *rchip = chip;
  1679. return 0;
  1680. }
  1681. static int __devinit snd_es18xx_mixer(struct snd_es18xx *chip)
  1682. {
  1683. struct snd_card *card;
  1684. int err;
  1685. unsigned int idx;
  1686. card = chip->card;
  1687. strcpy(card->mixername, chip->pcm->name);
  1688. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
  1689. struct snd_kcontrol *kctl;
  1690. kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
  1691. if (chip->caps & ES18XX_HWV) {
  1692. switch (idx) {
  1693. case 0:
  1694. chip->master_volume = kctl;
  1695. kctl->private_free = snd_es18xx_hwv_free;
  1696. break;
  1697. case 1:
  1698. chip->master_switch = kctl;
  1699. kctl->private_free = snd_es18xx_hwv_free;
  1700. break;
  1701. }
  1702. }
  1703. if ((err = snd_ctl_add(card, kctl)) < 0)
  1704. return err;
  1705. }
  1706. if (chip->caps & ES18XX_PCM2) {
  1707. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
  1708. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
  1709. return err;
  1710. }
  1711. } else {
  1712. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
  1713. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
  1714. return err;
  1715. }
  1716. }
  1717. if (chip->caps & ES18XX_RECMIX) {
  1718. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
  1719. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
  1720. return err;
  1721. }
  1722. }
  1723. switch (chip->version) {
  1724. default:
  1725. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
  1726. return err;
  1727. break;
  1728. case 0x1869:
  1729. case 0x1879:
  1730. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
  1731. return err;
  1732. break;
  1733. }
  1734. if (chip->caps & ES18XX_SPATIALIZER) {
  1735. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
  1736. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
  1737. return err;
  1738. }
  1739. }
  1740. if (chip->caps & ES18XX_HWV) {
  1741. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
  1742. struct snd_kcontrol *kctl;
  1743. kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
  1744. if (idx == 0)
  1745. chip->hw_volume = kctl;
  1746. else
  1747. chip->hw_switch = kctl;
  1748. kctl->private_free = snd_es18xx_hwv_free;
  1749. if ((err = snd_ctl_add(card, kctl)) < 0)
  1750. return err;
  1751. }
  1752. }
  1753. /* finish initializing other chipset specific controls
  1754. */
  1755. if (chip->version != 0x1868) {
  1756. err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
  1757. chip));
  1758. if (err < 0)
  1759. return err;
  1760. }
  1761. if (chip->version == 0x1869) {
  1762. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
  1763. err = snd_ctl_add(card,
  1764. snd_ctl_new1(&snd_es18xx_opt_1869[idx],
  1765. chip));
  1766. if (err < 0)
  1767. return err;
  1768. }
  1769. } else if (chip->version == 0x1878) {
  1770. err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
  1771. chip));
  1772. if (err < 0)
  1773. return err;
  1774. } else if (chip->version == 0x1879) {
  1775. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
  1776. err = snd_ctl_add(card,
  1777. snd_ctl_new1(&snd_es18xx_opt_1879[idx],
  1778. chip));
  1779. if (err < 0)
  1780. return err;
  1781. }
  1782. }
  1783. return 0;
  1784. }
  1785. /* Card level */
  1786. MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
  1787. MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
  1788. MODULE_LICENSE("GPL");
  1789. MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
  1790. "{ESS,ES1869 PnP AudioDrive},"
  1791. "{ESS,ES1878 PnP AudioDrive},"
  1792. "{ESS,ES1879 PnP AudioDrive},"
  1793. "{ESS,ES1887 PnP AudioDrive},"
  1794. "{ESS,ES1888 PnP AudioDrive},"
  1795. "{ESS,ES1887 AudioDrive},"
  1796. "{ESS,ES1888 AudioDrive}}");
  1797. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  1798. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  1799. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
  1800. #ifdef CONFIG_PNP
  1801. static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  1802. #endif
  1803. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
  1804. #ifndef CONFIG_PNP
  1805. static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
  1806. #else
  1807. static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  1808. #endif
  1809. static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  1810. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
  1811. static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
  1812. static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
  1813. module_param_array(index, int, NULL, 0444);
  1814. MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
  1815. module_param_array(id, charp, NULL, 0444);
  1816. MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
  1817. module_param_array(enable, bool, NULL, 0444);
  1818. MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
  1819. #ifdef CONFIG_PNP
  1820. module_param_array(isapnp, bool, NULL, 0444);
  1821. MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
  1822. #endif
  1823. module_param_array(port, long, NULL, 0444);
  1824. MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
  1825. module_param_array(mpu_port, long, NULL, 0444);
  1826. MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
  1827. module_param_array(fm_port, long, NULL, 0444);
  1828. MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
  1829. module_param_array(irq, int, NULL, 0444);
  1830. MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
  1831. module_param_array(dma1, int, NULL, 0444);
  1832. MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
  1833. module_param_array(dma2, int, NULL, 0444);
  1834. MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
  1835. #ifdef CONFIG_PNP
  1836. static int pnp_registered, pnpc_registered;
  1837. static struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
  1838. { .id = "ESS1869" },
  1839. { .id = "" } /* end */
  1840. };
  1841. MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
  1842. /* PnP main device initialization */
  1843. static int __devinit snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev,
  1844. struct pnp_resource_table *cfg)
  1845. {
  1846. int err;
  1847. pnp_init_resource_table(cfg);
  1848. if (port[dev] != SNDRV_AUTO_PORT)
  1849. pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
  1850. if (fm_port[dev] != SNDRV_AUTO_PORT)
  1851. pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
  1852. if (mpu_port[dev] != SNDRV_AUTO_PORT)
  1853. pnp_resource_change(&cfg->port_resource[2], mpu_port[dev], 2);
  1854. if (dma1[dev] != SNDRV_AUTO_DMA)
  1855. pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
  1856. if (dma2[dev] != SNDRV_AUTO_DMA)
  1857. pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
  1858. if (irq[dev] != SNDRV_AUTO_IRQ)
  1859. pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
  1860. if (pnp_device_is_isapnp(pdev)) {
  1861. err = pnp_manual_config_dev(pdev, cfg, 0);
  1862. if (err < 0)
  1863. snd_printk(KERN_ERR PFX "PnP manual resources are invalid, using auto config\n");
  1864. }
  1865. err = pnp_activate_dev(pdev);
  1866. if (err < 0) {
  1867. snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
  1868. return -EBUSY;
  1869. }
  1870. /* ok. hack using Vendor-Defined Card-Level registers */
  1871. /* skip csn and logdev initialization - already done in isapnp_configure */
  1872. if (pnp_device_is_isapnp(pdev)) {
  1873. isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
  1874. isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
  1875. if (mpu_port[dev] != SNDRV_AUTO_PORT)
  1876. isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
  1877. isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
  1878. isapnp_cfg_end();
  1879. }
  1880. port[dev] = pnp_port_start(pdev, 0);
  1881. fm_port[dev] = pnp_port_start(pdev, 1);
  1882. mpu_port[dev] = pnp_port_start(pdev, 2);
  1883. dma1[dev] = pnp_dma(pdev, 0);
  1884. dma2[dev] = pnp_dma(pdev, 1);
  1885. irq[dev] = pnp_irq(pdev, 0);
  1886. snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
  1887. snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
  1888. return 0;
  1889. }
  1890. static int __devinit snd_audiodrive_pnp(int dev, struct snd_audiodrive *acard,
  1891. struct pnp_dev *pdev)
  1892. {
  1893. struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
  1894. if (!cfg)
  1895. return -ENOMEM;
  1896. acard->dev = pdev;
  1897. if (snd_audiodrive_pnp_init_main(dev, acard->dev, cfg) < 0) {
  1898. kfree(cfg);
  1899. return -EBUSY;
  1900. }
  1901. kfree(cfg);
  1902. return 0;
  1903. }
  1904. static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
  1905. /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
  1906. { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
  1907. /* ESS 1868 (integrated on Maxisound Cards) */
  1908. { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
  1909. /* ESS 1868 (integrated on Maxisound Cards) */
  1910. { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
  1911. /* ESS ES1869 Plug and Play AudioDrive */
  1912. { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
  1913. /* ESS 1869 */
  1914. { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
  1915. /* ESS 1878 */
  1916. { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
  1917. /* ESS 1879 */
  1918. { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
  1919. /* --- */
  1920. { .id = "" } /* end */
  1921. };
  1922. MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
  1923. static int __devinit snd_audiodrive_pnpc(int dev, struct snd_audiodrive *acard,
  1924. struct pnp_card_link *card,
  1925. const struct pnp_card_device_id *id)
  1926. {
  1927. struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
  1928. if (!cfg)
  1929. return -ENOMEM;
  1930. acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
  1931. if (acard->dev == NULL) {
  1932. kfree(cfg);
  1933. return -EBUSY;
  1934. }
  1935. acard->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
  1936. if (acard->devc == NULL) {
  1937. kfree(cfg);
  1938. return -EBUSY;
  1939. }
  1940. /* Control port initialization */
  1941. if (pnp_activate_dev(acard->devc) < 0) {
  1942. kfree(cfg);
  1943. snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
  1944. return -EAGAIN;
  1945. }
  1946. snd_printdd("pnp: port=0x%llx\n",
  1947. (unsigned long long)pnp_port_start(acard->devc, 0));
  1948. if (snd_audiodrive_pnp_init_main(dev, acard->dev, cfg) < 0) {
  1949. kfree(cfg);
  1950. return -EBUSY;
  1951. }
  1952. kfree(cfg);
  1953. return 0;
  1954. }
  1955. #endif /* CONFIG_PNP */
  1956. #ifdef CONFIG_PNP
  1957. #define is_isapnp_selected(dev) isapnp[dev]
  1958. #else
  1959. #define is_isapnp_selected(dev) 0
  1960. #endif
  1961. static struct snd_card *snd_es18xx_card_new(int dev)
  1962. {
  1963. return snd_card_new(index[dev], id[dev], THIS_MODULE,
  1964. sizeof(struct snd_audiodrive));
  1965. }
  1966. static int __devinit snd_audiodrive_probe(struct snd_card *card, int dev)
  1967. {
  1968. struct snd_audiodrive *acard = card->private_data;
  1969. struct snd_es18xx *chip;
  1970. struct snd_opl3 *opl3;
  1971. int err;
  1972. if ((err = snd_es18xx_new_device(card,
  1973. port[dev],
  1974. mpu_port[dev],
  1975. fm_port[dev],
  1976. irq[dev], dma1[dev], dma2[dev],
  1977. &chip)) < 0)
  1978. return err;
  1979. acard->chip = chip;
  1980. sprintf(card->driver, "ES%x", chip->version);
  1981. sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
  1982. if (dma1[dev] != dma2[dev])
  1983. sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
  1984. card->shortname,
  1985. chip->port,
  1986. irq[dev], dma1[dev], dma2[dev]);
  1987. else
  1988. sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
  1989. card->shortname,
  1990. chip->port,
  1991. irq[dev], dma1[dev]);
  1992. if ((err = snd_es18xx_pcm(chip, 0, NULL)) < 0)
  1993. return err;
  1994. if ((err = snd_es18xx_mixer(chip)) < 0)
  1995. return err;
  1996. if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
  1997. if (snd_opl3_create(card, chip->fm_port, chip->fm_port + 2, OPL3_HW_OPL3, 0, &opl3) < 0) {
  1998. snd_printk(KERN_WARNING PFX "opl3 not detected at 0x%lx\n", chip->fm_port);
  1999. } else {
  2000. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0)
  2001. return err;
  2002. }
  2003. }
  2004. if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
  2005. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
  2006. chip->mpu_port, 0,
  2007. irq[dev], 0,
  2008. &chip->rmidi)) < 0)
  2009. return err;
  2010. }
  2011. return snd_card_register(card);
  2012. }
  2013. static int __devinit snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
  2014. {
  2015. return enable[dev] && !is_isapnp_selected(dev);
  2016. }
  2017. static int __devinit snd_es18xx_isa_probe1(int dev, struct device *devptr)
  2018. {
  2019. struct snd_card *card;
  2020. int err;
  2021. card = snd_es18xx_card_new(dev);
  2022. if (! card)
  2023. return -ENOMEM;
  2024. snd_card_set_dev(card, devptr);
  2025. if ((err = snd_audiodrive_probe(card, dev)) < 0) {
  2026. snd_card_free(card);
  2027. return err;
  2028. }
  2029. dev_set_drvdata(devptr, card);
  2030. return 0;
  2031. }
  2032. static int __devinit snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
  2033. {
  2034. int err;
  2035. static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
  2036. static int possible_dmas[] = {1, 0, 3, 5, -1};
  2037. if (irq[dev] == SNDRV_AUTO_IRQ) {
  2038. if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  2039. snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
  2040. return -EBUSY;
  2041. }
  2042. }
  2043. if (dma1[dev] == SNDRV_AUTO_DMA) {
  2044. if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  2045. snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
  2046. return -EBUSY;
  2047. }
  2048. }
  2049. if (dma2[dev] == SNDRV_AUTO_DMA) {
  2050. if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  2051. snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
  2052. return -EBUSY;
  2053. }
  2054. }
  2055. if (port[dev] != SNDRV_AUTO_PORT) {
  2056. return snd_es18xx_isa_probe1(dev, pdev);
  2057. } else {
  2058. static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
  2059. int i;
  2060. for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
  2061. port[dev] = possible_ports[i];
  2062. err = snd_es18xx_isa_probe1(dev, pdev);
  2063. if (! err)
  2064. return 0;
  2065. }
  2066. return err;
  2067. }
  2068. }
  2069. static int __devexit snd_es18xx_isa_remove(struct device *devptr,
  2070. unsigned int dev)
  2071. {
  2072. snd_card_free(dev_get_drvdata(devptr));
  2073. dev_set_drvdata(devptr, NULL);
  2074. return 0;
  2075. }
  2076. #ifdef CONFIG_PM
  2077. static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
  2078. pm_message_t state)
  2079. {
  2080. return snd_es18xx_suspend(dev_get_drvdata(dev), state);
  2081. }
  2082. static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
  2083. {
  2084. return snd_es18xx_resume(dev_get_drvdata(dev));
  2085. }
  2086. #endif
  2087. #define DEV_NAME "es18xx"
  2088. static struct isa_driver snd_es18xx_isa_driver = {
  2089. .match = snd_es18xx_isa_match,
  2090. .probe = snd_es18xx_isa_probe,
  2091. .remove = __devexit_p(snd_es18xx_isa_remove),
  2092. #ifdef CONFIG_PM
  2093. .suspend = snd_es18xx_isa_suspend,
  2094. .resume = snd_es18xx_isa_resume,
  2095. #endif
  2096. .driver = {
  2097. .name = DEV_NAME
  2098. },
  2099. };
  2100. #ifdef CONFIG_PNP
  2101. static int __devinit snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
  2102. const struct pnp_device_id *id)
  2103. {
  2104. static int dev;
  2105. int err;
  2106. struct snd_card *card;
  2107. if (pnp_device_is_isapnp(pdev))
  2108. return -ENOENT; /* we have another procedure - card */
  2109. for (; dev < SNDRV_CARDS; dev++) {
  2110. if (enable[dev] && isapnp[dev])
  2111. break;
  2112. }
  2113. if (dev >= SNDRV_CARDS)
  2114. return -ENODEV;
  2115. card = snd_es18xx_card_new(dev);
  2116. if (! card)
  2117. return -ENOMEM;
  2118. if ((err = snd_audiodrive_pnp(dev, card->private_data, pdev)) < 0) {
  2119. snd_card_free(card);
  2120. return err;
  2121. }
  2122. snd_card_set_dev(card, &pdev->dev);
  2123. if ((err = snd_audiodrive_probe(card, dev)) < 0) {
  2124. snd_card_free(card);
  2125. return err;
  2126. }
  2127. pnp_set_drvdata(pdev, card);
  2128. dev++;
  2129. return 0;
  2130. }
  2131. static void __devexit snd_audiodrive_pnp_remove(struct pnp_dev * pdev)
  2132. {
  2133. snd_card_free(pnp_get_drvdata(pdev));
  2134. pnp_set_drvdata(pdev, NULL);
  2135. }
  2136. #ifdef CONFIG_PM
  2137. static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
  2138. {
  2139. return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
  2140. }
  2141. static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
  2142. {
  2143. return snd_es18xx_resume(pnp_get_drvdata(pdev));
  2144. }
  2145. #endif
  2146. static struct pnp_driver es18xx_pnp_driver = {
  2147. .name = "es18xx-pnpbios",
  2148. .id_table = snd_audiodrive_pnpbiosids,
  2149. .probe = snd_audiodrive_pnp_detect,
  2150. .remove = __devexit_p(snd_audiodrive_pnp_remove),
  2151. #ifdef CONFIG_PM
  2152. .suspend = snd_audiodrive_pnp_suspend,
  2153. .resume = snd_audiodrive_pnp_resume,
  2154. #endif
  2155. };
  2156. static int __devinit snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
  2157. const struct pnp_card_device_id *pid)
  2158. {
  2159. static int dev;
  2160. struct snd_card *card;
  2161. int res;
  2162. for ( ; dev < SNDRV_CARDS; dev++) {
  2163. if (enable[dev] && isapnp[dev])
  2164. break;
  2165. }
  2166. if (dev >= SNDRV_CARDS)
  2167. return -ENODEV;
  2168. card = snd_es18xx_card_new(dev);
  2169. if (! card)
  2170. return -ENOMEM;
  2171. if ((res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid)) < 0) {
  2172. snd_card_free(card);
  2173. return res;
  2174. }
  2175. snd_card_set_dev(card, &pcard->card->dev);
  2176. if ((res = snd_audiodrive_probe(card, dev)) < 0) {
  2177. snd_card_free(card);
  2178. return res;
  2179. }
  2180. pnp_set_card_drvdata(pcard, card);
  2181. dev++;
  2182. return 0;
  2183. }
  2184. static void __devexit snd_audiodrive_pnpc_remove(struct pnp_card_link * pcard)
  2185. {
  2186. snd_card_free(pnp_get_card_drvdata(pcard));
  2187. pnp_set_card_drvdata(pcard, NULL);
  2188. }
  2189. #ifdef CONFIG_PM
  2190. static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
  2191. {
  2192. return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
  2193. }
  2194. static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
  2195. {
  2196. return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
  2197. }
  2198. #endif
  2199. static struct pnp_card_driver es18xx_pnpc_driver = {
  2200. .flags = PNP_DRIVER_RES_DISABLE,
  2201. .name = "es18xx",
  2202. .id_table = snd_audiodrive_pnpids,
  2203. .probe = snd_audiodrive_pnpc_detect,
  2204. .remove = __devexit_p(snd_audiodrive_pnpc_remove),
  2205. #ifdef CONFIG_PM
  2206. .suspend = snd_audiodrive_pnpc_suspend,
  2207. .resume = snd_audiodrive_pnpc_resume,
  2208. #endif
  2209. };
  2210. #endif /* CONFIG_PNP */
  2211. static int __init alsa_card_es18xx_init(void)
  2212. {
  2213. int err;
  2214. err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
  2215. if (err < 0)
  2216. return err;
  2217. #ifdef CONFIG_PNP
  2218. err = pnp_register_driver(&es18xx_pnp_driver);
  2219. if (!err)
  2220. pnp_registered = 1;
  2221. err = pnp_register_card_driver(&es18xx_pnpc_driver);
  2222. if (!err)
  2223. pnpc_registered = 1;
  2224. #endif
  2225. return 0;
  2226. }
  2227. static void __exit alsa_card_es18xx_exit(void)
  2228. {
  2229. #ifdef CONFIG_PNP
  2230. if (pnpc_registered)
  2231. pnp_unregister_card_driver(&es18xx_pnpc_driver);
  2232. if (pnp_registered)
  2233. pnp_unregister_driver(&es18xx_pnp_driver);
  2234. #endif
  2235. isa_unregister_driver(&snd_es18xx_isa_driver);
  2236. }
  2237. module_init(alsa_card_es18xx_init)
  2238. module_exit(alsa_card_es18xx_exit)