sb_common.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  3. * Uros Bizjak <uros@kss-loka.si>
  4. *
  5. * Lowlevel routines for control of Sound Blaster cards
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <sound/driver.h>
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/slab.h>
  27. #include <linux/ioport.h>
  28. #include <sound/core.h>
  29. #include <sound/sb.h>
  30. #include <sound/initval.h>
  31. #include <asm/io.h>
  32. #include <asm/dma.h>
  33. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  34. MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards");
  35. MODULE_LICENSE("GPL");
  36. #define BUSY_LOOPS 100000
  37. #undef IO_DEBUG
  38. int snd_sbdsp_command(struct snd_sb *chip, unsigned char val)
  39. {
  40. int i;
  41. #ifdef IO_DEBUG
  42. snd_printk(KERN_DEBUG "command 0x%x\n", val);
  43. #endif
  44. for (i = BUSY_LOOPS; i; i--)
  45. if ((inb(SBP(chip, STATUS)) & 0x80) == 0) {
  46. outb(val, SBP(chip, COMMAND));
  47. return 1;
  48. }
  49. snd_printd("%s [0x%lx]: timeout (0x%x)\n", __FUNCTION__, chip->port, val);
  50. return 0;
  51. }
  52. int snd_sbdsp_get_byte(struct snd_sb *chip)
  53. {
  54. int val;
  55. int i;
  56. for (i = BUSY_LOOPS; i; i--) {
  57. if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
  58. val = inb(SBP(chip, READ));
  59. #ifdef IO_DEBUG
  60. snd_printk(KERN_DEBUG "get_byte 0x%x\n", val);
  61. #endif
  62. return val;
  63. }
  64. }
  65. snd_printd("%s [0x%lx]: timeout\n", __FUNCTION__, chip->port);
  66. return -ENODEV;
  67. }
  68. int snd_sbdsp_reset(struct snd_sb *chip)
  69. {
  70. int i;
  71. outb(1, SBP(chip, RESET));
  72. udelay(10);
  73. outb(0, SBP(chip, RESET));
  74. udelay(30);
  75. for (i = BUSY_LOOPS; i; i--)
  76. if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
  77. if (inb(SBP(chip, READ)) == 0xaa)
  78. return 0;
  79. else
  80. break;
  81. }
  82. snd_printdd("%s [0x%lx] failed...\n", __FUNCTION__, chip->port);
  83. return -ENODEV;
  84. }
  85. static int snd_sbdsp_version(struct snd_sb * chip)
  86. {
  87. unsigned int result = -ENODEV;
  88. snd_sbdsp_command(chip, SB_DSP_GET_VERSION);
  89. result = (short) snd_sbdsp_get_byte(chip) << 8;
  90. result |= (short) snd_sbdsp_get_byte(chip);
  91. return result;
  92. }
  93. static int snd_sbdsp_probe(struct snd_sb * chip)
  94. {
  95. int version;
  96. int major, minor;
  97. char *str;
  98. unsigned long flags;
  99. /*
  100. * initialization sequence
  101. */
  102. spin_lock_irqsave(&chip->reg_lock, flags);
  103. if (snd_sbdsp_reset(chip) < 0) {
  104. spin_unlock_irqrestore(&chip->reg_lock, flags);
  105. return -ENODEV;
  106. }
  107. version = snd_sbdsp_version(chip);
  108. if (version < 0) {
  109. spin_unlock_irqrestore(&chip->reg_lock, flags);
  110. return -ENODEV;
  111. }
  112. spin_unlock_irqrestore(&chip->reg_lock, flags);
  113. major = version >> 8;
  114. minor = version & 0xff;
  115. snd_printdd("SB [0x%lx]: DSP chip found, version = %i.%i\n",
  116. chip->port, major, minor);
  117. switch (chip->hardware) {
  118. case SB_HW_AUTO:
  119. switch (major) {
  120. case 1:
  121. chip->hardware = SB_HW_10;
  122. str = "1.0";
  123. break;
  124. case 2:
  125. if (minor) {
  126. chip->hardware = SB_HW_201;
  127. str = "2.01+";
  128. } else {
  129. chip->hardware = SB_HW_20;
  130. str = "2.0";
  131. }
  132. break;
  133. case 3:
  134. chip->hardware = SB_HW_PRO;
  135. str = "Pro";
  136. break;
  137. case 4:
  138. chip->hardware = SB_HW_16;
  139. str = "16";
  140. break;
  141. default:
  142. snd_printk(KERN_INFO "SB [0x%lx]: unknown DSP chip version %i.%i\n",
  143. chip->port, major, minor);
  144. return -ENODEV;
  145. }
  146. break;
  147. case SB_HW_ALS100:
  148. str = "16 (ALS-100)";
  149. break;
  150. case SB_HW_ALS4000:
  151. str = "16 (ALS-4000)";
  152. break;
  153. case SB_HW_DT019X:
  154. str = "(DT019X/ALS007)";
  155. break;
  156. default:
  157. return -ENODEV;
  158. }
  159. sprintf(chip->name, "Sound Blaster %s", str);
  160. chip->version = (major << 8) | minor;
  161. return 0;
  162. }
  163. static int snd_sbdsp_free(struct snd_sb *chip)
  164. {
  165. if (chip->res_port)
  166. release_and_free_resource(chip->res_port);
  167. if (chip->irq >= 0)
  168. free_irq(chip->irq, (void *) chip);
  169. #ifdef CONFIG_ISA
  170. if (chip->dma8 >= 0) {
  171. disable_dma(chip->dma8);
  172. free_dma(chip->dma8);
  173. }
  174. if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) {
  175. disable_dma(chip->dma16);
  176. free_dma(chip->dma16);
  177. }
  178. #endif
  179. kfree(chip);
  180. return 0;
  181. }
  182. static int snd_sbdsp_dev_free(struct snd_device *device)
  183. {
  184. struct snd_sb *chip = device->device_data;
  185. return snd_sbdsp_free(chip);
  186. }
  187. int snd_sbdsp_create(struct snd_card *card,
  188. unsigned long port,
  189. int irq,
  190. irq_handler_t irq_handler,
  191. int dma8,
  192. int dma16,
  193. unsigned short hardware,
  194. struct snd_sb **r_chip)
  195. {
  196. struct snd_sb *chip;
  197. int err;
  198. static struct snd_device_ops ops = {
  199. .dev_free = snd_sbdsp_dev_free,
  200. };
  201. snd_assert(r_chip != NULL, return -EINVAL);
  202. *r_chip = NULL;
  203. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  204. if (chip == NULL)
  205. return -ENOMEM;
  206. spin_lock_init(&chip->reg_lock);
  207. spin_lock_init(&chip->open_lock);
  208. spin_lock_init(&chip->midi_input_lock);
  209. spin_lock_init(&chip->mixer_lock);
  210. chip->irq = -1;
  211. chip->dma8 = -1;
  212. chip->dma16 = -1;
  213. chip->port = port;
  214. if (request_irq(irq, irq_handler, hardware == SB_HW_ALS4000 ?
  215. IRQF_SHARED : IRQF_DISABLED,
  216. "SoundBlaster", (void *) chip)) {
  217. snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq);
  218. snd_sbdsp_free(chip);
  219. return -EBUSY;
  220. }
  221. chip->irq = irq;
  222. if (hardware == SB_HW_ALS4000)
  223. goto __skip_allocation;
  224. if ((chip->res_port = request_region(port, 16, "SoundBlaster")) == NULL) {
  225. snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port);
  226. snd_sbdsp_free(chip);
  227. return -EBUSY;
  228. }
  229. #ifdef CONFIG_ISA
  230. if (dma8 >= 0 && request_dma(dma8, "SoundBlaster - 8bit")) {
  231. snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8);
  232. snd_sbdsp_free(chip);
  233. return -EBUSY;
  234. }
  235. chip->dma8 = dma8;
  236. if (dma16 >= 0) {
  237. if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) {
  238. /* no duplex */
  239. dma16 = -1;
  240. } else if (request_dma(dma16, "SoundBlaster - 16bit")) {
  241. snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16);
  242. snd_sbdsp_free(chip);
  243. return -EBUSY;
  244. }
  245. }
  246. chip->dma16 = dma16;
  247. #endif
  248. __skip_allocation:
  249. chip->card = card;
  250. chip->hardware = hardware;
  251. if ((err = snd_sbdsp_probe(chip)) < 0) {
  252. snd_sbdsp_free(chip);
  253. return err;
  254. }
  255. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  256. snd_sbdsp_free(chip);
  257. return err;
  258. }
  259. *r_chip = chip;
  260. return 0;
  261. }
  262. EXPORT_SYMBOL(snd_sbdsp_command);
  263. EXPORT_SYMBOL(snd_sbdsp_get_byte);
  264. EXPORT_SYMBOL(snd_sbdsp_reset);
  265. EXPORT_SYMBOL(snd_sbdsp_create);
  266. /* sb_mixer.c */
  267. EXPORT_SYMBOL(snd_sbmixer_write);
  268. EXPORT_SYMBOL(snd_sbmixer_read);
  269. EXPORT_SYMBOL(snd_sbmixer_new);
  270. EXPORT_SYMBOL(snd_sbmixer_add_ctl);
  271. #ifdef CONFIG_PM
  272. EXPORT_SYMBOL(snd_sbmixer_suspend);
  273. EXPORT_SYMBOL(snd_sbmixer_resume);
  274. #endif
  275. /*
  276. * INIT part
  277. */
  278. static int __init alsa_sb_common_init(void)
  279. {
  280. return 0;
  281. }
  282. static void __exit alsa_sb_common_exit(void)
  283. {
  284. }
  285. module_init(alsa_sb_common_init)
  286. module_exit(alsa_sb_common_exit)