wavefront_fx.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. static int
  33. wavefront_fx_idle (snd_wavefront_t *dev)
  34. {
  35. int i;
  36. unsigned int x = 0x80;
  37. for (i = 0; i < 1000; i++) {
  38. x = inb (dev->fx_status);
  39. if ((x & 0x80) == 0) {
  40. break;
  41. }
  42. }
  43. if (x & 0x80) {
  44. snd_printk ("FX device never idle.\n");
  45. return 0;
  46. }
  47. return (1);
  48. }
  49. static void
  50. wavefront_fx_mute (snd_wavefront_t *dev, int onoff)
  51. {
  52. if (!wavefront_fx_idle(dev)) {
  53. return;
  54. }
  55. outb (onoff ? 0x02 : 0x00, dev->fx_op);
  56. }
  57. static int
  58. wavefront_fx_memset (snd_wavefront_t *dev,
  59. int page,
  60. int addr,
  61. int cnt,
  62. unsigned short *data)
  63. {
  64. if (page < 0 || page > 7) {
  65. snd_printk ("FX memset: "
  66. "page must be >= 0 and <= 7\n");
  67. return -(EINVAL);
  68. }
  69. if (addr < 0 || addr > 0x7f) {
  70. snd_printk ("FX memset: "
  71. "addr must be >= 0 and <= 7f\n");
  72. return -(EINVAL);
  73. }
  74. if (cnt == 1) {
  75. outb (FX_LSB_TRANSFER, dev->fx_lcr);
  76. outb (page, dev->fx_dsp_page);
  77. outb (addr, dev->fx_dsp_addr);
  78. outb ((data[0] >> 8), dev->fx_dsp_msb);
  79. outb ((data[0] & 0xff), dev->fx_dsp_lsb);
  80. snd_printk ("FX: addr %d:%x set to 0x%x\n",
  81. page, addr, data[0]);
  82. } else {
  83. int i;
  84. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev->fx_lcr);
  85. outb (page, dev->fx_dsp_page);
  86. outb (addr, dev->fx_dsp_addr);
  87. for (i = 0; i < cnt; i++) {
  88. outb ((data[i] >> 8), dev->fx_dsp_msb);
  89. outb ((data[i] & 0xff), dev->fx_dsp_lsb);
  90. if (!wavefront_fx_idle (dev)) {
  91. break;
  92. }
  93. }
  94. if (i != cnt) {
  95. snd_printk ("FX memset "
  96. "(0x%x, 0x%x, 0x%lx, %d) incomplete\n",
  97. page, addr, (unsigned long) data, cnt);
  98. return -(EIO);
  99. }
  100. }
  101. return 0;
  102. }
  103. int
  104. snd_wavefront_fx_detect (snd_wavefront_t *dev)
  105. {
  106. /* This is a crude check, but its the best one I have for now.
  107. Certainly on the Maui and the Tropez, wavefront_fx_idle() will
  108. report "never idle", which suggests that this test should
  109. work OK.
  110. */
  111. if (inb (dev->fx_status) & 0x80) {
  112. snd_printk ("Hmm, probably a Maui or Tropez.\n");
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. int
  118. snd_wavefront_fx_open (struct snd_hwdep *hw, struct file *file)
  119. {
  120. if (!try_module_get(hw->card->module))
  121. return -EFAULT;
  122. file->private_data = hw;
  123. return 0;
  124. }
  125. int
  126. snd_wavefront_fx_release (struct snd_hwdep *hw, struct file *file)
  127. {
  128. module_put(hw->card->module);
  129. return 0;
  130. }
  131. int
  132. snd_wavefront_fx_ioctl (struct snd_hwdep *sdev, struct file *file,
  133. unsigned int cmd, unsigned long arg)
  134. {
  135. struct snd_card *card;
  136. snd_wavefront_card_t *acard;
  137. snd_wavefront_t *dev;
  138. wavefront_fx_info r;
  139. unsigned short *page_data = NULL;
  140. unsigned short *pd;
  141. int err = 0;
  142. card = sdev->card;
  143. if (snd_BUG_ON(!card))
  144. return -ENODEV;
  145. if (snd_BUG_ON(!card->private_data))
  146. return -ENODEV;
  147. acard = card->private_data;
  148. dev = &acard->wavefront;
  149. if (copy_from_user (&r, (void __user *)arg, sizeof (wavefront_fx_info)))
  150. return -EFAULT;
  151. switch (r.request) {
  152. case WFFX_MUTE:
  153. wavefront_fx_mute (dev, r.data[0]);
  154. return -EIO;
  155. case WFFX_MEMSET:
  156. if (r.data[2] <= 0) {
  157. snd_printk ("cannot write "
  158. "<= 0 bytes to FX\n");
  159. return -EIO;
  160. } else if (r.data[2] == 1) {
  161. pd = (unsigned short *) &r.data[3];
  162. } else {
  163. if (r.data[2] > 256) {
  164. snd_printk ("cannot write "
  165. "> 512 bytes to FX\n");
  166. return -EIO;
  167. }
  168. page_data = kmalloc(r.data[2] * sizeof(short), GFP_KERNEL);
  169. if (!page_data)
  170. return -ENOMEM;
  171. if (copy_from_user (page_data,
  172. (unsigned char __user *) r.data[3],
  173. r.data[2] * sizeof(short))) {
  174. kfree(page_data);
  175. return -EFAULT;
  176. }
  177. pd = page_data;
  178. }
  179. err = wavefront_fx_memset (dev,
  180. r.data[0], /* page */
  181. r.data[1], /* addr */
  182. r.data[2], /* cnt */
  183. pd);
  184. kfree(page_data);
  185. break;
  186. default:
  187. snd_printk ("FX: ioctl %d not yet supported\n",
  188. r.request);
  189. return -ENOTTY;
  190. }
  191. return err;
  192. }
  193. /* YSS225 initialization.
  194. This code was developed using DOSEMU. The Turtle Beach SETUPSND
  195. utility was run with I/O tracing in DOSEMU enabled, and a reconstruction
  196. of the port I/O done, using the Yamaha faxback document as a guide
  197. to add more logic to the code. Its really pretty weird.
  198. This is the approach of just dumping the whole I/O
  199. sequence as a series of port/value pairs and a simple loop
  200. that outputs it.
  201. */
  202. int __devinit
  203. snd_wavefront_fx_start (snd_wavefront_t *dev)
  204. {
  205. unsigned int i;
  206. int err;
  207. const struct firmware *firmware = NULL;
  208. if (dev->fx_initialized)
  209. return 0;
  210. err = request_firmware(&firmware, "yamaha/yss225_registers.bin",
  211. dev->card->dev);
  212. if (err < 0) {
  213. err = -1;
  214. goto out;
  215. }
  216. for (i = 0; i + 1 < firmware->size; i += 2) {
  217. if (firmware->data[i] >= 8 && firmware->data[i] < 16) {
  218. outb(firmware->data[i + 1],
  219. dev->base + firmware->data[i]);
  220. } else if (firmware->data[i] == WAIT_IDLE) {
  221. if (!wavefront_fx_idle(dev)) {
  222. err = -1;
  223. goto out;
  224. }
  225. } else {
  226. snd_printk(KERN_ERR "invalid address"
  227. " in register data\n");
  228. err = -1;
  229. goto out;
  230. }
  231. }
  232. dev->fx_initialized = 1;
  233. err = 0;
  234. out:
  235. release_firmware(firmware);
  236. return err;
  237. }
  238. MODULE_FIRMWARE("yamaha/yss225_registers.bin");