jazz16.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * jazz16.c - driver for Media Vision Jazz16 based soundcards.
  3. * Copyright (C) 2009 Krzysztof Helt <krzysztof.h1@wp.pl>
  4. * Based on patches posted by Rask Ingemann Lambertsen and Rene Herman.
  5. * Based on OSS Sound Blaster driver.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive for
  9. * more details.
  10. *
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/io.h>
  15. #include <asm/dma.h>
  16. #include <linux/isa.h>
  17. #include <sound/core.h>
  18. #include <sound/mpu401.h>
  19. #include <sound/opl3.h>
  20. #include <sound/sb.h>
  21. #define SNDRV_LEGACY_FIND_FREE_IRQ
  22. #define SNDRV_LEGACY_FIND_FREE_DMA
  23. #include <sound/initval.h>
  24. #define PFX "jazz16: "
  25. MODULE_DESCRIPTION("Media Vision Jazz16");
  26. MODULE_SUPPORTED_DEVICE("{{Media Vision ??? },"
  27. "{RTL,RTL3000}}");
  28. MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
  29. MODULE_LICENSE("GPL");
  30. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  31. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  32. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  33. static unsigned long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  34. static unsigned long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  35. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  36. static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  37. static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  38. static int dma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  39. module_param_array(index, int, NULL, 0444);
  40. MODULE_PARM_DESC(index, "Index value for Media Vision Jazz16 based soundcard.");
  41. module_param_array(id, charp, NULL, 0444);
  42. MODULE_PARM_DESC(id, "ID string for Media Vision Jazz16 based soundcard.");
  43. module_param_array(enable, bool, NULL, 0444);
  44. MODULE_PARM_DESC(enable, "Enable Media Vision Jazz16 based soundcard.");
  45. module_param_array(port, long, NULL, 0444);
  46. MODULE_PARM_DESC(port, "Port # for jazz16 driver.");
  47. module_param_array(mpu_port, long, NULL, 0444);
  48. MODULE_PARM_DESC(mpu_port, "MPU-401 port # for jazz16 driver.");
  49. module_param_array(irq, int, NULL, 0444);
  50. MODULE_PARM_DESC(irq, "IRQ # for jazz16 driver.");
  51. module_param_array(mpu_irq, int, NULL, 0444);
  52. MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for jazz16 driver.");
  53. module_param_array(dma8, int, NULL, 0444);
  54. MODULE_PARM_DESC(dma8, "DMA8 # for jazz16 driver.");
  55. module_param_array(dma16, int, NULL, 0444);
  56. MODULE_PARM_DESC(dma16, "DMA16 # for jazz16 driver.");
  57. #define SB_JAZZ16_WAKEUP 0xaf
  58. #define SB_JAZZ16_SET_PORTS 0x50
  59. #define SB_DSP_GET_JAZZ_BRD_REV 0xfa
  60. #define SB_JAZZ16_SET_DMAINTR 0xfb
  61. #define SB_DSP_GET_JAZZ_MODEL 0xfe
  62. struct snd_card_jazz16 {
  63. struct snd_sb *chip;
  64. };
  65. static irqreturn_t jazz16_interrupt(int irq, void *chip)
  66. {
  67. return snd_sb8dsp_interrupt(chip);
  68. }
  69. static int __devinit jazz16_configure_ports(unsigned long port,
  70. unsigned long mpu_port, int idx)
  71. {
  72. unsigned char val;
  73. if (!request_region(0x201, 1, "jazz16 config")) {
  74. snd_printk(KERN_ERR "config port region is already in use.\n");
  75. return -EBUSY;
  76. }
  77. outb(SB_JAZZ16_WAKEUP - idx, 0x201);
  78. udelay(100);
  79. outb(SB_JAZZ16_SET_PORTS + idx, 0x201);
  80. udelay(100);
  81. val = port & 0x70;
  82. val |= (mpu_port & 0x30) >> 4;
  83. outb(val, 0x201);
  84. release_region(0x201, 1);
  85. return 0;
  86. }
  87. static int __devinit jazz16_detect_board(unsigned long port,
  88. unsigned long mpu_port)
  89. {
  90. int err;
  91. int val;
  92. struct snd_sb chip;
  93. if (!request_region(port, 0x10, "jazz16")) {
  94. snd_printk(KERN_ERR "I/O port region is already in use.\n");
  95. return -EBUSY;
  96. }
  97. /* just to call snd_sbdsp_command/reset/get_byte() */
  98. chip.port = port;
  99. err = snd_sbdsp_reset(&chip);
  100. if (err < 0)
  101. for (val = 0; val < 4; val++) {
  102. err = jazz16_configure_ports(port, mpu_port, val);
  103. if (err < 0)
  104. break;
  105. err = snd_sbdsp_reset(&chip);
  106. if (!err)
  107. break;
  108. }
  109. if (err < 0) {
  110. err = -ENODEV;
  111. goto err_unmap;
  112. }
  113. if (!snd_sbdsp_command(&chip, SB_DSP_GET_JAZZ_BRD_REV)) {
  114. err = -EBUSY;
  115. goto err_unmap;
  116. }
  117. val = snd_sbdsp_get_byte(&chip);
  118. if (val >= 0x30)
  119. snd_sbdsp_get_byte(&chip);
  120. if ((val & 0xf0) != 0x10) {
  121. err = -ENODEV;
  122. goto err_unmap;
  123. }
  124. if (!snd_sbdsp_command(&chip, SB_DSP_GET_JAZZ_MODEL)) {
  125. err = -EBUSY;
  126. goto err_unmap;
  127. }
  128. snd_sbdsp_get_byte(&chip);
  129. err = snd_sbdsp_get_byte(&chip);
  130. snd_printd("Media Vision Jazz16 board detected: rev 0x%x, model 0x%x\n",
  131. val, err);
  132. err = 0;
  133. err_unmap:
  134. release_region(port, 0x10);
  135. return err;
  136. }
  137. static int __devinit jazz16_configure_board(struct snd_sb *chip, int mpu_irq)
  138. {
  139. static unsigned char jazz_irq_bits[] = { 0, 0, 2, 3, 0, 1, 0, 4,
  140. 0, 2, 5, 0, 0, 0, 0, 6 };
  141. static unsigned char jazz_dma_bits[] = { 0, 1, 0, 2, 0, 3, 0, 4 };
  142. if (jazz_dma_bits[chip->dma8] == 0 ||
  143. jazz_dma_bits[chip->dma16] == 0 ||
  144. jazz_irq_bits[chip->irq] == 0)
  145. return -EINVAL;
  146. if (!snd_sbdsp_command(chip, SB_JAZZ16_SET_DMAINTR))
  147. return -EBUSY;
  148. if (!snd_sbdsp_command(chip,
  149. jazz_dma_bits[chip->dma8] |
  150. (jazz_dma_bits[chip->dma16] << 4)))
  151. return -EBUSY;
  152. if (!snd_sbdsp_command(chip,
  153. jazz_irq_bits[chip->irq] |
  154. (jazz_irq_bits[mpu_irq] << 4)))
  155. return -EBUSY;
  156. return 0;
  157. }
  158. static int __devinit snd_jazz16_match(struct device *devptr, unsigned int dev)
  159. {
  160. if (!enable[dev])
  161. return 0;
  162. if (port[dev] == SNDRV_AUTO_PORT) {
  163. snd_printk(KERN_ERR "please specify port\n");
  164. return 0;
  165. } else if (port[dev] == 0x200 || (port[dev] & ~0x270)) {
  166. snd_printk(KERN_ERR "incorrect port specified\n");
  167. return 0;
  168. }
  169. if (dma8[dev] != SNDRV_AUTO_DMA &&
  170. dma8[dev] != 1 && dma8[dev] != 3) {
  171. snd_printk(KERN_ERR "dma8 must be 1 or 3\n");
  172. return 0;
  173. }
  174. if (dma16[dev] != SNDRV_AUTO_DMA &&
  175. dma16[dev] != 5 && dma16[dev] != 7) {
  176. snd_printk(KERN_ERR "dma16 must be 5 or 7\n");
  177. return 0;
  178. }
  179. if (mpu_port[dev] != SNDRV_AUTO_PORT &&
  180. (mpu_port[dev] & ~0x030) != 0x300) {
  181. snd_printk(KERN_ERR "incorrect mpu_port specified\n");
  182. return 0;
  183. }
  184. if (mpu_irq[dev] != SNDRV_AUTO_DMA &&
  185. mpu_irq[dev] != 2 && mpu_irq[dev] != 3 &&
  186. mpu_irq[dev] != 5 && mpu_irq[dev] != 7) {
  187. snd_printk(KERN_ERR "mpu_irq must be 2, 3, 5 or 7\n");
  188. return 0;
  189. }
  190. return 1;
  191. }
  192. static int __devinit snd_jazz16_probe(struct device *devptr, unsigned int dev)
  193. {
  194. struct snd_card *card;
  195. struct snd_card_jazz16 *jazz16;
  196. struct snd_sb *chip;
  197. struct snd_opl3 *opl3;
  198. static int possible_irqs[] = {2, 3, 5, 7, 9, 10, 15, -1};
  199. static int possible_dmas8[] = {1, 3, -1};
  200. static int possible_dmas16[] = {5, 7, -1};
  201. int err, xirq, xdma8, xdma16, xmpu_port, xmpu_irq;
  202. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  203. sizeof(struct snd_card_jazz16), &card);
  204. if (err < 0)
  205. return err;
  206. jazz16 = card->private_data;
  207. xirq = irq[dev];
  208. if (xirq == SNDRV_AUTO_IRQ) {
  209. xirq = snd_legacy_find_free_irq(possible_irqs);
  210. if (xirq < 0) {
  211. snd_printk(KERN_ERR "unable to find a free IRQ\n");
  212. err = -EBUSY;
  213. goto err_free;
  214. }
  215. }
  216. xdma8 = dma8[dev];
  217. if (xdma8 == SNDRV_AUTO_DMA) {
  218. xdma8 = snd_legacy_find_free_dma(possible_dmas8);
  219. if (xdma8 < 0) {
  220. snd_printk(KERN_ERR "unable to find a free DMA8\n");
  221. err = -EBUSY;
  222. goto err_free;
  223. }
  224. }
  225. xdma16 = dma16[dev];
  226. if (xdma16 == SNDRV_AUTO_DMA) {
  227. xdma16 = snd_legacy_find_free_dma(possible_dmas16);
  228. if (xdma16 < 0) {
  229. snd_printk(KERN_ERR "unable to find a free DMA16\n");
  230. err = -EBUSY;
  231. goto err_free;
  232. }
  233. }
  234. xmpu_port = mpu_port[dev];
  235. if (xmpu_port == SNDRV_AUTO_PORT)
  236. xmpu_port = 0;
  237. err = jazz16_detect_board(port[dev], xmpu_port);
  238. if (err < 0) {
  239. printk(KERN_ERR "Media Vision Jazz16 board not detected\n");
  240. goto err_free;
  241. }
  242. err = snd_sbdsp_create(card, port[dev], irq[dev],
  243. jazz16_interrupt,
  244. dma8[dev], dma16[dev],
  245. SB_HW_JAZZ16,
  246. &chip);
  247. if (err < 0)
  248. goto err_free;
  249. xmpu_irq = mpu_irq[dev];
  250. if (xmpu_irq == SNDRV_AUTO_IRQ || mpu_port[dev] == SNDRV_AUTO_PORT)
  251. xmpu_irq = 0;
  252. err = jazz16_configure_board(chip, xmpu_irq);
  253. if (err < 0) {
  254. printk(KERN_ERR "Media Vision Jazz16 configuration failed\n");
  255. goto err_free;
  256. }
  257. jazz16->chip = chip;
  258. strcpy(card->driver, "jazz16");
  259. strcpy(card->shortname, "Media Vision Jazz16");
  260. sprintf(card->longname,
  261. "Media Vision Jazz16 at 0x%lx, irq %d, dma8 %d, dma16 %d",
  262. port[dev], xirq, xdma8, xdma16);
  263. err = snd_sb8dsp_pcm(chip, 0, NULL);
  264. if (err < 0)
  265. goto err_free;
  266. err = snd_sbmixer_new(chip);
  267. if (err < 0)
  268. goto err_free;
  269. err = snd_opl3_create(card, chip->port, chip->port + 2,
  270. OPL3_HW_AUTO, 1, &opl3);
  271. if (err < 0)
  272. snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
  273. chip->port, chip->port + 2);
  274. else {
  275. err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
  276. if (err < 0)
  277. goto err_free;
  278. }
  279. if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
  280. if (mpu_irq[dev] == SNDRV_AUTO_IRQ)
  281. mpu_irq[dev] = -1;
  282. if (snd_mpu401_uart_new(card, 0,
  283. MPU401_HW_MPU401,
  284. mpu_port[dev], 0,
  285. mpu_irq[dev],
  286. mpu_irq[dev] >= 0 ? IRQF_DISABLED : 0,
  287. NULL) < 0)
  288. snd_printk(KERN_ERR "no MPU-401 device at 0x%lx\n",
  289. mpu_port[dev]);
  290. }
  291. snd_card_set_dev(card, devptr);
  292. err = snd_card_register(card);
  293. if (err < 0)
  294. goto err_free;
  295. dev_set_drvdata(devptr, card);
  296. return 0;
  297. err_free:
  298. snd_card_free(card);
  299. return err;
  300. }
  301. static int __devexit snd_jazz16_remove(struct device *devptr, unsigned int dev)
  302. {
  303. struct snd_card *card = dev_get_drvdata(devptr);
  304. dev_set_drvdata(devptr, NULL);
  305. snd_card_free(card);
  306. return 0;
  307. }
  308. #ifdef CONFIG_PM
  309. static int snd_jazz16_suspend(struct device *pdev, unsigned int n,
  310. pm_message_t state)
  311. {
  312. struct snd_card *card = dev_get_drvdata(pdev);
  313. struct snd_card_jazz16 *acard = card->private_data;
  314. struct snd_sb *chip = acard->chip;
  315. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  316. snd_pcm_suspend_all(chip->pcm);
  317. snd_sbmixer_suspend(chip);
  318. return 0;
  319. }
  320. static int snd_jazz16_resume(struct device *pdev, unsigned int n)
  321. {
  322. struct snd_card *card = dev_get_drvdata(pdev);
  323. struct snd_card_jazz16 *acard = card->private_data;
  324. struct snd_sb *chip = acard->chip;
  325. snd_sbdsp_reset(chip);
  326. snd_sbmixer_resume(chip);
  327. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  328. return 0;
  329. }
  330. #endif
  331. static struct isa_driver snd_jazz16_driver = {
  332. .match = snd_jazz16_match,
  333. .probe = snd_jazz16_probe,
  334. .remove = __devexit_p(snd_jazz16_remove),
  335. #ifdef CONFIG_PM
  336. .suspend = snd_jazz16_suspend,
  337. .resume = snd_jazz16_resume,
  338. #endif
  339. .driver = {
  340. .name = "jazz16"
  341. },
  342. };
  343. static int __init alsa_card_jazz16_init(void)
  344. {
  345. return isa_register_driver(&snd_jazz16_driver, SNDRV_CARDS);
  346. }
  347. static void __exit alsa_card_jazz16_exit(void)
  348. {
  349. isa_unregister_driver(&snd_jazz16_driver);
  350. }
  351. module_init(alsa_card_jazz16_init)
  352. module_exit(alsa_card_jazz16_exit)