als4000.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. * NOTES
  10. *
  11. * Since Avance does not provide any meaningful documentation, and I
  12. * bought an ALS4000 based soundcard, I was forced to base this driver
  13. * on reverse engineering.
  14. *
  15. * Note: this is no longer true. Pretty verbose chip docu (ALS4000a.PDF)
  16. * can be found on the ALSA web site.
  17. *
  18. * The ALS4000 seems to be the PCI-cousin of the ALS100. It contains an
  19. * ALS100-like SB DSP/mixer, an OPL3 synth, a MPU401 and a gameport
  20. * interface. These subsystems can be mapped into ISA io-port space,
  21. * using the PCI-interface. In addition, the PCI-bit provides DMA and IRQ
  22. * services to the subsystems.
  23. *
  24. * While ALS4000 is very similar to a SoundBlaster, the differences in
  25. * DMA and capturing require more changes to the SoundBlaster than
  26. * desirable, so I made this separate driver.
  27. *
  28. * The ALS4000 can do real full duplex playback/capture.
  29. *
  30. * FMDAC:
  31. * - 0x4f -> port 0x14
  32. * - port 0x15 |= 1
  33. *
  34. * Enable/disable 3D sound:
  35. * - 0x50 -> port 0x14
  36. * - change bit 6 (0x40) of port 0x15
  37. *
  38. * Set QSound:
  39. * - 0xdb -> port 0x14
  40. * - set port 0x15:
  41. * 0x3e (mode 3), 0x3c (mode 2), 0x3a (mode 1), 0x38 (mode 0)
  42. *
  43. * Set KSound:
  44. * - value -> some port 0x0c0d
  45. *
  46. * This program is free software; you can redistribute it and/or modify
  47. * it under the terms of the GNU General Public License as published by
  48. * the Free Software Foundation; either version 2 of the License, or
  49. * (at your option) any later version.
  50. *
  51. * This program is distributed in the hope that it will be useful,
  52. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  53. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  54. * GNU General Public License for more details.
  55. * You should have received a copy of the GNU General Public License
  56. * along with this program; if not, write to the Free Software
  57. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  58. */
  59. #include <sound/driver.h>
  60. #include <asm/io.h>
  61. #include <linux/init.h>
  62. #include <linux/pci.h>
  63. #include <linux/slab.h>
  64. #include <linux/gameport.h>
  65. #include <linux/moduleparam.h>
  66. #include <sound/core.h>
  67. #include <sound/pcm.h>
  68. #include <sound/rawmidi.h>
  69. #include <sound/mpu401.h>
  70. #include <sound/opl3.h>
  71. #include <sound/sb.h>
  72. #include <sound/initval.h>
  73. MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>");
  74. MODULE_DESCRIPTION("Avance Logic ALS4000");
  75. MODULE_LICENSE("GPL");
  76. MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS4000}}");
  77. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  78. #define SUPPORT_JOYSTICK 1
  79. #endif
  80. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  81. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  82. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  83. #ifdef SUPPORT_JOYSTICK
  84. static int joystick_port[SNDRV_CARDS];
  85. #endif
  86. module_param_array(index, int, NULL, 0444);
  87. MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
  88. module_param_array(id, charp, NULL, 0444);
  89. MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
  90. module_param_array(enable, bool, NULL, 0444);
  91. MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
  92. #ifdef SUPPORT_JOYSTICK
  93. module_param_array(joystick_port, int, NULL, 0444);
  94. MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
  95. #endif
  96. typedef struct {
  97. struct pci_dev *pci;
  98. unsigned long gcr;
  99. #ifdef SUPPORT_JOYSTICK
  100. struct gameport *gameport;
  101. #endif
  102. } snd_card_als4000_t;
  103. static struct pci_device_id snd_als4000_ids[] = {
  104. { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* ALS4000 */
  105. { 0, }
  106. };
  107. MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
  108. static inline void snd_als4000_gcr_write_addr(unsigned long port, u32 reg, u32 val)
  109. {
  110. outb(reg, port+0x0c);
  111. outl(val, port+0x08);
  112. }
  113. static inline void snd_als4000_gcr_write(sb_t *sb, u32 reg, u32 val)
  114. {
  115. snd_als4000_gcr_write_addr(sb->alt_port, reg, val);
  116. }
  117. static inline u32 snd_als4000_gcr_read_addr(unsigned long port, u32 reg)
  118. {
  119. outb(reg, port+0x0c);
  120. return inl(port+0x08);
  121. }
  122. static inline u32 snd_als4000_gcr_read(sb_t *sb, u32 reg)
  123. {
  124. return snd_als4000_gcr_read_addr(sb->alt_port, reg);
  125. }
  126. static void snd_als4000_set_rate(sb_t *chip, unsigned int rate)
  127. {
  128. if (!(chip->mode & SB_RATE_LOCK)) {
  129. snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
  130. snd_sbdsp_command(chip, rate>>8);
  131. snd_sbdsp_command(chip, rate);
  132. }
  133. }
  134. static void snd_als4000_set_capture_dma(sb_t *chip, dma_addr_t addr, unsigned size)
  135. {
  136. snd_als4000_gcr_write(chip, 0xa2, addr);
  137. snd_als4000_gcr_write(chip, 0xa3, (size-1));
  138. }
  139. static void snd_als4000_set_playback_dma(sb_t *chip, dma_addr_t addr, unsigned size)
  140. {
  141. snd_als4000_gcr_write(chip, 0x91, addr);
  142. snd_als4000_gcr_write(chip, 0x92, (size-1)|0x180000);
  143. }
  144. #define ALS4000_FORMAT_SIGNED (1<<0)
  145. #define ALS4000_FORMAT_16BIT (1<<1)
  146. #define ALS4000_FORMAT_STEREO (1<<2)
  147. static int snd_als4000_get_format(snd_pcm_runtime_t *runtime)
  148. {
  149. int result;
  150. result = 0;
  151. if (snd_pcm_format_signed(runtime->format))
  152. result |= ALS4000_FORMAT_SIGNED;
  153. if (snd_pcm_format_physical_width(runtime->format) == 16)
  154. result |= ALS4000_FORMAT_16BIT;
  155. if (runtime->channels > 1)
  156. result |= ALS4000_FORMAT_STEREO;
  157. return result;
  158. }
  159. /* structure for setting up playback */
  160. static struct {
  161. unsigned char dsp_cmd, dma_on, dma_off, format;
  162. } playback_cmd_vals[]={
  163. /* ALS4000_FORMAT_U8_MONO */
  164. { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
  165. /* ALS4000_FORMAT_S8_MONO */
  166. { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
  167. /* ALS4000_FORMAT_U16L_MONO */
  168. { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
  169. /* ALS4000_FORMAT_S16L_MONO */
  170. { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
  171. /* ALS4000_FORMAT_U8_STEREO */
  172. { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
  173. /* ALS4000_FORMAT_S8_STEREO */
  174. { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
  175. /* ALS4000_FORMAT_U16L_STEREO */
  176. { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
  177. /* ALS4000_FORMAT_S16L_STEREO */
  178. { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
  179. };
  180. #define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
  181. /* structure for setting up capture */
  182. enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
  183. static unsigned char capture_cmd_vals[]=
  184. {
  185. CMD_WIDTH8|CMD_MONO, /* ALS4000_FORMAT_U8_MONO */
  186. CMD_WIDTH8|CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S8_MONO */
  187. CMD_MONO, /* ALS4000_FORMAT_U16L_MONO */
  188. CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S16L_MONO */
  189. CMD_WIDTH8|CMD_STEREO, /* ALS4000_FORMAT_U8_STEREO */
  190. CMD_WIDTH8|CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S8_STEREO */
  191. CMD_STEREO, /* ALS4000_FORMAT_U16L_STEREO */
  192. CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S16L_STEREO */
  193. };
  194. #define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
  195. static int snd_als4000_hw_params(snd_pcm_substream_t * substream,
  196. snd_pcm_hw_params_t * hw_params)
  197. {
  198. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  199. }
  200. static int snd_als4000_hw_free(snd_pcm_substream_t * substream)
  201. {
  202. snd_pcm_lib_free_pages(substream);
  203. return 0;
  204. }
  205. static int snd_als4000_capture_prepare(snd_pcm_substream_t * substream)
  206. {
  207. unsigned long flags;
  208. sb_t *chip = snd_pcm_substream_chip(substream);
  209. snd_pcm_runtime_t *runtime = substream->runtime;
  210. unsigned long size;
  211. unsigned count;
  212. chip->capture_format = snd_als4000_get_format(runtime);
  213. size = snd_pcm_lib_buffer_bytes(substream);
  214. count = snd_pcm_lib_period_bytes(substream);
  215. if (chip->capture_format & ALS4000_FORMAT_16BIT)
  216. count >>=1;
  217. count--;
  218. spin_lock_irqsave(&chip->reg_lock, flags);
  219. snd_als4000_set_rate(chip, runtime->rate);
  220. snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
  221. spin_unlock_irqrestore(&chip->reg_lock, flags);
  222. spin_lock_irqsave(&chip->mixer_lock, flags );
  223. snd_sbmixer_write(chip, 0xdc, count);
  224. snd_sbmixer_write(chip, 0xdd, count>>8);
  225. spin_unlock_irqrestore(&chip->mixer_lock, flags );
  226. return 0;
  227. }
  228. static int snd_als4000_playback_prepare(snd_pcm_substream_t *substream)
  229. {
  230. unsigned long flags;
  231. sb_t *chip = snd_pcm_substream_chip(substream);
  232. snd_pcm_runtime_t *runtime = substream->runtime;
  233. unsigned long size;
  234. unsigned count;
  235. chip->playback_format = snd_als4000_get_format(runtime);
  236. size = snd_pcm_lib_buffer_bytes(substream);
  237. count = snd_pcm_lib_period_bytes(substream);
  238. if (chip->playback_format & ALS4000_FORMAT_16BIT)
  239. count >>=1;
  240. count--;
  241. /* FIXME: from second playback on, there's a lot more clicks and pops
  242. * involved here than on first playback. Fiddling with
  243. * tons of different settings didn't help (DMA, speaker on/off,
  244. * reordering, ...). Something seems to get enabled on playback
  245. * that I haven't found out how to disable again, which then causes
  246. * the switching pops to reach the speakers the next time here. */
  247. spin_lock_irqsave(&chip->reg_lock, flags);
  248. snd_als4000_set_rate(chip, runtime->rate);
  249. snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
  250. /* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
  251. /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
  252. snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
  253. snd_sbdsp_command(chip, playback_cmd(chip).format);
  254. snd_sbdsp_command(chip, count);
  255. snd_sbdsp_command(chip, count>>8);
  256. snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
  257. spin_unlock_irqrestore(&chip->reg_lock, flags);
  258. return 0;
  259. }
  260. static int snd_als4000_capture_trigger(snd_pcm_substream_t * substream, int cmd)
  261. {
  262. sb_t *chip = snd_pcm_substream_chip(substream);
  263. int result = 0;
  264. spin_lock(&chip->mixer_lock);
  265. if (cmd == SNDRV_PCM_TRIGGER_START) {
  266. chip->mode |= SB_RATE_LOCK_CAPTURE;
  267. snd_sbmixer_write(chip, 0xde, capture_cmd(chip));
  268. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  269. chip->mode &= ~SB_RATE_LOCK_CAPTURE;
  270. snd_sbmixer_write(chip, 0xde, 0);
  271. } else {
  272. result = -EINVAL;
  273. }
  274. spin_unlock(&chip->mixer_lock);
  275. return result;
  276. }
  277. static int snd_als4000_playback_trigger(snd_pcm_substream_t * substream, int cmd)
  278. {
  279. sb_t *chip = snd_pcm_substream_chip(substream);
  280. int result = 0;
  281. spin_lock(&chip->reg_lock);
  282. if (cmd == SNDRV_PCM_TRIGGER_START) {
  283. chip->mode |= SB_RATE_LOCK_PLAYBACK;
  284. snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
  285. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  286. snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
  287. chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
  288. } else {
  289. result = -EINVAL;
  290. }
  291. spin_unlock(&chip->reg_lock);
  292. return result;
  293. }
  294. static snd_pcm_uframes_t snd_als4000_capture_pointer(snd_pcm_substream_t * substream)
  295. {
  296. sb_t *chip = snd_pcm_substream_chip(substream);
  297. unsigned int result;
  298. spin_lock(&chip->reg_lock);
  299. result = snd_als4000_gcr_read(chip, 0xa4) & 0xffff;
  300. spin_unlock(&chip->reg_lock);
  301. return bytes_to_frames( substream->runtime, result );
  302. }
  303. static snd_pcm_uframes_t snd_als4000_playback_pointer(snd_pcm_substream_t * substream)
  304. {
  305. sb_t *chip = snd_pcm_substream_chip(substream);
  306. unsigned result;
  307. spin_lock(&chip->reg_lock);
  308. result = snd_als4000_gcr_read(chip, 0xa0) & 0xffff;
  309. spin_unlock(&chip->reg_lock);
  310. return bytes_to_frames( substream->runtime, result );
  311. }
  312. static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  313. {
  314. sb_t *chip = dev_id;
  315. unsigned gcr_status;
  316. unsigned sb_status;
  317. /* find out which bit of the ALS4000 produced the interrupt */
  318. gcr_status = inb(chip->alt_port + 0xe);
  319. if ((gcr_status & 0x80) && (chip->playback_substream)) /* playback */
  320. snd_pcm_period_elapsed(chip->playback_substream);
  321. if ((gcr_status & 0x40) && (chip->capture_substream)) /* capturing */
  322. snd_pcm_period_elapsed(chip->capture_substream);
  323. if ((gcr_status & 0x10) && (chip->rmidi)) /* MPU401 interrupt */
  324. snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
  325. /* release the gcr */
  326. outb(gcr_status, chip->alt_port + 0xe);
  327. spin_lock(&chip->mixer_lock);
  328. sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
  329. spin_unlock(&chip->mixer_lock);
  330. if (sb_status & SB_IRQTYPE_8BIT)
  331. snd_sb_ack_8bit(chip);
  332. if (sb_status & SB_IRQTYPE_16BIT)
  333. snd_sb_ack_16bit(chip);
  334. if (sb_status & SB_IRQTYPE_MPUIN)
  335. inb(chip->mpu_port);
  336. if (sb_status & 0x20)
  337. inb(SBP(chip, RESET));
  338. return IRQ_HANDLED;
  339. }
  340. /*****************************************************************/
  341. static snd_pcm_hardware_t snd_als4000_playback =
  342. {
  343. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  344. SNDRV_PCM_INFO_MMAP_VALID),
  345. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  346. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
  347. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  348. .rate_min = 4000,
  349. .rate_max = 48000,
  350. .channels_min = 1,
  351. .channels_max = 2,
  352. .buffer_bytes_max = 65536,
  353. .period_bytes_min = 64,
  354. .period_bytes_max = 65536,
  355. .periods_min = 1,
  356. .periods_max = 1024,
  357. .fifo_size = 0
  358. };
  359. static snd_pcm_hardware_t snd_als4000_capture =
  360. {
  361. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  362. SNDRV_PCM_INFO_MMAP_VALID),
  363. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  364. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
  365. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  366. .rate_min = 4000,
  367. .rate_max = 48000,
  368. .channels_min = 1,
  369. .channels_max = 2,
  370. .buffer_bytes_max = 65536,
  371. .period_bytes_min = 64,
  372. .period_bytes_max = 65536,
  373. .periods_min = 1,
  374. .periods_max = 1024,
  375. .fifo_size = 0
  376. };
  377. /*****************************************************************/
  378. static int snd_als4000_playback_open(snd_pcm_substream_t * substream)
  379. {
  380. sb_t *chip = snd_pcm_substream_chip(substream);
  381. snd_pcm_runtime_t *runtime = substream->runtime;
  382. chip->playback_substream = substream;
  383. runtime->hw = snd_als4000_playback;
  384. return 0;
  385. }
  386. static int snd_als4000_playback_close(snd_pcm_substream_t * substream)
  387. {
  388. sb_t *chip = snd_pcm_substream_chip(substream);
  389. chip->playback_substream = NULL;
  390. snd_pcm_lib_free_pages(substream);
  391. return 0;
  392. }
  393. static int snd_als4000_capture_open(snd_pcm_substream_t * substream)
  394. {
  395. sb_t *chip = snd_pcm_substream_chip(substream);
  396. snd_pcm_runtime_t *runtime = substream->runtime;
  397. chip->capture_substream = substream;
  398. runtime->hw = snd_als4000_capture;
  399. return 0;
  400. }
  401. static int snd_als4000_capture_close(snd_pcm_substream_t * substream)
  402. {
  403. sb_t *chip = snd_pcm_substream_chip(substream);
  404. chip->capture_substream = NULL;
  405. snd_pcm_lib_free_pages(substream);
  406. return 0;
  407. }
  408. /******************************************************************/
  409. static snd_pcm_ops_t snd_als4000_playback_ops = {
  410. .open = snd_als4000_playback_open,
  411. .close = snd_als4000_playback_close,
  412. .ioctl = snd_pcm_lib_ioctl,
  413. .hw_params = snd_als4000_hw_params,
  414. .hw_free = snd_als4000_hw_free,
  415. .prepare = snd_als4000_playback_prepare,
  416. .trigger = snd_als4000_playback_trigger,
  417. .pointer = snd_als4000_playback_pointer
  418. };
  419. static snd_pcm_ops_t snd_als4000_capture_ops = {
  420. .open = snd_als4000_capture_open,
  421. .close = snd_als4000_capture_close,
  422. .ioctl = snd_pcm_lib_ioctl,
  423. .hw_params = snd_als4000_hw_params,
  424. .hw_free = snd_als4000_hw_free,
  425. .prepare = snd_als4000_capture_prepare,
  426. .trigger = snd_als4000_capture_trigger,
  427. .pointer = snd_als4000_capture_pointer
  428. };
  429. static void snd_als4000_pcm_free(snd_pcm_t *pcm)
  430. {
  431. sb_t *chip = pcm->private_data;
  432. chip->pcm = NULL;
  433. snd_pcm_lib_preallocate_free_for_all(pcm);
  434. }
  435. static int __devinit snd_als4000_pcm(sb_t *chip, int device)
  436. {
  437. snd_pcm_t *pcm;
  438. int err;
  439. if ((err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm)) < 0)
  440. return err;
  441. pcm->private_free = snd_als4000_pcm_free;
  442. pcm->private_data = chip;
  443. pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  444. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
  445. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
  446. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  447. 64*1024, 64*1024);
  448. chip->pcm = pcm;
  449. return 0;
  450. }
  451. /******************************************************************/
  452. static void snd_als4000_set_addr(unsigned long gcr,
  453. unsigned int sb,
  454. unsigned int mpu,
  455. unsigned int opl,
  456. unsigned int game)
  457. {
  458. u32 confA = 0;
  459. u32 confB = 0;
  460. if (mpu > 0)
  461. confB |= (mpu | 1) << 16;
  462. if (sb > 0)
  463. confB |= (sb | 1);
  464. if (game > 0)
  465. confA |= (game | 1) << 16;
  466. if (opl > 0)
  467. confA |= (opl | 1);
  468. snd_als4000_gcr_write_addr(gcr, 0xa8, confA);
  469. snd_als4000_gcr_write_addr(gcr, 0xa9, confB);
  470. }
  471. static void __devinit snd_als4000_configure(sb_t *chip)
  472. {
  473. unsigned tmp;
  474. int i;
  475. /* do some more configuration */
  476. spin_lock_irq(&chip->mixer_lock);
  477. tmp = snd_sbmixer_read(chip, 0xc0);
  478. snd_sbmixer_write(chip, 0xc0, tmp|0x80);
  479. /* always select DMA channel 0, since we do not actually use DMA */
  480. snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
  481. snd_sbmixer_write(chip, 0xc0, tmp&0x7f);
  482. spin_unlock_irq(&chip->mixer_lock);
  483. spin_lock_irq(&chip->reg_lock);
  484. /* magic number. Enables interrupts(?) */
  485. snd_als4000_gcr_write(chip, 0x8c, 0x28000);
  486. for(i = 0x91; i <= 0x96; ++i)
  487. snd_als4000_gcr_write(chip, i, 0);
  488. snd_als4000_gcr_write(chip, 0x99, snd_als4000_gcr_read(chip, 0x99));
  489. spin_unlock_irq(&chip->reg_lock);
  490. }
  491. #ifdef SUPPORT_JOYSTICK
  492. static int __devinit snd_als4000_create_gameport(snd_card_als4000_t *acard, int dev)
  493. {
  494. struct gameport *gp;
  495. struct resource *r;
  496. int io_port;
  497. if (joystick_port[dev] == 0)
  498. return -ENODEV;
  499. if (joystick_port[dev] == 1) { /* auto-detect */
  500. for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
  501. r = request_region(io_port, 8, "ALS4000 gameport");
  502. if (r)
  503. break;
  504. }
  505. } else {
  506. io_port = joystick_port[dev];
  507. r = request_region(io_port, 8, "ALS4000 gameport");
  508. }
  509. if (!r) {
  510. printk(KERN_WARNING "als4000: cannot reserve joystick ports\n");
  511. return -EBUSY;
  512. }
  513. acard->gameport = gp = gameport_allocate_port();
  514. if (!gp) {
  515. printk(KERN_ERR "als4000: cannot allocate memory for gameport\n");
  516. release_resource(r);
  517. kfree_nocheck(r);
  518. return -ENOMEM;
  519. }
  520. gameport_set_name(gp, "ALS4000 Gameport");
  521. gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
  522. gameport_set_dev_parent(gp, &acard->pci->dev);
  523. gp->io = io_port;
  524. gameport_set_port_data(gp, r);
  525. /* Enable legacy joystick port */
  526. snd_als4000_set_addr(acard->gcr, 0, 0, 0, 1);
  527. gameport_register_port(acard->gameport);
  528. return 0;
  529. }
  530. static void snd_als4000_free_gameport(snd_card_als4000_t *acard)
  531. {
  532. if (acard->gameport) {
  533. struct resource *r = gameport_get_port_data(acard->gameport);
  534. gameport_unregister_port(acard->gameport);
  535. acard->gameport = NULL;
  536. snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0); /* disable joystick */
  537. release_resource(r);
  538. kfree_nocheck(r);
  539. }
  540. }
  541. #else
  542. static inline int snd_als4000_create_gameport(snd_card_als4000_t *acard, int dev) { return -ENOSYS; }
  543. static inline void snd_als4000_free_gameport(snd_card_als4000_t *acard) { }
  544. #endif
  545. static void snd_card_als4000_free( snd_card_t *card )
  546. {
  547. snd_card_als4000_t * acard = (snd_card_als4000_t *)card->private_data;
  548. /* make sure that interrupts are disabled */
  549. snd_als4000_gcr_write_addr( acard->gcr, 0x8c, 0);
  550. /* free resources */
  551. snd_als4000_free_gameport(acard);
  552. pci_release_regions(acard->pci);
  553. pci_disable_device(acard->pci);
  554. }
  555. static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
  556. const struct pci_device_id *pci_id)
  557. {
  558. static int dev;
  559. snd_card_t *card;
  560. snd_card_als4000_t *acard;
  561. unsigned long gcr;
  562. sb_t *chip;
  563. opl3_t *opl3;
  564. unsigned short word;
  565. int err;
  566. if (dev >= SNDRV_CARDS)
  567. return -ENODEV;
  568. if (!enable[dev]) {
  569. dev++;
  570. return -ENOENT;
  571. }
  572. /* enable PCI device */
  573. if ((err = pci_enable_device(pci)) < 0) {
  574. return err;
  575. }
  576. /* check, if we can restrict PCI DMA transfers to 24 bits */
  577. if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
  578. pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
  579. snd_printk("architecture does not support 24bit PCI busmaster DMA\n");
  580. pci_disable_device(pci);
  581. return -ENXIO;
  582. }
  583. if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
  584. pci_disable_device(pci);
  585. return err;
  586. }
  587. gcr = pci_resource_start(pci, 0);
  588. pci_read_config_word(pci, PCI_COMMAND, &word);
  589. pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
  590. pci_set_master(pci);
  591. card = snd_card_new(index[dev], id[dev], THIS_MODULE,
  592. sizeof( snd_card_als4000_t ) );
  593. if (card == NULL) {
  594. pci_release_regions(pci);
  595. pci_disable_device(pci);
  596. return -ENOMEM;
  597. }
  598. acard = (snd_card_als4000_t *)card->private_data;
  599. acard->pci = pci;
  600. acard->gcr = gcr;
  601. card->private_free = snd_card_als4000_free;
  602. /* disable all legacy ISA stuff */
  603. snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0);
  604. if ((err = snd_sbdsp_create(card,
  605. gcr + 0x10,
  606. pci->irq,
  607. snd_als4000_interrupt,
  608. -1,
  609. -1,
  610. SB_HW_ALS4000,
  611. &chip)) < 0) {
  612. snd_card_free(card);
  613. return err;
  614. }
  615. chip->pci = pci;
  616. chip->alt_port = gcr;
  617. snd_card_set_dev(card, &pci->dev);
  618. snd_als4000_configure(chip);
  619. strcpy(card->driver, "ALS4000");
  620. strcpy(card->shortname, "Avance Logic ALS4000");
  621. sprintf(card->longname, "%s at 0x%lx, irq %i",
  622. card->shortname, chip->alt_port, chip->irq);
  623. if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
  624. gcr+0x30, 1, pci->irq, 0,
  625. &chip->rmidi)) < 0) {
  626. snd_card_free(card);
  627. printk(KERN_ERR "als4000: no MPU-401device at 0x%lx ?\n", gcr+0x30);
  628. return err;
  629. }
  630. if ((err = snd_als4000_pcm(chip, 0)) < 0) {
  631. snd_card_free(card);
  632. return err;
  633. }
  634. if ((err = snd_sbmixer_new(chip)) < 0) {
  635. snd_card_free(card);
  636. return err;
  637. }
  638. if (snd_opl3_create(card, gcr+0x10, gcr+0x12,
  639. OPL3_HW_AUTO, 1, &opl3) < 0) {
  640. printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx ?\n",
  641. gcr+0x10, gcr+0x12 );
  642. } else {
  643. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  644. snd_card_free(card);
  645. return err;
  646. }
  647. }
  648. snd_als4000_create_gameport(acard, dev);
  649. if ((err = snd_card_register(card)) < 0) {
  650. snd_card_free(card);
  651. return err;
  652. }
  653. pci_set_drvdata(pci, card);
  654. dev++;
  655. return 0;
  656. }
  657. static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
  658. {
  659. snd_card_free(pci_get_drvdata(pci));
  660. pci_set_drvdata(pci, NULL);
  661. }
  662. static struct pci_driver driver = {
  663. .name = "ALS4000",
  664. .id_table = snd_als4000_ids,
  665. .probe = snd_card_als4000_probe,
  666. .remove = __devexit_p(snd_card_als4000_remove),
  667. };
  668. static int __init alsa_card_als4000_init(void)
  669. {
  670. return pci_register_driver(&driver);
  671. }
  672. static void __exit alsa_card_als4000_exit(void)
  673. {
  674. pci_unregister_driver(&driver);
  675. }
  676. module_init(alsa_card_als4000_init)
  677. module_exit(alsa_card_als4000_exit)