1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 1998-2002 by Paul Davis <pbd@op.net>
7 #include <linux/init.h>
8 #include <linux/time.h>
9 #include <linux/wait.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/firmware.h>
13 #include <sound/core.h>
14 #include <sound/snd_wavefront.h>
15 #include <sound/initval.h>
17 /* Control bits for the Load Control Register
20 #define FX_LSB_TRANSFER 0x01 /* transfer after DSP LSB byte written */
21 #define FX_MSB_TRANSFER 0x02 /* transfer after DSP MSB byte written */
22 #define FX_AUTO_INCR 0x04 /* auto-increment DSP address after transfer */
24 #define WAIT_IDLE 0xff
27 wavefront_fx_idle (snd_wavefront_t
*dev
)
31 unsigned int x
= 0x80;
33 for (i
= 0; i
< 1000; i
++) {
34 x
= inb (dev
->fx_status
);
35 if ((x
& 0x80) == 0) {
41 snd_printk ("FX device never idle.\n");
49 wavefront_fx_mute (snd_wavefront_t
*dev
, int onoff
)
52 if (!wavefront_fx_idle(dev
)) {
56 outb (onoff
? 0x02 : 0x00, dev
->fx_op
);
60 wavefront_fx_memset (snd_wavefront_t
*dev
,
66 if (page
< 0 || page
> 7) {
67 snd_printk ("FX memset: "
68 "page must be >= 0 and <= 7\n");
72 if (addr
< 0 || addr
> 0x7f) {
73 snd_printk ("FX memset: "
74 "addr must be >= 0 and <= 7f\n");
80 outb (FX_LSB_TRANSFER
, dev
->fx_lcr
);
81 outb (page
, dev
->fx_dsp_page
);
82 outb (addr
, dev
->fx_dsp_addr
);
83 outb ((data
[0] >> 8), dev
->fx_dsp_msb
);
84 outb ((data
[0] & 0xff), dev
->fx_dsp_lsb
);
86 snd_printk ("FX: addr %d:%x set to 0x%x\n",
92 outb (FX_AUTO_INCR
|FX_LSB_TRANSFER
, dev
->fx_lcr
);
93 outb (page
, dev
->fx_dsp_page
);
94 outb (addr
, dev
->fx_dsp_addr
);
96 for (i
= 0; i
< cnt
; i
++) {
97 outb ((data
[i
] >> 8), dev
->fx_dsp_msb
);
98 outb ((data
[i
] & 0xff), dev
->fx_dsp_lsb
);
99 if (!wavefront_fx_idle (dev
)) {
105 snd_printk ("FX memset "
106 "(0x%x, 0x%x, 0x%lx, %d) incomplete\n",
107 page
, addr
, (unsigned long) data
, cnt
);
116 snd_wavefront_fx_detect (snd_wavefront_t
*dev
)
119 /* This is a crude check, but its the best one I have for now.
120 Certainly on the Maui and the Tropez, wavefront_fx_idle() will
121 report "never idle", which suggests that this test should
125 if (inb (dev
->fx_status
) & 0x80) {
126 snd_printk ("Hmm, probably a Maui or Tropez.\n");
134 snd_wavefront_fx_open (struct snd_hwdep
*hw
, struct file
*file
)
137 if (!try_module_get(hw
->card
->module
))
139 file
->private_data
= hw
;
144 snd_wavefront_fx_release (struct snd_hwdep
*hw
, struct file
*file
)
147 module_put(hw
->card
->module
);
152 snd_wavefront_fx_ioctl (struct snd_hwdep
*sdev
, struct file
*file
,
153 unsigned int cmd
, unsigned long arg
)
156 struct snd_card
*card
;
157 snd_wavefront_card_t
*acard
;
158 snd_wavefront_t
*dev
;
160 unsigned short *page_data
= NULL
;
165 if (snd_BUG_ON(!card
))
167 if (snd_BUG_ON(!card
->private_data
))
170 acard
= card
->private_data
;
171 dev
= &acard
->wavefront
;
173 if (copy_from_user (&r
, (void __user
*)arg
, sizeof (wavefront_fx_info
)))
178 wavefront_fx_mute (dev
, r
.data
[0]);
182 if (r
.data
[2] <= 0) {
183 snd_printk ("cannot write "
184 "<= 0 bytes to FX\n");
186 } else if (r
.data
[2] == 1) {
187 pd
= (unsigned short *) &r
.data
[3];
189 if (r
.data
[2] > 256) {
190 snd_printk ("cannot write "
191 "> 512 bytes to FX\n");
194 page_data
= memdup_user((unsigned char __user
*)
196 r
.data
[2] * sizeof(short));
197 if (IS_ERR(page_data
))
198 return PTR_ERR(page_data
);
202 err
= wavefront_fx_memset (dev
,
203 r
.data
[0], /* page */
204 r
.data
[1], /* addr */
211 snd_printk ("FX: ioctl %d not yet supported\n",
218 /* YSS225 initialization.
220 This code was developed using DOSEMU. The Turtle Beach SETUPSND
221 utility was run with I/O tracing in DOSEMU enabled, and a reconstruction
222 of the port I/O done, using the Yamaha faxback document as a guide
223 to add more logic to the code. Its really pretty weird.
225 This is the approach of just dumping the whole I/O
226 sequence as a series of port/value pairs and a simple loop
231 snd_wavefront_fx_start (snd_wavefront_t
*dev
)
235 const struct firmware
*firmware
= NULL
;
237 if (dev
->fx_initialized
)
240 err
= request_firmware(&firmware
, "yamaha/yss225_registers.bin",
247 for (i
= 0; i
+ 1 < firmware
->size
; i
+= 2) {
248 if (firmware
->data
[i
] >= 8 && firmware
->data
[i
] < 16) {
249 outb(firmware
->data
[i
+ 1],
250 dev
->base
+ firmware
->data
[i
]);
251 } else if (firmware
->data
[i
] == WAIT_IDLE
) {
252 if (!wavefront_fx_idle(dev
)) {
257 snd_printk(KERN_ERR
"invalid address"
258 " in register data\n");
264 dev
->fx_initialized
= 1;
268 release_firmware(firmware
);
272 MODULE_FIRMWARE("yamaha/yss225_registers.bin");