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