sb_common.c 7.2 KB

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