jazz16.c 9.8 KB

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