blob: fc95a870f69017aa17cf20f175633abb0d1af8d1 [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
19#include <sound/driver.h>
20#include <asm/io.h>
21#include <linux/init.h>
22#include <linux/time.h>
23#include <linux/wait.h>
Clemens Ladisch226968c2006-11-06 09:21:58 +010024#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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 Ladisch59540fe2006-11-06 09:20:04 +010036#define WAIT_IDLE 0xff
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai8ad2da12007-02-26 15:55:43 +010038#ifdef CONFIG_SND_WAVEFRONT_FIRMWARE_IN_KERNEL
Clemens Ladisch59540fe2006-11-06 09:20:04 +010039#include "yss225.c"
Clemens Ladisch226968c2006-11-06 09:21:58 +010040static const struct firmware yss225_registers_firmware = {
41 .data = (u8 *)yss225_registers,
42 .size = sizeof yss225_registers
43};
44#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46static int
47wavefront_fx_idle (snd_wavefront_t *dev)
48
49{
50 int i;
51 unsigned int x = 0x80;
52
53 for (i = 0; i < 1000; i++) {
54 x = inb (dev->fx_status);
55 if ((x & 0x80) == 0) {
56 break;
57 }
58 }
59
60 if (x & 0x80) {
61 snd_printk ("FX device never idle.\n");
62 return 0;
63 }
64
65 return (1);
66}
67
68static void
69wavefront_fx_mute (snd_wavefront_t *dev, int onoff)
70
71{
72 if (!wavefront_fx_idle(dev)) {
73 return;
74 }
75
76 outb (onoff ? 0x02 : 0x00, dev->fx_op);
77}
78
79static int
80wavefront_fx_memset (snd_wavefront_t *dev,
81 int page,
82 int addr,
83 int cnt,
84 unsigned short *data)
85{
86 if (page < 0 || page > 7) {
87 snd_printk ("FX memset: "
88 "page must be >= 0 and <= 7\n");
89 return -(EINVAL);
90 }
91
92 if (addr < 0 || addr > 0x7f) {
93 snd_printk ("FX memset: "
94 "addr must be >= 0 and <= 7f\n");
95 return -(EINVAL);
96 }
97
98 if (cnt == 1) {
99
100 outb (FX_LSB_TRANSFER, dev->fx_lcr);
101 outb (page, dev->fx_dsp_page);
102 outb (addr, dev->fx_dsp_addr);
103 outb ((data[0] >> 8), dev->fx_dsp_msb);
104 outb ((data[0] & 0xff), dev->fx_dsp_lsb);
105
106 snd_printk ("FX: addr %d:%x set to 0x%x\n",
107 page, addr, data[0]);
108
109 } else {
110 int i;
111
112 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev->fx_lcr);
113 outb (page, dev->fx_dsp_page);
114 outb (addr, dev->fx_dsp_addr);
115
116 for (i = 0; i < cnt; i++) {
117 outb ((data[i] >> 8), dev->fx_dsp_msb);
118 outb ((data[i] & 0xff), dev->fx_dsp_lsb);
119 if (!wavefront_fx_idle (dev)) {
120 break;
121 }
122 }
123
124 if (i != cnt) {
125 snd_printk ("FX memset "
126 "(0x%x, 0x%x, 0x%lx, %d) incomplete\n",
127 page, addr, (unsigned long) data, cnt);
128 return -(EIO);
129 }
130 }
131
132 return 0;
133}
134
135int
136snd_wavefront_fx_detect (snd_wavefront_t *dev)
137
138{
139 /* This is a crude check, but its the best one I have for now.
140 Certainly on the Maui and the Tropez, wavefront_fx_idle() will
141 report "never idle", which suggests that this test should
142 work OK.
143 */
144
145 if (inb (dev->fx_status) & 0x80) {
146 snd_printk ("Hmm, probably a Maui or Tropez.\n");
147 return -1;
148 }
149
150 return 0;
151}
152
153int
Takashi Iwai542172f2005-11-17 14:39:06 +0100154snd_wavefront_fx_open (struct snd_hwdep *hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156{
157 if (!try_module_get(hw->card->module))
158 return -EFAULT;
159 file->private_data = hw;
160 return 0;
161}
162
163int
Takashi Iwai542172f2005-11-17 14:39:06 +0100164snd_wavefront_fx_release (struct snd_hwdep *hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166{
167 module_put(hw->card->module);
168 return 0;
169}
170
171int
Takashi Iwai542172f2005-11-17 14:39:06 +0100172snd_wavefront_fx_ioctl (struct snd_hwdep *sdev, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned int cmd, unsigned long arg)
174
175{
Takashi Iwai542172f2005-11-17 14:39:06 +0100176 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 snd_wavefront_card_t *acard;
178 snd_wavefront_t *dev;
179 wavefront_fx_info r;
180 unsigned short *page_data = NULL;
181 unsigned short *pd;
182 int err = 0;
183
184 snd_assert(sdev->card != NULL, return -ENODEV);
185
186 card = sdev->card;
187
188 snd_assert(card->private_data != NULL, return -ENODEV);
189
190 acard = card->private_data;
191 dev = &acard->wavefront;
192
193 if (copy_from_user (&r, (void __user *)arg, sizeof (wavefront_fx_info)))
194 return -EFAULT;
195
196 switch (r.request) {
197 case WFFX_MUTE:
198 wavefront_fx_mute (dev, r.data[0]);
199 return -EIO;
200
201 case WFFX_MEMSET:
202 if (r.data[2] <= 0) {
203 snd_printk ("cannot write "
204 "<= 0 bytes to FX\n");
205 return -EIO;
206 } else if (r.data[2] == 1) {
207 pd = (unsigned short *) &r.data[3];
208 } else {
209 if (r.data[2] > 256) {
210 snd_printk ("cannot write "
211 "> 512 bytes to FX\n");
212 return -EIO;
213 }
214 page_data = kmalloc(r.data[2] * sizeof(short), GFP_KERNEL);
215 if (!page_data)
216 return -ENOMEM;
217 if (copy_from_user (page_data,
218 (unsigned char __user *) r.data[3],
219 r.data[2] * sizeof(short))) {
220 kfree(page_data);
221 return -EFAULT;
222 }
223 pd = page_data;
224 }
225
226 err = wavefront_fx_memset (dev,
227 r.data[0], /* page */
228 r.data[1], /* addr */
229 r.data[2], /* cnt */
230 pd);
231 kfree(page_data);
232 break;
233
234 default:
235 snd_printk ("FX: ioctl %d not yet supported\n",
236 r.request);
237 return -ENOTTY;
238 }
239 return err;
240}
241
242/* YSS225 initialization.
243
244 This code was developed using DOSEMU. The Turtle Beach SETUPSND
245 utility was run with I/O tracing in DOSEMU enabled, and a reconstruction
246 of the port I/O done, using the Yamaha faxback document as a guide
247 to add more logic to the code. Its really pretty weird.
248
Clemens Ladisch59540fe2006-11-06 09:20:04 +0100249 This is the approach of just dumping the whole I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 sequence as a series of port/value pairs and a simple loop
Clemens Ladisch59540fe2006-11-06 09:20:04 +0100251 that outputs it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252*/
253
Clemens Ladisch40e1a9c2006-07-03 16:38:28 +0200254int __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255snd_wavefront_fx_start (snd_wavefront_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Clemens Ladisch59540fe2006-11-06 09:20:04 +0100257 unsigned int i;
Clemens Ladisch226968c2006-11-06 09:21:58 +0100258 int err;
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200259 const struct firmware *firmware = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Clemens Ladisch59540fe2006-11-06 09:20:04 +0100261 if (dev->fx_initialized)
262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200264#ifdef CONFIG_SND_WAVEFRONT_FIRMWARE_IN_KERNEL
265 firmware = &yss225_registers_firmware;
266#else
Clemens Ladisch226968c2006-11-06 09:21:58 +0100267 err = request_firmware(&firmware, "yamaha/yss225_registers.bin",
268 dev->card->dev);
269 if (err < 0) {
Clemens Ladisch226968c2006-11-06 09:21:58 +0100270 err = -1;
271 goto out;
Clemens Ladisch226968c2006-11-06 09:21:58 +0100272 }
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200273#endif
Clemens Ladisch226968c2006-11-06 09:21:58 +0100274
275 for (i = 0; i + 1 < firmware->size; i += 2) {
276 if (firmware->data[i] >= 8 && firmware->data[i] < 16) {
277 outb(firmware->data[i + 1],
278 dev->base + firmware->data[i]);
279 } else if (firmware->data[i] == WAIT_IDLE) {
280 if (!wavefront_fx_idle(dev)) {
281 err = -1;
282 goto out;
283 }
Clemens Ladisch59540fe2006-11-06 09:20:04 +0100284 } else {
285 snd_printk(KERN_ERR "invalid address"
286 " in register data\n");
Clemens Ladisch226968c2006-11-06 09:21:58 +0100287 err = -1;
288 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 }
291
Clemens Ladisch59540fe2006-11-06 09:20:04 +0100292 dev->fx_initialized = 1;
Clemens Ladisch226968c2006-11-06 09:21:58 +0100293 err = 0;
294
295out:
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200296#ifndef CONFIG_SND_WAVEFRONT_FIRMWARE_IN_KERNEL
297 release_firmware(firmware);
Clemens Ladisch226968c2006-11-06 09:21:58 +0100298#endif
Clemens Ladisch226968c2006-11-06 09:21:58 +0100299 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
Clemens Ladisch7e0af292007-05-03 17:59:54 +0200301
302#ifndef CONFIG_SND_WAVEFRONT_FIRMWARE_IN_KERNEL
303MODULE_FIRMWARE("yamaha/yss225_registers.bin");
304#endif