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