es18xx.c 69 KB

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