wavefront_fx.c 6.1 KB

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