sgalaxy.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 <sound/driver.h>
  24. #include <asm/dma.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/moduleparam.h>
  30. #include <sound/core.h>
  31. #include <sound/sb.h>
  32. #include <sound/ad1848.h>
  33. #include <sound/control.h>
  34. #define SNDRV_LEGACY_FIND_FREE_IRQ
  35. #define SNDRV_LEGACY_FIND_FREE_DMA
  36. #include <sound/initval.h>
  37. MODULE_AUTHOR("Christopher Butler <chrisb@sandy.force9.co.uk>");
  38. MODULE_DESCRIPTION("Aztech Sound Galaxy");
  39. MODULE_LICENSE("GPL");
  40. MODULE_SUPPORTED_DEVICE("{{Aztech Systems,Sound Galaxy}}");
  41. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  42. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  43. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  44. static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240 */
  45. static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x530,0xe80,0xf40,0x604 */
  46. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 7,9,10,11 */
  47. static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
  48. module_param_array(index, int, NULL, 0444);
  49. MODULE_PARM_DESC(index, "Index value for Sound Galaxy soundcard.");
  50. module_param_array(id, charp, NULL, 0444);
  51. MODULE_PARM_DESC(id, "ID string for Sound Galaxy soundcard.");
  52. module_param_array(sbport, long, NULL, 0444);
  53. MODULE_PARM_DESC(sbport, "Port # for Sound Galaxy SB driver.");
  54. module_param_array(wssport, long, NULL, 0444);
  55. MODULE_PARM_DESC(wssport, "Port # for Sound Galaxy WSS driver.");
  56. module_param_array(irq, int, NULL, 0444);
  57. MODULE_PARM_DESC(irq, "IRQ # for Sound Galaxy driver.");
  58. module_param_array(dma1, int, NULL, 0444);
  59. MODULE_PARM_DESC(dma1, "DMA1 # for Sound Galaxy driver.");
  60. #define SGALAXY_AUXC_LEFT 18
  61. #define SGALAXY_AUXC_RIGHT 19
  62. static snd_card_t *snd_sgalaxy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
  63. /*
  64. */
  65. #define AD1848P1( port, x ) ( port + c_d_c_AD1848##x )
  66. /* from lowlevel/sb/sb.c - to avoid having to allocate a sb_t for the */
  67. /* short time we actually need it.. */
  68. static int snd_sgalaxy_sbdsp_reset(unsigned long port)
  69. {
  70. int i;
  71. outb(1, SBP1(port, RESET));
  72. udelay(10);
  73. outb(0, SBP1(port, RESET));
  74. udelay(30);
  75. for (i = 0; i < 1000 && !(inb(SBP1(port, DATA_AVAIL)) & 0x80); i++);
  76. if (inb(SBP1(port, READ)) != 0xaa) {
  77. snd_printd("sb_reset: failed at 0x%lx!!!\n", port);
  78. return -ENODEV;
  79. }
  80. return 0;
  81. }
  82. static int __init snd_sgalaxy_sbdsp_command(unsigned long port, unsigned char val)
  83. {
  84. int i;
  85. for (i = 10000; i; i--)
  86. if ((inb(SBP1(port, STATUS)) & 0x80) == 0) {
  87. outb(val, SBP1(port, COMMAND));
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. static irqreturn_t snd_sgalaxy_dummy_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  93. {
  94. return IRQ_NONE;
  95. }
  96. static int __init snd_sgalaxy_setup_wss(unsigned long port, int irq, int dma)
  97. {
  98. static int interrupt_bits[] = {-1, -1, -1, -1, -1, -1, -1, 0x08, -1,
  99. 0x10, 0x18, 0x20, -1, -1, -1, -1};
  100. static int dma_bits[] = {1, 2, 0, 3};
  101. int tmp, tmp1;
  102. if ((tmp = inb(port + 3)) == 0xff)
  103. {
  104. snd_printdd("I/O address dead (0x%lx)\n", port);
  105. return 0;
  106. }
  107. #if 0
  108. snd_printdd("WSS signature = 0x%x\n", tmp);
  109. #endif
  110. if ((tmp & 0x3f) != 0x04 &&
  111. (tmp & 0x3f) != 0x0f &&
  112. (tmp & 0x3f) != 0x00) {
  113. snd_printdd("No WSS signature detected on port 0x%lx\n",
  114. port + 3);
  115. return 0;
  116. }
  117. #if 0
  118. snd_printdd("sgalaxy - setting up IRQ/DMA for WSS\n");
  119. #endif
  120. /* initialize IRQ for WSS codec */
  121. tmp = interrupt_bits[irq % 16];
  122. if (tmp < 0)
  123. return -EINVAL;
  124. if (request_irq(irq, snd_sgalaxy_dummy_interrupt, SA_INTERRUPT, "sgalaxy", NULL)) {
  125. snd_printk(KERN_ERR "sgalaxy: can't grab irq %d\n", irq);
  126. return -EIO;
  127. }
  128. outb(tmp | 0x40, port);
  129. tmp1 = dma_bits[dma % 4];
  130. outb(tmp | tmp1, port);
  131. free_irq(irq, NULL);
  132. return 0;
  133. }
  134. static int __init snd_sgalaxy_detect(int dev, int irq, int dma)
  135. {
  136. #if 0
  137. snd_printdd("sgalaxy - switching to WSS mode\n");
  138. #endif
  139. /* switch to WSS mode */
  140. snd_sgalaxy_sbdsp_reset(sbport[dev]);
  141. snd_sgalaxy_sbdsp_command(sbport[dev], 9);
  142. snd_sgalaxy_sbdsp_command(sbport[dev], 0);
  143. udelay(400);
  144. return snd_sgalaxy_setup_wss(wssport[dev], irq, dma);
  145. }
  146. static struct ad1848_mix_elem snd_sgalaxy_controls[] = {
  147. AD1848_DOUBLE("Aux Playback Switch", 0, SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 7, 7, 1, 1),
  148. AD1848_DOUBLE("Aux Playback Volume", 0, SGALAXY_AUXC_LEFT, SGALAXY_AUXC_RIGHT, 0, 0, 31, 0)
  149. };
  150. static int __init snd_sgalaxy_mixer(ad1848_t *chip)
  151. {
  152. snd_card_t *card = chip->card;
  153. snd_ctl_elem_id_t id1, id2;
  154. unsigned int idx;
  155. int err;
  156. memset(&id1, 0, sizeof(id1));
  157. memset(&id2, 0, sizeof(id2));
  158. id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  159. /* reassign AUX0 to LINE */
  160. strcpy(id1.name, "Aux Playback Switch");
  161. strcpy(id2.name, "Line Playback Switch");
  162. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  163. return err;
  164. strcpy(id1.name, "Aux Playback Volume");
  165. strcpy(id2.name, "Line Playback Volume");
  166. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  167. return err;
  168. /* reassign AUX1 to FM */
  169. strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
  170. strcpy(id2.name, "FM Playback Switch");
  171. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  172. return err;
  173. strcpy(id1.name, "Aux Playback Volume");
  174. strcpy(id2.name, "FM Playback Volume");
  175. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0)
  176. return err;
  177. /* build AUX2 input */
  178. for (idx = 0; idx < ARRAY_SIZE(snd_sgalaxy_controls); idx++) {
  179. if ((err = snd_ad1848_add_ctl_elem(chip, &snd_sgalaxy_controls[idx])) < 0)
  180. return err;
  181. }
  182. return 0;
  183. }
  184. static int __init snd_sgalaxy_probe(int dev)
  185. {
  186. static int possible_irqs[] = {7, 9, 10, 11, -1};
  187. static int possible_dmas[] = {1, 3, 0, -1};
  188. int err, xirq, xdma1;
  189. snd_card_t *card;
  190. ad1848_t *chip;
  191. if (sbport[dev] == SNDRV_AUTO_PORT) {
  192. snd_printk("specify SB port\n");
  193. return -EINVAL;
  194. }
  195. if (wssport[dev] == SNDRV_AUTO_PORT) {
  196. snd_printk("specify WSS port\n");
  197. return -EINVAL;
  198. }
  199. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  200. if (card == NULL)
  201. return -ENOMEM;
  202. xirq = irq[dev];
  203. if (xirq == SNDRV_AUTO_IRQ) {
  204. if ((xirq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  205. snd_card_free(card);
  206. snd_printk("unable to find a free IRQ\n");
  207. return -EBUSY;
  208. }
  209. }
  210. xdma1 = dma1[dev];
  211. if (xdma1 == SNDRV_AUTO_DMA) {
  212. if ((xdma1 = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  213. snd_card_free(card);
  214. snd_printk("unable to find a free DMA\n");
  215. return -EBUSY;
  216. }
  217. }
  218. if ((err = snd_sgalaxy_detect(dev, xirq, xdma1)) < 0) {
  219. snd_card_free(card);
  220. return err;
  221. }
  222. if ((err = snd_ad1848_create(card, wssport[dev] + 4,
  223. xirq, xdma1,
  224. AD1848_HW_DETECT, &chip)) < 0) {
  225. snd_card_free(card);
  226. return err;
  227. }
  228. if ((err = snd_ad1848_pcm(chip, 0, NULL)) < 0) {
  229. snd_printdd("sgalaxy - error creating new ad1848 PCM device\n");
  230. snd_card_free(card);
  231. return err;
  232. }
  233. if ((err = snd_ad1848_mixer(chip)) < 0) {
  234. snd_printdd("sgalaxy - error creating new ad1848 mixer\n");
  235. snd_card_free(card);
  236. return err;
  237. }
  238. if (snd_sgalaxy_mixer(chip) < 0) {
  239. snd_printdd("sgalaxy - the mixer rewrite failed\n");
  240. snd_card_free(card);
  241. return err;
  242. }
  243. strcpy(card->driver, "Sound Galaxy");
  244. strcpy(card->shortname, "Sound Galaxy");
  245. sprintf(card->longname, "Sound Galaxy at 0x%lx, irq %d, dma %d",
  246. wssport[dev], xirq, xdma1);
  247. if ((err = snd_card_register(card)) < 0) {
  248. snd_card_free(card);
  249. return err;
  250. }
  251. snd_sgalaxy_cards[dev] = card;
  252. return 0;
  253. }
  254. static int __init alsa_card_sgalaxy_init(void)
  255. {
  256. int dev, cards;
  257. for (dev = cards = 0; dev < SNDRV_CARDS && enable[dev]; dev++) {
  258. if (snd_sgalaxy_probe(dev) >= 0)
  259. cards++;
  260. }
  261. if (!cards) {
  262. #ifdef MODULE
  263. snd_printk(KERN_ERR "Sound Galaxy soundcard not found or device busy\n");
  264. #endif
  265. return -ENODEV;
  266. }
  267. return 0;
  268. }
  269. static void __exit alsa_card_sgalaxy_exit(void)
  270. {
  271. int idx;
  272. for (idx = 0; idx < SNDRV_CARDS; idx++)
  273. snd_card_free(snd_sgalaxy_cards[idx]);
  274. }
  275. module_init(alsa_card_sgalaxy_init)
  276. module_exit(alsa_card_sgalaxy_exit)