es18xx.c 70 KB

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