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