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