sb_common.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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(sb_t *chip, unsigned char val)
  39. {
  40. int i;
  41. #ifdef IO_DEBUG
  42. snd_printk("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(sb_t *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("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(sb_t *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(sb_t * 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(sb_t * 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("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(sb_t *chip)
  164. {
  165. if (chip->res_port) {
  166. release_resource(chip->res_port);
  167. kfree_nocheck(chip->res_port);
  168. }
  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(snd_device_t *device)
  185. {
  186. sb_t *chip = device->device_data;
  187. return snd_sbdsp_free(chip);
  188. }
  189. int snd_sbdsp_create(snd_card_t *card,
  190. unsigned long port,
  191. int irq,
  192. irqreturn_t (*irq_handler)(int, void *, struct pt_regs *),
  193. int dma8,
  194. int dma16,
  195. unsigned short hardware,
  196. sb_t **r_chip)
  197. {
  198. sb_t *chip;
  199. int err;
  200. static snd_device_ops_t ops = {
  201. .dev_free = snd_sbdsp_dev_free,
  202. };
  203. snd_assert(r_chip != NULL, return -EINVAL);
  204. *r_chip = NULL;
  205. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  206. if (chip == NULL)
  207. return -ENOMEM;
  208. spin_lock_init(&chip->reg_lock);
  209. spin_lock_init(&chip->open_lock);
  210. spin_lock_init(&chip->midi_input_lock);
  211. spin_lock_init(&chip->mixer_lock);
  212. chip->irq = -1;
  213. chip->dma8 = -1;
  214. chip->dma16 = -1;
  215. chip->port = port;
  216. if (request_irq(irq, irq_handler, hardware == SB_HW_ALS4000 ?
  217. SA_INTERRUPT | SA_SHIRQ : SA_INTERRUPT,
  218. "SoundBlaster", (void *) chip)) {
  219. snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq);
  220. snd_sbdsp_free(chip);
  221. return -EBUSY;
  222. }
  223. chip->irq = irq;
  224. if (hardware == SB_HW_ALS4000)
  225. goto __skip_allocation;
  226. if ((chip->res_port = request_region(port, 16, "SoundBlaster")) == NULL) {
  227. snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port);
  228. snd_sbdsp_free(chip);
  229. return -EBUSY;
  230. }
  231. #ifdef CONFIG_ISA
  232. if (dma8 >= 0 && request_dma(dma8, "SoundBlaster - 8bit")) {
  233. snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8);
  234. snd_sbdsp_free(chip);
  235. return -EBUSY;
  236. }
  237. chip->dma8 = dma8;
  238. if (dma16 >= 0) {
  239. if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) {
  240. /* no duplex */
  241. dma16 = -1;
  242. } else if (request_dma(dma16, "SoundBlaster - 16bit")) {
  243. snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16);
  244. snd_sbdsp_free(chip);
  245. return -EBUSY;
  246. }
  247. }
  248. chip->dma16 = dma16;
  249. #endif
  250. __skip_allocation:
  251. chip->card = card;
  252. chip->hardware = hardware;
  253. if ((err = snd_sbdsp_probe(chip)) < 0) {
  254. snd_sbdsp_free(chip);
  255. return err;
  256. }
  257. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  258. snd_sbdsp_free(chip);
  259. return err;
  260. }
  261. *r_chip = chip;
  262. return 0;
  263. }
  264. EXPORT_SYMBOL(snd_sbdsp_command);
  265. EXPORT_SYMBOL(snd_sbdsp_get_byte);
  266. EXPORT_SYMBOL(snd_sbdsp_reset);
  267. EXPORT_SYMBOL(snd_sbdsp_create);
  268. /* sb_mixer.c */
  269. EXPORT_SYMBOL(snd_sbmixer_write);
  270. EXPORT_SYMBOL(snd_sbmixer_read);
  271. EXPORT_SYMBOL(snd_sbmixer_new);
  272. EXPORT_SYMBOL(snd_sbmixer_add_ctl);
  273. /*
  274. * INIT part
  275. */
  276. static int __init alsa_sb_common_init(void)
  277. {
  278. return 0;
  279. }
  280. static void __exit alsa_sb_common_exit(void)
  281. {
  282. }
  283. module_init(alsa_sb_common_init)
  284. module_exit(alsa_sb_common_exit)