sb_common.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 <linux/module.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@perex.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", __func__, 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", __func__, 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", __func__, 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. case SB_HW_CS5530:
  157. str = "16 (CS5530)";
  158. break;
  159. case SB_HW_JAZZ16:
  160. str = "Pro (Jazz16)";
  161. break;
  162. default:
  163. return -ENODEV;
  164. }
  165. sprintf(chip->name, "Sound Blaster %s", str);
  166. chip->version = (major << 8) | minor;
  167. return 0;
  168. }
  169. static int snd_sbdsp_free(struct snd_sb *chip)
  170. {
  171. if (chip->res_port)
  172. release_and_free_resource(chip->res_port);
  173. if (chip->irq >= 0)
  174. free_irq(chip->irq, (void *) chip);
  175. #ifdef CONFIG_ISA
  176. if (chip->dma8 >= 0) {
  177. disable_dma(chip->dma8);
  178. free_dma(chip->dma8);
  179. }
  180. if (chip->dma16 >= 0 && chip->dma16 != chip->dma8) {
  181. disable_dma(chip->dma16);
  182. free_dma(chip->dma16);
  183. }
  184. #endif
  185. kfree(chip);
  186. return 0;
  187. }
  188. static int snd_sbdsp_dev_free(struct snd_device *device)
  189. {
  190. struct snd_sb *chip = device->device_data;
  191. return snd_sbdsp_free(chip);
  192. }
  193. int snd_sbdsp_create(struct snd_card *card,
  194. unsigned long port,
  195. int irq,
  196. irq_handler_t irq_handler,
  197. int dma8,
  198. int dma16,
  199. unsigned short hardware,
  200. struct snd_sb **r_chip)
  201. {
  202. struct snd_sb *chip;
  203. int err;
  204. static struct snd_device_ops ops = {
  205. .dev_free = snd_sbdsp_dev_free,
  206. };
  207. if (snd_BUG_ON(!r_chip))
  208. return -EINVAL;
  209. *r_chip = NULL;
  210. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  211. if (chip == NULL)
  212. return -ENOMEM;
  213. spin_lock_init(&chip->reg_lock);
  214. spin_lock_init(&chip->open_lock);
  215. spin_lock_init(&chip->midi_input_lock);
  216. spin_lock_init(&chip->mixer_lock);
  217. chip->irq = -1;
  218. chip->dma8 = -1;
  219. chip->dma16 = -1;
  220. chip->port = port;
  221. if (request_irq(irq, irq_handler,
  222. (hardware == SB_HW_ALS4000 ||
  223. hardware == SB_HW_CS5530) ?
  224. IRQF_SHARED : 0,
  225. "SoundBlaster", (void *) chip)) {
  226. snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq);
  227. snd_sbdsp_free(chip);
  228. return -EBUSY;
  229. }
  230. chip->irq = irq;
  231. if (hardware == SB_HW_ALS4000)
  232. goto __skip_allocation;
  233. if ((chip->res_port = request_region(port, 16, "SoundBlaster")) == NULL) {
  234. snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port);
  235. snd_sbdsp_free(chip);
  236. return -EBUSY;
  237. }
  238. #ifdef CONFIG_ISA
  239. if (dma8 >= 0 && request_dma(dma8, "SoundBlaster - 8bit")) {
  240. snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8);
  241. snd_sbdsp_free(chip);
  242. return -EBUSY;
  243. }
  244. chip->dma8 = dma8;
  245. if (dma16 >= 0) {
  246. if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) {
  247. /* no duplex */
  248. dma16 = -1;
  249. } else if (request_dma(dma16, "SoundBlaster - 16bit")) {
  250. snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16);
  251. snd_sbdsp_free(chip);
  252. return -EBUSY;
  253. }
  254. }
  255. chip->dma16 = dma16;
  256. #endif
  257. __skip_allocation:
  258. chip->card = card;
  259. chip->hardware = hardware;
  260. if ((err = snd_sbdsp_probe(chip)) < 0) {
  261. snd_sbdsp_free(chip);
  262. return err;
  263. }
  264. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  265. snd_sbdsp_free(chip);
  266. return err;
  267. }
  268. *r_chip = chip;
  269. return 0;
  270. }
  271. EXPORT_SYMBOL(snd_sbdsp_command);
  272. EXPORT_SYMBOL(snd_sbdsp_get_byte);
  273. EXPORT_SYMBOL(snd_sbdsp_reset);
  274. EXPORT_SYMBOL(snd_sbdsp_create);
  275. /* sb_mixer.c */
  276. EXPORT_SYMBOL(snd_sbmixer_write);
  277. EXPORT_SYMBOL(snd_sbmixer_read);
  278. EXPORT_SYMBOL(snd_sbmixer_new);
  279. EXPORT_SYMBOL(snd_sbmixer_add_ctl);
  280. #ifdef CONFIG_PM
  281. EXPORT_SYMBOL(snd_sbmixer_suspend);
  282. EXPORT_SYMBOL(snd_sbmixer_resume);
  283. #endif
  284. /*
  285. * INIT part
  286. */
  287. static int __init alsa_sb_common_init(void)
  288. {
  289. return 0;
  290. }
  291. static void __exit alsa_sb_common_exit(void)
  292. {
  293. }
  294. module_init(alsa_sb_common_init)
  295. module_exit(alsa_sb_common_exit)