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