azt2320.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. card-azt2320.c - driver for Aztech Systems AZT2320 based soundcards.
  3. Copyright (C) 1999-2000 by Massimo Piccioni <dafastidio@libero.it>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. /*
  17. This driver should provide support for most Aztech AZT2320 based cards.
  18. Several AZT2316 chips are also supported/tested, but autoprobe doesn't
  19. work: all module option have to be set.
  20. No docs available for us at Aztech headquarters !!! Unbelievable ...
  21. No other help obtained.
  22. Thanks to Rainer Wiesner <rainer.wiesner@01019freenet.de> for the WSS
  23. activation method (full-duplex audio!).
  24. */
  25. #include <sound/driver.h>
  26. #include <asm/io.h>
  27. #include <linux/delay.h>
  28. #include <linux/init.h>
  29. #include <linux/time.h>
  30. #include <linux/wait.h>
  31. #include <linux/pnp.h>
  32. #include <linux/moduleparam.h>
  33. #include <sound/core.h>
  34. #include <sound/initval.h>
  35. #include <sound/cs4231.h>
  36. #include <sound/mpu401.h>
  37. #include <sound/opl3.h>
  38. #define PFX "azt2320: "
  39. MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
  40. MODULE_DESCRIPTION("Aztech Systems AZT2320");
  41. MODULE_LICENSE("GPL");
  42. MODULE_SUPPORTED_DEVICE("{{Aztech Systems,PRO16V},"
  43. "{Aztech Systems,AZT2320},"
  44. "{Aztech Systems,AZT3300},"
  45. "{Aztech Systems,AZT2320},"
  46. "{Aztech Systems,AZT3000}}");
  47. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  48. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  49. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
  50. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */
  51. static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */
  52. static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */
  53. static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */
  54. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* Pnp setup */
  55. static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* Pnp setup */
  56. static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* PnP setup */
  57. static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* PnP setup */
  58. module_param_array(index, int, NULL, 0444);
  59. MODULE_PARM_DESC(index, "Index value for azt2320 based soundcard.");
  60. module_param_array(id, charp, NULL, 0444);
  61. MODULE_PARM_DESC(id, "ID string for azt2320 based soundcard.");
  62. module_param_array(enable, bool, NULL, 0444);
  63. MODULE_PARM_DESC(enable, "Enable azt2320 based soundcard.");
  64. module_param_array(port, long, NULL, 0444);
  65. MODULE_PARM_DESC(port, "Port # for azt2320 driver.");
  66. module_param_array(wss_port, long, NULL, 0444);
  67. MODULE_PARM_DESC(wss_port, "WSS Port # for azt2320 driver.");
  68. module_param_array(mpu_port, long, NULL, 0444);
  69. MODULE_PARM_DESC(mpu_port, "MPU-401 port # for azt2320 driver.");
  70. module_param_array(fm_port, long, NULL, 0444);
  71. MODULE_PARM_DESC(fm_port, "FM port # for azt2320 driver.");
  72. module_param_array(irq, int, NULL, 0444);
  73. MODULE_PARM_DESC(irq, "IRQ # for azt2320 driver.");
  74. module_param_array(mpu_irq, int, NULL, 0444);
  75. MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for azt2320 driver.");
  76. module_param_array(dma1, int, NULL, 0444);
  77. MODULE_PARM_DESC(dma1, "1st DMA # for azt2320 driver.");
  78. module_param_array(dma2, int, NULL, 0444);
  79. MODULE_PARM_DESC(dma2, "2nd DMA # for azt2320 driver.");
  80. struct snd_card_azt2320 {
  81. int dev_no;
  82. struct pnp_dev *dev;
  83. struct pnp_dev *devmpu;
  84. };
  85. static struct pnp_card_device_id snd_azt2320_pnpids[] = {
  86. /* PRO16V */
  87. { .id = "AZT1008", .devs = { { "AZT1008" }, { "AZT2001" }, } },
  88. /* Aztech Sound Galaxy 16 */
  89. { .id = "AZT2320", .devs = { { "AZT0001" }, { "AZT0002" }, } },
  90. /* Packard Bell Sound III 336 AM/SP */
  91. { .id = "AZT3000", .devs = { { "AZT1003" }, { "AZT2001" }, } },
  92. /* AT3300 */
  93. { .id = "AZT3002", .devs = { { "AZT1004" }, { "AZT2001" }, } },
  94. /* --- */
  95. { .id = "AZT3005", .devs = { { "AZT1003" }, { "AZT2001" }, } },
  96. /* --- */
  97. { .id = "AZT3011", .devs = { { "AZT1003" }, { "AZT2001" }, } },
  98. { .id = "" } /* end */
  99. };
  100. MODULE_DEVICE_TABLE(pnp_card, snd_azt2320_pnpids);
  101. #define DRIVER_NAME "snd-card-azt2320"
  102. static int __devinit snd_card_azt2320_pnp(int dev, struct snd_card_azt2320 *acard,
  103. struct pnp_card_link *card,
  104. const struct pnp_card_device_id *id)
  105. {
  106. struct pnp_dev *pdev;
  107. struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
  108. int err;
  109. if (!cfg)
  110. return -ENOMEM;
  111. acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
  112. if (acard->dev == NULL) {
  113. kfree(cfg);
  114. return -ENODEV;
  115. }
  116. acard->devmpu = pnp_request_card_device(card, id->devs[1].id, NULL);
  117. pdev = acard->dev;
  118. pnp_init_resource_table(cfg);
  119. /* override resources */
  120. if (port[dev] != SNDRV_AUTO_PORT)
  121. pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
  122. if (fm_port[dev] != SNDRV_AUTO_PORT)
  123. pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
  124. if (wss_port[dev] != SNDRV_AUTO_PORT)
  125. pnp_resource_change(&cfg->port_resource[2], wss_port[dev], 4);
  126. if (dma1[dev] != SNDRV_AUTO_DMA)
  127. pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
  128. if (dma2[dev] != SNDRV_AUTO_DMA)
  129. pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
  130. if (irq[dev] != SNDRV_AUTO_IRQ)
  131. pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
  132. if ((pnp_manual_config_dev(pdev, cfg, 0)) < 0)
  133. snd_printk(KERN_ERR PFX "AUDIO the requested resources are invalid, using auto config\n");
  134. err = pnp_activate_dev(pdev);
  135. if (err < 0) {
  136. snd_printk(KERN_ERR PFX "AUDIO pnp configure failure\n");
  137. kfree(cfg);
  138. return err;
  139. }
  140. port[dev] = pnp_port_start(pdev, 0);
  141. fm_port[dev] = pnp_port_start(pdev, 1);
  142. wss_port[dev] = pnp_port_start(pdev, 2);
  143. dma1[dev] = pnp_dma(pdev, 0);
  144. dma2[dev] = pnp_dma(pdev, 1);
  145. irq[dev] = pnp_irq(pdev, 0);
  146. pdev = acard->devmpu;
  147. if (pdev != NULL) {
  148. pnp_init_resource_table(cfg);
  149. if (mpu_port[dev] != SNDRV_AUTO_PORT)
  150. pnp_resource_change(&cfg->port_resource[0], mpu_port[dev], 2);
  151. if (mpu_irq[dev] != SNDRV_AUTO_IRQ)
  152. pnp_resource_change(&cfg->irq_resource[0], mpu_irq[dev], 1);
  153. if ((pnp_manual_config_dev(pdev, cfg, 0)) < 0)
  154. snd_printk(KERN_ERR PFX "MPU401 the requested resources are invalid, using auto config\n");
  155. err = pnp_activate_dev(pdev);
  156. if (err < 0)
  157. goto __mpu_error;
  158. mpu_port[dev] = pnp_port_start(pdev, 0);
  159. mpu_irq[dev] = pnp_irq(pdev, 0);
  160. } else {
  161. __mpu_error:
  162. if (pdev) {
  163. pnp_release_card_device(pdev);
  164. snd_printk(KERN_ERR PFX "MPU401 pnp configure failure, skipping\n");
  165. }
  166. acard->devmpu = NULL;
  167. mpu_port[dev] = -1;
  168. }
  169. kfree (cfg);
  170. return 0;
  171. }
  172. /* same of snd_sbdsp_command by Jaroslav Kysela */
  173. static int __devinit snd_card_azt2320_command(unsigned long port, unsigned char val)
  174. {
  175. int i;
  176. unsigned long limit;
  177. limit = jiffies + HZ / 10;
  178. for (i = 50000; i && time_after(limit, jiffies); i--)
  179. if (!(inb(port + 0x0c) & 0x80)) {
  180. outb(val, port + 0x0c);
  181. return 0;
  182. }
  183. return -EBUSY;
  184. }
  185. static int __devinit snd_card_azt2320_enable_wss(unsigned long port)
  186. {
  187. int error;
  188. if ((error = snd_card_azt2320_command(port, 0x09)))
  189. return error;
  190. if ((error = snd_card_azt2320_command(port, 0x00)))
  191. return error;
  192. mdelay(5);
  193. return 0;
  194. }
  195. static int __devinit snd_card_azt2320_probe(int dev,
  196. struct pnp_card_link *pcard,
  197. const struct pnp_card_device_id *pid)
  198. {
  199. int error;
  200. snd_card_t *card;
  201. struct snd_card_azt2320 *acard;
  202. cs4231_t *chip;
  203. opl3_t *opl3;
  204. if ((card = snd_card_new(index[dev], id[dev], THIS_MODULE,
  205. sizeof(struct snd_card_azt2320))) == NULL)
  206. return -ENOMEM;
  207. acard = (struct snd_card_azt2320 *)card->private_data;
  208. if ((error = snd_card_azt2320_pnp(dev, acard, pcard, pid))) {
  209. snd_card_free(card);
  210. return error;
  211. }
  212. snd_card_set_dev(card, &pcard->card->dev);
  213. if ((error = snd_card_azt2320_enable_wss(port[dev]))) {
  214. snd_card_free(card);
  215. return error;
  216. }
  217. if ((error = snd_cs4231_create(card, wss_port[dev], -1,
  218. irq[dev],
  219. dma1[dev],
  220. dma2[dev],
  221. CS4231_HW_DETECT, 0, &chip)) < 0) {
  222. snd_card_free(card);
  223. return error;
  224. }
  225. strcpy(card->driver, "AZT2320");
  226. strcpy(card->shortname, "Aztech AZT2320");
  227. sprintf(card->longname, "%s, WSS at 0x%lx, irq %i, dma %i&%i",
  228. card->shortname, chip->port, irq[dev], dma1[dev], dma2[dev]);
  229. if ((error = snd_cs4231_pcm(chip, 0, NULL)) < 0) {
  230. snd_card_free(card);
  231. return error;
  232. }
  233. if ((error = snd_cs4231_mixer(chip)) < 0) {
  234. snd_card_free(card);
  235. return error;
  236. }
  237. if ((error = snd_cs4231_timer(chip, 0, NULL)) < 0) {
  238. snd_card_free(card);
  239. return error;
  240. }
  241. if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
  242. if (snd_mpu401_uart_new(card, 0, MPU401_HW_AZT2320,
  243. mpu_port[dev], 0,
  244. mpu_irq[dev], SA_INTERRUPT,
  245. NULL) < 0)
  246. snd_printk(KERN_ERR PFX "no MPU-401 device at 0x%lx\n", mpu_port[dev]);
  247. }
  248. if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
  249. if (snd_opl3_create(card,
  250. fm_port[dev], fm_port[dev] + 2,
  251. OPL3_HW_AUTO, 0, &opl3) < 0) {
  252. snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx\n",
  253. fm_port[dev], fm_port[dev] + 2);
  254. } else {
  255. if ((error = snd_opl3_timer_new(opl3, 1, 2)) < 0) {
  256. snd_card_free(card);
  257. return error;
  258. }
  259. if ((error = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  260. snd_card_free(card);
  261. return error;
  262. }
  263. }
  264. }
  265. if ((error = snd_card_register(card)) < 0) {
  266. snd_card_free(card);
  267. return error;
  268. }
  269. pnp_set_card_drvdata(pcard, card);
  270. return 0;
  271. }
  272. static int __devinit snd_azt2320_pnp_detect(struct pnp_card_link *card,
  273. const struct pnp_card_device_id *id)
  274. {
  275. static int dev;
  276. int res;
  277. for ( ; dev < SNDRV_CARDS; dev++) {
  278. if (!enable[dev])
  279. continue;
  280. res = snd_card_azt2320_probe(dev, card, id);
  281. if (res < 0)
  282. return res;
  283. dev++;
  284. return 0;
  285. }
  286. return -ENODEV;
  287. }
  288. static void __devexit snd_azt2320_pnp_remove(struct pnp_card_link * pcard)
  289. {
  290. snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
  291. snd_card_disconnect(card);
  292. snd_card_free_in_thread(card);
  293. }
  294. static struct pnp_card_driver azt2320_pnpc_driver = {
  295. .flags = PNP_DRIVER_RES_DISABLE,
  296. .name = "azt2320",
  297. .id_table = snd_azt2320_pnpids,
  298. .probe = snd_azt2320_pnp_detect,
  299. .remove = __devexit_p(snd_azt2320_pnp_remove),
  300. };
  301. static int __init alsa_card_azt2320_init(void)
  302. {
  303. int cards = 0;
  304. cards += pnp_register_card_driver(&azt2320_pnpc_driver);
  305. #ifdef MODULE
  306. if (!cards) {
  307. pnp_unregister_card_driver(&azt2320_pnpc_driver);
  308. snd_printk(KERN_ERR "no AZT2320 based soundcards found\n");
  309. }
  310. #endif
  311. return cards ? 0 : -ENODEV;
  312. }
  313. static void __exit alsa_card_azt2320_exit(void)
  314. {
  315. pnp_unregister_card_driver(&azt2320_pnpc_driver);
  316. }
  317. module_init(alsa_card_azt2320_init)
  318. module_exit(alsa_card_azt2320_exit)