wavefront_fx.c 6.7 KB

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