es18xx.c 65 KB

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