es18xx.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435
  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 ctrl_port; /* Control port of ESS chip */
  99. struct resource *res_port;
  100. struct resource *res_mpu_port;
  101. struct resource *res_ctrl_port;
  102. int irq; /* IRQ number of ESS chip */
  103. int dma1; /* DMA1 */
  104. int dma2; /* DMA2 */
  105. unsigned short version; /* version of ESS chip */
  106. int caps; /* Chip capabilities */
  107. unsigned short audio2_vol; /* volume level of audio2 */
  108. unsigned short active; /* active channel mask */
  109. unsigned int dma1_shift;
  110. unsigned int dma2_shift;
  111. struct snd_pcm *pcm;
  112. struct snd_pcm_substream *playback_a_substream;
  113. struct snd_pcm_substream *capture_a_substream;
  114. struct snd_pcm_substream *playback_b_substream;
  115. struct snd_rawmidi *rmidi;
  116. struct snd_kcontrol *hw_volume;
  117. struct snd_kcontrol *hw_switch;
  118. struct snd_kcontrol *master_volume;
  119. struct snd_kcontrol *master_switch;
  120. spinlock_t reg_lock;
  121. spinlock_t mixer_lock;
  122. #ifdef CONFIG_PM
  123. unsigned char pm_reg;
  124. #endif
  125. #ifdef CONFIG_PNP
  126. struct pnp_dev *dev;
  127. struct pnp_dev *devc;
  128. #endif
  129. };
  130. #define AUDIO1_IRQ 0x01
  131. #define AUDIO2_IRQ 0x02
  132. #define HWV_IRQ 0x04
  133. #define MPU_IRQ 0x08
  134. #define ES18XX_PCM2 0x0001 /* Has two useable PCM */
  135. #define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
  136. #define ES18XX_RECMIX 0x0004 /* Has record mixer */
  137. #define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
  138. #define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
  139. #define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
  140. #define ES18XX_AUXB 0x0040 /* AuxB mixer control */
  141. #define ES18XX_HWV 0x0080 /* Has separate hardware volume mixer controls*/
  142. #define ES18XX_MONO 0x0100 /* Mono_in mixer control */
  143. #define ES18XX_I2S 0x0200 /* I2S mixer control */
  144. #define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
  145. #define ES18XX_CONTROL 0x0800 /* Has control ports */
  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 __devinit 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 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 struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
  353. .nrats = 2,
  354. .rats = new_clocks,
  355. };
  356. static 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 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. udelay(100000);
  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. udelay(25000);
  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. udelay(100000);
  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. udelay(100000);
  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. udelay(25000);
  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 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 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 char *texts5Source[5] = {
  867. "Mic", "CD", "Line", "Master", "Mix"
  868. };
  869. static char *texts8Source[8] = {
  870. "Mic", "Mic Master", "CD", "AOUT",
  871. "Mic1", "Mix", "Line", "Master"
  872. };
  873. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  874. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  875. uinfo->count = 1;
  876. switch (chip->version) {
  877. case 0x1868:
  878. case 0x1878:
  879. uinfo->value.enumerated.items = 4;
  880. if (uinfo->value.enumerated.item > 3)
  881. uinfo->value.enumerated.item = 3;
  882. strcpy(uinfo->value.enumerated.name,
  883. texts5Source[uinfo->value.enumerated.item]);
  884. break;
  885. case 0x1887:
  886. case 0x1888:
  887. uinfo->value.enumerated.items = 5;
  888. if (uinfo->value.enumerated.item > 4)
  889. uinfo->value.enumerated.item = 4;
  890. strcpy(uinfo->value.enumerated.name, texts5Source[uinfo->value.enumerated.item]);
  891. break;
  892. case 0x1869: /* DS somewhat contradictory for 1869: could be be 5 or 8 */
  893. case 0x1879:
  894. uinfo->value.enumerated.items = 8;
  895. if (uinfo->value.enumerated.item > 7)
  896. uinfo->value.enumerated.item = 7;
  897. strcpy(uinfo->value.enumerated.name, texts8Source[uinfo->value.enumerated.item]);
  898. break;
  899. default:
  900. return -EINVAL;
  901. }
  902. return 0;
  903. }
  904. static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  905. {
  906. static unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
  907. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  908. int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
  909. if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
  910. muxSource = invMap4Source[muxSource];
  911. if (muxSource==3 &&
  912. (chip->version == 0x1887 || chip->version == 0x1888) &&
  913. (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
  914. )
  915. muxSource = 4;
  916. }
  917. ucontrol->value.enumerated.item[0] = muxSource;
  918. return 0;
  919. }
  920. static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  921. {
  922. static unsigned char map4Source[4] = {0, 2, 6, 7};
  923. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  924. unsigned char val = ucontrol->value.enumerated.item[0];
  925. unsigned char retVal = 0;
  926. switch (chip->version) {
  927. /* 5 source chips */
  928. case 0x1887:
  929. case 0x1888:
  930. if (val > 4)
  931. return -EINVAL;
  932. if (val == 4) {
  933. retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
  934. val = 3;
  935. } else
  936. retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
  937. /* 4 source chips */
  938. case 0x1868:
  939. case 0x1878:
  940. if (val > 3)
  941. return -EINVAL;
  942. val = map4Source[val];
  943. break;
  944. /* 8 source chips */
  945. case 0x1869:
  946. case 0x1879:
  947. if (val > 7)
  948. return -EINVAL;
  949. break;
  950. default:
  951. return -EINVAL;
  952. }
  953. return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
  954. }
  955. #define snd_es18xx_info_spatializer_enable snd_ctl_boolean_mono_info
  956. static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  957. {
  958. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  959. unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
  960. ucontrol->value.integer.value[0] = !!(val & 8);
  961. return 0;
  962. }
  963. static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  964. {
  965. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  966. unsigned char oval, nval;
  967. int change;
  968. nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
  969. oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
  970. change = nval != oval;
  971. if (change) {
  972. snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
  973. snd_es18xx_mixer_write(chip, 0x50, nval);
  974. }
  975. return change;
  976. }
  977. static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  978. {
  979. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  980. uinfo->count = 2;
  981. uinfo->value.integer.min = 0;
  982. uinfo->value.integer.max = 63;
  983. return 0;
  984. }
  985. static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  986. {
  987. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  988. ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
  989. ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
  990. return 0;
  991. }
  992. #define snd_es18xx_info_hw_switch snd_ctl_boolean_stereo_info
  993. static int snd_es18xx_get_hw_switch(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) & 0x40);
  997. ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
  998. return 0;
  999. }
  1000. static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
  1001. {
  1002. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  1003. chip->master_volume = NULL;
  1004. chip->master_switch = NULL;
  1005. chip->hw_volume = NULL;
  1006. chip->hw_switch = NULL;
  1007. }
  1008. static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
  1009. unsigned char mask, unsigned char val)
  1010. {
  1011. if (reg < 0xa0)
  1012. return snd_es18xx_mixer_bits(chip, reg, mask, val);
  1013. else
  1014. return snd_es18xx_bits(chip, reg, mask, val);
  1015. }
  1016. static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
  1017. {
  1018. if (reg < 0xa0)
  1019. return snd_es18xx_mixer_read(chip, reg);
  1020. else
  1021. return snd_es18xx_read(chip, reg);
  1022. }
  1023. #define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
  1024. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1025. .info = snd_es18xx_info_single, \
  1026. .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
  1027. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  1028. static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1029. {
  1030. int mask = (kcontrol->private_value >> 16) & 0xff;
  1031. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1032. uinfo->count = 1;
  1033. uinfo->value.integer.min = 0;
  1034. uinfo->value.integer.max = mask;
  1035. return 0;
  1036. }
  1037. static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1038. {
  1039. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  1040. int reg = kcontrol->private_value & 0xff;
  1041. int shift = (kcontrol->private_value >> 8) & 0xff;
  1042. int mask = (kcontrol->private_value >> 16) & 0xff;
  1043. int invert = (kcontrol->private_value >> 24) & 0xff;
  1044. int val;
  1045. val = snd_es18xx_reg_read(chip, reg);
  1046. ucontrol->value.integer.value[0] = (val >> shift) & mask;
  1047. if (invert)
  1048. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1049. return 0;
  1050. }
  1051. static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1052. {
  1053. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  1054. int reg = kcontrol->private_value & 0xff;
  1055. int shift = (kcontrol->private_value >> 8) & 0xff;
  1056. int mask = (kcontrol->private_value >> 16) & 0xff;
  1057. int invert = (kcontrol->private_value >> 24) & 0xff;
  1058. unsigned char val;
  1059. val = (ucontrol->value.integer.value[0] & mask);
  1060. if (invert)
  1061. val = mask - val;
  1062. mask <<= shift;
  1063. val <<= shift;
  1064. return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
  1065. }
  1066. #define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  1067. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1068. .info = snd_es18xx_info_double, \
  1069. .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
  1070. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  1071. static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1072. {
  1073. int mask = (kcontrol->private_value >> 24) & 0xff;
  1074. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1075. uinfo->count = 2;
  1076. uinfo->value.integer.min = 0;
  1077. uinfo->value.integer.max = mask;
  1078. return 0;
  1079. }
  1080. static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1081. {
  1082. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  1083. int left_reg = kcontrol->private_value & 0xff;
  1084. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1085. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1086. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1087. int mask = (kcontrol->private_value >> 24) & 0xff;
  1088. int invert = (kcontrol->private_value >> 22) & 1;
  1089. unsigned char left, right;
  1090. left = snd_es18xx_reg_read(chip, left_reg);
  1091. if (left_reg != right_reg)
  1092. right = snd_es18xx_reg_read(chip, right_reg);
  1093. else
  1094. right = left;
  1095. ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
  1096. ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
  1097. if (invert) {
  1098. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1099. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  1100. }
  1101. return 0;
  1102. }
  1103. static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1104. {
  1105. struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
  1106. int left_reg = kcontrol->private_value & 0xff;
  1107. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1108. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1109. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1110. int mask = (kcontrol->private_value >> 24) & 0xff;
  1111. int invert = (kcontrol->private_value >> 22) & 1;
  1112. int change;
  1113. unsigned char val1, val2, mask1, mask2;
  1114. val1 = ucontrol->value.integer.value[0] & mask;
  1115. val2 = ucontrol->value.integer.value[1] & mask;
  1116. if (invert) {
  1117. val1 = mask - val1;
  1118. val2 = mask - val2;
  1119. }
  1120. val1 <<= shift_left;
  1121. val2 <<= shift_right;
  1122. mask1 = mask << shift_left;
  1123. mask2 = mask << shift_right;
  1124. if (left_reg != right_reg) {
  1125. change = 0;
  1126. if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
  1127. change = 1;
  1128. if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
  1129. change = 1;
  1130. } else {
  1131. change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
  1132. val1 | val2) != (val1 | val2));
  1133. }
  1134. return change;
  1135. }
  1136. /* Mixer controls
  1137. * These arrays contain setup data for mixer controls.
  1138. *
  1139. * The controls that are universal to all chipsets are fully initialized
  1140. * here.
  1141. */
  1142. static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
  1143. ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
  1144. ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
  1145. ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
  1146. ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
  1147. ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
  1148. ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
  1149. ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
  1150. ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
  1151. ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
  1152. {
  1153. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1154. .name = "Capture Source",
  1155. .info = snd_es18xx_info_mux,
  1156. .get = snd_es18xx_get_mux,
  1157. .put = snd_es18xx_put_mux,
  1158. }
  1159. };
  1160. static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
  1161. ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
  1162. ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
  1163. ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
  1164. ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
  1165. ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
  1166. ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
  1167. };
  1168. /*
  1169. * The chipset specific mixer controls
  1170. */
  1171. static struct snd_kcontrol_new snd_es18xx_opt_speaker =
  1172. ES18XX_SINGLE("Beep Playback Volume", 0, 0x3c, 0, 7, 0);
  1173. static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
  1174. ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
  1175. ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
  1176. ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
  1177. ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
  1178. };
  1179. static struct snd_kcontrol_new snd_es18xx_opt_1878 =
  1180. ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
  1181. static struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
  1182. ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
  1183. ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
  1184. ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
  1185. };
  1186. static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
  1187. ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
  1188. };
  1189. static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
  1190. ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
  1191. ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
  1192. };
  1193. static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
  1194. ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
  1195. {
  1196. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1197. .name = "3D Control - Switch",
  1198. .info = snd_es18xx_info_spatializer_enable,
  1199. .get = snd_es18xx_get_spatializer_enable,
  1200. .put = snd_es18xx_put_spatializer_enable,
  1201. }
  1202. };
  1203. static struct snd_kcontrol_new snd_es18xx_micpre1_control =
  1204. ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
  1205. static struct snd_kcontrol_new snd_es18xx_micpre2_control =
  1206. ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
  1207. static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
  1208. {
  1209. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1210. .name = "Hardware Master Playback Volume",
  1211. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1212. .info = snd_es18xx_info_hw_volume,
  1213. .get = snd_es18xx_get_hw_volume,
  1214. },
  1215. {
  1216. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1217. .name = "Hardware Master Playback Switch",
  1218. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1219. .info = snd_es18xx_info_hw_switch,
  1220. .get = snd_es18xx_get_hw_switch,
  1221. },
  1222. ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
  1223. };
  1224. static int __devinit snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
  1225. {
  1226. int data;
  1227. outb(reg, chip->ctrl_port);
  1228. data = inb(chip->ctrl_port + 1);
  1229. return data;
  1230. }
  1231. static void __devinit snd_es18xx_config_write(struct snd_es18xx *chip,
  1232. unsigned char reg, unsigned char data)
  1233. {
  1234. /* No need for spinlocks, this function is used only in
  1235. otherwise protected init code */
  1236. outb(reg, chip->ctrl_port);
  1237. outb(data, chip->ctrl_port + 1);
  1238. #ifdef REG_DEBUG
  1239. snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
  1240. #endif
  1241. }
  1242. static int __devinit snd_es18xx_initialize(struct snd_es18xx *chip,
  1243. unsigned long mpu_port,
  1244. unsigned long fm_port)
  1245. {
  1246. int mask = 0;
  1247. /* enable extended mode */
  1248. snd_es18xx_dsp_command(chip, 0xC6);
  1249. /* Reset mixer registers */
  1250. snd_es18xx_mixer_write(chip, 0x00, 0x00);
  1251. /* Audio 1 DMA demand mode (4 bytes/request) */
  1252. snd_es18xx_write(chip, 0xB9, 2);
  1253. if (chip->caps & ES18XX_CONTROL) {
  1254. /* Hardware volume IRQ */
  1255. snd_es18xx_config_write(chip, 0x27, chip->irq);
  1256. if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
  1257. /* FM I/O */
  1258. snd_es18xx_config_write(chip, 0x62, fm_port >> 8);
  1259. snd_es18xx_config_write(chip, 0x63, fm_port & 0xff);
  1260. }
  1261. if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
  1262. /* MPU-401 I/O */
  1263. snd_es18xx_config_write(chip, 0x64, mpu_port >> 8);
  1264. snd_es18xx_config_write(chip, 0x65, mpu_port & 0xff);
  1265. /* MPU-401 IRQ */
  1266. snd_es18xx_config_write(chip, 0x28, chip->irq);
  1267. }
  1268. /* Audio1 IRQ */
  1269. snd_es18xx_config_write(chip, 0x70, chip->irq);
  1270. /* Audio2 IRQ */
  1271. snd_es18xx_config_write(chip, 0x72, chip->irq);
  1272. /* Audio1 DMA */
  1273. snd_es18xx_config_write(chip, 0x74, chip->dma1);
  1274. /* Audio2 DMA */
  1275. snd_es18xx_config_write(chip, 0x75, chip->dma2);
  1276. /* Enable Audio 1 IRQ */
  1277. snd_es18xx_write(chip, 0xB1, 0x50);
  1278. /* Enable Audio 2 IRQ */
  1279. snd_es18xx_mixer_write(chip, 0x7A, 0x40);
  1280. /* Enable Audio 1 DMA */
  1281. snd_es18xx_write(chip, 0xB2, 0x50);
  1282. /* Enable MPU and hardware volume interrupt */
  1283. snd_es18xx_mixer_write(chip, 0x64, 0x42);
  1284. /* Enable ESS wavetable input */
  1285. snd_es18xx_mixer_bits(chip, 0x48, 0x10, 0x10);
  1286. }
  1287. else {
  1288. int irqmask, dma1mask, dma2mask;
  1289. switch (chip->irq) {
  1290. case 2:
  1291. case 9:
  1292. irqmask = 0;
  1293. break;
  1294. case 5:
  1295. irqmask = 1;
  1296. break;
  1297. case 7:
  1298. irqmask = 2;
  1299. break;
  1300. case 10:
  1301. irqmask = 3;
  1302. break;
  1303. default:
  1304. snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
  1305. return -ENODEV;
  1306. }
  1307. switch (chip->dma1) {
  1308. case 0:
  1309. dma1mask = 1;
  1310. break;
  1311. case 1:
  1312. dma1mask = 2;
  1313. break;
  1314. case 3:
  1315. dma1mask = 3;
  1316. break;
  1317. default:
  1318. snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
  1319. return -ENODEV;
  1320. }
  1321. switch (chip->dma2) {
  1322. case 0:
  1323. dma2mask = 0;
  1324. break;
  1325. case 1:
  1326. dma2mask = 1;
  1327. break;
  1328. case 3:
  1329. dma2mask = 2;
  1330. break;
  1331. case 5:
  1332. dma2mask = 3;
  1333. break;
  1334. default:
  1335. snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
  1336. return -ENODEV;
  1337. }
  1338. /* Enable and set Audio 1 IRQ */
  1339. snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
  1340. /* Enable and set Audio 1 DMA */
  1341. snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
  1342. /* Set Audio 2 DMA */
  1343. snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
  1344. /* Enable Audio 2 IRQ and DMA
  1345. Set capture mixer input */
  1346. snd_es18xx_mixer_write(chip, 0x7A, 0x68);
  1347. /* Enable and set hardware volume interrupt */
  1348. snd_es18xx_mixer_write(chip, 0x64, 0x06);
  1349. if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
  1350. /* MPU401 share irq with audio
  1351. Joystick enabled
  1352. FM enabled */
  1353. snd_es18xx_mixer_write(chip, 0x40,
  1354. 0x43 | (mpu_port & 0xf0) >> 1);
  1355. }
  1356. snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
  1357. }
  1358. if (chip->caps & ES18XX_NEW_RATE) {
  1359. /* Change behaviour of register A1
  1360. 4x oversampling
  1361. 2nd channel DAC asynchronous */
  1362. snd_es18xx_mixer_write(chip, 0x71, 0x32);
  1363. }
  1364. if (!(chip->caps & ES18XX_PCM2)) {
  1365. /* Enable DMA FIFO */
  1366. snd_es18xx_write(chip, 0xB7, 0x80);
  1367. }
  1368. if (chip->caps & ES18XX_SPATIALIZER) {
  1369. /* Set spatializer parameters to recommended values */
  1370. snd_es18xx_mixer_write(chip, 0x54, 0x8f);
  1371. snd_es18xx_mixer_write(chip, 0x56, 0x95);
  1372. snd_es18xx_mixer_write(chip, 0x58, 0x94);
  1373. snd_es18xx_mixer_write(chip, 0x5a, 0x80);
  1374. }
  1375. /* Flip the "enable I2S" bits for those chipsets that need it */
  1376. switch (chip->version) {
  1377. case 0x1879:
  1378. //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
  1379. //so a Switch control has been added to toggle this 0x71 bit on/off:
  1380. //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
  1381. /* Note: we fall through on purpose here. */
  1382. case 0x1878:
  1383. snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
  1384. break;
  1385. }
  1386. /* Mute input source */
  1387. if (chip->caps & ES18XX_MUTEREC)
  1388. mask = 0x10;
  1389. if (chip->caps & ES18XX_RECMIX)
  1390. snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
  1391. else {
  1392. snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
  1393. snd_es18xx_write(chip, 0xb4, 0x00);
  1394. }
  1395. #ifndef AVOID_POPS
  1396. /* Enable PCM output */
  1397. snd_es18xx_dsp_command(chip, 0xD1);
  1398. #endif
  1399. return 0;
  1400. }
  1401. static int __devinit snd_es18xx_identify(struct snd_es18xx *chip)
  1402. {
  1403. int hi,lo;
  1404. /* reset */
  1405. if (snd_es18xx_reset(chip) < 0) {
  1406. snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
  1407. return -ENODEV;
  1408. }
  1409. snd_es18xx_dsp_command(chip, 0xe7);
  1410. hi = snd_es18xx_dsp_get_byte(chip);
  1411. if (hi < 0) {
  1412. return hi;
  1413. }
  1414. lo = snd_es18xx_dsp_get_byte(chip);
  1415. if ((lo & 0xf0) != 0x80) {
  1416. return -ENODEV;
  1417. }
  1418. if (hi == 0x48) {
  1419. chip->version = 0x488;
  1420. return 0;
  1421. }
  1422. if (hi != 0x68) {
  1423. return -ENODEV;
  1424. }
  1425. if ((lo & 0x0f) < 8) {
  1426. chip->version = 0x688;
  1427. return 0;
  1428. }
  1429. outb(0x40, chip->port + 0x04);
  1430. udelay(10);
  1431. hi = inb(chip->port + 0x05);
  1432. udelay(10);
  1433. lo = inb(chip->port + 0x05);
  1434. if (hi != lo) {
  1435. chip->version = hi << 8 | lo;
  1436. chip->ctrl_port = inb(chip->port + 0x05) << 8;
  1437. udelay(10);
  1438. chip->ctrl_port += inb(chip->port + 0x05);
  1439. if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
  1440. snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
  1441. return -EBUSY;
  1442. }
  1443. return 0;
  1444. }
  1445. /* If has Hardware volume */
  1446. if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
  1447. /* If has Audio2 */
  1448. if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
  1449. /* If has volume count */
  1450. if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
  1451. chip->version = 0x1887;
  1452. } else {
  1453. chip->version = 0x1888;
  1454. }
  1455. } else {
  1456. chip->version = 0x1788;
  1457. }
  1458. }
  1459. else
  1460. chip->version = 0x1688;
  1461. return 0;
  1462. }
  1463. static int __devinit snd_es18xx_probe(struct snd_es18xx *chip,
  1464. unsigned long mpu_port,
  1465. unsigned long fm_port)
  1466. {
  1467. if (snd_es18xx_identify(chip) < 0) {
  1468. snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
  1469. return -ENODEV;
  1470. }
  1471. switch (chip->version) {
  1472. case 0x1868:
  1473. chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL;
  1474. break;
  1475. case 0x1869:
  1476. chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
  1477. break;
  1478. case 0x1878:
  1479. chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
  1480. break;
  1481. case 0x1879:
  1482. chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
  1483. break;
  1484. case 0x1887:
  1485. case 0x1888:
  1486. chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
  1487. break;
  1488. default:
  1489. snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
  1490. chip->port, chip->version);
  1491. return -ENODEV;
  1492. }
  1493. snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
  1494. if (chip->dma1 == chip->dma2)
  1495. chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
  1496. return snd_es18xx_initialize(chip, mpu_port, fm_port);
  1497. }
  1498. static struct snd_pcm_ops snd_es18xx_playback_ops = {
  1499. .open = snd_es18xx_playback_open,
  1500. .close = snd_es18xx_playback_close,
  1501. .ioctl = snd_pcm_lib_ioctl,
  1502. .hw_params = snd_es18xx_playback_hw_params,
  1503. .hw_free = snd_es18xx_pcm_hw_free,
  1504. .prepare = snd_es18xx_playback_prepare,
  1505. .trigger = snd_es18xx_playback_trigger,
  1506. .pointer = snd_es18xx_playback_pointer,
  1507. };
  1508. static struct snd_pcm_ops snd_es18xx_capture_ops = {
  1509. .open = snd_es18xx_capture_open,
  1510. .close = snd_es18xx_capture_close,
  1511. .ioctl = snd_pcm_lib_ioctl,
  1512. .hw_params = snd_es18xx_capture_hw_params,
  1513. .hw_free = snd_es18xx_pcm_hw_free,
  1514. .prepare = snd_es18xx_capture_prepare,
  1515. .trigger = snd_es18xx_capture_trigger,
  1516. .pointer = snd_es18xx_capture_pointer,
  1517. };
  1518. static int __devinit snd_es18xx_pcm(struct snd_card *card, int device,
  1519. struct snd_pcm **rpcm)
  1520. {
  1521. struct snd_es18xx *chip = card->private_data;
  1522. struct snd_pcm *pcm;
  1523. char str[16];
  1524. int err;
  1525. if (rpcm)
  1526. *rpcm = NULL;
  1527. sprintf(str, "ES%x", chip->version);
  1528. if (chip->caps & ES18XX_PCM2)
  1529. err = snd_pcm_new(card, str, device, 2, 1, &pcm);
  1530. else
  1531. err = snd_pcm_new(card, str, device, 1, 1, &pcm);
  1532. if (err < 0)
  1533. return err;
  1534. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
  1535. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
  1536. /* global setup */
  1537. pcm->private_data = chip;
  1538. pcm->info_flags = 0;
  1539. if (chip->caps & ES18XX_DUPLEX_SAME)
  1540. pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  1541. if (! (chip->caps & ES18XX_PCM2))
  1542. pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
  1543. sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
  1544. chip->pcm = pcm;
  1545. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1546. snd_dma_isa_data(),
  1547. 64*1024,
  1548. chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
  1549. if (rpcm)
  1550. *rpcm = pcm;
  1551. return 0;
  1552. }
  1553. /* Power Management support functions */
  1554. #ifdef CONFIG_PM
  1555. static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
  1556. {
  1557. struct snd_es18xx *chip = card->private_data;
  1558. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1559. snd_pcm_suspend_all(chip->pcm);
  1560. /* power down */
  1561. chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
  1562. chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
  1563. snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
  1564. snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
  1565. return 0;
  1566. }
  1567. static int snd_es18xx_resume(struct snd_card *card)
  1568. {
  1569. struct snd_es18xx *chip = card->private_data;
  1570. /* restore PM register, we won't wake till (not 0x07) i/o activity though */
  1571. snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
  1572. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1573. return 0;
  1574. }
  1575. #endif /* CONFIG_PM */
  1576. static int snd_es18xx_free(struct snd_card *card)
  1577. {
  1578. struct snd_es18xx *chip = card->private_data;
  1579. release_and_free_resource(chip->res_port);
  1580. release_and_free_resource(chip->res_ctrl_port);
  1581. release_and_free_resource(chip->res_mpu_port);
  1582. if (chip->irq >= 0)
  1583. free_irq(chip->irq, (void *) card);
  1584. if (chip->dma1 >= 0) {
  1585. disable_dma(chip->dma1);
  1586. free_dma(chip->dma1);
  1587. }
  1588. if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
  1589. disable_dma(chip->dma2);
  1590. free_dma(chip->dma2);
  1591. }
  1592. return 0;
  1593. }
  1594. static int snd_es18xx_dev_free(struct snd_device *device)
  1595. {
  1596. return snd_es18xx_free(device->card);
  1597. }
  1598. static int __devinit snd_es18xx_new_device(struct snd_card *card,
  1599. unsigned long port,
  1600. unsigned long mpu_port,
  1601. unsigned long fm_port,
  1602. int irq, int dma1, int dma2)
  1603. {
  1604. struct snd_es18xx *chip = card->private_data;
  1605. static struct snd_device_ops ops = {
  1606. .dev_free = snd_es18xx_dev_free,
  1607. };
  1608. int err;
  1609. spin_lock_init(&chip->reg_lock);
  1610. spin_lock_init(&chip->mixer_lock);
  1611. chip->port = port;
  1612. chip->irq = -1;
  1613. chip->dma1 = -1;
  1614. chip->dma2 = -1;
  1615. chip->audio2_vol = 0x00;
  1616. chip->active = 0;
  1617. chip->res_port = request_region(port, 16, "ES18xx");
  1618. if (chip->res_port == NULL) {
  1619. snd_es18xx_free(card);
  1620. snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
  1621. return -EBUSY;
  1622. }
  1623. if (request_irq(irq, snd_es18xx_interrupt, IRQF_DISABLED, "ES18xx",
  1624. (void *) card)) {
  1625. snd_es18xx_free(card);
  1626. snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
  1627. return -EBUSY;
  1628. }
  1629. chip->irq = irq;
  1630. if (request_dma(dma1, "ES18xx DMA 1")) {
  1631. snd_es18xx_free(card);
  1632. snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
  1633. return -EBUSY;
  1634. }
  1635. chip->dma1 = dma1;
  1636. if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
  1637. snd_es18xx_free(card);
  1638. snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
  1639. return -EBUSY;
  1640. }
  1641. chip->dma2 = dma2;
  1642. if (snd_es18xx_probe(chip, mpu_port, fm_port) < 0) {
  1643. snd_es18xx_free(card);
  1644. return -ENODEV;
  1645. }
  1646. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  1647. if (err < 0) {
  1648. snd_es18xx_free(card);
  1649. return err;
  1650. }
  1651. return 0;
  1652. }
  1653. static int __devinit snd_es18xx_mixer(struct snd_card *card)
  1654. {
  1655. struct snd_es18xx *chip = card->private_data;
  1656. int err;
  1657. unsigned int idx;
  1658. strcpy(card->mixername, chip->pcm->name);
  1659. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
  1660. struct snd_kcontrol *kctl;
  1661. kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
  1662. if (chip->caps & ES18XX_HWV) {
  1663. switch (idx) {
  1664. case 0:
  1665. chip->master_volume = kctl;
  1666. kctl->private_free = snd_es18xx_hwv_free;
  1667. break;
  1668. case 1:
  1669. chip->master_switch = kctl;
  1670. kctl->private_free = snd_es18xx_hwv_free;
  1671. break;
  1672. }
  1673. }
  1674. if ((err = snd_ctl_add(card, kctl)) < 0)
  1675. return err;
  1676. }
  1677. if (chip->caps & ES18XX_PCM2) {
  1678. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
  1679. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
  1680. return err;
  1681. }
  1682. } else {
  1683. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
  1684. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
  1685. return err;
  1686. }
  1687. }
  1688. if (chip->caps & ES18XX_RECMIX) {
  1689. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
  1690. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
  1691. return err;
  1692. }
  1693. }
  1694. switch (chip->version) {
  1695. default:
  1696. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
  1697. return err;
  1698. break;
  1699. case 0x1869:
  1700. case 0x1879:
  1701. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
  1702. return err;
  1703. break;
  1704. }
  1705. if (chip->caps & ES18XX_SPATIALIZER) {
  1706. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
  1707. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
  1708. return err;
  1709. }
  1710. }
  1711. if (chip->caps & ES18XX_HWV) {
  1712. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
  1713. struct snd_kcontrol *kctl;
  1714. kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
  1715. if (idx == 0)
  1716. chip->hw_volume = kctl;
  1717. else
  1718. chip->hw_switch = kctl;
  1719. kctl->private_free = snd_es18xx_hwv_free;
  1720. if ((err = snd_ctl_add(card, kctl)) < 0)
  1721. return err;
  1722. }
  1723. }
  1724. /* finish initializing other chipset specific controls
  1725. */
  1726. if (chip->version != 0x1868) {
  1727. err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
  1728. chip));
  1729. if (err < 0)
  1730. return err;
  1731. }
  1732. if (chip->version == 0x1869) {
  1733. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
  1734. err = snd_ctl_add(card,
  1735. snd_ctl_new1(&snd_es18xx_opt_1869[idx],
  1736. chip));
  1737. if (err < 0)
  1738. return err;
  1739. }
  1740. } else if (chip->version == 0x1878) {
  1741. err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
  1742. chip));
  1743. if (err < 0)
  1744. return err;
  1745. } else if (chip->version == 0x1879) {
  1746. for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
  1747. err = snd_ctl_add(card,
  1748. snd_ctl_new1(&snd_es18xx_opt_1879[idx],
  1749. chip));
  1750. if (err < 0)
  1751. return err;
  1752. }
  1753. }
  1754. return 0;
  1755. }
  1756. /* Card level */
  1757. MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
  1758. MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
  1759. MODULE_LICENSE("GPL");
  1760. MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
  1761. "{ESS,ES1869 PnP AudioDrive},"
  1762. "{ESS,ES1878 PnP AudioDrive},"
  1763. "{ESS,ES1879 PnP AudioDrive},"
  1764. "{ESS,ES1887 PnP AudioDrive},"
  1765. "{ESS,ES1888 PnP AudioDrive},"
  1766. "{ESS,ES1887 AudioDrive},"
  1767. "{ESS,ES1888 AudioDrive}}");
  1768. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  1769. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  1770. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
  1771. #ifdef CONFIG_PNP
  1772. static int isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
  1773. #endif
  1774. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
  1775. #ifndef CONFIG_PNP
  1776. static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
  1777. #else
  1778. static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  1779. #endif
  1780. static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  1781. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
  1782. static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
  1783. static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
  1784. module_param_array(index, int, NULL, 0444);
  1785. MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
  1786. module_param_array(id, charp, NULL, 0444);
  1787. MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
  1788. module_param_array(enable, bool, NULL, 0444);
  1789. MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
  1790. #ifdef CONFIG_PNP
  1791. module_param_array(isapnp, bool, NULL, 0444);
  1792. MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
  1793. #endif
  1794. module_param_array(port, long, NULL, 0444);
  1795. MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
  1796. module_param_array(mpu_port, long, NULL, 0444);
  1797. MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
  1798. module_param_array(fm_port, long, NULL, 0444);
  1799. MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
  1800. module_param_array(irq, int, NULL, 0444);
  1801. MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
  1802. module_param_array(dma1, int, NULL, 0444);
  1803. MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
  1804. module_param_array(dma2, int, NULL, 0444);
  1805. MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
  1806. #ifdef CONFIG_PNP
  1807. static int isa_registered;
  1808. static int pnp_registered;
  1809. static int pnpc_registered;
  1810. static struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
  1811. { .id = "ESS1869" },
  1812. { .id = "ESS1879" },
  1813. { .id = "" } /* end */
  1814. };
  1815. MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
  1816. /* PnP main device initialization */
  1817. static int __devinit snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
  1818. {
  1819. if (pnp_activate_dev(pdev) < 0) {
  1820. snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
  1821. return -EBUSY;
  1822. }
  1823. /* ok. hack using Vendor-Defined Card-Level registers */
  1824. /* skip csn and logdev initialization - already done in isapnp_configure */
  1825. if (pnp_device_is_isapnp(pdev)) {
  1826. isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
  1827. isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
  1828. if (mpu_port[dev] != SNDRV_AUTO_PORT)
  1829. isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
  1830. isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
  1831. isapnp_cfg_end();
  1832. }
  1833. port[dev] = pnp_port_start(pdev, 0);
  1834. fm_port[dev] = pnp_port_start(pdev, 1);
  1835. mpu_port[dev] = pnp_port_start(pdev, 2);
  1836. dma1[dev] = pnp_dma(pdev, 0);
  1837. dma2[dev] = pnp_dma(pdev, 1);
  1838. irq[dev] = pnp_irq(pdev, 0);
  1839. snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
  1840. snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
  1841. return 0;
  1842. }
  1843. static int __devinit snd_audiodrive_pnp(int dev, struct snd_es18xx *chip,
  1844. struct pnp_dev *pdev)
  1845. {
  1846. chip->dev = pdev;
  1847. if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
  1848. return -EBUSY;
  1849. return 0;
  1850. }
  1851. static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
  1852. /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
  1853. { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
  1854. /* ESS 1868 (integrated on Maxisound Cards) */
  1855. { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
  1856. /* ESS 1868 (integrated on Maxisound Cards) */
  1857. { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
  1858. /* ESS ES1869 Plug and Play AudioDrive */
  1859. { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
  1860. /* ESS 1869 */
  1861. { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
  1862. /* ESS 1878 */
  1863. { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
  1864. /* ESS 1879 */
  1865. { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
  1866. /* --- */
  1867. { .id = "" } /* end */
  1868. };
  1869. MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
  1870. static int __devinit snd_audiodrive_pnpc(int dev, struct snd_es18xx *chip,
  1871. struct pnp_card_link *card,
  1872. const struct pnp_card_device_id *id)
  1873. {
  1874. chip->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
  1875. if (chip->dev == NULL)
  1876. return -EBUSY;
  1877. chip->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
  1878. if (chip->devc == NULL)
  1879. return -EBUSY;
  1880. /* Control port initialization */
  1881. if (pnp_activate_dev(chip->devc) < 0) {
  1882. snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
  1883. return -EAGAIN;
  1884. }
  1885. snd_printdd("pnp: port=0x%llx\n",
  1886. (unsigned long long)pnp_port_start(chip->devc, 0));
  1887. if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
  1888. return -EBUSY;
  1889. return 0;
  1890. }
  1891. #endif /* CONFIG_PNP */
  1892. #ifdef CONFIG_PNP
  1893. #define is_isapnp_selected(dev) isapnp[dev]
  1894. #else
  1895. #define is_isapnp_selected(dev) 0
  1896. #endif
  1897. static int snd_es18xx_card_new(int dev, struct snd_card **cardp)
  1898. {
  1899. return snd_card_create(index[dev], id[dev], THIS_MODULE,
  1900. sizeof(struct snd_es18xx), cardp);
  1901. }
  1902. static int __devinit snd_audiodrive_probe(struct snd_card *card, int dev)
  1903. {
  1904. struct snd_es18xx *chip = card->private_data;
  1905. struct snd_opl3 *opl3;
  1906. int err;
  1907. err = snd_es18xx_new_device(card,
  1908. port[dev], mpu_port[dev], fm_port[dev],
  1909. irq[dev], dma1[dev], dma2[dev]);
  1910. if (err < 0)
  1911. return err;
  1912. sprintf(card->driver, "ES%x", chip->version);
  1913. sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
  1914. if (dma1[dev] != dma2[dev])
  1915. sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
  1916. card->shortname,
  1917. chip->port,
  1918. irq[dev], dma1[dev], dma2[dev]);
  1919. else
  1920. sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
  1921. card->shortname,
  1922. chip->port,
  1923. irq[dev], dma1[dev]);
  1924. err = snd_es18xx_pcm(card, 0, NULL);
  1925. if (err < 0)
  1926. return err;
  1927. err = snd_es18xx_mixer(card);
  1928. if (err < 0)
  1929. return err;
  1930. if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
  1931. if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
  1932. OPL3_HW_OPL3, 0, &opl3) < 0) {
  1933. snd_printk(KERN_WARNING PFX
  1934. "opl3 not detected at 0x%lx\n",
  1935. fm_port[dev]);
  1936. } else {
  1937. err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
  1938. if (err < 0)
  1939. return err;
  1940. }
  1941. }
  1942. if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
  1943. err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
  1944. mpu_port[dev], 0,
  1945. irq[dev], 0, &chip->rmidi);
  1946. if (err < 0)
  1947. return err;
  1948. }
  1949. return snd_card_register(card);
  1950. }
  1951. static int __devinit snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
  1952. {
  1953. return enable[dev] && !is_isapnp_selected(dev);
  1954. }
  1955. static int __devinit snd_es18xx_isa_probe1(int dev, struct device *devptr)
  1956. {
  1957. struct snd_card *card;
  1958. int err;
  1959. err = snd_es18xx_card_new(dev, &card);
  1960. if (err < 0)
  1961. return err;
  1962. snd_card_set_dev(card, devptr);
  1963. if ((err = snd_audiodrive_probe(card, dev)) < 0) {
  1964. snd_card_free(card);
  1965. return err;
  1966. }
  1967. dev_set_drvdata(devptr, card);
  1968. return 0;
  1969. }
  1970. static int __devinit snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
  1971. {
  1972. int err;
  1973. static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
  1974. static int possible_dmas[] = {1, 0, 3, 5, -1};
  1975. if (irq[dev] == SNDRV_AUTO_IRQ) {
  1976. if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  1977. snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
  1978. return -EBUSY;
  1979. }
  1980. }
  1981. if (dma1[dev] == SNDRV_AUTO_DMA) {
  1982. if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  1983. snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
  1984. return -EBUSY;
  1985. }
  1986. }
  1987. if (dma2[dev] == SNDRV_AUTO_DMA) {
  1988. if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  1989. snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
  1990. return -EBUSY;
  1991. }
  1992. }
  1993. if (port[dev] != SNDRV_AUTO_PORT) {
  1994. return snd_es18xx_isa_probe1(dev, pdev);
  1995. } else {
  1996. static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
  1997. int i;
  1998. for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
  1999. port[dev] = possible_ports[i];
  2000. err = snd_es18xx_isa_probe1(dev, pdev);
  2001. if (! err)
  2002. return 0;
  2003. }
  2004. return err;
  2005. }
  2006. }
  2007. static int __devexit snd_es18xx_isa_remove(struct device *devptr,
  2008. unsigned int dev)
  2009. {
  2010. snd_card_free(dev_get_drvdata(devptr));
  2011. dev_set_drvdata(devptr, NULL);
  2012. return 0;
  2013. }
  2014. #ifdef CONFIG_PM
  2015. static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
  2016. pm_message_t state)
  2017. {
  2018. return snd_es18xx_suspend(dev_get_drvdata(dev), state);
  2019. }
  2020. static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
  2021. {
  2022. return snd_es18xx_resume(dev_get_drvdata(dev));
  2023. }
  2024. #endif
  2025. #define DEV_NAME "es18xx"
  2026. static struct isa_driver snd_es18xx_isa_driver = {
  2027. .match = snd_es18xx_isa_match,
  2028. .probe = snd_es18xx_isa_probe,
  2029. .remove = __devexit_p(snd_es18xx_isa_remove),
  2030. #ifdef CONFIG_PM
  2031. .suspend = snd_es18xx_isa_suspend,
  2032. .resume = snd_es18xx_isa_resume,
  2033. #endif
  2034. .driver = {
  2035. .name = DEV_NAME
  2036. },
  2037. };
  2038. #ifdef CONFIG_PNP
  2039. static int __devinit snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
  2040. const struct pnp_device_id *id)
  2041. {
  2042. static int dev;
  2043. int err;
  2044. struct snd_card *card;
  2045. if (pnp_device_is_isapnp(pdev))
  2046. return -ENOENT; /* we have another procedure - card */
  2047. for (; dev < SNDRV_CARDS; dev++) {
  2048. if (enable[dev] && isapnp[dev])
  2049. break;
  2050. }
  2051. if (dev >= SNDRV_CARDS)
  2052. return -ENODEV;
  2053. err = snd_es18xx_card_new(dev, &card);
  2054. if (err < 0)
  2055. return err;
  2056. if ((err = snd_audiodrive_pnp(dev, card->private_data, pdev)) < 0) {
  2057. snd_card_free(card);
  2058. return err;
  2059. }
  2060. snd_card_set_dev(card, &pdev->dev);
  2061. if ((err = snd_audiodrive_probe(card, dev)) < 0) {
  2062. snd_card_free(card);
  2063. return err;
  2064. }
  2065. pnp_set_drvdata(pdev, card);
  2066. dev++;
  2067. return 0;
  2068. }
  2069. static void __devexit snd_audiodrive_pnp_remove(struct pnp_dev * pdev)
  2070. {
  2071. snd_card_free(pnp_get_drvdata(pdev));
  2072. pnp_set_drvdata(pdev, NULL);
  2073. }
  2074. #ifdef CONFIG_PM
  2075. static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
  2076. {
  2077. return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
  2078. }
  2079. static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
  2080. {
  2081. return snd_es18xx_resume(pnp_get_drvdata(pdev));
  2082. }
  2083. #endif
  2084. static struct pnp_driver es18xx_pnp_driver = {
  2085. .name = "es18xx-pnpbios",
  2086. .id_table = snd_audiodrive_pnpbiosids,
  2087. .probe = snd_audiodrive_pnp_detect,
  2088. .remove = __devexit_p(snd_audiodrive_pnp_remove),
  2089. #ifdef CONFIG_PM
  2090. .suspend = snd_audiodrive_pnp_suspend,
  2091. .resume = snd_audiodrive_pnp_resume,
  2092. #endif
  2093. };
  2094. static int __devinit snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
  2095. const struct pnp_card_device_id *pid)
  2096. {
  2097. static int dev;
  2098. struct snd_card *card;
  2099. int res;
  2100. for ( ; dev < SNDRV_CARDS; dev++) {
  2101. if (enable[dev] && isapnp[dev])
  2102. break;
  2103. }
  2104. if (dev >= SNDRV_CARDS)
  2105. return -ENODEV;
  2106. res = snd_es18xx_card_new(dev, &card);
  2107. if (res < 0)
  2108. return res;
  2109. if ((res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid)) < 0) {
  2110. snd_card_free(card);
  2111. return res;
  2112. }
  2113. snd_card_set_dev(card, &pcard->card->dev);
  2114. if ((res = snd_audiodrive_probe(card, dev)) < 0) {
  2115. snd_card_free(card);
  2116. return res;
  2117. }
  2118. pnp_set_card_drvdata(pcard, card);
  2119. dev++;
  2120. return 0;
  2121. }
  2122. static void __devexit snd_audiodrive_pnpc_remove(struct pnp_card_link * pcard)
  2123. {
  2124. snd_card_free(pnp_get_card_drvdata(pcard));
  2125. pnp_set_card_drvdata(pcard, NULL);
  2126. }
  2127. #ifdef CONFIG_PM
  2128. static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
  2129. {
  2130. return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
  2131. }
  2132. static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
  2133. {
  2134. return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
  2135. }
  2136. #endif
  2137. static struct pnp_card_driver es18xx_pnpc_driver = {
  2138. .flags = PNP_DRIVER_RES_DISABLE,
  2139. .name = "es18xx",
  2140. .id_table = snd_audiodrive_pnpids,
  2141. .probe = snd_audiodrive_pnpc_detect,
  2142. .remove = __devexit_p(snd_audiodrive_pnpc_remove),
  2143. #ifdef CONFIG_PM
  2144. .suspend = snd_audiodrive_pnpc_suspend,
  2145. .resume = snd_audiodrive_pnpc_resume,
  2146. #endif
  2147. };
  2148. #endif /* CONFIG_PNP */
  2149. static int __init alsa_card_es18xx_init(void)
  2150. {
  2151. int err;
  2152. err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
  2153. #ifdef CONFIG_PNP
  2154. if (!err)
  2155. isa_registered = 1;
  2156. err = pnp_register_driver(&es18xx_pnp_driver);
  2157. if (!err)
  2158. pnp_registered = 1;
  2159. err = pnp_register_card_driver(&es18xx_pnpc_driver);
  2160. if (!err)
  2161. pnpc_registered = 1;
  2162. if (isa_registered || pnp_registered)
  2163. err = 0;
  2164. #endif
  2165. return err;
  2166. }
  2167. static void __exit alsa_card_es18xx_exit(void)
  2168. {
  2169. #ifdef CONFIG_PNP
  2170. if (pnpc_registered)
  2171. pnp_unregister_card_driver(&es18xx_pnpc_driver);
  2172. if (pnp_registered)
  2173. pnp_unregister_driver(&es18xx_pnp_driver);
  2174. if (isa_registered)
  2175. #endif
  2176. isa_unregister_driver(&snd_es18xx_isa_driver);
  2177. }
  2178. module_init(alsa_card_es18xx_init)
  2179. module_exit(alsa_card_es18xx_exit)