es1688.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Driver for generic ESS AudioDrive ESx688 soundcards
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/isa.h>
  24. #include <linux/isapnp.h>
  25. #include <linux/time.h>
  26. #include <linux/wait.h>
  27. #include <linux/module.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. #define CRD_NAME "Generic ESS ES1688/ES688 AudioDrive"
  37. #define DEV_NAME "es1688"
  38. MODULE_DESCRIPTION(CRD_NAME);
  39. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  40. MODULE_LICENSE("GPL");
  41. MODULE_SUPPORTED_DEVICE("{{ESS,ES688 PnP AudioDrive,pnp:ESS0100},"
  42. "{ESS,ES1688 PnP AudioDrive,pnp:ESS0102},"
  43. "{ESS,ES688 AudioDrive,pnp:ESS6881},"
  44. "{ESS,ES1688 AudioDrive,pnp:ESS1681}}");
  45. MODULE_ALIAS("snd_es968");
  46. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  47. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  48. #ifdef CONFIG_PNP
  49. static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
  50. #endif
  51. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  52. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */
  53. static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* Usually 0x388 */
  54. static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
  55. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
  56. static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
  57. static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
  58. module_param_array(index, int, NULL, 0444);
  59. MODULE_PARM_DESC(index, "Index value for " CRD_NAME " soundcard.");
  60. module_param_array(id, charp, NULL, 0444);
  61. MODULE_PARM_DESC(id, "ID string for " CRD_NAME " soundcard.");
  62. module_param_array(enable, bool, NULL, 0444);
  63. #ifdef CONFIG_PNP
  64. module_param_array(isapnp, bool, NULL, 0444);
  65. MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
  66. #endif
  67. MODULE_PARM_DESC(enable, "Enable " CRD_NAME " soundcard.");
  68. module_param_array(port, long, NULL, 0444);
  69. MODULE_PARM_DESC(port, "Port # for " CRD_NAME " driver.");
  70. module_param_array(mpu_port, long, NULL, 0444);
  71. MODULE_PARM_DESC(mpu_port, "MPU-401 port # for " CRD_NAME " driver.");
  72. module_param_array(irq, int, NULL, 0444);
  73. module_param_array(fm_port, long, NULL, 0444);
  74. MODULE_PARM_DESC(fm_port, "FM port # for ES1688 driver.");
  75. MODULE_PARM_DESC(irq, "IRQ # for " CRD_NAME " driver.");
  76. module_param_array(mpu_irq, int, NULL, 0444);
  77. MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for " CRD_NAME " driver.");
  78. module_param_array(dma8, int, NULL, 0444);
  79. MODULE_PARM_DESC(dma8, "8-bit DMA # for " CRD_NAME " driver.");
  80. #ifdef CONFIG_PNP
  81. #define is_isapnp_selected(dev) isapnp[dev]
  82. #else
  83. #define is_isapnp_selected(dev) 0
  84. #endif
  85. static int snd_es1688_match(struct device *dev, unsigned int n)
  86. {
  87. return enable[n] && !is_isapnp_selected(n);
  88. }
  89. static int snd_es1688_legacy_create(struct snd_card *card,
  90. struct device *dev, unsigned int n)
  91. {
  92. struct snd_es1688 *chip = card->private_data;
  93. static long possible_ports[] = {0x220, 0x240, 0x260};
  94. static int possible_irqs[] = {5, 9, 10, 7, -1};
  95. static int possible_dmas[] = {1, 3, 0, -1};
  96. int i, error;
  97. if (irq[n] == SNDRV_AUTO_IRQ) {
  98. irq[n] = snd_legacy_find_free_irq(possible_irqs);
  99. if (irq[n] < 0) {
  100. dev_err(dev, "unable to find a free IRQ\n");
  101. return -EBUSY;
  102. }
  103. }
  104. if (dma8[n] == SNDRV_AUTO_DMA) {
  105. dma8[n] = snd_legacy_find_free_dma(possible_dmas);
  106. if (dma8[n] < 0) {
  107. dev_err(dev, "unable to find a free DMA\n");
  108. return -EBUSY;
  109. }
  110. }
  111. if (port[n] != SNDRV_AUTO_PORT)
  112. return snd_es1688_create(card, chip, port[n], mpu_port[n],
  113. irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO);
  114. i = 0;
  115. do {
  116. port[n] = possible_ports[i];
  117. error = snd_es1688_create(card, chip, port[n], mpu_port[n],
  118. irq[n], mpu_irq[n], dma8[n], ES1688_HW_AUTO);
  119. } while (error < 0 && ++i < ARRAY_SIZE(possible_ports));
  120. return error;
  121. }
  122. static int snd_es1688_probe(struct snd_card *card, unsigned int n)
  123. {
  124. struct snd_es1688 *chip = card->private_data;
  125. struct snd_opl3 *opl3;
  126. int error;
  127. error = snd_es1688_pcm(card, chip, 0);
  128. if (error < 0)
  129. return error;
  130. error = snd_es1688_mixer(card, chip);
  131. if (error < 0)
  132. return error;
  133. strlcpy(card->driver, "ES1688", sizeof(card->driver));
  134. strlcpy(card->shortname, chip->pcm->name, sizeof(card->shortname));
  135. snprintf(card->longname, sizeof(card->longname),
  136. "%s at 0x%lx, irq %i, dma %i", chip->pcm->name, chip->port,
  137. chip->irq, chip->dma8);
  138. if (fm_port[n] == SNDRV_AUTO_PORT)
  139. fm_port[n] = port[n]; /* share the same port */
  140. if (fm_port[n] > 0) {
  141. if (snd_opl3_create(card, fm_port[n], fm_port[n] + 2,
  142. OPL3_HW_OPL3, 0, &opl3) < 0)
  143. dev_warn(card->dev,
  144. "opl3 not detected at 0x%lx\n", fm_port[n]);
  145. else {
  146. error = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
  147. if (error < 0)
  148. return error;
  149. }
  150. }
  151. if (mpu_irq[n] >= 0 && mpu_irq[n] != SNDRV_AUTO_IRQ &&
  152. chip->mpu_port > 0) {
  153. error = snd_mpu401_uart_new(card, 0, MPU401_HW_ES1688,
  154. chip->mpu_port, 0,
  155. mpu_irq[n], NULL);
  156. if (error < 0)
  157. return error;
  158. }
  159. return snd_card_register(card);
  160. }
  161. static int snd_es1688_isa_probe(struct device *dev, unsigned int n)
  162. {
  163. struct snd_card *card;
  164. int error;
  165. error = snd_card_new(dev, index[n], id[n], THIS_MODULE,
  166. sizeof(struct snd_es1688), &card);
  167. if (error < 0)
  168. return error;
  169. error = snd_es1688_legacy_create(card, dev, n);
  170. if (error < 0)
  171. goto out;
  172. error = snd_es1688_probe(card, n);
  173. if (error < 0)
  174. goto out;
  175. dev_set_drvdata(dev, card);
  176. return 0;
  177. out:
  178. snd_card_free(card);
  179. return error;
  180. }
  181. static int snd_es1688_isa_remove(struct device *dev, unsigned int n)
  182. {
  183. snd_card_free(dev_get_drvdata(dev));
  184. return 0;
  185. }
  186. static struct isa_driver snd_es1688_driver = {
  187. .match = snd_es1688_match,
  188. .probe = snd_es1688_isa_probe,
  189. .remove = snd_es1688_isa_remove,
  190. #if 0 /* FIXME */
  191. .suspend = snd_es1688_suspend,
  192. .resume = snd_es1688_resume,
  193. #endif
  194. .driver = {
  195. .name = DEV_NAME
  196. }
  197. };
  198. static int snd_es968_pnp_is_probed;
  199. #ifdef CONFIG_PNP
  200. static int snd_card_es968_pnp(struct snd_card *card, unsigned int n,
  201. struct pnp_card_link *pcard,
  202. const struct pnp_card_device_id *pid)
  203. {
  204. struct snd_es1688 *chip = card->private_data;
  205. struct pnp_dev *pdev;
  206. int error;
  207. pdev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
  208. if (pdev == NULL)
  209. return -ENODEV;
  210. error = pnp_activate_dev(pdev);
  211. if (error < 0) {
  212. snd_printk(KERN_ERR "ES968 pnp configure failure\n");
  213. return error;
  214. }
  215. port[n] = pnp_port_start(pdev, 0);
  216. dma8[n] = pnp_dma(pdev, 0);
  217. irq[n] = pnp_irq(pdev, 0);
  218. return snd_es1688_create(card, chip, port[n], mpu_port[n], irq[n],
  219. mpu_irq[n], dma8[n], ES1688_HW_AUTO);
  220. }
  221. static int snd_es968_pnp_detect(struct pnp_card_link *pcard,
  222. const struct pnp_card_device_id *pid)
  223. {
  224. struct snd_card *card;
  225. static unsigned int dev;
  226. int error;
  227. struct snd_es1688 *chip;
  228. if (snd_es968_pnp_is_probed)
  229. return -EBUSY;
  230. for ( ; dev < SNDRV_CARDS; dev++) {
  231. if (enable[dev] && isapnp[dev])
  232. break;
  233. }
  234. if (dev == SNDRV_CARDS)
  235. return -ENODEV;
  236. error = snd_card_new(&pcard->card->dev,
  237. index[dev], id[dev], THIS_MODULE,
  238. sizeof(struct snd_es1688), &card);
  239. if (error < 0)
  240. return error;
  241. chip = card->private_data;
  242. error = snd_card_es968_pnp(card, dev, pcard, pid);
  243. if (error < 0) {
  244. snd_card_free(card);
  245. return error;
  246. }
  247. error = snd_es1688_probe(card, dev);
  248. if (error < 0)
  249. return error;
  250. pnp_set_card_drvdata(pcard, card);
  251. snd_es968_pnp_is_probed = 1;
  252. return 0;
  253. }
  254. static void snd_es968_pnp_remove(struct pnp_card_link *pcard)
  255. {
  256. snd_card_free(pnp_get_card_drvdata(pcard));
  257. pnp_set_card_drvdata(pcard, NULL);
  258. snd_es968_pnp_is_probed = 0;
  259. }
  260. #ifdef CONFIG_PM
  261. static int snd_es968_pnp_suspend(struct pnp_card_link *pcard,
  262. pm_message_t state)
  263. {
  264. struct snd_card *card = pnp_get_card_drvdata(pcard);
  265. struct snd_es1688 *chip = card->private_data;
  266. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  267. snd_pcm_suspend_all(chip->pcm);
  268. return 0;
  269. }
  270. static int snd_es968_pnp_resume(struct pnp_card_link *pcard)
  271. {
  272. struct snd_card *card = pnp_get_card_drvdata(pcard);
  273. struct snd_es1688 *chip = card->private_data;
  274. snd_es1688_reset(chip);
  275. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  276. return 0;
  277. }
  278. #endif
  279. static struct pnp_card_device_id snd_es968_pnpids[] = {
  280. { .id = "ESS0968", .devs = { { "@@@0968" }, } },
  281. { .id = "ESS0968", .devs = { { "ESS0968" }, } },
  282. { .id = "", } /* end */
  283. };
  284. MODULE_DEVICE_TABLE(pnp_card, snd_es968_pnpids);
  285. static struct pnp_card_driver es968_pnpc_driver = {
  286. .flags = PNP_DRIVER_RES_DISABLE,
  287. .name = DEV_NAME " PnP",
  288. .id_table = snd_es968_pnpids,
  289. .probe = snd_es968_pnp_detect,
  290. .remove = snd_es968_pnp_remove,
  291. #ifdef CONFIG_PM
  292. .suspend = snd_es968_pnp_suspend,
  293. .resume = snd_es968_pnp_resume,
  294. #endif
  295. };
  296. #endif
  297. static int __init alsa_card_es1688_init(void)
  298. {
  299. #ifdef CONFIG_PNP
  300. pnp_register_card_driver(&es968_pnpc_driver);
  301. if (snd_es968_pnp_is_probed)
  302. return 0;
  303. pnp_unregister_card_driver(&es968_pnpc_driver);
  304. #endif
  305. return isa_register_driver(&snd_es1688_driver, SNDRV_CARDS);
  306. }
  307. static void __exit alsa_card_es1688_exit(void)
  308. {
  309. if (!snd_es968_pnp_is_probed) {
  310. isa_unregister_driver(&snd_es1688_driver);
  311. return;
  312. }
  313. #ifdef CONFIG_PNP
  314. pnp_unregister_card_driver(&es968_pnpc_driver);
  315. #endif
  316. }
  317. module_init(alsa_card_es1688_init);
  318. module_exit(alsa_card_es1688_exit);