sb_common.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) by Jaroslav Kysela <[email protected]>
  4. * Uros Bizjak <[email protected]>
  5. *
  6. * Lowlevel routines for control of Sound Blaster cards
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/init.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/slab.h>
  12. #include <linux/ioport.h>
  13. #include <linux/module.h>
  14. #include <linux/io.h>
  15. #include <sound/core.h>
  16. #include <sound/sb.h>
  17. #include <sound/initval.h>
  18. #include <asm/dma.h>
  19. MODULE_AUTHOR("Jaroslav Kysela <[email protected]>");
  20. MODULE_DESCRIPTION("ALSA lowlevel driver for Sound Blaster cards");
  21. MODULE_LICENSE("GPL");
  22. #define BUSY_LOOPS 100000
  23. #undef IO_DEBUG
  24. int snd_sbdsp_command(struct snd_sb *chip, unsigned char val)
  25. {
  26. int i;
  27. #ifdef IO_DEBUG
  28. snd_printk(KERN_DEBUG "command 0x%x\n", val);
  29. #endif
  30. for (i = BUSY_LOOPS; i; i--)
  31. if ((inb(SBP(chip, STATUS)) & 0x80) == 0) {
  32. outb(val, SBP(chip, COMMAND));
  33. return 1;
  34. }
  35. snd_printd("%s [0x%lx]: timeout (0x%x)\n", __func__, chip->port, val);
  36. return 0;
  37. }
  38. int snd_sbdsp_get_byte(struct snd_sb *chip)
  39. {
  40. int val;
  41. int i;
  42. for (i = BUSY_LOOPS; i; i--) {
  43. if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
  44. val = inb(SBP(chip, READ));
  45. #ifdef IO_DEBUG
  46. snd_printk(KERN_DEBUG "get_byte 0x%x\n", val);
  47. #endif
  48. return val;
  49. }
  50. }
  51. snd_printd("%s [0x%lx]: timeout\n", __func__, chip->port);
  52. return -ENODEV;
  53. }
  54. int snd_sbdsp_reset(struct snd_sb *chip)
  55. {
  56. int i;
  57. outb(1, SBP(chip, RESET));
  58. udelay(10);
  59. outb(0, SBP(chip, RESET));
  60. udelay(30);
  61. for (i = BUSY_LOOPS; i; i--)
  62. if (inb(SBP(chip, DATA_AVAIL)) & 0x80) {
  63. if (inb(SBP(chip, READ)) == 0xaa)
  64. return 0;
  65. else
  66. break;
  67. }
  68. snd_printdd("%s [0x%lx] failed...\n", __func__, chip->port);
  69. return -ENODEV;
  70. }
  71. static int snd_sbdsp_version(struct snd_sb * chip)
  72. {
  73. unsigned int result;
  74. snd_sbdsp_command(chip, SB_DSP_GET_VERSION);
  75. result = (short) snd_sbdsp_get_byte(chip) << 8;
  76. result |= (short) snd_sbdsp_get_byte(chip);
  77. return result;
  78. }
  79. static int snd_sbdsp_probe(struct snd_sb * chip)
  80. {
  81. int version;
  82. int major, minor;
  83. char *str;
  84. unsigned long flags;
  85. /*
  86. * initialization sequence
  87. */
  88. spin_lock_irqsave(&chip->reg_lock, flags);
  89. if (snd_sbdsp_reset(chip) < 0) {
  90. spin_unlock_irqrestore(&chip->reg_lock, flags);
  91. return -ENODEV;
  92. }
  93. version = snd_sbdsp_version(chip);
  94. if (version < 0) {
  95. spin_unlock_irqrestore(&chip->reg_lock, flags);
  96. return -ENODEV;
  97. }
  98. spin_unlock_irqrestore(&chip->reg_lock, flags);
  99. major = version >> 8;
  100. minor = version & 0xff;
  101. snd_printdd("SB [0x%lx]: DSP chip found, version = %i.%i\n",
  102. chip->port, major, minor);
  103. switch (chip->hardware) {
  104. case SB_HW_AUTO:
  105. switch (major) {
  106. case 1:
  107. chip->hardware = SB_HW_10;
  108. str = "1.0";
  109. break;
  110. case 2:
  111. if (minor) {
  112. chip->hardware = SB_HW_201;
  113. str = "2.01+";
  114. } else {
  115. chip->hardware = SB_HW_20;
  116. str = "2.0";
  117. }
  118. break;
  119. case 3:
  120. chip->hardware = SB_HW_PRO;
  121. str = "Pro";
  122. break;
  123. case 4:
  124. chip->hardware = SB_HW_16;
  125. str = "16";
  126. break;
  127. default:
  128. snd_printk(KERN_INFO "SB [0x%lx]: unknown DSP chip version %i.%i\n",
  129. chip->port, major, minor);
  130. return -ENODEV;
  131. }
  132. break;
  133. case SB_HW_ALS100:
  134. str = "16 (ALS-100)";
  135. break;
  136. case SB_HW_ALS4000:
  137. str = "16 (ALS-4000)";
  138. break;
  139. case SB_HW_DT019X:
  140. str = "(DT019X/ALS007)";
  141. break;
  142. case SB_HW_CS5530:
  143. str = "16 (CS5530)";
  144. break;
  145. case SB_HW_JAZZ16:
  146. str = "Pro (Jazz16)";
  147. break;
  148. default:
  149. return -ENODEV;
  150. }
  151. sprintf(chip->name, "Sound Blaster %s", str);
  152. chip->version = (major << 8) | minor;
  153. return 0;
  154. }
  155. int snd_sbdsp_create(struct snd_card *card,
  156. unsigned long port,
  157. int irq,
  158. irq_handler_t irq_handler,
  159. int dma8,
  160. int dma16,
  161. unsigned short hardware,
  162. struct snd_sb **r_chip)
  163. {
  164. struct snd_sb *chip;
  165. int err;
  166. if (snd_BUG_ON(!r_chip))
  167. return -EINVAL;
  168. *r_chip = NULL;
  169. chip = devm_kzalloc(card->dev, sizeof(*chip), GFP_KERNEL);
  170. if (!chip)
  171. return -ENOMEM;
  172. spin_lock_init(&chip->reg_lock);
  173. spin_lock_init(&chip->open_lock);
  174. spin_lock_init(&chip->midi_input_lock);
  175. spin_lock_init(&chip->mixer_lock);
  176. chip->irq = -1;
  177. chip->dma8 = -1;
  178. chip->dma16 = -1;
  179. chip->port = port;
  180. if (devm_request_irq(card->dev, irq, irq_handler,
  181. (hardware == SB_HW_ALS4000 ||
  182. hardware == SB_HW_CS5530) ?
  183. IRQF_SHARED : 0,
  184. "SoundBlaster", (void *) chip)) {
  185. snd_printk(KERN_ERR "sb: can't grab irq %d\n", irq);
  186. return -EBUSY;
  187. }
  188. chip->irq = irq;
  189. card->sync_irq = chip->irq;
  190. if (hardware == SB_HW_ALS4000)
  191. goto __skip_allocation;
  192. chip->res_port = devm_request_region(card->dev, port, 16,
  193. "SoundBlaster");
  194. if (!chip->res_port) {
  195. snd_printk(KERN_ERR "sb: can't grab port 0x%lx\n", port);
  196. return -EBUSY;
  197. }
  198. #ifdef CONFIG_ISA
  199. if (dma8 >= 0 && snd_devm_request_dma(card->dev, dma8,
  200. "SoundBlaster - 8bit")) {
  201. snd_printk(KERN_ERR "sb: can't grab DMA8 %d\n", dma8);
  202. return -EBUSY;
  203. }
  204. chip->dma8 = dma8;
  205. if (dma16 >= 0) {
  206. if (hardware != SB_HW_ALS100 && (dma16 < 5 || dma16 > 7)) {
  207. /* no duplex */
  208. dma16 = -1;
  209. } else if (snd_devm_request_dma(card->dev, dma16,
  210. "SoundBlaster - 16bit")) {
  211. snd_printk(KERN_ERR "sb: can't grab DMA16 %d\n", dma16);
  212. return -EBUSY;
  213. }
  214. }
  215. chip->dma16 = dma16;
  216. #endif
  217. __skip_allocation:
  218. chip->card = card;
  219. chip->hardware = hardware;
  220. err = snd_sbdsp_probe(chip);
  221. if (err < 0)
  222. return err;
  223. *r_chip = chip;
  224. return 0;
  225. }
  226. EXPORT_SYMBOL(snd_sbdsp_command);
  227. EXPORT_SYMBOL(snd_sbdsp_get_byte);
  228. EXPORT_SYMBOL(snd_sbdsp_reset);
  229. EXPORT_SYMBOL(snd_sbdsp_create);
  230. /* sb_mixer.c */
  231. EXPORT_SYMBOL(snd_sbmixer_write);
  232. EXPORT_SYMBOL(snd_sbmixer_read);
  233. EXPORT_SYMBOL(snd_sbmixer_new);
  234. EXPORT_SYMBOL(snd_sbmixer_add_ctl);
  235. #ifdef CONFIG_PM
  236. EXPORT_SYMBOL(snd_sbmixer_suspend);
  237. EXPORT_SYMBOL(snd_sbmixer_resume);
  238. #endif