es1938.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  1. /*
  2. * Driver for ESS Solo-1 (ES1938, ES1946, ES1969) soundcard
  3. * Copyright (c) by Jaromir Koutek <miri@punknet.cz>,
  4. * Jaroslav Kysela <perex@suse.cz>,
  5. * Thomas Sailer <sailer@ife.ee.ethz.ch>,
  6. * Abramo Bagnara <abramo@alsa-project.org>,
  7. * Markus Gruber <gruber@eikon.tum.de>
  8. *
  9. * Rewritten from sonicvibes.c source.
  10. *
  11. * TODO:
  12. * Rewrite better spinlocks
  13. *
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. /*
  31. NOTES:
  32. - Capture data is written unaligned starting from dma_base + 1 so I need to
  33. disable mmap and to add a copy callback.
  34. - After several cycle of the following:
  35. while : ; do arecord -d1 -f cd -t raw | aplay -f cd ; done
  36. a "playback write error (DMA or IRQ trouble?)" may happen.
  37. This is due to playback interrupts not generated.
  38. I suspect a timing issue.
  39. - Sometimes the interrupt handler is invoked wrongly during playback.
  40. This generates some harmless "Unexpected hw_pointer: wrong interrupt
  41. acknowledge".
  42. I've seen that using small period sizes.
  43. Reproducible with:
  44. mpg123 test.mp3 &
  45. hdparm -t -T /dev/hda
  46. */
  47. #include <sound/driver.h>
  48. #include <linux/init.h>
  49. #include <linux/interrupt.h>
  50. #include <linux/pci.h>
  51. #include <linux/slab.h>
  52. #include <linux/gameport.h>
  53. #include <linux/moduleparam.h>
  54. #include <linux/delay.h>
  55. #include <sound/core.h>
  56. #include <sound/control.h>
  57. #include <sound/pcm.h>
  58. #include <sound/opl3.h>
  59. #include <sound/mpu401.h>
  60. #include <sound/initval.h>
  61. #include <asm/io.h>
  62. MODULE_AUTHOR("Jaromir Koutek <miri@punknet.cz>");
  63. MODULE_DESCRIPTION("ESS Solo-1");
  64. MODULE_LICENSE("GPL");
  65. MODULE_SUPPORTED_DEVICE("{{ESS,ES1938},"
  66. "{ESS,ES1946},"
  67. "{ESS,ES1969},"
  68. "{TerraTec,128i PCI}}");
  69. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  70. #define SUPPORT_JOYSTICK 1
  71. #endif
  72. #ifndef PCI_VENDOR_ID_ESS
  73. #define PCI_VENDOR_ID_ESS 0x125d
  74. #endif
  75. #ifndef PCI_DEVICE_ID_ESS_ES1938
  76. #define PCI_DEVICE_ID_ESS_ES1938 0x1969
  77. #endif
  78. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  79. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  80. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  81. module_param_array(index, int, NULL, 0444);
  82. MODULE_PARM_DESC(index, "Index value for ESS Solo-1 soundcard.");
  83. module_param_array(id, charp, NULL, 0444);
  84. MODULE_PARM_DESC(id, "ID string for ESS Solo-1 soundcard.");
  85. module_param_array(enable, bool, NULL, 0444);
  86. MODULE_PARM_DESC(enable, "Enable ESS Solo-1 soundcard.");
  87. #define SLIO_REG(chip, x) ((chip)->io_port + ESSIO_REG_##x)
  88. #define SLDM_REG(chip, x) ((chip)->ddma_port + ESSDM_REG_##x)
  89. #define SLSB_REG(chip, x) ((chip)->sb_port + ESSSB_REG_##x)
  90. #define SL_PCI_LEGACYCONTROL 0x40
  91. #define SL_PCI_CONFIG 0x50
  92. #define SL_PCI_DDMACONTROL 0x60
  93. #define ESSIO_REG_AUDIO2DMAADDR 0
  94. #define ESSIO_REG_AUDIO2DMACOUNT 4
  95. #define ESSIO_REG_AUDIO2MODE 6
  96. #define ESSIO_REG_IRQCONTROL 7
  97. #define ESSDM_REG_DMAADDR 0x00
  98. #define ESSDM_REG_DMACOUNT 0x04
  99. #define ESSDM_REG_DMACOMMAND 0x08
  100. #define ESSDM_REG_DMASTATUS 0x08
  101. #define ESSDM_REG_DMAMODE 0x0b
  102. #define ESSDM_REG_DMACLEAR 0x0d
  103. #define ESSDM_REG_DMAMASK 0x0f
  104. #define ESSSB_REG_FMLOWADDR 0x00
  105. #define ESSSB_REG_FMHIGHADDR 0x02
  106. #define ESSSB_REG_MIXERADDR 0x04
  107. #define ESSSB_REG_MIXERDATA 0x05
  108. #define ESSSB_IREG_AUDIO1 0x14
  109. #define ESSSB_IREG_MICMIX 0x1a
  110. #define ESSSB_IREG_RECSRC 0x1c
  111. #define ESSSB_IREG_MASTER 0x32
  112. #define ESSSB_IREG_FM 0x36
  113. #define ESSSB_IREG_AUXACD 0x38
  114. #define ESSSB_IREG_AUXB 0x3a
  115. #define ESSSB_IREG_PCSPEAKER 0x3c
  116. #define ESSSB_IREG_LINE 0x3e
  117. #define ESSSB_IREG_SPATCONTROL 0x50
  118. #define ESSSB_IREG_SPATLEVEL 0x52
  119. #define ESSSB_IREG_MASTER_LEFT 0x60
  120. #define ESSSB_IREG_MASTER_RIGHT 0x62
  121. #define ESSSB_IREG_MPU401CONTROL 0x64
  122. #define ESSSB_IREG_MICMIXRECORD 0x68
  123. #define ESSSB_IREG_AUDIO2RECORD 0x69
  124. #define ESSSB_IREG_AUXACDRECORD 0x6a
  125. #define ESSSB_IREG_FMRECORD 0x6b
  126. #define ESSSB_IREG_AUXBRECORD 0x6c
  127. #define ESSSB_IREG_MONO 0x6d
  128. #define ESSSB_IREG_LINERECORD 0x6e
  129. #define ESSSB_IREG_MONORECORD 0x6f
  130. #define ESSSB_IREG_AUDIO2SAMPLE 0x70
  131. #define ESSSB_IREG_AUDIO2MODE 0x71
  132. #define ESSSB_IREG_AUDIO2FILTER 0x72
  133. #define ESSSB_IREG_AUDIO2TCOUNTL 0x74
  134. #define ESSSB_IREG_AUDIO2TCOUNTH 0x76
  135. #define ESSSB_IREG_AUDIO2CONTROL1 0x78
  136. #define ESSSB_IREG_AUDIO2CONTROL2 0x7a
  137. #define ESSSB_IREG_AUDIO2 0x7c
  138. #define ESSSB_REG_RESET 0x06
  139. #define ESSSB_REG_READDATA 0x0a
  140. #define ESSSB_REG_WRITEDATA 0x0c
  141. #define ESSSB_REG_READSTATUS 0x0c
  142. #define ESSSB_REG_STATUS 0x0e
  143. #define ESS_CMD_EXTSAMPLERATE 0xa1
  144. #define ESS_CMD_FILTERDIV 0xa2
  145. #define ESS_CMD_DMACNTRELOADL 0xa4
  146. #define ESS_CMD_DMACNTRELOADH 0xa5
  147. #define ESS_CMD_ANALOGCONTROL 0xa8
  148. #define ESS_CMD_IRQCONTROL 0xb1
  149. #define ESS_CMD_DRQCONTROL 0xb2
  150. #define ESS_CMD_RECLEVEL 0xb4
  151. #define ESS_CMD_SETFORMAT 0xb6
  152. #define ESS_CMD_SETFORMAT2 0xb7
  153. #define ESS_CMD_DMACONTROL 0xb8
  154. #define ESS_CMD_DMATYPE 0xb9
  155. #define ESS_CMD_OFFSETLEFT 0xba
  156. #define ESS_CMD_OFFSETRIGHT 0xbb
  157. #define ESS_CMD_READREG 0xc0
  158. #define ESS_CMD_ENABLEEXT 0xc6
  159. #define ESS_CMD_PAUSEDMA 0xd0
  160. #define ESS_CMD_ENABLEAUDIO1 0xd1
  161. #define ESS_CMD_STOPAUDIO1 0xd3
  162. #define ESS_CMD_AUDIO1STATUS 0xd8
  163. #define ESS_CMD_CONTDMA 0xd4
  164. #define ESS_CMD_TESTIRQ 0xf2
  165. #define ESS_RECSRC_MIC 0
  166. #define ESS_RECSRC_AUXACD 2
  167. #define ESS_RECSRC_AUXB 5
  168. #define ESS_RECSRC_LINE 6
  169. #define ESS_RECSRC_NONE 7
  170. #define DAC1 0x01
  171. #define ADC1 0x02
  172. #define DAC2 0x04
  173. /*
  174. */
  175. typedef struct _snd_es1938 es1938_t;
  176. #define SAVED_REG_SIZE 32 /* max. number of registers to save */
  177. struct _snd_es1938 {
  178. int irq;
  179. unsigned long io_port;
  180. unsigned long sb_port;
  181. unsigned long vc_port;
  182. unsigned long mpu_port;
  183. unsigned long game_port;
  184. unsigned long ddma_port;
  185. unsigned char irqmask;
  186. unsigned char revision;
  187. snd_kcontrol_t *hw_volume;
  188. snd_kcontrol_t *hw_switch;
  189. snd_kcontrol_t *master_volume;
  190. snd_kcontrol_t *master_switch;
  191. struct pci_dev *pci;
  192. snd_card_t *card;
  193. snd_pcm_t *pcm;
  194. snd_pcm_substream_t *capture_substream;
  195. snd_pcm_substream_t *playback1_substream;
  196. snd_pcm_substream_t *playback2_substream;
  197. snd_kmixer_t *mixer;
  198. snd_rawmidi_t *rmidi;
  199. unsigned int dma1_size;
  200. unsigned int dma2_size;
  201. unsigned int dma1_start;
  202. unsigned int dma2_start;
  203. unsigned int dma1_shift;
  204. unsigned int dma2_shift;
  205. unsigned int active;
  206. spinlock_t reg_lock;
  207. spinlock_t mixer_lock;
  208. snd_info_entry_t *proc_entry;
  209. #ifdef SUPPORT_JOYSTICK
  210. struct gameport *gameport;
  211. #endif
  212. #ifdef CONFIG_PM
  213. unsigned char saved_regs[SAVED_REG_SIZE];
  214. #endif
  215. };
  216. static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  217. static struct pci_device_id snd_es1938_ids[] = {
  218. { 0x125d, 0x1969, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* Solo-1 */
  219. { 0, }
  220. };
  221. MODULE_DEVICE_TABLE(pci, snd_es1938_ids);
  222. #define RESET_LOOP_TIMEOUT 0x10000
  223. #define WRITE_LOOP_TIMEOUT 0x10000
  224. #define GET_LOOP_TIMEOUT 0x01000
  225. #undef REG_DEBUG
  226. /* -----------------------------------------------------------------
  227. * Write to a mixer register
  228. * -----------------------------------------------------------------*/
  229. static void snd_es1938_mixer_write(es1938_t *chip, unsigned char reg, unsigned char val)
  230. {
  231. unsigned long flags;
  232. spin_lock_irqsave(&chip->mixer_lock, flags);
  233. outb(reg, SLSB_REG(chip, MIXERADDR));
  234. outb(val, SLSB_REG(chip, MIXERDATA));
  235. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  236. #ifdef REG_DEBUG
  237. snd_printk("Mixer reg %02x set to %02x\n", reg, val);
  238. #endif
  239. }
  240. /* -----------------------------------------------------------------
  241. * Read from a mixer register
  242. * -----------------------------------------------------------------*/
  243. static int snd_es1938_mixer_read(es1938_t *chip, unsigned char reg)
  244. {
  245. int data;
  246. unsigned long flags;
  247. spin_lock_irqsave(&chip->mixer_lock, flags);
  248. outb(reg, SLSB_REG(chip, MIXERADDR));
  249. data = inb(SLSB_REG(chip, MIXERDATA));
  250. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  251. #ifdef REG_DEBUG
  252. snd_printk("Mixer reg %02x now is %02x\n", reg, data);
  253. #endif
  254. return data;
  255. }
  256. /* -----------------------------------------------------------------
  257. * Write to some bits of a mixer register (return old value)
  258. * -----------------------------------------------------------------*/
  259. static int snd_es1938_mixer_bits(es1938_t *chip, unsigned char reg, unsigned char mask, unsigned char val)
  260. {
  261. unsigned long flags;
  262. unsigned char old, new, oval;
  263. spin_lock_irqsave(&chip->mixer_lock, flags);
  264. outb(reg, SLSB_REG(chip, MIXERADDR));
  265. old = inb(SLSB_REG(chip, MIXERDATA));
  266. oval = old & mask;
  267. if (val != oval) {
  268. new = (old & ~mask) | (val & mask);
  269. outb(new, SLSB_REG(chip, MIXERDATA));
  270. #ifdef REG_DEBUG
  271. snd_printk("Mixer reg %02x was %02x, set to %02x\n", reg, old, new);
  272. #endif
  273. }
  274. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  275. return oval;
  276. }
  277. /* -----------------------------------------------------------------
  278. * Write command to Controller Registers
  279. * -----------------------------------------------------------------*/
  280. static void snd_es1938_write_cmd(es1938_t *chip, unsigned char cmd)
  281. {
  282. int i;
  283. unsigned char v;
  284. for (i = 0; i < WRITE_LOOP_TIMEOUT; i++) {
  285. if (!(v = inb(SLSB_REG(chip, READSTATUS)) & 0x80)) {
  286. outb(cmd, SLSB_REG(chip, WRITEDATA));
  287. return;
  288. }
  289. }
  290. printk("snd_es1938_write_cmd timeout (0x02%x/0x02%x)\n", cmd, v);
  291. }
  292. /* -----------------------------------------------------------------
  293. * Read the Read Data Buffer
  294. * -----------------------------------------------------------------*/
  295. static int snd_es1938_get_byte(es1938_t *chip)
  296. {
  297. int i;
  298. unsigned char v;
  299. for (i = GET_LOOP_TIMEOUT; i; i--)
  300. if ((v = inb(SLSB_REG(chip, STATUS))) & 0x80)
  301. return inb(SLSB_REG(chip, READDATA));
  302. snd_printk("get_byte timeout: status 0x02%x\n", v);
  303. return -ENODEV;
  304. }
  305. /* -----------------------------------------------------------------
  306. * Write value cmd register
  307. * -----------------------------------------------------------------*/
  308. static void snd_es1938_write(es1938_t *chip, unsigned char reg, unsigned char val)
  309. {
  310. unsigned long flags;
  311. spin_lock_irqsave(&chip->reg_lock, flags);
  312. snd_es1938_write_cmd(chip, reg);
  313. snd_es1938_write_cmd(chip, val);
  314. spin_unlock_irqrestore(&chip->reg_lock, flags);
  315. #ifdef REG_DEBUG
  316. snd_printk("Reg %02x set to %02x\n", reg, val);
  317. #endif
  318. }
  319. /* -----------------------------------------------------------------
  320. * Read data from cmd register and return it
  321. * -----------------------------------------------------------------*/
  322. static unsigned char snd_es1938_read(es1938_t *chip, unsigned char reg)
  323. {
  324. unsigned char val;
  325. unsigned long flags;
  326. spin_lock_irqsave(&chip->reg_lock, flags);
  327. snd_es1938_write_cmd(chip, ESS_CMD_READREG);
  328. snd_es1938_write_cmd(chip, reg);
  329. val = snd_es1938_get_byte(chip);
  330. spin_unlock_irqrestore(&chip->reg_lock, flags);
  331. #ifdef REG_DEBUG
  332. snd_printk("Reg %02x now is %02x\n", reg, val);
  333. #endif
  334. return val;
  335. }
  336. /* -----------------------------------------------------------------
  337. * Write data to cmd register and return old value
  338. * -----------------------------------------------------------------*/
  339. static int snd_es1938_bits(es1938_t *chip, unsigned char reg, unsigned char mask, unsigned char val)
  340. {
  341. unsigned long flags;
  342. unsigned char old, new, oval;
  343. spin_lock_irqsave(&chip->reg_lock, flags);
  344. snd_es1938_write_cmd(chip, ESS_CMD_READREG);
  345. snd_es1938_write_cmd(chip, reg);
  346. old = snd_es1938_get_byte(chip);
  347. oval = old & mask;
  348. if (val != oval) {
  349. snd_es1938_write_cmd(chip, reg);
  350. new = (old & ~mask) | (val & mask);
  351. snd_es1938_write_cmd(chip, new);
  352. #ifdef REG_DEBUG
  353. snd_printk("Reg %02x was %02x, set to %02x\n", reg, old, new);
  354. #endif
  355. }
  356. spin_unlock_irqrestore(&chip->reg_lock, flags);
  357. return oval;
  358. }
  359. /* --------------------------------------------------------------------
  360. * Reset the chip
  361. * --------------------------------------------------------------------*/
  362. static void snd_es1938_reset(es1938_t *chip)
  363. {
  364. int i;
  365. outb(3, SLSB_REG(chip, RESET));
  366. inb(SLSB_REG(chip, RESET));
  367. outb(0, SLSB_REG(chip, RESET));
  368. for (i = 0; i < RESET_LOOP_TIMEOUT; i++) {
  369. if (inb(SLSB_REG(chip, STATUS)) & 0x80) {
  370. if (inb(SLSB_REG(chip, READDATA)) == 0xaa)
  371. goto __next;
  372. }
  373. }
  374. snd_printk("ESS Solo-1 reset failed\n");
  375. __next:
  376. snd_es1938_write_cmd(chip, ESS_CMD_ENABLEEXT);
  377. /* Demand transfer DMA: 4 bytes per DMA request */
  378. snd_es1938_write(chip, ESS_CMD_DMATYPE, 2);
  379. /* Change behaviour of register A1
  380. 4x oversampling
  381. 2nd channel DAC asynchronous */
  382. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2MODE, 0x32);
  383. /* enable/select DMA channel and IRQ channel */
  384. snd_es1938_bits(chip, ESS_CMD_IRQCONTROL, 0xf0, 0x50);
  385. snd_es1938_bits(chip, ESS_CMD_DRQCONTROL, 0xf0, 0x50);
  386. snd_es1938_write_cmd(chip, ESS_CMD_ENABLEAUDIO1);
  387. /* Set spatializer parameters to recommended values */
  388. snd_es1938_mixer_write(chip, 0x54, 0x8f);
  389. snd_es1938_mixer_write(chip, 0x56, 0x95);
  390. snd_es1938_mixer_write(chip, 0x58, 0x94);
  391. snd_es1938_mixer_write(chip, 0x5a, 0x80);
  392. }
  393. /* --------------------------------------------------------------------
  394. * Reset the FIFOs
  395. * --------------------------------------------------------------------*/
  396. static void snd_es1938_reset_fifo(es1938_t *chip)
  397. {
  398. outb(2, SLSB_REG(chip, RESET));
  399. outb(0, SLSB_REG(chip, RESET));
  400. }
  401. static ratnum_t clocks[2] = {
  402. {
  403. .num = 793800,
  404. .den_min = 1,
  405. .den_max = 128,
  406. .den_step = 1,
  407. },
  408. {
  409. .num = 768000,
  410. .den_min = 1,
  411. .den_max = 128,
  412. .den_step = 1,
  413. }
  414. };
  415. static snd_pcm_hw_constraint_ratnums_t hw_constraints_clocks = {
  416. .nrats = 2,
  417. .rats = clocks,
  418. };
  419. static void snd_es1938_rate_set(es1938_t *chip,
  420. snd_pcm_substream_t *substream,
  421. int mode)
  422. {
  423. unsigned int bits, div0;
  424. snd_pcm_runtime_t *runtime = substream->runtime;
  425. if (runtime->rate_num == clocks[0].num)
  426. bits = 128 - runtime->rate_den;
  427. else
  428. bits = 256 - runtime->rate_den;
  429. /* set filter register */
  430. div0 = 256 - 7160000*20/(8*82*runtime->rate);
  431. if (mode == DAC2) {
  432. snd_es1938_mixer_write(chip, 0x70, bits);
  433. snd_es1938_mixer_write(chip, 0x72, div0);
  434. } else {
  435. snd_es1938_write(chip, 0xA1, bits);
  436. snd_es1938_write(chip, 0xA2, div0);
  437. }
  438. }
  439. /* --------------------------------------------------------------------
  440. * Configure Solo1 builtin DMA Controller
  441. * --------------------------------------------------------------------*/
  442. static void snd_es1938_playback1_setdma(es1938_t *chip)
  443. {
  444. outb(0x00, SLIO_REG(chip, AUDIO2MODE));
  445. outl(chip->dma2_start, SLIO_REG(chip, AUDIO2DMAADDR));
  446. outw(0, SLIO_REG(chip, AUDIO2DMACOUNT));
  447. outw(chip->dma2_size, SLIO_REG(chip, AUDIO2DMACOUNT));
  448. }
  449. static void snd_es1938_playback2_setdma(es1938_t *chip)
  450. {
  451. /* Enable DMA controller */
  452. outb(0xc4, SLDM_REG(chip, DMACOMMAND));
  453. /* 1. Master reset */
  454. outb(0, SLDM_REG(chip, DMACLEAR));
  455. /* 2. Mask DMA */
  456. outb(1, SLDM_REG(chip, DMAMASK));
  457. outb(0x18, SLDM_REG(chip, DMAMODE));
  458. outl(chip->dma1_start, SLDM_REG(chip, DMAADDR));
  459. outw(chip->dma1_size - 1, SLDM_REG(chip, DMACOUNT));
  460. /* 3. Unmask DMA */
  461. outb(0, SLDM_REG(chip, DMAMASK));
  462. }
  463. static void snd_es1938_capture_setdma(es1938_t *chip)
  464. {
  465. /* Enable DMA controller */
  466. outb(0xc4, SLDM_REG(chip, DMACOMMAND));
  467. /* 1. Master reset */
  468. outb(0, SLDM_REG(chip, DMACLEAR));
  469. /* 2. Mask DMA */
  470. outb(1, SLDM_REG(chip, DMAMASK));
  471. outb(0x14, SLDM_REG(chip, DMAMODE));
  472. outl(chip->dma1_start, SLDM_REG(chip, DMAADDR));
  473. outw(chip->dma1_size - 1, SLDM_REG(chip, DMACOUNT));
  474. /* 3. Unmask DMA */
  475. outb(0, SLDM_REG(chip, DMAMASK));
  476. }
  477. /* ----------------------------------------------------------------------
  478. *
  479. * *** PCM part ***
  480. */
  481. static int snd_es1938_capture_trigger(snd_pcm_substream_t * substream,
  482. int cmd)
  483. {
  484. es1938_t *chip = snd_pcm_substream_chip(substream);
  485. int val;
  486. switch (cmd) {
  487. case SNDRV_PCM_TRIGGER_START:
  488. val = 0x0f;
  489. chip->active |= ADC1;
  490. break;
  491. case SNDRV_PCM_TRIGGER_STOP:
  492. val = 0x00;
  493. chip->active &= ~ADC1;
  494. break;
  495. default:
  496. return -EINVAL;
  497. }
  498. snd_es1938_write(chip, ESS_CMD_DMACONTROL, val);
  499. return 0;
  500. }
  501. static int snd_es1938_playback1_trigger(snd_pcm_substream_t * substream,
  502. int cmd)
  503. {
  504. es1938_t *chip = snd_pcm_substream_chip(substream);
  505. switch (cmd) {
  506. case SNDRV_PCM_TRIGGER_START:
  507. /* According to the documentation this should be:
  508. 0x13 but that value may randomly swap stereo channels */
  509. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL1, 0x92);
  510. udelay(10);
  511. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL1, 0x93);
  512. /* This two stage init gives the FIFO -> DAC connection time to
  513. * settle before first data from DMA flows in. This should ensure
  514. * no swapping of stereo channels. Report a bug if otherwise :-) */
  515. outb(0x0a, SLIO_REG(chip, AUDIO2MODE));
  516. chip->active |= DAC2;
  517. break;
  518. case SNDRV_PCM_TRIGGER_STOP:
  519. outb(0, SLIO_REG(chip, AUDIO2MODE));
  520. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL1, 0);
  521. chip->active &= ~DAC2;
  522. break;
  523. default:
  524. return -EINVAL;
  525. }
  526. return 0;
  527. }
  528. static int snd_es1938_playback2_trigger(snd_pcm_substream_t * substream,
  529. int cmd)
  530. {
  531. es1938_t *chip = snd_pcm_substream_chip(substream);
  532. int val;
  533. switch (cmd) {
  534. case SNDRV_PCM_TRIGGER_START:
  535. val = 5;
  536. chip->active |= DAC1;
  537. break;
  538. case SNDRV_PCM_TRIGGER_STOP:
  539. val = 0;
  540. chip->active &= ~DAC1;
  541. break;
  542. default:
  543. return -EINVAL;
  544. }
  545. snd_es1938_write(chip, ESS_CMD_DMACONTROL, val);
  546. return 0;
  547. }
  548. static int snd_es1938_playback_trigger(snd_pcm_substream_t *substream,
  549. int cmd)
  550. {
  551. switch (substream->number) {
  552. case 0:
  553. return snd_es1938_playback1_trigger(substream, cmd);
  554. case 1:
  555. return snd_es1938_playback2_trigger(substream, cmd);
  556. }
  557. snd_BUG();
  558. return -EINVAL;
  559. }
  560. /* --------------------------------------------------------------------
  561. * First channel for Extended Mode Audio 1 ADC Operation
  562. * --------------------------------------------------------------------*/
  563. static int snd_es1938_capture_prepare(snd_pcm_substream_t * substream)
  564. {
  565. es1938_t *chip = snd_pcm_substream_chip(substream);
  566. snd_pcm_runtime_t *runtime = substream->runtime;
  567. int u, is8, mono;
  568. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  569. unsigned int count = snd_pcm_lib_period_bytes(substream);
  570. chip->dma1_size = size;
  571. chip->dma1_start = runtime->dma_addr;
  572. mono = (runtime->channels > 1) ? 0 : 1;
  573. is8 = snd_pcm_format_width(runtime->format) == 16 ? 0 : 1;
  574. u = snd_pcm_format_unsigned(runtime->format);
  575. chip->dma1_shift = 2 - mono - is8;
  576. snd_es1938_reset_fifo(chip);
  577. /* program type */
  578. snd_es1938_bits(chip, ESS_CMD_ANALOGCONTROL, 0x03, (mono ? 2 : 1));
  579. /* set clock and counters */
  580. snd_es1938_rate_set(chip, substream, ADC1);
  581. count = 0x10000 - count;
  582. snd_es1938_write(chip, ESS_CMD_DMACNTRELOADL, count & 0xff);
  583. snd_es1938_write(chip, ESS_CMD_DMACNTRELOADH, count >> 8);
  584. /* initialize and configure ADC */
  585. snd_es1938_write(chip, ESS_CMD_SETFORMAT2, u ? 0x51 : 0x71);
  586. snd_es1938_write(chip, ESS_CMD_SETFORMAT2, 0x90 |
  587. (u ? 0x00 : 0x20) |
  588. (is8 ? 0x00 : 0x04) |
  589. (mono ? 0x40 : 0x08));
  590. // snd_es1938_reset_fifo(chip);
  591. /* 11. configure system interrupt controller and DMA controller */
  592. snd_es1938_capture_setdma(chip);
  593. return 0;
  594. }
  595. /* ------------------------------------------------------------------------------
  596. * Second Audio channel DAC Operation
  597. * ------------------------------------------------------------------------------*/
  598. static int snd_es1938_playback1_prepare(snd_pcm_substream_t * substream)
  599. {
  600. es1938_t *chip = snd_pcm_substream_chip(substream);
  601. snd_pcm_runtime_t *runtime = substream->runtime;
  602. int u, is8, mono;
  603. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  604. unsigned int count = snd_pcm_lib_period_bytes(substream);
  605. chip->dma2_size = size;
  606. chip->dma2_start = runtime->dma_addr;
  607. mono = (runtime->channels > 1) ? 0 : 1;
  608. is8 = snd_pcm_format_width(runtime->format) == 16 ? 0 : 1;
  609. u = snd_pcm_format_unsigned(runtime->format);
  610. chip->dma2_shift = 2 - mono - is8;
  611. snd_es1938_reset_fifo(chip);
  612. /* set clock and counters */
  613. snd_es1938_rate_set(chip, substream, DAC2);
  614. count >>= 1;
  615. count = 0x10000 - count;
  616. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2TCOUNTL, count & 0xff);
  617. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2TCOUNTH, count >> 8);
  618. /* initialize and configure Audio 2 DAC */
  619. snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL2, 0x40 | (u ? 0 : 4) | (mono ? 0 : 2) | (is8 ? 0 : 1));
  620. /* program DMA */
  621. snd_es1938_playback1_setdma(chip);
  622. return 0;
  623. }
  624. static int snd_es1938_playback2_prepare(snd_pcm_substream_t * substream)
  625. {
  626. es1938_t *chip = snd_pcm_substream_chip(substream);
  627. snd_pcm_runtime_t *runtime = substream->runtime;
  628. int u, is8, mono;
  629. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  630. unsigned int count = snd_pcm_lib_period_bytes(substream);
  631. chip->dma1_size = size;
  632. chip->dma1_start = runtime->dma_addr;
  633. mono = (runtime->channels > 1) ? 0 : 1;
  634. is8 = snd_pcm_format_width(runtime->format) == 16 ? 0 : 1;
  635. u = snd_pcm_format_unsigned(runtime->format);
  636. chip->dma1_shift = 2 - mono - is8;
  637. count = 0x10000 - count;
  638. /* reset */
  639. snd_es1938_reset_fifo(chip);
  640. snd_es1938_bits(chip, ESS_CMD_ANALOGCONTROL, 0x03, (mono ? 2 : 1));
  641. /* set clock and counters */
  642. snd_es1938_rate_set(chip, substream, DAC1);
  643. snd_es1938_write(chip, ESS_CMD_DMACNTRELOADL, count & 0xff);
  644. snd_es1938_write(chip, ESS_CMD_DMACNTRELOADH, count >> 8);
  645. /* initialized and configure DAC */
  646. snd_es1938_write(chip, ESS_CMD_SETFORMAT, u ? 0x80 : 0x00);
  647. snd_es1938_write(chip, ESS_CMD_SETFORMAT, u ? 0x51 : 0x71);
  648. snd_es1938_write(chip, ESS_CMD_SETFORMAT2,
  649. 0x90 | (mono ? 0x40 : 0x08) |
  650. (is8 ? 0x00 : 0x04) | (u ? 0x00 : 0x20));
  651. /* program DMA */
  652. snd_es1938_playback2_setdma(chip);
  653. return 0;
  654. }
  655. static int snd_es1938_playback_prepare(snd_pcm_substream_t *substream)
  656. {
  657. switch (substream->number) {
  658. case 0:
  659. return snd_es1938_playback1_prepare(substream);
  660. case 1:
  661. return snd_es1938_playback2_prepare(substream);
  662. }
  663. snd_BUG();
  664. return -EINVAL;
  665. }
  666. static snd_pcm_uframes_t snd_es1938_capture_pointer(snd_pcm_substream_t * substream)
  667. {
  668. es1938_t *chip = snd_pcm_substream_chip(substream);
  669. size_t ptr;
  670. size_t old, new;
  671. #if 1
  672. /* This stuff is *needed*, don't ask why - AB */
  673. old = inw(SLDM_REG(chip, DMACOUNT));
  674. while ((new = inw(SLDM_REG(chip, DMACOUNT))) != old)
  675. old = new;
  676. ptr = chip->dma1_size - 1 - new;
  677. #else
  678. ptr = inl(SLDM_REG(chip, DMAADDR)) - chip->dma1_start;
  679. #endif
  680. return ptr >> chip->dma1_shift;
  681. }
  682. static snd_pcm_uframes_t snd_es1938_playback1_pointer(snd_pcm_substream_t * substream)
  683. {
  684. es1938_t *chip = snd_pcm_substream_chip(substream);
  685. size_t ptr;
  686. #if 1
  687. ptr = chip->dma2_size - inw(SLIO_REG(chip, AUDIO2DMACOUNT));
  688. #else
  689. ptr = inl(SLIO_REG(chip, AUDIO2DMAADDR)) - chip->dma2_start;
  690. #endif
  691. return ptr >> chip->dma2_shift;
  692. }
  693. static snd_pcm_uframes_t snd_es1938_playback2_pointer(snd_pcm_substream_t * substream)
  694. {
  695. es1938_t *chip = snd_pcm_substream_chip(substream);
  696. size_t ptr;
  697. size_t old, new;
  698. #if 1
  699. /* This stuff is *needed*, don't ask why - AB */
  700. old = inw(SLDM_REG(chip, DMACOUNT));
  701. while ((new = inw(SLDM_REG(chip, DMACOUNT))) != old)
  702. old = new;
  703. ptr = chip->dma1_size - 1 - new;
  704. #else
  705. ptr = inl(SLDM_REG(chip, DMAADDR)) - chip->dma1_start;
  706. #endif
  707. return ptr >> chip->dma1_shift;
  708. }
  709. static snd_pcm_uframes_t snd_es1938_playback_pointer(snd_pcm_substream_t *substream)
  710. {
  711. switch (substream->number) {
  712. case 0:
  713. return snd_es1938_playback1_pointer(substream);
  714. case 1:
  715. return snd_es1938_playback2_pointer(substream);
  716. }
  717. snd_BUG();
  718. return -EINVAL;
  719. }
  720. static int snd_es1938_capture_copy(snd_pcm_substream_t *substream,
  721. int channel,
  722. snd_pcm_uframes_t pos,
  723. void __user *dst,
  724. snd_pcm_uframes_t count)
  725. {
  726. snd_pcm_runtime_t *runtime = substream->runtime;
  727. es1938_t *chip = snd_pcm_substream_chip(substream);
  728. pos <<= chip->dma1_shift;
  729. count <<= chip->dma1_shift;
  730. snd_assert(pos + count <= chip->dma1_size, return -EINVAL);
  731. if (pos + count < chip->dma1_size) {
  732. if (copy_to_user(dst, runtime->dma_area + pos + 1, count))
  733. return -EFAULT;
  734. } else {
  735. if (copy_to_user(dst, runtime->dma_area + pos + 1, count - 1))
  736. return -EFAULT;
  737. if (put_user(runtime->dma_area[0], ((unsigned char __user *)dst) + count - 1))
  738. return -EFAULT;
  739. }
  740. return 0;
  741. }
  742. /*
  743. * buffer management
  744. */
  745. static int snd_es1938_pcm_hw_params(snd_pcm_substream_t *substream,
  746. snd_pcm_hw_params_t * hw_params)
  747. {
  748. int err;
  749. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  750. return err;
  751. return 0;
  752. }
  753. static int snd_es1938_pcm_hw_free(snd_pcm_substream_t *substream)
  754. {
  755. return snd_pcm_lib_free_pages(substream);
  756. }
  757. /* ----------------------------------------------------------------------
  758. * Audio1 Capture (ADC)
  759. * ----------------------------------------------------------------------*/
  760. static snd_pcm_hardware_t snd_es1938_capture =
  761. {
  762. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  763. SNDRV_PCM_INFO_BLOCK_TRANSFER),
  764. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE,
  765. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  766. .rate_min = 6000,
  767. .rate_max = 48000,
  768. .channels_min = 1,
  769. .channels_max = 2,
  770. .buffer_bytes_max = 0x8000, /* DMA controller screws on higher values */
  771. .period_bytes_min = 64,
  772. .period_bytes_max = 0x8000,
  773. .periods_min = 1,
  774. .periods_max = 1024,
  775. .fifo_size = 256,
  776. };
  777. /* -----------------------------------------------------------------------
  778. * Audio2 Playback (DAC)
  779. * -----------------------------------------------------------------------*/
  780. static snd_pcm_hardware_t snd_es1938_playback =
  781. {
  782. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  783. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  784. SNDRV_PCM_INFO_MMAP_VALID),
  785. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE,
  786. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  787. .rate_min = 6000,
  788. .rate_max = 48000,
  789. .channels_min = 1,
  790. .channels_max = 2,
  791. .buffer_bytes_max = 0x8000, /* DMA controller screws on higher values */
  792. .period_bytes_min = 64,
  793. .period_bytes_max = 0x8000,
  794. .periods_min = 1,
  795. .periods_max = 1024,
  796. .fifo_size = 256,
  797. };
  798. static int snd_es1938_capture_open(snd_pcm_substream_t * substream)
  799. {
  800. es1938_t *chip = snd_pcm_substream_chip(substream);
  801. snd_pcm_runtime_t *runtime = substream->runtime;
  802. if (chip->playback2_substream)
  803. return -EAGAIN;
  804. chip->capture_substream = substream;
  805. runtime->hw = snd_es1938_capture;
  806. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  807. &hw_constraints_clocks);
  808. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, 0xff00);
  809. return 0;
  810. }
  811. static int snd_es1938_playback_open(snd_pcm_substream_t * substream)
  812. {
  813. es1938_t *chip = snd_pcm_substream_chip(substream);
  814. snd_pcm_runtime_t *runtime = substream->runtime;
  815. switch (substream->number) {
  816. case 0:
  817. chip->playback1_substream = substream;
  818. break;
  819. case 1:
  820. if (chip->capture_substream)
  821. return -EAGAIN;
  822. chip->playback2_substream = substream;
  823. break;
  824. default:
  825. snd_BUG();
  826. return -EINVAL;
  827. }
  828. runtime->hw = snd_es1938_playback;
  829. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  830. &hw_constraints_clocks);
  831. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, 0xff00);
  832. return 0;
  833. }
  834. static int snd_es1938_capture_close(snd_pcm_substream_t * substream)
  835. {
  836. es1938_t *chip = snd_pcm_substream_chip(substream);
  837. chip->capture_substream = NULL;
  838. return 0;
  839. }
  840. static int snd_es1938_playback_close(snd_pcm_substream_t * substream)
  841. {
  842. es1938_t *chip = snd_pcm_substream_chip(substream);
  843. switch (substream->number) {
  844. case 0:
  845. chip->playback1_substream = NULL;
  846. break;
  847. case 1:
  848. chip->playback2_substream = NULL;
  849. break;
  850. default:
  851. snd_BUG();
  852. return -EINVAL;
  853. }
  854. return 0;
  855. }
  856. static snd_pcm_ops_t snd_es1938_playback_ops = {
  857. .open = snd_es1938_playback_open,
  858. .close = snd_es1938_playback_close,
  859. .ioctl = snd_pcm_lib_ioctl,
  860. .hw_params = snd_es1938_pcm_hw_params,
  861. .hw_free = snd_es1938_pcm_hw_free,
  862. .prepare = snd_es1938_playback_prepare,
  863. .trigger = snd_es1938_playback_trigger,
  864. .pointer = snd_es1938_playback_pointer,
  865. };
  866. static snd_pcm_ops_t snd_es1938_capture_ops = {
  867. .open = snd_es1938_capture_open,
  868. .close = snd_es1938_capture_close,
  869. .ioctl = snd_pcm_lib_ioctl,
  870. .hw_params = snd_es1938_pcm_hw_params,
  871. .hw_free = snd_es1938_pcm_hw_free,
  872. .prepare = snd_es1938_capture_prepare,
  873. .trigger = snd_es1938_capture_trigger,
  874. .pointer = snd_es1938_capture_pointer,
  875. .copy = snd_es1938_capture_copy,
  876. };
  877. static void snd_es1938_free_pcm(snd_pcm_t *pcm)
  878. {
  879. snd_pcm_lib_preallocate_free_for_all(pcm);
  880. }
  881. static int __devinit snd_es1938_new_pcm(es1938_t *chip, int device)
  882. {
  883. snd_pcm_t *pcm;
  884. int err;
  885. if ((err = snd_pcm_new(chip->card, "es-1938-1946", device, 2, 1, &pcm)) < 0)
  886. return err;
  887. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1938_playback_ops);
  888. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1938_capture_ops);
  889. pcm->private_data = chip;
  890. pcm->private_free = snd_es1938_free_pcm;
  891. pcm->info_flags = 0;
  892. strcpy(pcm->name, "ESS Solo-1");
  893. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  894. snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
  895. chip->pcm = pcm;
  896. return 0;
  897. }
  898. /* -------------------------------------------------------------------
  899. *
  900. * *** Mixer part ***
  901. */
  902. static int snd_es1938_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  903. {
  904. static char *texts[8] = {
  905. "Mic", "Mic Master", "CD", "AOUT",
  906. "Mic1", "Mix", "Line", "Master"
  907. };
  908. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  909. uinfo->count = 1;
  910. uinfo->value.enumerated.items = 8;
  911. if (uinfo->value.enumerated.item > 7)
  912. uinfo->value.enumerated.item = 7;
  913. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  914. return 0;
  915. }
  916. static int snd_es1938_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  917. {
  918. es1938_t *chip = snd_kcontrol_chip(kcontrol);
  919. ucontrol->value.enumerated.item[0] = snd_es1938_mixer_read(chip, 0x1c) & 0x07;
  920. return 0;
  921. }
  922. static int snd_es1938_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  923. {
  924. es1938_t *chip = snd_kcontrol_chip(kcontrol);
  925. unsigned char val = ucontrol->value.enumerated.item[0];
  926. if (val > 7)
  927. return -EINVAL;
  928. return snd_es1938_mixer_bits(chip, 0x1c, 0x07, val) != val;
  929. }
  930. static int snd_es1938_info_spatializer_enable(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  931. {
  932. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  933. uinfo->count = 1;
  934. uinfo->value.integer.min = 0;
  935. uinfo->value.integer.max = 1;
  936. return 0;
  937. }
  938. static int snd_es1938_get_spatializer_enable(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  939. {
  940. es1938_t *chip = snd_kcontrol_chip(kcontrol);
  941. unsigned char val = snd_es1938_mixer_read(chip, 0x50);
  942. ucontrol->value.integer.value[0] = !!(val & 8);
  943. return 0;
  944. }
  945. static int snd_es1938_put_spatializer_enable(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  946. {
  947. es1938_t *chip = snd_kcontrol_chip(kcontrol);
  948. unsigned char oval, nval;
  949. int change;
  950. nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
  951. oval = snd_es1938_mixer_read(chip, 0x50) & 0x0c;
  952. change = nval != oval;
  953. if (change) {
  954. snd_es1938_mixer_write(chip, 0x50, nval & ~0x04);
  955. snd_es1938_mixer_write(chip, 0x50, nval);
  956. }
  957. return change;
  958. }
  959. static int snd_es1938_info_hw_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  960. {
  961. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  962. uinfo->count = 2;
  963. uinfo->value.integer.min = 0;
  964. uinfo->value.integer.max = 63;
  965. return 0;
  966. }
  967. static int snd_es1938_get_hw_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  968. {
  969. es1938_t *chip = snd_kcontrol_chip(kcontrol);
  970. ucontrol->value.integer.value[0] = snd_es1938_mixer_read(chip, 0x61) & 0x3f;
  971. ucontrol->value.integer.value[1] = snd_es1938_mixer_read(chip, 0x63) & 0x3f;
  972. return 0;
  973. }
  974. static int snd_es1938_info_hw_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  975. {
  976. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  977. uinfo->count = 2;
  978. uinfo->value.integer.min = 0;
  979. uinfo->value.integer.max = 1;
  980. return 0;
  981. }
  982. static int snd_es1938_get_hw_switch(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  983. {
  984. es1938_t *chip = snd_kcontrol_chip(kcontrol);
  985. ucontrol->value.integer.value[0] = !(snd_es1938_mixer_read(chip, 0x61) & 0x40);
  986. ucontrol->value.integer.value[1] = !(snd_es1938_mixer_read(chip, 0x63) & 0x40);
  987. return 0;
  988. }
  989. static void snd_es1938_hwv_free(snd_kcontrol_t *kcontrol)
  990. {
  991. es1938_t *chip = snd_kcontrol_chip(kcontrol);
  992. chip->master_volume = NULL;
  993. chip->master_switch = NULL;
  994. chip->hw_volume = NULL;
  995. chip->hw_switch = NULL;
  996. }
  997. static int snd_es1938_reg_bits(es1938_t *chip, unsigned char reg,
  998. unsigned char mask, unsigned char val)
  999. {
  1000. if (reg < 0xa0)
  1001. return snd_es1938_mixer_bits(chip, reg, mask, val);
  1002. else
  1003. return snd_es1938_bits(chip, reg, mask, val);
  1004. }
  1005. static int snd_es1938_reg_read(es1938_t *chip, unsigned char reg)
  1006. {
  1007. if (reg < 0xa0)
  1008. return snd_es1938_mixer_read(chip, reg);
  1009. else
  1010. return snd_es1938_read(chip, reg);
  1011. }
  1012. #define ES1938_SINGLE(xname, xindex, reg, shift, mask, invert) \
  1013. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1014. .info = snd_es1938_info_single, \
  1015. .get = snd_es1938_get_single, .put = snd_es1938_put_single, \
  1016. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  1017. static int snd_es1938_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1018. {
  1019. int mask = (kcontrol->private_value >> 16) & 0xff;
  1020. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1021. uinfo->count = 1;
  1022. uinfo->value.integer.min = 0;
  1023. uinfo->value.integer.max = mask;
  1024. return 0;
  1025. }
  1026. static int snd_es1938_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1027. {
  1028. es1938_t *chip = snd_kcontrol_chip(kcontrol);
  1029. int reg = kcontrol->private_value & 0xff;
  1030. int shift = (kcontrol->private_value >> 8) & 0xff;
  1031. int mask = (kcontrol->private_value >> 16) & 0xff;
  1032. int invert = (kcontrol->private_value >> 24) & 0xff;
  1033. int val;
  1034. val = snd_es1938_reg_read(chip, reg);
  1035. ucontrol->value.integer.value[0] = (val >> shift) & mask;
  1036. if (invert)
  1037. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1038. return 0;
  1039. }
  1040. static int snd_es1938_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1041. {
  1042. es1938_t *chip = snd_kcontrol_chip(kcontrol);
  1043. int reg = kcontrol->private_value & 0xff;
  1044. int shift = (kcontrol->private_value >> 8) & 0xff;
  1045. int mask = (kcontrol->private_value >> 16) & 0xff;
  1046. int invert = (kcontrol->private_value >> 24) & 0xff;
  1047. unsigned char val;
  1048. val = (ucontrol->value.integer.value[0] & mask);
  1049. if (invert)
  1050. val = mask - val;
  1051. mask <<= shift;
  1052. val <<= shift;
  1053. return snd_es1938_reg_bits(chip, reg, mask, val) != val;
  1054. }
  1055. #define ES1938_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  1056. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1057. .info = snd_es1938_info_double, \
  1058. .get = snd_es1938_get_double, .put = snd_es1938_put_double, \
  1059. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  1060. static int snd_es1938_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1061. {
  1062. int mask = (kcontrol->private_value >> 24) & 0xff;
  1063. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1064. uinfo->count = 2;
  1065. uinfo->value.integer.min = 0;
  1066. uinfo->value.integer.max = mask;
  1067. return 0;
  1068. }
  1069. static int snd_es1938_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1070. {
  1071. es1938_t *chip = snd_kcontrol_chip(kcontrol);
  1072. int left_reg = kcontrol->private_value & 0xff;
  1073. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1074. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1075. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1076. int mask = (kcontrol->private_value >> 24) & 0xff;
  1077. int invert = (kcontrol->private_value >> 22) & 1;
  1078. unsigned char left, right;
  1079. left = snd_es1938_reg_read(chip, left_reg);
  1080. if (left_reg != right_reg)
  1081. right = snd_es1938_reg_read(chip, right_reg);
  1082. else
  1083. right = left;
  1084. ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
  1085. ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
  1086. if (invert) {
  1087. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1088. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  1089. }
  1090. return 0;
  1091. }
  1092. static int snd_es1938_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1093. {
  1094. es1938_t *chip = snd_kcontrol_chip(kcontrol);
  1095. int left_reg = kcontrol->private_value & 0xff;
  1096. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1097. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1098. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1099. int mask = (kcontrol->private_value >> 24) & 0xff;
  1100. int invert = (kcontrol->private_value >> 22) & 1;
  1101. int change;
  1102. unsigned char val1, val2, mask1, mask2;
  1103. val1 = ucontrol->value.integer.value[0] & mask;
  1104. val2 = ucontrol->value.integer.value[1] & mask;
  1105. if (invert) {
  1106. val1 = mask - val1;
  1107. val2 = mask - val2;
  1108. }
  1109. val1 <<= shift_left;
  1110. val2 <<= shift_right;
  1111. mask1 = mask << shift_left;
  1112. mask2 = mask << shift_right;
  1113. if (left_reg != right_reg) {
  1114. change = 0;
  1115. if (snd_es1938_reg_bits(chip, left_reg, mask1, val1) != val1)
  1116. change = 1;
  1117. if (snd_es1938_reg_bits(chip, right_reg, mask2, val2) != val2)
  1118. change = 1;
  1119. } else {
  1120. change = (snd_es1938_reg_bits(chip, left_reg, mask1 | mask2,
  1121. val1 | val2) != (val1 | val2));
  1122. }
  1123. return change;
  1124. }
  1125. static snd_kcontrol_new_t snd_es1938_controls[] = {
  1126. ES1938_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
  1127. ES1938_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
  1128. {
  1129. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1130. .name = "Hardware Master Playback Volume",
  1131. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1132. .info = snd_es1938_info_hw_volume,
  1133. .get = snd_es1938_get_hw_volume,
  1134. },
  1135. {
  1136. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1137. .name = "Hardware Master Playback Switch",
  1138. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1139. .info = snd_es1938_info_hw_switch,
  1140. .get = snd_es1938_get_hw_switch,
  1141. },
  1142. ES1938_SINGLE("Hardware Volume Split", 0, 0x64, 7, 1, 0),
  1143. ES1938_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
  1144. ES1938_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
  1145. ES1938_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
  1146. ES1938_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
  1147. ES1938_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
  1148. ES1938_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
  1149. ES1938_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
  1150. ES1938_SINGLE("PC Speaker Volume", 0, 0x3c, 0, 7, 0),
  1151. ES1938_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
  1152. ES1938_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
  1153. {
  1154. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1155. .name = "Capture Source",
  1156. .info = snd_es1938_info_mux,
  1157. .get = snd_es1938_get_mux,
  1158. .put = snd_es1938_put_mux,
  1159. },
  1160. ES1938_DOUBLE("Mono Input Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
  1161. ES1938_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
  1162. ES1938_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
  1163. ES1938_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
  1164. ES1938_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
  1165. ES1938_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0),
  1166. ES1938_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
  1167. ES1938_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0),
  1168. ES1938_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
  1169. ES1938_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0),
  1170. ES1938_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
  1171. {
  1172. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1173. .name = "3D Control - Switch",
  1174. .info = snd_es1938_info_spatializer_enable,
  1175. .get = snd_es1938_get_spatializer_enable,
  1176. .put = snd_es1938_put_spatializer_enable,
  1177. },
  1178. ES1938_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0)
  1179. };
  1180. /* ---------------------------------------------------------------------------- */
  1181. /* ---------------------------------------------------------------------------- */
  1182. /*
  1183. * initialize the chip - used by resume callback, too
  1184. */
  1185. static void snd_es1938_chip_init(es1938_t *chip)
  1186. {
  1187. /* reset chip */
  1188. snd_es1938_reset(chip);
  1189. /* configure native mode */
  1190. /* enable bus master */
  1191. pci_set_master(chip->pci);
  1192. /* disable legacy audio */
  1193. pci_write_config_word(chip->pci, SL_PCI_LEGACYCONTROL, 0x805f);
  1194. /* set DDMA base */
  1195. pci_write_config_word(chip->pci, SL_PCI_DDMACONTROL, chip->ddma_port | 1);
  1196. /* set DMA/IRQ policy */
  1197. pci_write_config_dword(chip->pci, SL_PCI_CONFIG, 0);
  1198. /* enable Audio 1, Audio 2, MPU401 IRQ and HW volume IRQ*/
  1199. outb(0xf0, SLIO_REG(chip, IRQCONTROL));
  1200. /* reset DMA */
  1201. outb(0, SLDM_REG(chip, DMACLEAR));
  1202. }
  1203. #ifdef CONFIG_PM
  1204. /*
  1205. * PM support
  1206. */
  1207. static unsigned char saved_regs[SAVED_REG_SIZE+1] = {
  1208. 0x14, 0x1a, 0x1c, 0x3a, 0x3c, 0x3e, 0x36, 0x38,
  1209. 0x50, 0x52, 0x60, 0x61, 0x62, 0x63, 0x64, 0x68,
  1210. 0x69, 0x6a, 0x6b, 0x6d, 0x6e, 0x6f, 0x7c, 0x7d,
  1211. 0xa8, 0xb4,
  1212. };
  1213. static int es1938_suspend(snd_card_t *card, pm_message_t state)
  1214. {
  1215. es1938_t *chip = card->pm_private_data;
  1216. unsigned char *s, *d;
  1217. snd_pcm_suspend_all(chip->pcm);
  1218. /* save mixer-related registers */
  1219. for (s = saved_regs, d = chip->saved_regs; *s; s++, d++)
  1220. *d = snd_es1938_reg_read(chip, *s);
  1221. outb(0x00, SLIO_REG(chip, IRQCONTROL)); /* disable irqs */
  1222. pci_disable_device(chip->pci);
  1223. return 0;
  1224. }
  1225. static int es1938_resume(snd_card_t *card)
  1226. {
  1227. es1938_t *chip = card->pm_private_data;
  1228. unsigned char *s, *d;
  1229. pci_enable_device(chip->pci);
  1230. snd_es1938_chip_init(chip);
  1231. /* restore mixer-related registers */
  1232. for (s = saved_regs, d = chip->saved_regs; *s; s++, d++) {
  1233. if (*s < 0xa0)
  1234. snd_es1938_mixer_write(chip, *s, *d);
  1235. else
  1236. snd_es1938_write(chip, *s, *d);
  1237. }
  1238. return 0;
  1239. }
  1240. #endif /* CONFIG_PM */
  1241. #ifdef SUPPORT_JOYSTICK
  1242. static int __devinit snd_es1938_create_gameport(es1938_t *chip)
  1243. {
  1244. struct gameport *gp;
  1245. chip->gameport = gp = gameport_allocate_port();
  1246. if (!gp) {
  1247. printk(KERN_ERR "es1938: cannot allocate memory for gameport\n");
  1248. return -ENOMEM;
  1249. }
  1250. gameport_set_name(gp, "ES1938");
  1251. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  1252. gameport_set_dev_parent(gp, &chip->pci->dev);
  1253. gp->io = chip->game_port;
  1254. gameport_register_port(gp);
  1255. return 0;
  1256. }
  1257. static void snd_es1938_free_gameport(es1938_t *chip)
  1258. {
  1259. if (chip->gameport) {
  1260. gameport_unregister_port(chip->gameport);
  1261. chip->gameport = NULL;
  1262. }
  1263. }
  1264. #else
  1265. static inline int snd_es1938_create_gameport(es1938_t *chip) { return -ENOSYS; }
  1266. static inline void snd_es1938_free_gameport(es1938_t *chip) { }
  1267. #endif /* SUPPORT_JOYSTICK */
  1268. static int snd_es1938_free(es1938_t *chip)
  1269. {
  1270. /* disable irqs */
  1271. outb(0x00, SLIO_REG(chip, IRQCONTROL));
  1272. if (chip->rmidi)
  1273. snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0);
  1274. snd_es1938_free_gameport(chip);
  1275. if (chip->irq >= 0)
  1276. free_irq(chip->irq, (void *)chip);
  1277. pci_release_regions(chip->pci);
  1278. pci_disable_device(chip->pci);
  1279. kfree(chip);
  1280. return 0;
  1281. }
  1282. static int snd_es1938_dev_free(snd_device_t *device)
  1283. {
  1284. es1938_t *chip = device->device_data;
  1285. return snd_es1938_free(chip);
  1286. }
  1287. static int __devinit snd_es1938_create(snd_card_t * card,
  1288. struct pci_dev * pci,
  1289. es1938_t ** rchip)
  1290. {
  1291. es1938_t *chip;
  1292. int err;
  1293. static snd_device_ops_t ops = {
  1294. .dev_free = snd_es1938_dev_free,
  1295. };
  1296. *rchip = NULL;
  1297. /* enable PCI device */
  1298. if ((err = pci_enable_device(pci)) < 0)
  1299. return err;
  1300. /* check, if we can restrict PCI DMA transfers to 24 bits */
  1301. if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
  1302. pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
  1303. snd_printk("architecture does not support 24bit PCI busmaster DMA\n");
  1304. pci_disable_device(pci);
  1305. return -ENXIO;
  1306. }
  1307. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  1308. if (chip == NULL) {
  1309. pci_disable_device(pci);
  1310. return -ENOMEM;
  1311. }
  1312. spin_lock_init(&chip->reg_lock);
  1313. spin_lock_init(&chip->mixer_lock);
  1314. chip->card = card;
  1315. chip->pci = pci;
  1316. if ((err = pci_request_regions(pci, "ESS Solo-1")) < 0) {
  1317. kfree(chip);
  1318. pci_disable_device(pci);
  1319. return err;
  1320. }
  1321. chip->io_port = pci_resource_start(pci, 0);
  1322. chip->sb_port = pci_resource_start(pci, 1);
  1323. chip->vc_port = pci_resource_start(pci, 2);
  1324. chip->mpu_port = pci_resource_start(pci, 3);
  1325. chip->game_port = pci_resource_start(pci, 4);
  1326. if (request_irq(pci->irq, snd_es1938_interrupt, SA_INTERRUPT|SA_SHIRQ, "ES1938", (void *)chip)) {
  1327. snd_printk("unable to grab IRQ %d\n", pci->irq);
  1328. snd_es1938_free(chip);
  1329. return -EBUSY;
  1330. }
  1331. chip->irq = pci->irq;
  1332. #ifdef ES1938_DDEBUG
  1333. snd_printk("create: io: 0x%lx, sb: 0x%lx, vc: 0x%lx, mpu: 0x%lx, game: 0x%lx\n",
  1334. chip->io_port, chip->sb_port, chip->vc_port, chip->mpu_port, chip->game_port);
  1335. #endif
  1336. chip->ddma_port = chip->vc_port + 0x00; /* fix from Thomas Sailer */
  1337. snd_es1938_chip_init(chip);
  1338. snd_card_set_pm_callback(card, es1938_suspend, es1938_resume, chip);
  1339. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1340. snd_es1938_free(chip);
  1341. return err;
  1342. }
  1343. snd_card_set_dev(card, &pci->dev);
  1344. *rchip = chip;
  1345. return 0;
  1346. }
  1347. /* --------------------------------------------------------------------
  1348. * Interrupt handler
  1349. * -------------------------------------------------------------------- */
  1350. static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1351. {
  1352. es1938_t *chip = dev_id;
  1353. unsigned char status, audiostatus;
  1354. int handled = 0;
  1355. status = inb(SLIO_REG(chip, IRQCONTROL));
  1356. #if 0
  1357. printk("Es1938debug - interrupt status: =0x%x\n", status);
  1358. #endif
  1359. /* AUDIO 1 */
  1360. if (status & 0x10) {
  1361. #if 0
  1362. printk("Es1938debug - AUDIO channel 1 interrupt\n");
  1363. printk("Es1938debug - AUDIO channel 1 DMAC DMA count: %u\n", inw(SLDM_REG(chip, DMACOUNT)));
  1364. printk("Es1938debug - AUDIO channel 1 DMAC DMA base: %u\n", inl(SLDM_REG(chip, DMAADDR)));
  1365. printk("Es1938debug - AUDIO channel 1 DMAC DMA status: 0x%x\n", inl(SLDM_REG(chip, DMASTATUS)));
  1366. #endif
  1367. /* clear irq */
  1368. handled = 1;
  1369. audiostatus = inb(SLSB_REG(chip, STATUS));
  1370. if (chip->active & ADC1)
  1371. snd_pcm_period_elapsed(chip->capture_substream);
  1372. else if (chip->active & DAC1)
  1373. snd_pcm_period_elapsed(chip->playback2_substream);
  1374. }
  1375. /* AUDIO 2 */
  1376. if (status & 0x20) {
  1377. #if 0
  1378. printk("Es1938debug - AUDIO channel 2 interrupt\n");
  1379. printk("Es1938debug - AUDIO channel 2 DMAC DMA count: %u\n", inw(SLIO_REG(chip, AUDIO2DMACOUNT)));
  1380. printk("Es1938debug - AUDIO channel 2 DMAC DMA base: %u\n", inl(SLIO_REG(chip, AUDIO2DMAADDR)));
  1381. #endif
  1382. /* clear irq */
  1383. handled = 1;
  1384. snd_es1938_mixer_bits(chip, ESSSB_IREG_AUDIO2CONTROL2, 0x80, 0);
  1385. if (chip->active & DAC2)
  1386. snd_pcm_period_elapsed(chip->playback1_substream);
  1387. }
  1388. /* Hardware volume */
  1389. if (status & 0x40) {
  1390. int split = snd_es1938_mixer_read(chip, 0x64) & 0x80;
  1391. handled = 1;
  1392. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
  1393. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
  1394. if (!split) {
  1395. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
  1396. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
  1397. }
  1398. /* ack interrupt */
  1399. snd_es1938_mixer_write(chip, 0x66, 0x00);
  1400. }
  1401. /* MPU401 */
  1402. if (status & 0x80) {
  1403. // the following line is evil! It switches off MIDI interrupt handling after the first interrupt received.
  1404. // replacing the last 0 by 0x40 works for ESS-Solo1, but just doing nothing works as well!
  1405. // andreas@flying-snail.de
  1406. // snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0); /* ack? */
  1407. if (chip->rmidi) {
  1408. handled = 1;
  1409. snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
  1410. }
  1411. }
  1412. return IRQ_RETVAL(handled);
  1413. }
  1414. #define ES1938_DMA_SIZE 64
  1415. static int __devinit snd_es1938_mixer(es1938_t *chip)
  1416. {
  1417. snd_card_t *card;
  1418. unsigned int idx;
  1419. int err;
  1420. card = chip->card;
  1421. strcpy(card->mixername, "ESS Solo-1");
  1422. for (idx = 0; idx < ARRAY_SIZE(snd_es1938_controls); idx++) {
  1423. snd_kcontrol_t *kctl;
  1424. kctl = snd_ctl_new1(&snd_es1938_controls[idx], chip);
  1425. switch (idx) {
  1426. case 0:
  1427. chip->master_volume = kctl;
  1428. kctl->private_free = snd_es1938_hwv_free;
  1429. break;
  1430. case 1:
  1431. chip->master_switch = kctl;
  1432. kctl->private_free = snd_es1938_hwv_free;
  1433. break;
  1434. case 2:
  1435. chip->hw_volume = kctl;
  1436. kctl->private_free = snd_es1938_hwv_free;
  1437. break;
  1438. case 3:
  1439. chip->hw_switch = kctl;
  1440. kctl->private_free = snd_es1938_hwv_free;
  1441. break;
  1442. }
  1443. if ((err = snd_ctl_add(card, kctl)) < 0)
  1444. return err;
  1445. }
  1446. return 0;
  1447. }
  1448. static int __devinit snd_es1938_probe(struct pci_dev *pci,
  1449. const struct pci_device_id *pci_id)
  1450. {
  1451. static int dev;
  1452. snd_card_t *card;
  1453. es1938_t *chip;
  1454. opl3_t *opl3;
  1455. int idx, err;
  1456. if (dev >= SNDRV_CARDS)
  1457. return -ENODEV;
  1458. if (!enable[dev]) {
  1459. dev++;
  1460. return -ENOENT;
  1461. }
  1462. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1463. if (card == NULL)
  1464. return -ENOMEM;
  1465. for (idx = 0; idx < 5; idx++) {
  1466. if (pci_resource_start(pci, idx) == 0 ||
  1467. !(pci_resource_flags(pci, idx) & IORESOURCE_IO)) {
  1468. snd_card_free(card);
  1469. return -ENODEV;
  1470. }
  1471. }
  1472. if ((err = snd_es1938_create(card, pci, &chip)) < 0) {
  1473. snd_card_free(card);
  1474. return err;
  1475. }
  1476. strcpy(card->driver, "ES1938");
  1477. strcpy(card->shortname, "ESS ES1938 (Solo-1)");
  1478. sprintf(card->longname, "%s rev %i, irq %i",
  1479. card->shortname,
  1480. chip->revision,
  1481. chip->irq);
  1482. if ((err = snd_es1938_new_pcm(chip, 0)) < 0) {
  1483. snd_card_free(card);
  1484. return err;
  1485. }
  1486. if ((err = snd_es1938_mixer(chip)) < 0) {
  1487. snd_card_free(card);
  1488. return err;
  1489. }
  1490. if (snd_opl3_create(card,
  1491. SLSB_REG(chip, FMLOWADDR),
  1492. SLSB_REG(chip, FMHIGHADDR),
  1493. OPL3_HW_OPL3, 1, &opl3) < 0) {
  1494. printk(KERN_ERR "es1938: OPL3 not detected at 0x%lx\n",
  1495. SLSB_REG(chip, FMLOWADDR));
  1496. } else {
  1497. if ((err = snd_opl3_timer_new(opl3, 0, 1)) < 0) {
  1498. snd_card_free(card);
  1499. return err;
  1500. }
  1501. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  1502. snd_card_free(card);
  1503. return err;
  1504. }
  1505. }
  1506. if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
  1507. chip->mpu_port, 1, chip->irq, 0, &chip->rmidi) < 0) {
  1508. printk(KERN_ERR "es1938: unable to initialize MPU-401\n");
  1509. } else {
  1510. // this line is vital for MIDI interrupt handling on ess-solo1
  1511. // andreas@flying-snail.de
  1512. snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0x40);
  1513. }
  1514. snd_es1938_create_gameport(chip);
  1515. if ((err = snd_card_register(card)) < 0) {
  1516. snd_card_free(card);
  1517. return err;
  1518. }
  1519. pci_set_drvdata(pci, card);
  1520. dev++;
  1521. return 0;
  1522. }
  1523. static void __devexit snd_es1938_remove(struct pci_dev *pci)
  1524. {
  1525. snd_card_free(pci_get_drvdata(pci));
  1526. pci_set_drvdata(pci, NULL);
  1527. }
  1528. static struct pci_driver driver = {
  1529. .name = "ESS ES1938 (Solo-1)",
  1530. .id_table = snd_es1938_ids,
  1531. .probe = snd_es1938_probe,
  1532. .remove = __devexit_p(snd_es1938_remove),
  1533. SND_PCI_PM_CALLBACKS
  1534. };
  1535. static int __init alsa_card_es1938_init(void)
  1536. {
  1537. return pci_register_driver(&driver);
  1538. }
  1539. static void __exit alsa_card_es1938_exit(void)
  1540. {
  1541. pci_unregister_driver(&driver);
  1542. }
  1543. module_init(alsa_card_es1938_init)
  1544. module_exit(alsa_card_es1938_exit)