als4000.c 25 KB

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