wavefront_fx.c 6.1 KB

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