es1688.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Driver for generic ESS AudioDrive ESx688 soundcards
  3. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <sound/driver.h>
  22. #include <linux/init.h>
  23. #include <linux/err.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/time.h>
  26. #include <linux/wait.h>
  27. #include <linux/moduleparam.h>
  28. #include <asm/dma.h>
  29. #include <sound/core.h>
  30. #include <sound/es1688.h>
  31. #include <sound/mpu401.h>
  32. #include <sound/opl3.h>
  33. #define SNDRV_LEGACY_FIND_FREE_IRQ
  34. #define SNDRV_LEGACY_FIND_FREE_DMA
  35. #include <sound/initval.h>
  36. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  37. MODULE_DESCRIPTION("ESS ESx688 AudioDrive");
  38. MODULE_LICENSE("GPL");
  39. MODULE_SUPPORTED_DEVICE("{{ESS,ES688 PnP AudioDrive,pnp:ESS0100},"
  40. "{ESS,ES1688 PnP AudioDrive,pnp:ESS0102},"
  41. "{ESS,ES688 AudioDrive,pnp:ESS6881},"
  42. "{ESS,ES1688 AudioDrive,pnp:ESS1681}}");
  43. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  44. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  45. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  46. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */
  47. static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
  48. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
  49. static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
  50. static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
  51. module_param_array(index, int, NULL, 0444);
  52. MODULE_PARM_DESC(index, "Index value for ESx688 soundcard.");
  53. module_param_array(id, charp, NULL, 0444);
  54. MODULE_PARM_DESC(id, "ID string for ESx688 soundcard.");
  55. module_param_array(enable, bool, NULL, 0444);
  56. MODULE_PARM_DESC(enable, "Enable ESx688 soundcard.");
  57. module_param_array(port, long, NULL, 0444);
  58. MODULE_PARM_DESC(port, "Port # for ESx688 driver.");
  59. module_param_array(mpu_port, long, NULL, 0444);
  60. MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ESx688 driver.");
  61. module_param_array(irq, int, NULL, 0444);
  62. MODULE_PARM_DESC(irq, "IRQ # for ESx688 driver.");
  63. module_param_array(mpu_irq, int, NULL, 0444);
  64. MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for ESx688 driver.");
  65. module_param_array(dma8, int, NULL, 0444);
  66. MODULE_PARM_DESC(dma8, "8-bit DMA # for ESx688 driver.");
  67. static struct platform_device *devices[SNDRV_CARDS];
  68. #define PFX "es1688: "
  69. static int __devinit snd_es1688_probe(struct platform_device *pdev)
  70. {
  71. int dev = pdev->id;
  72. static int possible_irqs[] = {5, 9, 10, 7, -1};
  73. static int possible_dmas[] = {1, 3, 0, -1};
  74. int xirq, xdma, xmpu_irq;
  75. struct snd_card *card;
  76. struct snd_es1688 *chip;
  77. struct snd_opl3 *opl3;
  78. struct snd_pcm *pcm;
  79. int err;
  80. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  81. if (card == NULL)
  82. return -ENOMEM;
  83. xirq = irq[dev];
  84. if (xirq == SNDRV_AUTO_IRQ) {
  85. if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  86. snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
  87. err = -EBUSY;
  88. goto _err;
  89. }
  90. }
  91. xmpu_irq = mpu_irq[dev];
  92. xdma = dma8[dev];
  93. if (xdma == SNDRV_AUTO_DMA) {
  94. if ((xdma = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  95. snd_printk(KERN_ERR PFX "unable to find a free DMA\n");
  96. err = -EBUSY;
  97. goto _err;
  98. }
  99. }
  100. if (port[dev] != SNDRV_AUTO_PORT) {
  101. if ((err = snd_es1688_create(card, port[dev], mpu_port[dev],
  102. xirq, xmpu_irq, xdma,
  103. ES1688_HW_AUTO, &chip)) < 0)
  104. goto _err;
  105. } else {
  106. /* auto-probe legacy ports */
  107. static unsigned long possible_ports[] = {
  108. 0x220, 0x240, 0x260,
  109. };
  110. int i;
  111. for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
  112. err = snd_es1688_create(card, possible_ports[i],
  113. mpu_port[dev],
  114. xirq, xmpu_irq, xdma,
  115. ES1688_HW_AUTO, &chip);
  116. if (err >= 0) {
  117. port[dev] = possible_ports[i];
  118. break;
  119. }
  120. }
  121. if (i >= ARRAY_SIZE(possible_ports))
  122. goto _err;
  123. }
  124. if ((err = snd_es1688_pcm(chip, 0, &pcm)) < 0)
  125. goto _err;
  126. if ((err = snd_es1688_mixer(chip)) < 0)
  127. goto _err;
  128. strcpy(card->driver, "ES1688");
  129. strcpy(card->shortname, pcm->name);
  130. sprintf(card->longname, "%s at 0x%lx, irq %i, dma %i", pcm->name, chip->port, xirq, xdma);
  131. if ((snd_opl3_create(card, chip->port, chip->port + 2, OPL3_HW_OPL3, 0, &opl3)) < 0) {
  132. printk(KERN_WARNING PFX "opl3 not detected at 0x%lx\n", chip->port);
  133. } else {
  134. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0)
  135. goto _err;
  136. }
  137. if (xmpu_irq >= 0 && xmpu_irq != SNDRV_AUTO_IRQ && chip->mpu_port > 0) {
  138. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688,
  139. chip->mpu_port, 0,
  140. xmpu_irq,
  141. IRQF_DISABLED,
  142. NULL)) < 0)
  143. goto _err;
  144. }
  145. snd_card_set_dev(card, &pdev->dev);
  146. if ((err = snd_card_register(card)) < 0)
  147. goto _err;
  148. platform_set_drvdata(pdev, card);
  149. return 0;
  150. _err:
  151. snd_card_free(card);
  152. return err;
  153. }
  154. static int __devexit snd_es1688_remove(struct platform_device *devptr)
  155. {
  156. snd_card_free(platform_get_drvdata(devptr));
  157. platform_set_drvdata(devptr, NULL);
  158. return 0;
  159. }
  160. #define ES1688_DRIVER "snd_es1688"
  161. static struct platform_driver snd_es1688_driver = {
  162. .probe = snd_es1688_probe,
  163. .remove = __devexit_p(snd_es1688_remove),
  164. /* FIXME: suspend/resume */
  165. .driver = {
  166. .name = ES1688_DRIVER
  167. },
  168. };
  169. static void __init_or_module snd_es1688_unregister_all(void)
  170. {
  171. int i;
  172. for (i = 0; i < ARRAY_SIZE(devices); ++i)
  173. platform_device_unregister(devices[i]);
  174. platform_driver_unregister(&snd_es1688_driver);
  175. }
  176. static int __init alsa_card_es1688_init(void)
  177. {
  178. int i, cards, err;
  179. err = platform_driver_register(&snd_es1688_driver);
  180. if (err < 0)
  181. return err;
  182. cards = 0;
  183. for (i = 0; i < SNDRV_CARDS; i++) {
  184. struct platform_device *device;
  185. if (! enable[i])
  186. continue;
  187. device = platform_device_register_simple(ES1688_DRIVER,
  188. i, NULL, 0);
  189. if (IS_ERR(device))
  190. continue;
  191. if (!platform_get_drvdata(device)) {
  192. platform_device_unregister(device);
  193. continue;
  194. }
  195. devices[i] = device;
  196. cards++;
  197. }
  198. if (!cards) {
  199. #ifdef MODULE
  200. printk(KERN_ERR "ESS AudioDrive ES1688 soundcard not found or device busy\n");
  201. #endif
  202. snd_es1688_unregister_all();
  203. return -ENODEV;
  204. }
  205. return 0;
  206. }
  207. static void __exit alsa_card_es1688_exit(void)
  208. {
  209. snd_es1688_unregister_all();
  210. }
  211. module_init(alsa_card_es1688_init)
  212. module_exit(alsa_card_es1688_exit)