als4000.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. * card-als4000.c - driver for Avance Logic ALS4000 based soundcards.
  3. * Copyright (C) 2000 by Bart Hartgers <bart@etpmod.phys.tue.nl>,
  4. * Jaroslav Kysela <perex@perex.cz>
  5. * Copyright (C) 2002 by Andreas Mohr <hw7oshyuv3001@sneakemail.com>
  6. *
  7. * Framework borrowed from Massimo Piccioni's card-als100.c.
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * NOTES
  24. *
  25. * Since Avance does not provide any meaningful documentation, and I
  26. * bought an ALS4000 based soundcard, I was forced to base this driver
  27. * on reverse engineering.
  28. *
  29. * Note: this is no longer true. Pretty verbose chip docu (ALS4000a.PDF)
  30. * can be found on the ALSA web site.
  31. *
  32. * The ALS4000 seems to be the PCI-cousin of the ALS100. It contains an
  33. * ALS100-like SB DSP/mixer, an OPL3 synth, a MPU401 and a gameport
  34. * interface. These subsystems can be mapped into ISA io-port space,
  35. * using the PCI-interface. In addition, the PCI-bit provides DMA and IRQ
  36. * services to the subsystems.
  37. *
  38. * While ALS4000 is very similar to a SoundBlaster, the differences in
  39. * DMA and capturing require more changes to the SoundBlaster than
  40. * desirable, so I made this separate driver.
  41. *
  42. * The ALS4000 can do real full duplex playback/capture.
  43. *
  44. * FMDAC:
  45. * - 0x4f -> port 0x14
  46. * - port 0x15 |= 1
  47. *
  48. * Enable/disable 3D sound:
  49. * - 0x50 -> port 0x14
  50. * - change bit 6 (0x40) of port 0x15
  51. *
  52. * Set QSound:
  53. * - 0xdb -> port 0x14
  54. * - set port 0x15:
  55. * 0x3e (mode 3), 0x3c (mode 2), 0x3a (mode 1), 0x38 (mode 0)
  56. *
  57. * Set KSound:
  58. * - value -> some port 0x0c0d
  59. *
  60. * ToDo:
  61. * - Proper shared IRQ handling?
  62. * - by default, don't enable legacy game and use PCI game I/O
  63. * - power management? (card can do voice wakeup according to datasheet!!)
  64. */
  65. #include <asm/io.h>
  66. #include <linux/init.h>
  67. #include <linux/pci.h>
  68. #include <linux/slab.h>
  69. #include <linux/gameport.h>
  70. #include <linux/moduleparam.h>
  71. #include <linux/dma-mapping.h>
  72. #include <sound/core.h>
  73. #include <sound/pcm.h>
  74. #include <sound/rawmidi.h>
  75. #include <sound/mpu401.h>
  76. #include <sound/opl3.h>
  77. #include <sound/sb.h>
  78. #include <sound/initval.h>
  79. MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>");
  80. MODULE_DESCRIPTION("Avance Logic ALS4000");
  81. MODULE_LICENSE("GPL");
  82. MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS4000}}");
  83. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  84. #define SUPPORT_JOYSTICK 1
  85. #endif
  86. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  87. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  88. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  89. #ifdef SUPPORT_JOYSTICK
  90. static int joystick_port[SNDRV_CARDS];
  91. #endif
  92. module_param_array(index, int, NULL, 0444);
  93. MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
  94. module_param_array(id, charp, NULL, 0444);
  95. MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
  96. module_param_array(enable, bool, NULL, 0444);
  97. MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
  98. #ifdef SUPPORT_JOYSTICK
  99. module_param_array(joystick_port, int, NULL, 0444);
  100. MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
  101. #endif
  102. struct snd_card_als4000 {
  103. /* most frequent access first */
  104. unsigned long iobase;
  105. struct pci_dev *pci;
  106. struct snd_sb *chip;
  107. #ifdef SUPPORT_JOYSTICK
  108. struct gameport *gameport;
  109. #endif
  110. };
  111. static struct pci_device_id snd_als4000_ids[] = {
  112. { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* ALS4000 */
  113. { 0, }
  114. };
  115. MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
  116. enum als4k_iobase_t {
  117. /* IOx: B == Byte, W = Word, D = DWord */
  118. ALS4K_IOD_00_AC97_ACCESS = 0x00,
  119. ALS4K_IOW_04_AC97_READ = 0x04,
  120. ALS4K_IOB_06_AC97_STATUS = 0x06,
  121. ALS4K_IOB_07_IRQSTATUS = 0x07,
  122. ALS4K_IOD_08_GCR_DATA = 0x08,
  123. ALS4K_IOB_0C_GCR_INDEX = 0x0c,
  124. ALS4K_IOB_0E_SB_MPU_IRQ = 0x0e,
  125. ALS4K_IOB_10_ADLIB_ADDR0 = 0x10,
  126. ALS4K_IOB_11_ADLIB_ADDR1 = 0x11,
  127. ALS4K_IOB_12_ADLIB_ADDR2 = 0x12,
  128. ALS4K_IOB_13_ADLIB_ADDR3 = 0x13,
  129. ALS4K_IOB_14_MIXER_INDEX = 0x14,
  130. ALS4K_IOB_15_MIXER_DATA = 0x15,
  131. ALS4K_IOB_16_ESP_RST_PORT = 0x16,
  132. ALS4K_IOB_16_CR1E_ACK_PORT = 0x16, /* 2nd function */
  133. ALS4K_IOB_18_OPL_ADDR0 = 0x18,
  134. ALS4K_IOB_19_OPL_ADDR1 = 0x19,
  135. ALS4K_IOB_1A_ESP_RD_DATA = 0x1a,
  136. ALS4K_IOB_1C_ESP_CMD_DATA = 0x1c,
  137. ALS4K_IOB_1C_ESP_WR_STATUS = 0x1c, /* 2nd function */
  138. ALS4K_IOB_1E_ESP_RD_STATUS8 = 0x1e,
  139. ALS4K_IOB_1F_ESP_RD_STATUS16 = 0x1f,
  140. ALS4K_IOB_20_ESP_GAMEPORT_200 = 0x20,
  141. ALS4K_IOB_21_ESP_GAMEPORT_201 = 0x21,
  142. ALS4K_IOB_30_MIDI_DATA = 0x30,
  143. ALS4K_IOB_31_MIDI_STATUS = 0x31,
  144. ALS4K_IOB_31_MIDI_COMMAND = 0x31, /* 2nd function */
  145. };
  146. enum als4k_gcr_t {
  147. /* all registers 32bit wide */
  148. ALS4K_GCR_8C_MISC_CTRL = 0x8c,
  149. ALS4K_GCR_90_TEST_MODE_REG = 0x90,
  150. ALS4K_GCR_91_DMA0_ADDR = 0x91,
  151. ALS4K_GCR_92_DMA0_MODE_COUNT = 0x92,
  152. ALS4K_GCR_93_DMA1_ADDR = 0x93,
  153. ALS4K_GCR_94_DMA1_MODE_COUNT = 0x94,
  154. ALS4K_GCR_95_DMA3_ADDR = 0x95,
  155. ALS4K_GCR_96_DMA3_MODE_COUNT = 0x96,
  156. ALS4K_GCR_99_DMA_EMULATION_CTRL = 0x99,
  157. ALS4K_GCR_A0_FIFO1_CURRENT_ADDR = 0xa0,
  158. ALS4K_GCR_A1_FIFO1_STATUS_BYTECOUNT = 0xa1,
  159. ALS4K_GCR_A2_FIFO2_PCIADDR = 0xa2,
  160. ALS4K_GCR_A3_FIFO2_COUNT = 0xa3,
  161. ALS4K_GCR_A4_FIFO2_CURRENT_ADDR = 0xa4,
  162. ALS4K_GCR_A5_FIFO1_STATUS_BYTECOUNT = 0xa5,
  163. ALS4K_GCR_A6_PM_CTRL = 0xa6,
  164. ALS4K_GCR_A7_PCI_ACCESS_STORAGE = 0xa7,
  165. ALS4K_GCR_A8_LEGACY_CFG1 = 0xa8,
  166. ALS4K_GCR_A9_LEGACY_CFG2 = 0xa9,
  167. ALS4K_GCR_FF_DUMMY_SCRATCH = 0xff,
  168. };
  169. enum als4k_gcr_8c_t {
  170. ALS4K_GCR_8C_IRQ_MASK_CTRL_ENABLE = 0x8000,
  171. ALS4K_GCR_8C_CHIP_REV_MASK = 0xf0000
  172. };
  173. static inline void snd_als4000_gcr_write_addr(unsigned long iobase,
  174. enum als4k_gcr_t reg,
  175. u32 val)
  176. {
  177. outb(reg, iobase + ALS4K_IOB_0C_GCR_INDEX);
  178. outl(val, iobase + ALS4K_IOD_08_GCR_DATA);
  179. }
  180. static inline void snd_als4000_gcr_write(struct snd_sb *sb,
  181. enum als4k_gcr_t reg,
  182. u32 val)
  183. {
  184. snd_als4000_gcr_write_addr(sb->alt_port, reg, val);
  185. }
  186. static inline u32 snd_als4000_gcr_read_addr(unsigned long iobase,
  187. enum als4k_gcr_t reg)
  188. {
  189. outb(reg, iobase + ALS4K_IOB_0C_GCR_INDEX);
  190. return inl(iobase + ALS4K_IOD_08_GCR_DATA);
  191. }
  192. static inline u32 snd_als4000_gcr_read(struct snd_sb *sb, enum als4k_gcr_t reg)
  193. {
  194. return snd_als4000_gcr_read_addr(sb->alt_port, reg);
  195. }
  196. static void snd_als4000_set_rate(struct snd_sb *chip, unsigned int rate)
  197. {
  198. if (!(chip->mode & SB_RATE_LOCK)) {
  199. snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
  200. snd_sbdsp_command(chip, rate>>8);
  201. snd_sbdsp_command(chip, rate);
  202. }
  203. }
  204. static inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
  205. dma_addr_t addr, unsigned size)
  206. {
  207. snd_als4000_gcr_write(chip, ALS4K_GCR_A2_FIFO2_PCIADDR, addr);
  208. snd_als4000_gcr_write(chip, ALS4K_GCR_A3_FIFO2_COUNT, (size-1));
  209. }
  210. static inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
  211. dma_addr_t addr,
  212. unsigned size)
  213. {
  214. snd_als4000_gcr_write(chip, ALS4K_GCR_91_DMA0_ADDR, addr);
  215. snd_als4000_gcr_write(chip, ALS4K_GCR_92_DMA0_MODE_COUNT,
  216. (size-1)|0x180000);
  217. }
  218. #define ALS4000_FORMAT_SIGNED (1<<0)
  219. #define ALS4000_FORMAT_16BIT (1<<1)
  220. #define ALS4000_FORMAT_STEREO (1<<2)
  221. static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
  222. {
  223. int result;
  224. result = 0;
  225. if (snd_pcm_format_signed(runtime->format))
  226. result |= ALS4000_FORMAT_SIGNED;
  227. if (snd_pcm_format_physical_width(runtime->format) == 16)
  228. result |= ALS4000_FORMAT_16BIT;
  229. if (runtime->channels > 1)
  230. result |= ALS4000_FORMAT_STEREO;
  231. return result;
  232. }
  233. /* structure for setting up playback */
  234. static const struct {
  235. unsigned char dsp_cmd, dma_on, dma_off, format;
  236. } playback_cmd_vals[]={
  237. /* ALS4000_FORMAT_U8_MONO */
  238. { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
  239. /* ALS4000_FORMAT_S8_MONO */
  240. { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
  241. /* ALS4000_FORMAT_U16L_MONO */
  242. { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
  243. /* ALS4000_FORMAT_S16L_MONO */
  244. { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
  245. /* ALS4000_FORMAT_U8_STEREO */
  246. { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
  247. /* ALS4000_FORMAT_S8_STEREO */
  248. { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
  249. /* ALS4000_FORMAT_U16L_STEREO */
  250. { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
  251. /* ALS4000_FORMAT_S16L_STEREO */
  252. { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
  253. };
  254. #define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
  255. /* structure for setting up capture */
  256. enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
  257. static const unsigned char capture_cmd_vals[]=
  258. {
  259. CMD_WIDTH8|CMD_MONO, /* ALS4000_FORMAT_U8_MONO */
  260. CMD_WIDTH8|CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S8_MONO */
  261. CMD_MONO, /* ALS4000_FORMAT_U16L_MONO */
  262. CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S16L_MONO */
  263. CMD_WIDTH8|CMD_STEREO, /* ALS4000_FORMAT_U8_STEREO */
  264. CMD_WIDTH8|CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S8_STEREO */
  265. CMD_STEREO, /* ALS4000_FORMAT_U16L_STEREO */
  266. CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S16L_STEREO */
  267. };
  268. #define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
  269. static int snd_als4000_hw_params(struct snd_pcm_substream *substream,
  270. struct snd_pcm_hw_params *hw_params)
  271. {
  272. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  273. }
  274. static int snd_als4000_hw_free(struct snd_pcm_substream *substream)
  275. {
  276. snd_pcm_lib_free_pages(substream);
  277. return 0;
  278. }
  279. static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
  280. {
  281. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  282. struct snd_pcm_runtime *runtime = substream->runtime;
  283. unsigned long size;
  284. unsigned count;
  285. chip->capture_format = snd_als4000_get_format(runtime);
  286. size = snd_pcm_lib_buffer_bytes(substream);
  287. count = snd_pcm_lib_period_bytes(substream);
  288. if (chip->capture_format & ALS4000_FORMAT_16BIT)
  289. count >>=1;
  290. count--;
  291. spin_lock_irq(&chip->reg_lock);
  292. snd_als4000_set_rate(chip, runtime->rate);
  293. snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
  294. spin_unlock_irq(&chip->reg_lock);
  295. spin_lock_irq(&chip->mixer_lock);
  296. snd_sbmixer_write(chip, 0xdc, count);
  297. snd_sbmixer_write(chip, 0xdd, count>>8);
  298. spin_unlock_irq(&chip->mixer_lock);
  299. return 0;
  300. }
  301. static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
  302. {
  303. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  304. struct snd_pcm_runtime *runtime = substream->runtime;
  305. unsigned long size;
  306. unsigned count;
  307. chip->playback_format = snd_als4000_get_format(runtime);
  308. size = snd_pcm_lib_buffer_bytes(substream);
  309. count = snd_pcm_lib_period_bytes(substream);
  310. if (chip->playback_format & ALS4000_FORMAT_16BIT)
  311. count >>=1;
  312. count--;
  313. /* FIXME: from second playback on, there's a lot more clicks and pops
  314. * involved here than on first playback. Fiddling with
  315. * tons of different settings didn't help (DMA, speaker on/off,
  316. * reordering, ...). Something seems to get enabled on playback
  317. * that I haven't found out how to disable again, which then causes
  318. * the switching pops to reach the speakers the next time here. */
  319. spin_lock_irq(&chip->reg_lock);
  320. snd_als4000_set_rate(chip, runtime->rate);
  321. snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
  322. /* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
  323. /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
  324. snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
  325. snd_sbdsp_command(chip, playback_cmd(chip).format);
  326. snd_sbdsp_command(chip, count);
  327. snd_sbdsp_command(chip, count>>8);
  328. snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
  329. spin_unlock_irq(&chip->reg_lock);
  330. return 0;
  331. }
  332. static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  333. {
  334. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  335. int result = 0;
  336. /* FIXME race condition in here!!!
  337. chip->mode non-atomic update gets consistently protected
  338. by reg_lock always, _except_ for this place!!
  339. Probably need to take reg_lock as outer (or inner??) lock, too.
  340. (or serialize both lock operations? probably not, though... - racy?)
  341. */
  342. spin_lock(&chip->mixer_lock);
  343. switch (cmd) {
  344. case SNDRV_PCM_TRIGGER_START:
  345. case SNDRV_PCM_TRIGGER_RESUME:
  346. chip->mode |= SB_RATE_LOCK_CAPTURE;
  347. snd_sbmixer_write(chip, 0xde, capture_cmd(chip));
  348. break;
  349. case SNDRV_PCM_TRIGGER_STOP:
  350. case SNDRV_PCM_TRIGGER_SUSPEND:
  351. chip->mode &= ~SB_RATE_LOCK_CAPTURE;
  352. snd_sbmixer_write(chip, 0xde, 0);
  353. break;
  354. default:
  355. result = -EINVAL;
  356. break;
  357. }
  358. spin_unlock(&chip->mixer_lock);
  359. return result;
  360. }
  361. static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  362. {
  363. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  364. int result = 0;
  365. spin_lock(&chip->reg_lock);
  366. switch (cmd) {
  367. case SNDRV_PCM_TRIGGER_START:
  368. case SNDRV_PCM_TRIGGER_RESUME:
  369. chip->mode |= SB_RATE_LOCK_PLAYBACK;
  370. snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
  371. break;
  372. case SNDRV_PCM_TRIGGER_STOP:
  373. case SNDRV_PCM_TRIGGER_SUSPEND:
  374. snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
  375. chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
  376. break;
  377. default:
  378. result = -EINVAL;
  379. break;
  380. }
  381. spin_unlock(&chip->reg_lock);
  382. return result;
  383. }
  384. static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
  385. {
  386. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  387. unsigned int result;
  388. spin_lock(&chip->reg_lock);
  389. result = snd_als4000_gcr_read(chip, ALS4K_GCR_A4_FIFO2_CURRENT_ADDR);
  390. result &= 0xffff;
  391. spin_unlock(&chip->reg_lock);
  392. return bytes_to_frames( substream->runtime, result );
  393. }
  394. static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
  395. {
  396. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  397. unsigned result;
  398. spin_lock(&chip->reg_lock);
  399. result = snd_als4000_gcr_read(chip, ALS4K_GCR_A0_FIFO1_CURRENT_ADDR);
  400. result &= 0xffff;
  401. spin_unlock(&chip->reg_lock);
  402. return bytes_to_frames( substream->runtime, result );
  403. }
  404. /* FIXME: this IRQ routine doesn't really support IRQ sharing (we always
  405. * return IRQ_HANDLED no matter whether we actually had an IRQ flag or not).
  406. * ALS4000a.PDF writes that while ACKing IRQ in PCI block will *not* ACK
  407. * the IRQ in the SB core, ACKing IRQ in SB block *will* ACK the PCI IRQ
  408. * register (alt_port + ALS4K_IOB_0E_SB_MPU_IRQ). Probably something
  409. * could be optimized here to query/write one register only...
  410. * And even if both registers need to be queried, then there's still the
  411. * question of whether it's actually correct to ACK PCI IRQ before reading
  412. * SB IRQ like we do now, since ALS4000a.PDF mentions that PCI IRQ will *clear*
  413. * SB IRQ status.
  414. * (hmm, page 38 mentions it the other way around!)
  415. * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
  416. * */
  417. static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id)
  418. {
  419. struct snd_sb *chip = dev_id;
  420. unsigned gcr_status;
  421. unsigned sb_status;
  422. /* find out which bit of the ALS4000 produced the interrupt */
  423. gcr_status = inb(chip->alt_port + ALS4K_IOB_0E_SB_MPU_IRQ);
  424. if ((gcr_status & 0x80) && (chip->playback_substream)) /* playback */
  425. snd_pcm_period_elapsed(chip->playback_substream);
  426. if ((gcr_status & 0x40) && (chip->capture_substream)) /* capturing */
  427. snd_pcm_period_elapsed(chip->capture_substream);
  428. if ((gcr_status & 0x10) && (chip->rmidi)) /* MPU401 interrupt */
  429. snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
  430. /* release the gcr */
  431. outb(gcr_status, chip->alt_port + ALS4K_IOB_0E_SB_MPU_IRQ);
  432. spin_lock(&chip->mixer_lock);
  433. sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
  434. spin_unlock(&chip->mixer_lock);
  435. if (sb_status & SB_IRQTYPE_8BIT)
  436. snd_sb_ack_8bit(chip);
  437. if (sb_status & SB_IRQTYPE_16BIT)
  438. snd_sb_ack_16bit(chip);
  439. if (sb_status & SB_IRQTYPE_MPUIN)
  440. inb(chip->mpu_port);
  441. if (sb_status & 0x20)
  442. inb(SBP(chip, RESET));
  443. return IRQ_HANDLED;
  444. }
  445. /*****************************************************************/
  446. static struct snd_pcm_hardware snd_als4000_playback =
  447. {
  448. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  449. SNDRV_PCM_INFO_MMAP_VALID),
  450. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  451. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
  452. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  453. .rate_min = 4000,
  454. .rate_max = 48000,
  455. .channels_min = 1,
  456. .channels_max = 2,
  457. .buffer_bytes_max = 65536,
  458. .period_bytes_min = 64,
  459. .period_bytes_max = 65536,
  460. .periods_min = 1,
  461. .periods_max = 1024,
  462. .fifo_size = 0
  463. };
  464. static struct snd_pcm_hardware snd_als4000_capture =
  465. {
  466. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  467. SNDRV_PCM_INFO_MMAP_VALID),
  468. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  469. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
  470. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  471. .rate_min = 4000,
  472. .rate_max = 48000,
  473. .channels_min = 1,
  474. .channels_max = 2,
  475. .buffer_bytes_max = 65536,
  476. .period_bytes_min = 64,
  477. .period_bytes_max = 65536,
  478. .periods_min = 1,
  479. .periods_max = 1024,
  480. .fifo_size = 0
  481. };
  482. /*****************************************************************/
  483. static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
  484. {
  485. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  486. struct snd_pcm_runtime *runtime = substream->runtime;
  487. chip->playback_substream = substream;
  488. runtime->hw = snd_als4000_playback;
  489. return 0;
  490. }
  491. static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
  492. {
  493. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  494. chip->playback_substream = NULL;
  495. snd_pcm_lib_free_pages(substream);
  496. return 0;
  497. }
  498. static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
  499. {
  500. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  501. struct snd_pcm_runtime *runtime = substream->runtime;
  502. chip->capture_substream = substream;
  503. runtime->hw = snd_als4000_capture;
  504. return 0;
  505. }
  506. static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
  507. {
  508. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  509. chip->capture_substream = NULL;
  510. snd_pcm_lib_free_pages(substream);
  511. return 0;
  512. }
  513. /******************************************************************/
  514. static struct snd_pcm_ops snd_als4000_playback_ops = {
  515. .open = snd_als4000_playback_open,
  516. .close = snd_als4000_playback_close,
  517. .ioctl = snd_pcm_lib_ioctl,
  518. .hw_params = snd_als4000_hw_params,
  519. .hw_free = snd_als4000_hw_free,
  520. .prepare = snd_als4000_playback_prepare,
  521. .trigger = snd_als4000_playback_trigger,
  522. .pointer = snd_als4000_playback_pointer
  523. };
  524. static struct snd_pcm_ops snd_als4000_capture_ops = {
  525. .open = snd_als4000_capture_open,
  526. .close = snd_als4000_capture_close,
  527. .ioctl = snd_pcm_lib_ioctl,
  528. .hw_params = snd_als4000_hw_params,
  529. .hw_free = snd_als4000_hw_free,
  530. .prepare = snd_als4000_capture_prepare,
  531. .trigger = snd_als4000_capture_trigger,
  532. .pointer = snd_als4000_capture_pointer
  533. };
  534. static int __devinit snd_als4000_pcm(struct snd_sb *chip, int device)
  535. {
  536. struct snd_pcm *pcm;
  537. int err;
  538. if ((err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm)) < 0)
  539. return err;
  540. pcm->private_data = chip;
  541. pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  542. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
  543. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
  544. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  545. 64*1024, 64*1024);
  546. chip->pcm = pcm;
  547. return 0;
  548. }
  549. /******************************************************************/
  550. static void snd_als4000_set_addr(unsigned long iobase,
  551. unsigned int sb_io,
  552. unsigned int mpu_io,
  553. unsigned int opl_io,
  554. unsigned int game_io)
  555. {
  556. u32 cfg1 = 0;
  557. u32 cfg2 = 0;
  558. if (mpu_io > 0)
  559. cfg2 |= (mpu_io | 1) << 16;
  560. if (sb_io > 0)
  561. cfg2 |= (sb_io | 1);
  562. if (game_io > 0)
  563. cfg1 |= (game_io | 1) << 16;
  564. if (opl_io > 0)
  565. cfg1 |= (opl_io | 1);
  566. snd_als4000_gcr_write_addr(iobase, ALS4K_GCR_A8_LEGACY_CFG1, cfg1);
  567. snd_als4000_gcr_write_addr(iobase, ALS4K_GCR_A9_LEGACY_CFG2, cfg2);
  568. }
  569. static void snd_als4000_configure(struct snd_sb *chip)
  570. {
  571. unsigned tmp;
  572. int i;
  573. /* do some more configuration */
  574. spin_lock_irq(&chip->mixer_lock);
  575. tmp = snd_sbmixer_read(chip, 0xc0);
  576. snd_sbmixer_write(chip, 0xc0, tmp|0x80);
  577. /* always select DMA channel 0, since we do not actually use DMA */
  578. snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
  579. snd_sbmixer_write(chip, 0xc0, tmp&0x7f);
  580. spin_unlock_irq(&chip->mixer_lock);
  581. spin_lock_irq(&chip->reg_lock);
  582. /* enable interrupts */
  583. snd_als4000_gcr_write(chip, ALS4K_GCR_8C_MISC_CTRL,
  584. ALS4K_GCR_8C_IRQ_MASK_CTRL_ENABLE);
  585. for (i = ALS4K_GCR_91_DMA0_ADDR; i <= ALS4K_GCR_96_DMA3_MODE_COUNT; ++i)
  586. snd_als4000_gcr_write(chip, i, 0);
  587. snd_als4000_gcr_write(chip, ALS4K_GCR_99_DMA_EMULATION_CTRL,
  588. snd_als4000_gcr_read(chip, ALS4K_GCR_99_DMA_EMULATION_CTRL));
  589. spin_unlock_irq(&chip->reg_lock);
  590. }
  591. #ifdef SUPPORT_JOYSTICK
  592. static int __devinit snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
  593. {
  594. struct gameport *gp;
  595. struct resource *r;
  596. int io_port;
  597. if (joystick_port[dev] == 0)
  598. return -ENODEV;
  599. if (joystick_port[dev] == 1) { /* auto-detect */
  600. for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
  601. r = request_region(io_port, 8, "ALS4000 gameport");
  602. if (r)
  603. break;
  604. }
  605. } else {
  606. io_port = joystick_port[dev];
  607. r = request_region(io_port, 8, "ALS4000 gameport");
  608. }
  609. if (!r) {
  610. printk(KERN_WARNING "als4000: cannot reserve joystick ports\n");
  611. return -EBUSY;
  612. }
  613. acard->gameport = gp = gameport_allocate_port();
  614. if (!gp) {
  615. printk(KERN_ERR "als4000: cannot allocate memory for gameport\n");
  616. release_and_free_resource(r);
  617. return -ENOMEM;
  618. }
  619. gameport_set_name(gp, "ALS4000 Gameport");
  620. gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
  621. gameport_set_dev_parent(gp, &acard->pci->dev);
  622. gp->io = io_port;
  623. gameport_set_port_data(gp, r);
  624. /* Enable legacy joystick port */
  625. snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
  626. gameport_register_port(acard->gameport);
  627. return 0;
  628. }
  629. static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
  630. {
  631. if (acard->gameport) {
  632. struct resource *r = gameport_get_port_data(acard->gameport);
  633. gameport_unregister_port(acard->gameport);
  634. acard->gameport = NULL;
  635. /* disable joystick */
  636. snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
  637. release_and_free_resource(r);
  638. }
  639. }
  640. #else
  641. static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
  642. static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
  643. #endif
  644. static void snd_card_als4000_free( struct snd_card *card )
  645. {
  646. struct snd_card_als4000 *acard = card->private_data;
  647. /* make sure that interrupts are disabled */
  648. snd_als4000_gcr_write_addr(acard->iobase, ALS4K_GCR_8C_MISC_CTRL, 0);
  649. /* free resources */
  650. snd_als4000_free_gameport(acard);
  651. pci_release_regions(acard->pci);
  652. pci_disable_device(acard->pci);
  653. }
  654. static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
  655. const struct pci_device_id *pci_id)
  656. {
  657. static int dev;
  658. struct snd_card *card;
  659. struct snd_card_als4000 *acard;
  660. unsigned long iobase;
  661. struct snd_sb *chip;
  662. struct snd_opl3 *opl3;
  663. unsigned short word;
  664. int err;
  665. if (dev >= SNDRV_CARDS)
  666. return -ENODEV;
  667. if (!enable[dev]) {
  668. dev++;
  669. return -ENOENT;
  670. }
  671. /* enable PCI device */
  672. if ((err = pci_enable_device(pci)) < 0) {
  673. return err;
  674. }
  675. /* check, if we can restrict PCI DMA transfers to 24 bits */
  676. if (pci_set_dma_mask(pci, DMA_24BIT_MASK) < 0 ||
  677. pci_set_consistent_dma_mask(pci, DMA_24BIT_MASK) < 0) {
  678. snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
  679. pci_disable_device(pci);
  680. return -ENXIO;
  681. }
  682. if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
  683. pci_disable_device(pci);
  684. return err;
  685. }
  686. iobase = pci_resource_start(pci, 0);
  687. pci_read_config_word(pci, PCI_COMMAND, &word);
  688. pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
  689. pci_set_master(pci);
  690. card = snd_card_new(index[dev], id[dev], THIS_MODULE,
  691. sizeof( struct snd_card_als4000 ) );
  692. if (card == NULL) {
  693. pci_release_regions(pci);
  694. pci_disable_device(pci);
  695. return -ENOMEM;
  696. }
  697. acard = card->private_data;
  698. acard->pci = pci;
  699. acard->iobase = iobase;
  700. card->private_free = snd_card_als4000_free;
  701. /* disable all legacy ISA stuff */
  702. snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
  703. if ((err = snd_sbdsp_create(card,
  704. iobase + ALS4K_IOB_10_ADLIB_ADDR0,
  705. pci->irq,
  706. snd_als4000_interrupt,
  707. -1,
  708. -1,
  709. SB_HW_ALS4000,
  710. &chip)) < 0) {
  711. goto out_err;
  712. }
  713. acard->chip = chip;
  714. chip->pci = pci;
  715. chip->alt_port = iobase;
  716. snd_card_set_dev(card, &pci->dev);
  717. snd_als4000_configure(chip);
  718. strcpy(card->driver, "ALS4000");
  719. strcpy(card->shortname, "Avance Logic ALS4000");
  720. sprintf(card->longname, "%s at 0x%lx, irq %i",
  721. card->shortname, chip->alt_port, chip->irq);
  722. if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
  723. iobase + ALS4K_IOB_30_MIDI_DATA,
  724. MPU401_INFO_INTEGRATED,
  725. pci->irq, 0, &chip->rmidi)) < 0) {
  726. printk(KERN_ERR "als4000: no MPU-401 device at 0x%lx?\n",
  727. iobase + ALS4K_IOB_30_MIDI_DATA);
  728. goto out_err;
  729. }
  730. /* FIXME: ALS4000 has interesting MPU401 configuration features
  731. * at CR 0x1A (pass-thru / UART switching, fast MIDI clock, etc.),
  732. * however there doesn't seem to be an ALSA API for this... */
  733. if ((err = snd_als4000_pcm(chip, 0)) < 0) {
  734. goto out_err;
  735. }
  736. if ((err = snd_sbmixer_new(chip)) < 0) {
  737. goto out_err;
  738. }
  739. if (snd_opl3_create(card,
  740. iobase + ALS4K_IOB_10_ADLIB_ADDR0,
  741. iobase + ALS4K_IOB_12_ADLIB_ADDR2,
  742. OPL3_HW_AUTO, 1, &opl3) < 0) {
  743. printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx?\n",
  744. iobase + ALS4K_IOB_10_ADLIB_ADDR0,
  745. iobase + ALS4K_IOB_12_ADLIB_ADDR2);
  746. } else {
  747. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  748. goto out_err;
  749. }
  750. }
  751. snd_als4000_create_gameport(acard, dev);
  752. if ((err = snd_card_register(card)) < 0) {
  753. goto out_err;
  754. }
  755. pci_set_drvdata(pci, card);
  756. dev++;
  757. err = 0;
  758. goto out;
  759. out_err:
  760. snd_card_free(card);
  761. out:
  762. return err;
  763. }
  764. static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
  765. {
  766. snd_card_free(pci_get_drvdata(pci));
  767. pci_set_drvdata(pci, NULL);
  768. }
  769. #ifdef CONFIG_PM
  770. static int snd_als4000_suspend(struct pci_dev *pci, pm_message_t state)
  771. {
  772. struct snd_card *card = pci_get_drvdata(pci);
  773. struct snd_card_als4000 *acard = card->private_data;
  774. struct snd_sb *chip = acard->chip;
  775. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  776. snd_pcm_suspend_all(chip->pcm);
  777. snd_sbmixer_suspend(chip);
  778. pci_disable_device(pci);
  779. pci_save_state(pci);
  780. pci_set_power_state(pci, pci_choose_state(pci, state));
  781. return 0;
  782. }
  783. static int snd_als4000_resume(struct pci_dev *pci)
  784. {
  785. struct snd_card *card = pci_get_drvdata(pci);
  786. struct snd_card_als4000 *acard = card->private_data;
  787. struct snd_sb *chip = acard->chip;
  788. pci_set_power_state(pci, PCI_D0);
  789. pci_restore_state(pci);
  790. if (pci_enable_device(pci) < 0) {
  791. printk(KERN_ERR "als4000: pci_enable_device failed, "
  792. "disabling device\n");
  793. snd_card_disconnect(card);
  794. return -EIO;
  795. }
  796. pci_set_master(pci);
  797. snd_als4000_configure(chip);
  798. snd_sbdsp_reset(chip);
  799. snd_sbmixer_resume(chip);
  800. #ifdef SUPPORT_JOYSTICK
  801. if (acard->gameport)
  802. snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
  803. #endif
  804. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  805. return 0;
  806. }
  807. #endif /* CONFIG_PM */
  808. static struct pci_driver driver = {
  809. .name = "ALS4000",
  810. .id_table = snd_als4000_ids,
  811. .probe = snd_card_als4000_probe,
  812. .remove = __devexit_p(snd_card_als4000_remove),
  813. #ifdef CONFIG_PM
  814. .suspend = snd_als4000_suspend,
  815. .resume = snd_als4000_resume,
  816. #endif
  817. };
  818. static int __init alsa_card_als4000_init(void)
  819. {
  820. return pci_register_driver(&driver);
  821. }
  822. static void __exit alsa_card_als4000_exit(void)
  823. {
  824. pci_unregister_driver(&driver);
  825. }
  826. module_init(alsa_card_als4000_init)
  827. module_exit(alsa_card_als4000_exit)