blob: 5e6870baa296ad206afaa4cdd084e317751f05c2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/io.h>
20#include <linux/init.h>
21#include <linux/time.h>
22#include <linux/wait.h>
Clemens Ladisch226968c2006-11-06 09:21:58 +010023#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <sound/core.h>
25#include <sound/snd_wavefront.h>
26#include <sound/initval.h>
27
28/* Control bits for the Load Control Register
29 */
30
31#define FX_LSB_TRANSFER 0x01 /* transfer after DSP LSB byte written */
32#define FX_MSB_TRANSFER 0x02 /* transfer after DSP MSB byte written */
33#define FX_AUTO_INCR 0x04 /* auto-increment DSP address after transfer */
34
Clemens Ladisch59540fe2006-11-06 09:20:04 +010035#define WAIT_IDLE 0xff
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Takashi Iwai8ad2da12007-02-26 15:55:43 +010037#ifdef CONFIG_SND_WAVEFRONT_FIRMWARE_IN_KERNEL
Clemens Ladisch59540fe2006-11-06 09:20:04 +010038#include "yss225.c"
Clemens Ladisch226968c2006-11-06 09:21:58 +010039static const struct firmware yss225_registers_firmware = {
40 .data = (u8 *)yss225_registers,
41 .size = sizeof yss225_registers
42};
43#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static int
46wavefront_fx_idle (snd_wavefront_t *dev)
47
48{
49 int i;
50 unsigned int x = 0x80;
51
52 for (i = 0; i < 1000; i++) {
53 x = inb (dev->fx_status);
54 if ((x & 0x80) == 0) {
55 break;
56 }
57 }
58
59 if (x & 0x80) {
60 snd_printk ("FX device never idle.\n");
61 return 0;
62 }
63
64 return (1);
65}
66
67static void
68wavefront_fx_mute (snd_wavefront_t *dev, int onoff)
69
70{
71 if (!wavefront_fx_idle(dev)) {
72 return;
73 }
74
75 outb (onoff ? 0x02 : 0x00, dev->fx_op);
76}
77
78static int
79wavefront_fx_memset (snd_wavefront_t *dev,
80 int page,
81 int addr,
82 int cnt,
83 unsigned short *data)
84{
85 if (page < 0 || page > 7) {
86 snd_printk ("FX memset: "
87 "page must be >= 0 and <= 7\n");
88 return -(EINVAL);
89 }
90
91 if (addr < 0 || addr > 0x7f) {
92 snd_printk ("FX memset: "
93 "addr must be >= 0 and <= 7f\n");
94 return -(EINVAL);
95 }
96
97 if (cnt == 1) {
98
99 outb (FX_LSB_TRANSFER, dev->fx_lcr);
100 outb (page, dev->fx_dsp_page);
101 outb (addr, dev->fx_dsp_addr);
102 outb ((data[0] >> 8), dev->fx_dsp_msb);
103 outb ((data[0] & 0xff), dev->fx_dsp_lsb);
104
105 snd_printk ("FX: addr %d:%x set to 0x%x\n",
106 page, addr, data[0]);
107
108 } else {
109 int i;
110
111 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev->fx_lcr);
112 outb (page, dev->fx_dsp_page);
113 outb (addr, dev->fx_dsp_addr);
114
115 for (i = 0; i < cnt; i++) {
116 outb ((data[i] >> 8), dev->fx_dsp_msb);
117 outb ((data[i] & 0xff), dev->fx_dsp_lsb);
118 if (!wavefront_fx_idle (dev)) {
119 break;
120 }
121 }
122
123 if (i != cnt) {
124 snd_printk ("FX memset "
125 "(0x%x, 0x%x, 0x%lx, %d) incomplete\n",
126 page, addr, (unsigned long) data, cnt);
127 return -(EIO);
128 }
129 }
130
131 return 0;
132}
133
134int
135snd_wavefront_fx_detect (snd_wavefront_t *dev)
136
137{
138 /* This is a crude check, but its the best one I have for now.
139 Certainly on the Maui and the Tropez, wavefront_fx_idle() will
140 report "never idle", which suggests that this test should
141 work OK.
142 */
143
144 if (inb (dev->fx_status) & 0x80) {
145 snd_printk ("Hmm, probably a Maui or Tropez.\n");
146 return -1;
147 }
148
149 return 0;
150}
151
152int
Takashi Iwai542172f2005-11-17 14:39:06 +0100153snd_wavefront_fx_open (struct snd_hwdep *hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155{
156 if (!try_module_get(hw->card->module))
157 return -EFAULT;
158 file->private_data = hw;
159 return 0;
160}
161
162int
Takashi Iwai542172f2005-11-17 14:39:06 +0100163snd_wavefront_fx_release (struct snd_hwdep *hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165{
166 module_put(hw->card->module);
167 return 0;
168}
169
170int
Takashi Iwai542172f2005-11-17 14:39:06 +0100171snd_wavefront_fx_ioctl (struct snd_hwdep *sdev, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned int cmd, unsigned long arg)
173
174{
Takashi Iwai542172f2005-11-17 14:39:06 +0100175 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 snd_wavefront_card_t *acard;
177 snd_wavefront_t *dev;
178 wavefront_fx_info r;
179 unsigned short *page_data = NULL;
180 unsigned short *pd;
181 int err = 0;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 card = sdev->card;
Takashi Iwai622207d2008-08-08 17:11:45 +0200184 if (snd_BUG_ON(!card))
185 return -ENODEV;
186 if (snd_BUG_ON(!card->private_data))
187 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 acard = card->private_data;
190 dev = &acard->wavefront;
191
192 if (copy_from_user (&r, (void __user *)arg, sizeof (wavefront_fx_info)))
193 return -EFAULT;
194
195 switch (r.request) {
196 case WFFX_MUTE:
197 wavefront_fx_mute (dev, r.data[0]);
198 return -EIO;
199
200 case WFFX_MEMSET:
201 if (r.data[2] <= 0) {
202 snd_printk ("cannot write "
203 "<= 0 bytes to FX\n");
204 return -EIO;
205 } else if (r.data[2] == 1) {
206 pd = (unsigned short *) &r.data[3];
207 } else {
208 if (r.data[2] > 256) {
209 snd_printk ("cannot write "
210 "> 512 bytes to FX\n");
211 return -EIO;
212 }
Li Zefan68425ad2009-04-10 09:43:36 +0800213 page_data = memdup_user((unsigned char __user *)
214 r.data[3],
215 r.data[2] * sizeof(short));
216 if (IS_ERR(page_data))
217 return PTR_ERR(page_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 pd = page_data;
219 }
220
221 err = wavefront_fx_memset (dev,
222 r.data[0], /* page */
223 r.data[1], /* addr */
224 r.data[2], /* cnt */
225 pd);
226 kfree(page_data);
227 break;
228
229 default:
230 snd_printk ("FX: ioctl %d not yet supported\n",
231 r.request);
232 return -ENOTTY;
233 }
234 return err;
235}
236
237/* YSS225 initialization.
238
239 This code was developed using DOSEMU. The Turtle Beach SETUPSND
240 utility was run with I/O tracing in DOSEMU enabled, and a reconstruction
241 of the port I/O done, using the Yamaha faxback document as a guide
242 to add more logic to the code. Its really pretty weird.
243
Clemens Ladisch59540fe2006-11-06 09:20:04 +0100244 This is the approach of just dumping the whole I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 sequence as a series of port/value pairs and a simple loop
Clemens Ladisch59540fe2006-11-06 09:20:04 +0100246 that outputs it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247*/
248
Clemens Ladisch40e1a9c2006-07-03 16:38:28 +0200249int __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250snd_wavefront_fx_start (snd_wavefront_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Clemens Ladisch59540fe2006-11-06 09:20:04 +0100252 unsigned int i;
Clemens Ladisch226968c2006-11-06 09:21:58 +0100253 int err;
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200254 const struct firmware *firmware = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Clemens Ladisch59540fe2006-11-06 09:20:04 +0100256 if (dev->fx_initialized)
257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200259#ifdef CONFIG_SND_WAVEFRONT_FIRMWARE_IN_KERNEL
260 firmware = &yss225_registers_firmware;
261#else
Clemens Ladisch226968c2006-11-06 09:21:58 +0100262 err = request_firmware(&firmware, "yamaha/yss225_registers.bin",
263 dev->card->dev);
264 if (err < 0) {
Clemens Ladisch226968c2006-11-06 09:21:58 +0100265 err = -1;
266 goto out;
Clemens Ladisch226968c2006-11-06 09:21:58 +0100267 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200268#endif
Clemens Ladisch226968c2006-11-06 09:21:58 +0100269
270 for (i = 0; i + 1 < firmware->size; i += 2) {
271 if (firmware->data[i] >= 8 && firmware->data[i] < 16) {
272 outb(firmware->data[i + 1],
273 dev->base + firmware->data[i]);
274 } else if (firmware->data[i] == WAIT_IDLE) {
275 if (!wavefront_fx_idle(dev)) {
276 err = -1;
277 goto out;
278 }
Clemens Ladisch59540fe2006-11-06 09:20:04 +0100279 } else {
280 snd_printk(KERN_ERR "invalid address"
281 " in register data\n");
Clemens Ladisch226968c2006-11-06 09:21:58 +0100282 err = -1;
283 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285 }
286
Clemens Ladisch59540fe2006-11-06 09:20:04 +0100287 dev->fx_initialized = 1;
Clemens Ladisch226968c2006-11-06 09:21:58 +0100288 err = 0;
289
290out:
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200291#ifndef CONFIG_SND_WAVEFRONT_FIRMWARE_IN_KERNEL
292 release_firmware(firmware);
Clemens Ladisch226968c2006-11-06 09:21:58 +0100293#endif
Clemens Ladisch226968c2006-11-06 09:21:58 +0100294 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
Clemens Ladisch7e0af292007-05-03 17:59:54 +0200296
297#ifndef CONFIG_SND_WAVEFRONT_FIRMWARE_IN_KERNEL
298MODULE_FIRMWARE("yamaha/yss225_registers.bin");
299#endif