als4000.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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@suse.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. * - power management? (card can do voice wakeup according to datasheet!!)
  63. */
  64. #include <sound/driver.h>
  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 <sound/core.h>
  72. #include <sound/pcm.h>
  73. #include <sound/rawmidi.h>
  74. #include <sound/mpu401.h>
  75. #include <sound/opl3.h>
  76. #include <sound/sb.h>
  77. #include <sound/initval.h>
  78. MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>");
  79. MODULE_DESCRIPTION("Avance Logic ALS4000");
  80. MODULE_LICENSE("GPL");
  81. MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS4000}}");
  82. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  83. #define SUPPORT_JOYSTICK 1
  84. #endif
  85. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  86. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  87. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  88. #ifdef SUPPORT_JOYSTICK
  89. static int joystick_port[SNDRV_CARDS];
  90. #endif
  91. module_param_array(index, int, NULL, 0444);
  92. MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
  93. module_param_array(id, charp, NULL, 0444);
  94. MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
  95. module_param_array(enable, bool, NULL, 0444);
  96. MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
  97. #ifdef SUPPORT_JOYSTICK
  98. module_param_array(joystick_port, int, NULL, 0444);
  99. MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
  100. #endif
  101. struct snd_card_als4000 {
  102. /* most frequent access first */
  103. unsigned long gcr;
  104. struct pci_dev *pci;
  105. struct snd_sb *chip;
  106. #ifdef SUPPORT_JOYSTICK
  107. struct gameport *gameport;
  108. #endif
  109. };
  110. static struct pci_device_id snd_als4000_ids[] = {
  111. { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* ALS4000 */
  112. { 0, }
  113. };
  114. MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
  115. static inline void snd_als4000_gcr_write_addr(unsigned long port, u32 reg, u32 val)
  116. {
  117. outb(reg, port+0x0c);
  118. outl(val, port+0x08);
  119. }
  120. static inline void snd_als4000_gcr_write(struct snd_sb *sb, u32 reg, u32 val)
  121. {
  122. snd_als4000_gcr_write_addr(sb->alt_port, reg, val);
  123. }
  124. static inline u32 snd_als4000_gcr_read_addr(unsigned long port, u32 reg)
  125. {
  126. outb(reg, port+0x0c);
  127. return inl(port+0x08);
  128. }
  129. static inline u32 snd_als4000_gcr_read(struct snd_sb *sb, u32 reg)
  130. {
  131. return snd_als4000_gcr_read_addr(sb->alt_port, reg);
  132. }
  133. static void snd_als4000_set_rate(struct snd_sb *chip, unsigned int rate)
  134. {
  135. if (!(chip->mode & SB_RATE_LOCK)) {
  136. snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
  137. snd_sbdsp_command(chip, rate>>8);
  138. snd_sbdsp_command(chip, rate);
  139. }
  140. }
  141. static inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
  142. dma_addr_t addr, unsigned size)
  143. {
  144. snd_als4000_gcr_write(chip, 0xa2, addr);
  145. snd_als4000_gcr_write(chip, 0xa3, (size-1));
  146. }
  147. static inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
  148. dma_addr_t addr, unsigned size)
  149. {
  150. snd_als4000_gcr_write(chip, 0x91, addr);
  151. snd_als4000_gcr_write(chip, 0x92, (size-1)|0x180000);
  152. }
  153. #define ALS4000_FORMAT_SIGNED (1<<0)
  154. #define ALS4000_FORMAT_16BIT (1<<1)
  155. #define ALS4000_FORMAT_STEREO (1<<2)
  156. static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
  157. {
  158. int result;
  159. result = 0;
  160. if (snd_pcm_format_signed(runtime->format))
  161. result |= ALS4000_FORMAT_SIGNED;
  162. if (snd_pcm_format_physical_width(runtime->format) == 16)
  163. result |= ALS4000_FORMAT_16BIT;
  164. if (runtime->channels > 1)
  165. result |= ALS4000_FORMAT_STEREO;
  166. return result;
  167. }
  168. /* structure for setting up playback */
  169. static const struct {
  170. unsigned char dsp_cmd, dma_on, dma_off, format;
  171. } playback_cmd_vals[]={
  172. /* ALS4000_FORMAT_U8_MONO */
  173. { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
  174. /* ALS4000_FORMAT_S8_MONO */
  175. { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
  176. /* ALS4000_FORMAT_U16L_MONO */
  177. { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
  178. /* ALS4000_FORMAT_S16L_MONO */
  179. { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
  180. /* ALS4000_FORMAT_U8_STEREO */
  181. { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
  182. /* ALS4000_FORMAT_S8_STEREO */
  183. { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
  184. /* ALS4000_FORMAT_U16L_STEREO */
  185. { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
  186. /* ALS4000_FORMAT_S16L_STEREO */
  187. { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
  188. };
  189. #define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
  190. /* structure for setting up capture */
  191. enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
  192. static const unsigned char capture_cmd_vals[]=
  193. {
  194. CMD_WIDTH8|CMD_MONO, /* ALS4000_FORMAT_U8_MONO */
  195. CMD_WIDTH8|CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S8_MONO */
  196. CMD_MONO, /* ALS4000_FORMAT_U16L_MONO */
  197. CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S16L_MONO */
  198. CMD_WIDTH8|CMD_STEREO, /* ALS4000_FORMAT_U8_STEREO */
  199. CMD_WIDTH8|CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S8_STEREO */
  200. CMD_STEREO, /* ALS4000_FORMAT_U16L_STEREO */
  201. CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S16L_STEREO */
  202. };
  203. #define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
  204. static int snd_als4000_hw_params(struct snd_pcm_substream *substream,
  205. struct snd_pcm_hw_params *hw_params)
  206. {
  207. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  208. }
  209. static int snd_als4000_hw_free(struct snd_pcm_substream *substream)
  210. {
  211. snd_pcm_lib_free_pages(substream);
  212. return 0;
  213. }
  214. static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
  215. {
  216. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  217. struct snd_pcm_runtime *runtime = substream->runtime;
  218. unsigned long size;
  219. unsigned count;
  220. chip->capture_format = snd_als4000_get_format(runtime);
  221. size = snd_pcm_lib_buffer_bytes(substream);
  222. count = snd_pcm_lib_period_bytes(substream);
  223. if (chip->capture_format & ALS4000_FORMAT_16BIT)
  224. count >>=1;
  225. count--;
  226. spin_lock_irq(&chip->reg_lock);
  227. snd_als4000_set_rate(chip, runtime->rate);
  228. snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
  229. spin_unlock_irq(&chip->reg_lock);
  230. spin_lock_irq(&chip->mixer_lock);
  231. snd_sbmixer_write(chip, 0xdc, count);
  232. snd_sbmixer_write(chip, 0xdd, count>>8);
  233. spin_unlock_irq(&chip->mixer_lock);
  234. return 0;
  235. }
  236. static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
  237. {
  238. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  239. struct snd_pcm_runtime *runtime = substream->runtime;
  240. unsigned long size;
  241. unsigned count;
  242. chip->playback_format = snd_als4000_get_format(runtime);
  243. size = snd_pcm_lib_buffer_bytes(substream);
  244. count = snd_pcm_lib_period_bytes(substream);
  245. if (chip->playback_format & ALS4000_FORMAT_16BIT)
  246. count >>=1;
  247. count--;
  248. /* FIXME: from second playback on, there's a lot more clicks and pops
  249. * involved here than on first playback. Fiddling with
  250. * tons of different settings didn't help (DMA, speaker on/off,
  251. * reordering, ...). Something seems to get enabled on playback
  252. * that I haven't found out how to disable again, which then causes
  253. * the switching pops to reach the speakers the next time here. */
  254. spin_lock_irq(&chip->reg_lock);
  255. snd_als4000_set_rate(chip, runtime->rate);
  256. snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
  257. /* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
  258. /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
  259. snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
  260. snd_sbdsp_command(chip, playback_cmd(chip).format);
  261. snd_sbdsp_command(chip, count);
  262. snd_sbdsp_command(chip, count>>8);
  263. snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
  264. spin_unlock_irq(&chip->reg_lock);
  265. return 0;
  266. }
  267. static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  268. {
  269. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  270. int result = 0;
  271. spin_lock(&chip->mixer_lock);
  272. switch (cmd) {
  273. case SNDRV_PCM_TRIGGER_START:
  274. case SNDRV_PCM_TRIGGER_RESUME:
  275. chip->mode |= SB_RATE_LOCK_CAPTURE;
  276. snd_sbmixer_write(chip, 0xde, capture_cmd(chip));
  277. break;
  278. case SNDRV_PCM_TRIGGER_STOP:
  279. case SNDRV_PCM_TRIGGER_SUSPEND:
  280. chip->mode &= ~SB_RATE_LOCK_CAPTURE;
  281. snd_sbmixer_write(chip, 0xde, 0);
  282. break;
  283. default:
  284. result = -EINVAL;
  285. break;
  286. }
  287. spin_unlock(&chip->mixer_lock);
  288. return result;
  289. }
  290. static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  291. {
  292. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  293. int result = 0;
  294. spin_lock(&chip->reg_lock);
  295. switch (cmd) {
  296. case SNDRV_PCM_TRIGGER_START:
  297. case SNDRV_PCM_TRIGGER_RESUME:
  298. chip->mode |= SB_RATE_LOCK_PLAYBACK;
  299. snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
  300. break;
  301. case SNDRV_PCM_TRIGGER_STOP:
  302. case SNDRV_PCM_TRIGGER_SUSPEND:
  303. snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
  304. chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
  305. break;
  306. default:
  307. result = -EINVAL;
  308. break;
  309. }
  310. spin_unlock(&chip->reg_lock);
  311. return result;
  312. }
  313. static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
  314. {
  315. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  316. unsigned int result;
  317. spin_lock(&chip->reg_lock);
  318. result = snd_als4000_gcr_read(chip, 0xa4) & 0xffff;
  319. spin_unlock(&chip->reg_lock);
  320. return bytes_to_frames( substream->runtime, result );
  321. }
  322. static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
  323. {
  324. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  325. unsigned result;
  326. spin_lock(&chip->reg_lock);
  327. result = snd_als4000_gcr_read(chip, 0xa0) & 0xffff;
  328. spin_unlock(&chip->reg_lock);
  329. return bytes_to_frames( substream->runtime, result );
  330. }
  331. /* FIXME: this IRQ routine doesn't really support IRQ sharing (we always
  332. * return IRQ_HANDLED no matter whether we actually had an IRQ flag or not).
  333. * ALS4000a.PDF writes that while ACKing IRQ in PCI block will *not* ACK
  334. * the IRQ in the SB core, ACKing IRQ in SB block *will* ACK the PCI IRQ
  335. * register (alt_port + 0x0e). Probably something could be optimized here to
  336. * query/write one register only...
  337. * And even if both registers need to be queried, then there's still the
  338. * question of whether it's actually correct to ACK PCI IRQ before reading
  339. * SB IRQ like we do now, since ALS4000a.PDF mentions that PCI IRQ will *clear*
  340. * SB IRQ status.
  341. * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
  342. * */
  343. static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  344. {
  345. struct snd_sb *chip = dev_id;
  346. unsigned gcr_status;
  347. unsigned sb_status;
  348. /* find out which bit of the ALS4000 produced the interrupt */
  349. gcr_status = inb(chip->alt_port + 0xe);
  350. if ((gcr_status & 0x80) && (chip->playback_substream)) /* playback */
  351. snd_pcm_period_elapsed(chip->playback_substream);
  352. if ((gcr_status & 0x40) && (chip->capture_substream)) /* capturing */
  353. snd_pcm_period_elapsed(chip->capture_substream);
  354. if ((gcr_status & 0x10) && (chip->rmidi)) /* MPU401 interrupt */
  355. snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
  356. /* release the gcr */
  357. outb(gcr_status, chip->alt_port + 0xe);
  358. spin_lock(&chip->mixer_lock);
  359. sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
  360. spin_unlock(&chip->mixer_lock);
  361. if (sb_status & SB_IRQTYPE_8BIT)
  362. snd_sb_ack_8bit(chip);
  363. if (sb_status & SB_IRQTYPE_16BIT)
  364. snd_sb_ack_16bit(chip);
  365. if (sb_status & SB_IRQTYPE_MPUIN)
  366. inb(chip->mpu_port);
  367. if (sb_status & 0x20)
  368. inb(SBP(chip, RESET));
  369. return IRQ_HANDLED;
  370. }
  371. /*****************************************************************/
  372. static struct snd_pcm_hardware snd_als4000_playback =
  373. {
  374. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  375. SNDRV_PCM_INFO_MMAP_VALID),
  376. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  377. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
  378. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  379. .rate_min = 4000,
  380. .rate_max = 48000,
  381. .channels_min = 1,
  382. .channels_max = 2,
  383. .buffer_bytes_max = 65536,
  384. .period_bytes_min = 64,
  385. .period_bytes_max = 65536,
  386. .periods_min = 1,
  387. .periods_max = 1024,
  388. .fifo_size = 0
  389. };
  390. static struct snd_pcm_hardware snd_als4000_capture =
  391. {
  392. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  393. SNDRV_PCM_INFO_MMAP_VALID),
  394. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  395. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
  396. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  397. .rate_min = 4000,
  398. .rate_max = 48000,
  399. .channels_min = 1,
  400. .channels_max = 2,
  401. .buffer_bytes_max = 65536,
  402. .period_bytes_min = 64,
  403. .period_bytes_max = 65536,
  404. .periods_min = 1,
  405. .periods_max = 1024,
  406. .fifo_size = 0
  407. };
  408. /*****************************************************************/
  409. static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
  410. {
  411. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  412. struct snd_pcm_runtime *runtime = substream->runtime;
  413. chip->playback_substream = substream;
  414. runtime->hw = snd_als4000_playback;
  415. return 0;
  416. }
  417. static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
  418. {
  419. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  420. chip->playback_substream = NULL;
  421. snd_pcm_lib_free_pages(substream);
  422. return 0;
  423. }
  424. static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
  425. {
  426. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  427. struct snd_pcm_runtime *runtime = substream->runtime;
  428. chip->capture_substream = substream;
  429. runtime->hw = snd_als4000_capture;
  430. return 0;
  431. }
  432. static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
  433. {
  434. struct snd_sb *chip = snd_pcm_substream_chip(substream);
  435. chip->capture_substream = NULL;
  436. snd_pcm_lib_free_pages(substream);
  437. return 0;
  438. }
  439. /******************************************************************/
  440. static struct snd_pcm_ops snd_als4000_playback_ops = {
  441. .open = snd_als4000_playback_open,
  442. .close = snd_als4000_playback_close,
  443. .ioctl = snd_pcm_lib_ioctl,
  444. .hw_params = snd_als4000_hw_params,
  445. .hw_free = snd_als4000_hw_free,
  446. .prepare = snd_als4000_playback_prepare,
  447. .trigger = snd_als4000_playback_trigger,
  448. .pointer = snd_als4000_playback_pointer
  449. };
  450. static struct snd_pcm_ops snd_als4000_capture_ops = {
  451. .open = snd_als4000_capture_open,
  452. .close = snd_als4000_capture_close,
  453. .ioctl = snd_pcm_lib_ioctl,
  454. .hw_params = snd_als4000_hw_params,
  455. .hw_free = snd_als4000_hw_free,
  456. .prepare = snd_als4000_capture_prepare,
  457. .trigger = snd_als4000_capture_trigger,
  458. .pointer = snd_als4000_capture_pointer
  459. };
  460. static int __devinit snd_als4000_pcm(struct snd_sb *chip, int device)
  461. {
  462. struct snd_pcm *pcm;
  463. int err;
  464. if ((err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm)) < 0)
  465. return err;
  466. pcm->private_data = chip;
  467. pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  468. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
  469. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
  470. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  471. 64*1024, 64*1024);
  472. chip->pcm = pcm;
  473. return 0;
  474. }
  475. /******************************************************************/
  476. static void snd_als4000_set_addr(unsigned long gcr,
  477. unsigned int sb,
  478. unsigned int mpu,
  479. unsigned int opl,
  480. unsigned int game)
  481. {
  482. u32 confA = 0;
  483. u32 confB = 0;
  484. if (mpu > 0)
  485. confB |= (mpu | 1) << 16;
  486. if (sb > 0)
  487. confB |= (sb | 1);
  488. if (game > 0)
  489. confA |= (game | 1) << 16;
  490. if (opl > 0)
  491. confA |= (opl | 1);
  492. snd_als4000_gcr_write_addr(gcr, 0xa8, confA);
  493. snd_als4000_gcr_write_addr(gcr, 0xa9, confB);
  494. }
  495. static void snd_als4000_configure(struct snd_sb *chip)
  496. {
  497. unsigned tmp;
  498. int i;
  499. /* do some more configuration */
  500. spin_lock_irq(&chip->mixer_lock);
  501. tmp = snd_sbmixer_read(chip, 0xc0);
  502. snd_sbmixer_write(chip, 0xc0, tmp|0x80);
  503. /* always select DMA channel 0, since we do not actually use DMA */
  504. snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
  505. snd_sbmixer_write(chip, 0xc0, tmp&0x7f);
  506. spin_unlock_irq(&chip->mixer_lock);
  507. spin_lock_irq(&chip->reg_lock);
  508. /* magic number. Enables interrupts(?) */
  509. snd_als4000_gcr_write(chip, 0x8c, 0x28000);
  510. for(i = 0x91; i <= 0x96; ++i)
  511. snd_als4000_gcr_write(chip, i, 0);
  512. snd_als4000_gcr_write(chip, 0x99, snd_als4000_gcr_read(chip, 0x99));
  513. spin_unlock_irq(&chip->reg_lock);
  514. }
  515. #ifdef SUPPORT_JOYSTICK
  516. static int __devinit snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
  517. {
  518. struct gameport *gp;
  519. struct resource *r;
  520. int io_port;
  521. if (joystick_port[dev] == 0)
  522. return -ENODEV;
  523. if (joystick_port[dev] == 1) { /* auto-detect */
  524. for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
  525. r = request_region(io_port, 8, "ALS4000 gameport");
  526. if (r)
  527. break;
  528. }
  529. } else {
  530. io_port = joystick_port[dev];
  531. r = request_region(io_port, 8, "ALS4000 gameport");
  532. }
  533. if (!r) {
  534. printk(KERN_WARNING "als4000: cannot reserve joystick ports\n");
  535. return -EBUSY;
  536. }
  537. acard->gameport = gp = gameport_allocate_port();
  538. if (!gp) {
  539. printk(KERN_ERR "als4000: cannot allocate memory for gameport\n");
  540. release_and_free_resource(r);
  541. return -ENOMEM;
  542. }
  543. gameport_set_name(gp, "ALS4000 Gameport");
  544. gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
  545. gameport_set_dev_parent(gp, &acard->pci->dev);
  546. gp->io = io_port;
  547. gameport_set_port_data(gp, r);
  548. /* Enable legacy joystick port */
  549. snd_als4000_set_addr(acard->gcr, 0, 0, 0, 1);
  550. gameport_register_port(acard->gameport);
  551. return 0;
  552. }
  553. static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
  554. {
  555. if (acard->gameport) {
  556. struct resource *r = gameport_get_port_data(acard->gameport);
  557. gameport_unregister_port(acard->gameport);
  558. acard->gameport = NULL;
  559. snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0); /* disable joystick */
  560. release_and_free_resource(r);
  561. }
  562. }
  563. #else
  564. static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
  565. static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
  566. #endif
  567. static void snd_card_als4000_free( struct snd_card *card )
  568. {
  569. struct snd_card_als4000 * acard = (struct snd_card_als4000 *)card->private_data;
  570. /* make sure that interrupts are disabled */
  571. snd_als4000_gcr_write_addr( acard->gcr, 0x8c, 0);
  572. /* free resources */
  573. snd_als4000_free_gameport(acard);
  574. pci_release_regions(acard->pci);
  575. pci_disable_device(acard->pci);
  576. }
  577. static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
  578. const struct pci_device_id *pci_id)
  579. {
  580. static int dev;
  581. struct snd_card *card;
  582. struct snd_card_als4000 *acard;
  583. unsigned long gcr;
  584. struct snd_sb *chip;
  585. struct snd_opl3 *opl3;
  586. unsigned short word;
  587. int err;
  588. if (dev >= SNDRV_CARDS)
  589. return -ENODEV;
  590. if (!enable[dev]) {
  591. dev++;
  592. return -ENOENT;
  593. }
  594. /* enable PCI device */
  595. if ((err = pci_enable_device(pci)) < 0) {
  596. return err;
  597. }
  598. /* check, if we can restrict PCI DMA transfers to 24 bits */
  599. if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
  600. pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
  601. snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
  602. pci_disable_device(pci);
  603. return -ENXIO;
  604. }
  605. if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
  606. pci_disable_device(pci);
  607. return err;
  608. }
  609. gcr = pci_resource_start(pci, 0);
  610. pci_read_config_word(pci, PCI_COMMAND, &word);
  611. pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
  612. pci_set_master(pci);
  613. card = snd_card_new(index[dev], id[dev], THIS_MODULE,
  614. sizeof( struct snd_card_als4000 ) );
  615. if (card == NULL) {
  616. pci_release_regions(pci);
  617. pci_disable_device(pci);
  618. return -ENOMEM;
  619. }
  620. acard = (struct snd_card_als4000 *)card->private_data;
  621. acard->pci = pci;
  622. acard->gcr = gcr;
  623. card->private_free = snd_card_als4000_free;
  624. /* disable all legacy ISA stuff */
  625. snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0);
  626. if ((err = snd_sbdsp_create(card,
  627. gcr + 0x10,
  628. pci->irq,
  629. snd_als4000_interrupt,
  630. -1,
  631. -1,
  632. SB_HW_ALS4000,
  633. &chip)) < 0) {
  634. goto out_err;
  635. }
  636. acard->chip = chip;
  637. chip->pci = pci;
  638. chip->alt_port = gcr;
  639. snd_card_set_dev(card, &pci->dev);
  640. snd_als4000_configure(chip);
  641. strcpy(card->driver, "ALS4000");
  642. strcpy(card->shortname, "Avance Logic ALS4000");
  643. sprintf(card->longname, "%s at 0x%lx, irq %i",
  644. card->shortname, chip->alt_port, chip->irq);
  645. if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
  646. gcr+0x30, 1, pci->irq, 0,
  647. &chip->rmidi)) < 0) {
  648. printk(KERN_ERR "als4000: no MPU-401 device at 0x%lx?\n", gcr+0x30);
  649. goto out_err;
  650. }
  651. if ((err = snd_als4000_pcm(chip, 0)) < 0) {
  652. goto out_err;
  653. }
  654. if ((err = snd_sbmixer_new(chip)) < 0) {
  655. goto out_err;
  656. }
  657. if (snd_opl3_create(card, gcr+0x10, gcr+0x12,
  658. OPL3_HW_AUTO, 1, &opl3) < 0) {
  659. printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx?\n",
  660. gcr+0x10, gcr+0x12 );
  661. } else {
  662. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  663. goto out_err;
  664. }
  665. }
  666. snd_als4000_create_gameport(acard, dev);
  667. if ((err = snd_card_register(card)) < 0) {
  668. goto out_err;
  669. }
  670. pci_set_drvdata(pci, card);
  671. dev++;
  672. err = 0;
  673. goto out;
  674. out_err:
  675. snd_card_free(card);
  676. out:
  677. return err;
  678. }
  679. static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
  680. {
  681. snd_card_free(pci_get_drvdata(pci));
  682. pci_set_drvdata(pci, NULL);
  683. }
  684. #ifdef CONFIG_PM
  685. static int snd_als4000_suspend(struct pci_dev *pci, pm_message_t state)
  686. {
  687. struct snd_card *card = pci_get_drvdata(pci);
  688. struct snd_card_als4000 *acard = card->private_data;
  689. struct snd_sb *chip = acard->chip;
  690. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  691. snd_pcm_suspend_all(chip->pcm);
  692. snd_sbmixer_suspend(chip);
  693. pci_set_power_state(pci, PCI_D3hot);
  694. pci_disable_device(pci);
  695. pci_save_state(pci);
  696. return 0;
  697. }
  698. static int snd_als4000_resume(struct pci_dev *pci)
  699. {
  700. struct snd_card *card = pci_get_drvdata(pci);
  701. struct snd_card_als4000 *acard = card->private_data;
  702. struct snd_sb *chip = acard->chip;
  703. pci_restore_state(pci);
  704. pci_enable_device(pci);
  705. pci_set_power_state(pci, PCI_D0);
  706. pci_set_master(pci);
  707. snd_als4000_configure(chip);
  708. snd_sbdsp_reset(chip);
  709. snd_sbmixer_resume(chip);
  710. #ifdef SUPPORT_JOYSTICK
  711. if (acard->gameport)
  712. snd_als4000_set_addr(acard->gcr, 0, 0, 0, 1);
  713. #endif
  714. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  715. return 0;
  716. }
  717. #endif
  718. static struct pci_driver driver = {
  719. .name = "ALS4000",
  720. .id_table = snd_als4000_ids,
  721. .probe = snd_card_als4000_probe,
  722. .remove = __devexit_p(snd_card_als4000_remove),
  723. #ifdef CONFIG_PM
  724. .suspend = snd_als4000_suspend,
  725. .resume = snd_als4000_resume,
  726. #endif
  727. };
  728. static int __init alsa_card_als4000_init(void)
  729. {
  730. return pci_register_driver(&driver);
  731. }
  732. static void __exit alsa_card_als4000_exit(void)
  733. {
  734. pci_unregister_driver(&driver);
  735. }
  736. module_init(alsa_card_als4000_init)
  737. module_exit(alsa_card_als4000_exit)