wavefront_fx.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 1998-2002 by Paul Davis <pbd@op.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <asm/io.h>
  19. #include <linux/init.h>
  20. #include <linux/time.h>
  21. #include <linux/wait.h>
  22. #include <linux/firmware.h>
  23. #include <sound/core.h>
  24. #include <sound/snd_wavefront.h>
  25. #include <sound/initval.h>
  26. /* Control bits for the Load Control Register
  27. */
  28. #define FX_LSB_TRANSFER 0x01 /* transfer after DSP LSB byte written */
  29. #define FX_MSB_TRANSFER 0x02 /* transfer after DSP MSB byte written */
  30. #define FX_AUTO_INCR 0x04 /* auto-increment DSP address after transfer */
  31. #define WAIT_IDLE 0xff
  32. #ifdef CONFIG_SND_WAVEFRONT_FIRMWARE_IN_KERNEL
  33. #include "yss225.c"
  34. static const struct firmware yss225_registers_firmware = {
  35. .data = (u8 *)yss225_registers,
  36. .size = sizeof yss225_registers
  37. };
  38. #endif
  39. static int
  40. wavefront_fx_idle (snd_wavefront_t *dev)
  41. {
  42. int i;
  43. unsigned int x = 0x80;
  44. for (i = 0; i < 1000; i++) {
  45. x = inb (dev->fx_status);
  46. if ((x & 0x80) == 0) {
  47. break;
  48. }
  49. }
  50. if (x & 0x80) {
  51. snd_printk ("FX device never idle.\n");
  52. return 0;
  53. }
  54. return (1);
  55. }
  56. static void
  57. wavefront_fx_mute (snd_wavefront_t *dev, int onoff)
  58. {
  59. if (!wavefront_fx_idle(dev)) {
  60. return;
  61. }
  62. outb (onoff ? 0x02 : 0x00, dev->fx_op);
  63. }
  64. static int
  65. wavefront_fx_memset (snd_wavefront_t *dev,
  66. int page,
  67. int addr,
  68. int cnt,
  69. unsigned short *data)
  70. {
  71. if (page < 0 || page > 7) {
  72. snd_printk ("FX memset: "
  73. "page must be >= 0 and <= 7\n");
  74. return -(EINVAL);
  75. }
  76. if (addr < 0 || addr > 0x7f) {
  77. snd_printk ("FX memset: "
  78. "addr must be >= 0 and <= 7f\n");
  79. return -(EINVAL);
  80. }
  81. if (cnt == 1) {
  82. outb (FX_LSB_TRANSFER, dev->fx_lcr);
  83. outb (page, dev->fx_dsp_page);
  84. outb (addr, dev->fx_dsp_addr);
  85. outb ((data[0] >> 8), dev->fx_dsp_msb);
  86. outb ((data[0] & 0xff), dev->fx_dsp_lsb);
  87. snd_printk ("FX: addr %d:%x set to 0x%x\n",
  88. page, addr, data[0]);
  89. } else {
  90. int i;
  91. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev->fx_lcr);
  92. outb (page, dev->fx_dsp_page);
  93. outb (addr, dev->fx_dsp_addr);
  94. for (i = 0; i < cnt; i++) {
  95. outb ((data[i] >> 8), dev->fx_dsp_msb);
  96. outb ((data[i] & 0xff), dev->fx_dsp_lsb);
  97. if (!wavefront_fx_idle (dev)) {
  98. break;
  99. }
  100. }
  101. if (i != cnt) {
  102. snd_printk ("FX memset "
  103. "(0x%x, 0x%x, 0x%lx, %d) incomplete\n",
  104. page, addr, (unsigned long) data, cnt);
  105. return -(EIO);
  106. }
  107. }
  108. return 0;
  109. }
  110. int
  111. snd_wavefront_fx_detect (snd_wavefront_t *dev)
  112. {
  113. /* This is a crude check, but its the best one I have for now.
  114. Certainly on the Maui and the Tropez, wavefront_fx_idle() will
  115. report "never idle", which suggests that this test should
  116. work OK.
  117. */
  118. if (inb (dev->fx_status) & 0x80) {
  119. snd_printk ("Hmm, probably a Maui or Tropez.\n");
  120. return -1;
  121. }
  122. return 0;
  123. }
  124. int
  125. snd_wavefront_fx_open (struct snd_hwdep *hw, struct file *file)
  126. {
  127. if (!try_module_get(hw->card->module))
  128. return -EFAULT;
  129. file->private_data = hw;
  130. return 0;
  131. }
  132. int
  133. snd_wavefront_fx_release (struct snd_hwdep *hw, struct file *file)
  134. {
  135. module_put(hw->card->module);
  136. return 0;
  137. }
  138. int
  139. snd_wavefront_fx_ioctl (struct snd_hwdep *sdev, struct file *file,
  140. unsigned int cmd, unsigned long arg)
  141. {
  142. struct snd_card *card;
  143. snd_wavefront_card_t *acard;
  144. snd_wavefront_t *dev;
  145. wavefront_fx_info r;
  146. unsigned short *page_data = NULL;
  147. unsigned short *pd;
  148. int err = 0;
  149. snd_assert(sdev->card != NULL, return -ENODEV);
  150. card = sdev->card;
  151. snd_assert(card->private_data != NULL, return -ENODEV);
  152. acard = card->private_data;
  153. dev = &acard->wavefront;
  154. if (copy_from_user (&r, (void __user *)arg, sizeof (wavefront_fx_info)))
  155. return -EFAULT;
  156. switch (r.request) {
  157. case WFFX_MUTE:
  158. wavefront_fx_mute (dev, r.data[0]);
  159. return -EIO;
  160. case WFFX_MEMSET:
  161. if (r.data[2] <= 0) {
  162. snd_printk ("cannot write "
  163. "<= 0 bytes to FX\n");
  164. return -EIO;
  165. } else if (r.data[2] == 1) {
  166. pd = (unsigned short *) &r.data[3];
  167. } else {
  168. if (r.data[2] > 256) {
  169. snd_printk ("cannot write "
  170. "> 512 bytes to FX\n");
  171. return -EIO;
  172. }
  173. page_data = kmalloc(r.data[2] * sizeof(short), GFP_KERNEL);
  174. if (!page_data)
  175. return -ENOMEM;
  176. if (copy_from_user (page_data,
  177. (unsigned char __user *) r.data[3],
  178. r.data[2] * sizeof(short))) {
  179. kfree(page_data);
  180. return -EFAULT;
  181. }
  182. pd = page_data;
  183. }
  184. err = wavefront_fx_memset (dev,
  185. r.data[0], /* page */
  186. r.data[1], /* addr */
  187. r.data[2], /* cnt */
  188. pd);
  189. kfree(page_data);
  190. break;
  191. default:
  192. snd_printk ("FX: ioctl %d not yet supported\n",
  193. r.request);
  194. return -ENOTTY;
  195. }
  196. return err;
  197. }
  198. /* YSS225 initialization.
  199. This code was developed using DOSEMU. The Turtle Beach SETUPSND
  200. utility was run with I/O tracing in DOSEMU enabled, and a reconstruction
  201. of the port I/O done, using the Yamaha faxback document as a guide
  202. to add more logic to the code. Its really pretty weird.
  203. This is the approach of just dumping the whole I/O
  204. sequence as a series of port/value pairs and a simple loop
  205. that outputs it.
  206. */
  207. int __devinit
  208. snd_wavefront_fx_start (snd_wavefront_t *dev)
  209. {
  210. unsigned int i;
  211. int err;
  212. const struct firmware *firmware = NULL;
  213. if (dev->fx_initialized)
  214. return 0;
  215. #ifdef CONFIG_SND_WAVEFRONT_FIRMWARE_IN_KERNEL
  216. firmware = &yss225_registers_firmware;
  217. #else
  218. err = request_firmware(&firmware, "yamaha/yss225_registers.bin",
  219. dev->card->dev);
  220. if (err < 0) {
  221. err = -1;
  222. goto out;
  223. }
  224. #endif
  225. for (i = 0; i + 1 < firmware->size; i += 2) {
  226. if (firmware->data[i] >= 8 && firmware->data[i] < 16) {
  227. outb(firmware->data[i + 1],
  228. dev->base + firmware->data[i]);
  229. } else if (firmware->data[i] == WAIT_IDLE) {
  230. if (!wavefront_fx_idle(dev)) {
  231. err = -1;
  232. goto out;
  233. }
  234. } else {
  235. snd_printk(KERN_ERR "invalid address"
  236. " in register data\n");
  237. err = -1;
  238. goto out;
  239. }
  240. }
  241. dev->fx_initialized = 1;
  242. err = 0;
  243. out:
  244. #ifndef CONFIG_SND_WAVEFRONT_FIRMWARE_IN_KERNEL
  245. release_firmware(firmware);
  246. #endif
  247. return err;
  248. }
  249. #ifndef CONFIG_SND_WAVEFRONT_FIRMWARE_IN_KERNEL
  250. MODULE_FIRMWARE("yamaha/yss225_registers.bin");
  251. #endif