es18xx.c 70 KB

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