es18xx.c 72 KB

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