sgalaxy.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Driver for Aztech Sound Galaxy cards
  3. * Copyright (c) by Christopher Butler <chrisb@sandy.force9.co.uk.
  4. *
  5. * I don't have documentation for this card, I based this driver on the
  6. * driver for OSS/Free included in the kernel source (drivers/sound/sgalaxy.c)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/init.h>
  24. #include <linux/err.h>
  25. #include <linux/isa.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/moduleparam.h>
  30. #include <asm/dma.h>
  31. #include <sound/core.h>
  32. #include <sound/sb.h>
  33. #include <sound/wss.h>
  34. #include <sound/control.h>
  35. #define SNDRV_LEGACY_FIND_FREE_IRQ
  36. #define SNDRV_LEGACY_FIND_FREE_DMA
  37. #include <sound/initval.h>
  38. MODULE_AUTHOR("Christopher Butler <chrisb@sandy.force9.co.uk>");
  39. MODULE_DESCRIPTION("Aztech Sound Galaxy");
  40. MODULE_LICENSE("GPL");
  41. MODULE_SUPPORTED_DEVICE("{{Aztech Systems,Sound Galaxy}}");
  42. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  43. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  44. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  45. static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240 */
  46. static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x530,0xe80,0xf40,0x604 */
  47. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 7,9,10,11 */
  48. static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
  49. module_param_array(index, int, NULL, 0444);
  50. MODULE_PARM_DESC(index, "Index value for Sound Galaxy soundcard.");
  51. module_param_array(id, charp, NULL, 0444);
  52. MODULE_PARM_DESC(id, "ID string for Sound Galaxy soundcard.");
  53. module_param_array(sbport, long, NULL, 0444);
  54. MODULE_PARM_DESC(sbport, "Port # for Sound Galaxy SB driver.");
  55. module_param_array(wssport, long, NULL, 0444);
  56. MODULE_PARM_DESC(wssport, "Port # for Sound Galaxy WSS driver.");
  57. module_param_array(irq, int, NULL, 0444);
  58. MODULE_PARM_DESC(irq, "IRQ # for Sound Galaxy driver.");
  59. module_param_array(dma1, int, NULL, 0444);
  60. MODULE_PARM_DESC(dma1, "DMA1 # for Sound Galaxy driver.");
  61. #define SGALAXY_AUXC_LEFT 18
  62. #define SGALAXY_AUXC_RIGHT 19
  63. #define PFX "sgalaxy: "
  64. /*
  65. */
  66. #define AD1848P1( port, x ) ( port + c_d_c_AD1848##x )
  67. /* from lowlevel/sb/sb.c - to avoid having to allocate a struct snd_sb for the */
  68. /* short time we actually need it.. */
  69. static int snd_sgalaxy_sbdsp_reset(unsigned long port)
  70. {
  71. int i;
  72. outb(1, SBP1(port, RESET));
  73. udelay(10);
  74. outb(0, SBP1(port, RESET));
  75. udelay(30);
  76. for (i = 0; i < 1000 && !(inb(SBP1(port, DATA_AVAIL)) & 0x80); i++);
  77. if (inb(SBP1(port, READ)) != 0xaa) {
  78. snd_printd("sb_reset: failed at 0x%lx!!!\n", port);
  79. return -ENODEV;
  80. }
  81. return 0;
  82. }
  83. static int __devinit snd_sgalaxy_sbdsp_command(unsigned long port,
  84. unsigned char val)
  85. {
  86. int i;
  87. for (i = 10000; i; i--)
  88. if ((inb(SBP1(port, STATUS)) & 0x80) == 0) {
  89. outb(val, SBP1(port, COMMAND));
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. static irqreturn_t snd_sgalaxy_dummy_interrupt(int irq, void *dev_id)
  95. {
  96. return IRQ_NONE;
  97. }
  98. static int __devinit snd_sgalaxy_setup_wss(unsigned long port, int irq, int dma)
  99. {
  100. static int interrupt_bits[] = {-1, -1, -1, -1, -1, -1, -1, 0x08, -1,
  101. 0x10, 0x18, 0x20, -1, -1, -1, -1};
  102. static int dma_bits[] = {1, 2, 0, 3};
  103. int tmp, tmp1;
  104. if ((tmp = inb(port + 3)) == 0xff)
  105. {
  106. snd_printdd("I/O address dead (0x%lx)\n", port);
  107. return 0;
  108. }
  109. #if 0
  110. snd_printdd("WSS signature = 0x%x\n", tmp);
  111. #endif
  112. if ((tmp & 0x3f) != 0x04 &&
  113. (tmp & 0x3f) != 0x0f &&
  114. (tmp & 0x3f) != 0x00) {
  115. snd_printdd("No WSS signature detected on port 0x%lx\n",
  116. port + 3);
  117. return 0;
  118. }
  119. #if 0
  120. snd_printdd(PFX "setting up IRQ/DMA for WSS\n");
  121. #endif
  122. /* initialize IRQ for WSS codec */
  123. tmp = interrupt_bits[irq % 16];
  124. if (tmp < 0)
  125. return -EINVAL;
  126. if (request_irq(irq, snd_sgalaxy_dummy_interrupt, IRQF_DISABLED, "sgalaxy", NULL)) {
  127. snd_printk(KERN_ERR "sgalaxy: can't grab irq %d\n", irq);
  128. return -EIO;
  129. }
  130. outb(tmp | 0x40, port);
  131. tmp1 = dma_bits[dma % 4];
  132. outb(tmp | tmp1, port);
  133. free_irq(irq, NULL);
  134. return 0;
  135. }
  136. static int __devinit snd_sgalaxy_detect(int dev, int irq, int dma)
  137. {
  138. #if 0
  139. snd_printdd(PFX "switching to WSS mode\n");
  140. #endif
  141. /* switch to WSS mode */
  142. snd_sgalaxy_sbdsp_reset(sbport[dev]);
  143. snd_sgalaxy_sbdsp_command(sbport[dev], 9);
  144. snd_sgalaxy_sbdsp_command(sbport[dev], 0);
  145. udelay(400);
  146. return snd_sgalaxy_setup_wss(wssport[dev], irq, dma);
  147. }
  148. static struct snd_kcontrol_new snd_sgalaxy_controls[] = {
  149. WSS_DOUBLE("Aux Playback Switch", 0,
  150. SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 7, 7, 1, 1),
  151. WSS_DOUBLE("Aux Playback Volume", 0,
  152. SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 0, 0, 31, 0)
  153. };
  154. static int __devinit snd_sgalaxy_mixer(struct snd_wss *chip)
  155. {
  156. struct snd_card *card = chip->card;
  157. struct snd_ctl_elem_id id1, id2;
  158. unsigned int idx;
  159. int err;
  160. memset(&id1, 0, sizeof(id1));
  161. memset(&id2, 0, sizeof(id2));
  162. id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  163. /* reassign AUX0 to LINE */
  164. strcpy(id1.name, "Aux Playback Switch");
  165. strcpy(id2.name, "Line Playback Switch");
  166. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  167. return err;
  168. strcpy(id1.name, "Aux Playback Volume");
  169. strcpy(id2.name, "Line Playback Volume");
  170. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  171. return err;
  172. /* reassign AUX1 to FM */
  173. strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
  174. strcpy(id2.name, "FM Playback Switch");
  175. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  176. return err;
  177. strcpy(id1.name, "Aux Playback Volume");
  178. strcpy(id2.name, "FM Playback Volume");
  179. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  180. return err;
  181. /* build AUX2 input */
  182. for (idx = 0; idx < ARRAY_SIZE(snd_sgalaxy_controls); idx++) {
  183. err = snd_ctl_add(card,
  184. snd_ctl_new1(&snd_sgalaxy_controls[idx], chip));
  185. if (err < 0)
  186. return err;
  187. }
  188. return 0;
  189. }
  190. static int __devinit snd_sgalaxy_match(struct device *devptr, unsigned int dev)
  191. {
  192. if (!enable[dev])
  193. return 0;
  194. if (sbport[dev] == SNDRV_AUTO_PORT) {
  195. snd_printk(KERN_ERR PFX "specify SB port\n");
  196. return 0;
  197. }
  198. if (wssport[dev] == SNDRV_AUTO_PORT) {
  199. snd_printk(KERN_ERR PFX "specify WSS port\n");
  200. return 0;
  201. }
  202. return 1;
  203. }
  204. static int __devinit snd_sgalaxy_probe(struct device *devptr, unsigned int dev)
  205. {
  206. static int possible_irqs[] = {7, 9, 10, 11, -1};
  207. static int possible_dmas[] = {1, 3, 0, -1};
  208. int err, xirq, xdma1;
  209. struct snd_card *card;
  210. struct snd_wss *chip;
  211. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  212. if (err < 0)
  213. return err;
  214. xirq = irq[dev];
  215. if (xirq == SNDRV_AUTO_IRQ) {
  216. if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  217. snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
  218. err = -EBUSY;
  219. goto _err;
  220. }
  221. }
  222. xdma1 = dma1[dev];
  223. if (xdma1 == SNDRV_AUTO_DMA) {
  224. if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  225. snd_printk(KERN_ERR PFX "unable to find a free DMA\n");
  226. err = -EBUSY;
  227. goto _err;
  228. }
  229. }
  230. if ((err = snd_sgalaxy_detect(dev, xirq, xdma1)) < 0)
  231. goto _err;
  232. err = snd_wss_create(card, wssport[dev] + 4, -1,
  233. xirq, xdma1, -1,
  234. WSS_HW_DETECT, 0, &chip);
  235. if (err < 0)
  236. goto _err;
  237. card->private_data = chip;
  238. err = snd_wss_pcm(chip, 0, NULL);
  239. if (err < 0) {
  240. snd_printdd(PFX "error creating new WSS PCM device\n");
  241. goto _err;
  242. }
  243. err = snd_wss_mixer(chip);
  244. if (err < 0) {
  245. snd_printdd(PFX "error creating new WSS mixer\n");
  246. goto _err;
  247. }
  248. if ((err = snd_sgalaxy_mixer(chip)) < 0) {
  249. snd_printdd(PFX "the mixer rewrite failed\n");
  250. goto _err;
  251. }
  252. strcpy(card->driver, "Sound Galaxy");
  253. strcpy(card->shortname, "Sound Galaxy");
  254. sprintf(card->longname, "Sound Galaxy at 0x%lx, irq %d, dma %d",
  255. wssport[dev], xirq, xdma1);
  256. snd_card_set_dev(card, devptr);
  257. if ((err = snd_card_register(card)) < 0)
  258. goto _err;
  259. dev_set_drvdata(devptr, card);
  260. return 0;
  261. _err:
  262. snd_card_free(card);
  263. return err;
  264. }
  265. static int __devexit snd_sgalaxy_remove(struct device *devptr, unsigned int dev)
  266. {
  267. snd_card_free(dev_get_drvdata(devptr));
  268. dev_set_drvdata(devptr, NULL);
  269. return 0;
  270. }
  271. #ifdef CONFIG_PM
  272. static int snd_sgalaxy_suspend(struct device *pdev, unsigned int n,
  273. pm_message_t state)
  274. {
  275. struct snd_card *card = dev_get_drvdata(pdev);
  276. struct snd_wss *chip = card->private_data;
  277. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  278. chip->suspend(chip);
  279. return 0;
  280. }
  281. static int snd_sgalaxy_resume(struct device *pdev, unsigned int n)
  282. {
  283. struct snd_card *card = dev_get_drvdata(pdev);
  284. struct snd_wss *chip = card->private_data;
  285. chip->resume(chip);
  286. snd_wss_out(chip, SGALAXY_AUXC_LEFT, chip->image[SGALAXY_AUXC_LEFT]);
  287. snd_wss_out(chip, SGALAXY_AUXC_RIGHT, chip->image[SGALAXY_AUXC_RIGHT]);
  288. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  289. return 0;
  290. }
  291. #endif
  292. #define DEV_NAME "sgalaxy"
  293. static struct isa_driver snd_sgalaxy_driver = {
  294. .match = snd_sgalaxy_match,
  295. .probe = snd_sgalaxy_probe,
  296. .remove = __devexit_p(snd_sgalaxy_remove),
  297. #ifdef CONFIG_PM
  298. .suspend = snd_sgalaxy_suspend,
  299. .resume = snd_sgalaxy_resume,
  300. #endif
  301. .driver = {
  302. .name = DEV_NAME
  303. },
  304. };
  305. static int __init alsa_card_sgalaxy_init(void)
  306. {
  307. return isa_register_driver(&snd_sgalaxy_driver, SNDRV_CARDS);
  308. }
  309. static void __exit alsa_card_sgalaxy_exit(void)
  310. {
  311. isa_unregister_driver(&snd_sgalaxy_driver);
  312. }
  313. module_init(alsa_card_sgalaxy_init)
  314. module_exit(alsa_card_sgalaxy_exit)