jazz16.c 10 KB

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