sb_common.c 7.2 KB

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