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