es18xx.c 70 KB

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