xref: /OK3568_Linux_fs/kernel/sound/isa/wavefront/wavefront_synth.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /* Copyright (C) by Paul Barton-Davis 1998-1999
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Some portions of this file are taken from work that is
5*4882a593Smuzhiyun  * copyright (C) by Hannu Savolainen 1993-1996
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun  * An ALSA lowlevel driver for Turtle Beach ICS2115 wavetable synth
10*4882a593Smuzhiyun  *                                             (Maui, Tropez, Tropez Plus)
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * This driver supports the onboard wavetable synthesizer (an ICS2115),
13*4882a593Smuzhiyun  * including patch, sample and program loading and unloading, conversion
14*4882a593Smuzhiyun  * of GUS patches during loading, and full user-level access to all
15*4882a593Smuzhiyun  * WaveFront commands. It tries to provide semi-intelligent patch and
16*4882a593Smuzhiyun  * sample management as well.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <linux/io.h>
21*4882a593Smuzhiyun #include <linux/interrupt.h>
22*4882a593Smuzhiyun #include <linux/init.h>
23*4882a593Smuzhiyun #include <linux/delay.h>
24*4882a593Smuzhiyun #include <linux/time.h>
25*4882a593Smuzhiyun #include <linux/wait.h>
26*4882a593Smuzhiyun #include <linux/sched/signal.h>
27*4882a593Smuzhiyun #include <linux/firmware.h>
28*4882a593Smuzhiyun #include <linux/moduleparam.h>
29*4882a593Smuzhiyun #include <linux/slab.h>
30*4882a593Smuzhiyun #include <linux/module.h>
31*4882a593Smuzhiyun #include <sound/core.h>
32*4882a593Smuzhiyun #include <sound/snd_wavefront.h>
33*4882a593Smuzhiyun #include <sound/initval.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun static int wf_raw = 0; /* we normally check for "raw state" to firmware
36*4882a593Smuzhiyun 			  loading. if non-zero, then during driver loading, the
37*4882a593Smuzhiyun 			  state of the board is ignored, and we reset the
38*4882a593Smuzhiyun 			  board and load the firmware anyway.
39*4882a593Smuzhiyun 		       */
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun static int fx_raw = 1; /* if this is zero, we'll leave the FX processor in
42*4882a593Smuzhiyun 			  whatever state it is when the driver is loaded.
43*4882a593Smuzhiyun 			  The default is to download the microprogram and
44*4882a593Smuzhiyun 			  associated coefficients to set it up for "default"
45*4882a593Smuzhiyun 			  operation, whatever that means.
46*4882a593Smuzhiyun 		       */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun static int debug_default = 0;  /* you can set this to control debugging
49*4882a593Smuzhiyun 				  during driver loading. it takes any combination
50*4882a593Smuzhiyun 				  of the WF_DEBUG_* flags defined in
51*4882a593Smuzhiyun 				  wavefront.h
52*4882a593Smuzhiyun 			       */
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /* XXX this needs to be made firmware and hardware version dependent */
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define DEFAULT_OSPATH	"wavefront.os"
57*4882a593Smuzhiyun static char *ospath = DEFAULT_OSPATH; /* the firmware file name */
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun static int wait_usecs = 150; /* This magic number seems to give pretty optimal
60*4882a593Smuzhiyun 				throughput based on my limited experimentation.
61*4882a593Smuzhiyun 				If you want to play around with it and find a better
62*4882a593Smuzhiyun 				value, be my guest. Remember, the idea is to
63*4882a593Smuzhiyun 				get a number that causes us to just busy wait
64*4882a593Smuzhiyun 				for as many WaveFront commands as possible, without
65*4882a593Smuzhiyun 				coming up with a number so large that we hog the
66*4882a593Smuzhiyun 				whole CPU.
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 				Specifically, with this number, out of about 134,000
69*4882a593Smuzhiyun 				status waits, only about 250 result in a sleep.
70*4882a593Smuzhiyun 			    */
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun static int sleep_interval = 100;   /* HZ/sleep_interval seconds per sleep */
73*4882a593Smuzhiyun static int sleep_tries = 50;       /* number of times we'll try to sleep */
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun static int reset_time = 2;        /* hundreths of a second we wait after a HW
76*4882a593Smuzhiyun 				     reset for the expected interrupt.
77*4882a593Smuzhiyun 				  */
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun static int ramcheck_time = 20;    /* time in seconds to wait while ROM code
80*4882a593Smuzhiyun 				     checks on-board RAM.
81*4882a593Smuzhiyun 				  */
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun static int osrun_time = 10;       /* time in seconds we wait for the OS to
84*4882a593Smuzhiyun 				     start running.
85*4882a593Smuzhiyun 				  */
86*4882a593Smuzhiyun module_param(wf_raw, int, 0444);
87*4882a593Smuzhiyun MODULE_PARM_DESC(wf_raw, "if non-zero, assume that we need to boot the OS");
88*4882a593Smuzhiyun module_param(fx_raw, int, 0444);
89*4882a593Smuzhiyun MODULE_PARM_DESC(fx_raw, "if non-zero, assume that the FX process needs help");
90*4882a593Smuzhiyun module_param(debug_default, int, 0444);
91*4882a593Smuzhiyun MODULE_PARM_DESC(debug_default, "debug parameters for card initialization");
92*4882a593Smuzhiyun module_param(wait_usecs, int, 0444);
93*4882a593Smuzhiyun MODULE_PARM_DESC(wait_usecs, "how long to wait without sleeping, usecs");
94*4882a593Smuzhiyun module_param(sleep_interval, int, 0444);
95*4882a593Smuzhiyun MODULE_PARM_DESC(sleep_interval, "how long to sleep when waiting for reply");
96*4882a593Smuzhiyun module_param(sleep_tries, int, 0444);
97*4882a593Smuzhiyun MODULE_PARM_DESC(sleep_tries, "how many times to try sleeping during a wait");
98*4882a593Smuzhiyun module_param(ospath, charp, 0444);
99*4882a593Smuzhiyun MODULE_PARM_DESC(ospath, "pathname to processed ICS2115 OS firmware");
100*4882a593Smuzhiyun module_param(reset_time, int, 0444);
101*4882a593Smuzhiyun MODULE_PARM_DESC(reset_time, "how long to wait for a reset to take effect");
102*4882a593Smuzhiyun module_param(ramcheck_time, int, 0444);
103*4882a593Smuzhiyun MODULE_PARM_DESC(ramcheck_time, "how many seconds to wait for the RAM test");
104*4882a593Smuzhiyun module_param(osrun_time, int, 0444);
105*4882a593Smuzhiyun MODULE_PARM_DESC(osrun_time, "how many seconds to wait for the ICS2115 OS");
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun /* if WF_DEBUG not defined, no run-time debugging messages will
108*4882a593Smuzhiyun    be available via the debug flag setting. Given the current
109*4882a593Smuzhiyun    beta state of the driver, this will remain set until a future
110*4882a593Smuzhiyun    version.
111*4882a593Smuzhiyun */
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun #define WF_DEBUG 1
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun #ifdef WF_DEBUG
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun #define DPRINT(cond, ...) \
118*4882a593Smuzhiyun        if ((dev->debug & (cond)) == (cond)) { \
119*4882a593Smuzhiyun 	     snd_printk (__VA_ARGS__); \
120*4882a593Smuzhiyun        }
121*4882a593Smuzhiyun #else
122*4882a593Smuzhiyun #define DPRINT(cond, args...)
123*4882a593Smuzhiyun #endif /* WF_DEBUG */
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun #define LOGNAME "WaveFront: "
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /* bitmasks for WaveFront status port value */
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun #define STAT_RINTR_ENABLED	0x01
130*4882a593Smuzhiyun #define STAT_CAN_READ		0x02
131*4882a593Smuzhiyun #define STAT_INTR_READ		0x04
132*4882a593Smuzhiyun #define STAT_WINTR_ENABLED	0x10
133*4882a593Smuzhiyun #define STAT_CAN_WRITE		0x20
134*4882a593Smuzhiyun #define STAT_INTR_WRITE		0x40
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun static int wavefront_delete_sample (snd_wavefront_t *, int sampnum);
137*4882a593Smuzhiyun static int wavefront_find_free_sample (snd_wavefront_t *);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun struct wavefront_command {
140*4882a593Smuzhiyun 	int cmd;
141*4882a593Smuzhiyun 	char *action;
142*4882a593Smuzhiyun 	unsigned int read_cnt;
143*4882a593Smuzhiyun 	unsigned int write_cnt;
144*4882a593Smuzhiyun 	int need_ack;
145*4882a593Smuzhiyun };
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun static struct {
148*4882a593Smuzhiyun 	int errno;
149*4882a593Smuzhiyun 	const char *errstr;
150*4882a593Smuzhiyun } wavefront_errors[] = {
151*4882a593Smuzhiyun 	{ 0x01, "Bad sample number" },
152*4882a593Smuzhiyun 	{ 0x02, "Out of sample memory" },
153*4882a593Smuzhiyun 	{ 0x03, "Bad patch number" },
154*4882a593Smuzhiyun 	{ 0x04, "Error in number of voices" },
155*4882a593Smuzhiyun 	{ 0x06, "Sample load already in progress" },
156*4882a593Smuzhiyun 	{ 0x0B, "No sample load request pending" },
157*4882a593Smuzhiyun 	{ 0x0E, "Bad MIDI channel number" },
158*4882a593Smuzhiyun 	{ 0x10, "Download Record Error" },
159*4882a593Smuzhiyun 	{ 0x80, "Success" },
160*4882a593Smuzhiyun 	{ 0x0 }
161*4882a593Smuzhiyun };
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun #define NEEDS_ACK 1
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun static struct wavefront_command wavefront_commands[] = {
166*4882a593Smuzhiyun 	{ WFC_SET_SYNTHVOL, "set synthesizer volume", 0, 1, NEEDS_ACK },
167*4882a593Smuzhiyun 	{ WFC_GET_SYNTHVOL, "get synthesizer volume", 1, 0, 0},
168*4882a593Smuzhiyun 	{ WFC_SET_NVOICES, "set number of voices", 0, 1, NEEDS_ACK },
169*4882a593Smuzhiyun 	{ WFC_GET_NVOICES, "get number of voices", 1, 0, 0 },
170*4882a593Smuzhiyun 	{ WFC_SET_TUNING, "set synthesizer tuning", 0, 2, NEEDS_ACK },
171*4882a593Smuzhiyun 	{ WFC_GET_TUNING, "get synthesizer tuning", 2, 0, 0 },
172*4882a593Smuzhiyun 	{ WFC_DISABLE_CHANNEL, "disable synth channel", 0, 1, NEEDS_ACK },
173*4882a593Smuzhiyun 	{ WFC_ENABLE_CHANNEL, "enable synth channel", 0, 1, NEEDS_ACK },
174*4882a593Smuzhiyun 	{ WFC_GET_CHANNEL_STATUS, "get synth channel status", 3, 0, 0 },
175*4882a593Smuzhiyun 	{ WFC_MISYNTH_OFF, "disable midi-in to synth", 0, 0, NEEDS_ACK },
176*4882a593Smuzhiyun 	{ WFC_MISYNTH_ON, "enable midi-in to synth", 0, 0, NEEDS_ACK },
177*4882a593Smuzhiyun 	{ WFC_VMIDI_ON, "enable virtual midi mode", 0, 0, NEEDS_ACK },
178*4882a593Smuzhiyun 	{ WFC_VMIDI_OFF, "disable virtual midi mode", 0, 0, NEEDS_ACK },
179*4882a593Smuzhiyun 	{ WFC_MIDI_STATUS, "report midi status", 1, 0, 0 },
180*4882a593Smuzhiyun 	{ WFC_FIRMWARE_VERSION, "report firmware version", 2, 0, 0 },
181*4882a593Smuzhiyun 	{ WFC_HARDWARE_VERSION, "report hardware version", 2, 0, 0 },
182*4882a593Smuzhiyun 	{ WFC_GET_NSAMPLES, "report number of samples", 2, 0, 0 },
183*4882a593Smuzhiyun 	{ WFC_INSTOUT_LEVELS, "report instantaneous output levels", 7, 0, 0 },
184*4882a593Smuzhiyun 	{ WFC_PEAKOUT_LEVELS, "report peak output levels", 7, 0, 0 },
185*4882a593Smuzhiyun 	{ WFC_DOWNLOAD_SAMPLE, "download sample",
186*4882a593Smuzhiyun 	  0, WF_SAMPLE_BYTES, NEEDS_ACK },
187*4882a593Smuzhiyun 	{ WFC_DOWNLOAD_BLOCK, "download block", 0, 0, NEEDS_ACK},
188*4882a593Smuzhiyun 	{ WFC_DOWNLOAD_SAMPLE_HEADER, "download sample header",
189*4882a593Smuzhiyun 	  0, WF_SAMPLE_HDR_BYTES, NEEDS_ACK },
190*4882a593Smuzhiyun 	{ WFC_UPLOAD_SAMPLE_HEADER, "upload sample header", 13, 2, 0 },
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	/* This command requires a variable number of bytes to be written.
193*4882a593Smuzhiyun 	   There is a hack in snd_wavefront_cmd() to support this. The actual
194*4882a593Smuzhiyun 	   count is passed in as the read buffer ptr, cast appropriately.
195*4882a593Smuzhiyun 	   Ugh.
196*4882a593Smuzhiyun 	*/
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	{ WFC_DOWNLOAD_MULTISAMPLE, "download multisample", 0, 0, NEEDS_ACK },
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	/* This one is a hack as well. We just read the first byte of the
201*4882a593Smuzhiyun 	   response, don't fetch an ACK, and leave the rest to the
202*4882a593Smuzhiyun 	   calling function. Ugly, ugly, ugly.
203*4882a593Smuzhiyun 	*/
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	{ WFC_UPLOAD_MULTISAMPLE, "upload multisample", 2, 1, 0 },
206*4882a593Smuzhiyun 	{ WFC_DOWNLOAD_SAMPLE_ALIAS, "download sample alias",
207*4882a593Smuzhiyun 	  0, WF_ALIAS_BYTES, NEEDS_ACK },
208*4882a593Smuzhiyun 	{ WFC_UPLOAD_SAMPLE_ALIAS, "upload sample alias", WF_ALIAS_BYTES, 2, 0},
209*4882a593Smuzhiyun 	{ WFC_DELETE_SAMPLE, "delete sample", 0, 2, NEEDS_ACK },
210*4882a593Smuzhiyun 	{ WFC_IDENTIFY_SAMPLE_TYPE, "identify sample type", 5, 2, 0 },
211*4882a593Smuzhiyun 	{ WFC_UPLOAD_SAMPLE_PARAMS, "upload sample parameters" },
212*4882a593Smuzhiyun 	{ WFC_REPORT_FREE_MEMORY, "report free memory", 4, 0, 0 },
213*4882a593Smuzhiyun 	{ WFC_DOWNLOAD_PATCH, "download patch", 0, 134, NEEDS_ACK },
214*4882a593Smuzhiyun 	{ WFC_UPLOAD_PATCH, "upload patch", 132, 2, 0 },
215*4882a593Smuzhiyun 	{ WFC_DOWNLOAD_PROGRAM, "download program", 0, 33, NEEDS_ACK },
216*4882a593Smuzhiyun 	{ WFC_UPLOAD_PROGRAM, "upload program", 32, 1, 0 },
217*4882a593Smuzhiyun 	{ WFC_DOWNLOAD_EDRUM_PROGRAM, "download enhanced drum program", 0, 9,
218*4882a593Smuzhiyun 	  NEEDS_ACK},
219*4882a593Smuzhiyun 	{ WFC_UPLOAD_EDRUM_PROGRAM, "upload enhanced drum program", 8, 1, 0},
220*4882a593Smuzhiyun 	{ WFC_SET_EDRUM_CHANNEL, "set enhanced drum program channel",
221*4882a593Smuzhiyun 	  0, 1, NEEDS_ACK },
222*4882a593Smuzhiyun 	{ WFC_DISABLE_DRUM_PROGRAM, "disable drum program", 0, 1, NEEDS_ACK },
223*4882a593Smuzhiyun 	{ WFC_REPORT_CHANNEL_PROGRAMS, "report channel program numbers",
224*4882a593Smuzhiyun 	  32, 0, 0 },
225*4882a593Smuzhiyun 	{ WFC_NOOP, "the no-op command", 0, 0, NEEDS_ACK },
226*4882a593Smuzhiyun 	{ 0x00 }
227*4882a593Smuzhiyun };
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun static const char *
wavefront_errorstr(int errnum)230*4882a593Smuzhiyun wavefront_errorstr (int errnum)
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun 	int i;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	for (i = 0; wavefront_errors[i].errstr; i++) {
236*4882a593Smuzhiyun 		if (wavefront_errors[i].errno == errnum) {
237*4882a593Smuzhiyun 			return wavefront_errors[i].errstr;
238*4882a593Smuzhiyun 		}
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	return "Unknown WaveFront error";
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun static struct wavefront_command *
wavefront_get_command(int cmd)245*4882a593Smuzhiyun wavefront_get_command (int cmd)
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	int i;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	for (i = 0; wavefront_commands[i].cmd != 0; i++) {
251*4882a593Smuzhiyun 		if (cmd == wavefront_commands[i].cmd) {
252*4882a593Smuzhiyun 			return &wavefront_commands[i];
253*4882a593Smuzhiyun 		}
254*4882a593Smuzhiyun 	}
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	return NULL;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun static inline int
wavefront_status(snd_wavefront_t * dev)260*4882a593Smuzhiyun wavefront_status (snd_wavefront_t *dev)
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun 	return inb (dev->status_port);
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun static int
wavefront_sleep(int limit)267*4882a593Smuzhiyun wavefront_sleep (int limit)
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	schedule_timeout_interruptible(limit);
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	return signal_pending(current);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun static int
wavefront_wait(snd_wavefront_t * dev,int mask)276*4882a593Smuzhiyun wavefront_wait (snd_wavefront_t *dev, int mask)
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	int             i;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	/* Spin for a short period of time, because >99% of all
282*4882a593Smuzhiyun 	   requests to the WaveFront can be serviced inline like this.
283*4882a593Smuzhiyun 	*/
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	for (i = 0; i < wait_usecs; i += 5) {
286*4882a593Smuzhiyun 		if (wavefront_status (dev) & mask) {
287*4882a593Smuzhiyun 			return 1;
288*4882a593Smuzhiyun 		}
289*4882a593Smuzhiyun 		udelay(5);
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	for (i = 0; i < sleep_tries; i++) {
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 		if (wavefront_status (dev) & mask) {
295*4882a593Smuzhiyun 			return 1;
296*4882a593Smuzhiyun 		}
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 		if (wavefront_sleep (HZ/sleep_interval)) {
299*4882a593Smuzhiyun 			return (0);
300*4882a593Smuzhiyun 		}
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	return (0);
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun static int
wavefront_read(snd_wavefront_t * dev)307*4882a593Smuzhiyun wavefront_read (snd_wavefront_t *dev)
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun 	if (wavefront_wait (dev, STAT_CAN_READ))
311*4882a593Smuzhiyun 		return inb (dev->data_port);
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_DATA, "read timeout.\n");
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	return -1;
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun static int
wavefront_write(snd_wavefront_t * dev,unsigned char data)319*4882a593Smuzhiyun wavefront_write (snd_wavefront_t *dev, unsigned char data)
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	if (wavefront_wait (dev, STAT_CAN_WRITE)) {
323*4882a593Smuzhiyun 		outb (data, dev->data_port);
324*4882a593Smuzhiyun 		return 0;
325*4882a593Smuzhiyun 	}
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_DATA, "write timeout.\n");
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	return -1;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun int
snd_wavefront_cmd(snd_wavefront_t * dev,int cmd,unsigned char * rbuf,unsigned char * wbuf)333*4882a593Smuzhiyun snd_wavefront_cmd (snd_wavefront_t *dev,
334*4882a593Smuzhiyun 		   int cmd, unsigned char *rbuf, unsigned char *wbuf)
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	int ack;
338*4882a593Smuzhiyun 	unsigned int i;
339*4882a593Smuzhiyun 	int c;
340*4882a593Smuzhiyun 	struct wavefront_command *wfcmd;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	if ((wfcmd = wavefront_get_command (cmd)) == NULL) {
343*4882a593Smuzhiyun 		snd_printk ("command 0x%x not supported.\n",
344*4882a593Smuzhiyun 			cmd);
345*4882a593Smuzhiyun 		return 1;
346*4882a593Smuzhiyun 	}
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	/* Hack to handle the one variable-size write command. See
349*4882a593Smuzhiyun 	   wavefront_send_multisample() for the other half of this
350*4882a593Smuzhiyun 	   gross and ugly strategy.
351*4882a593Smuzhiyun 	*/
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	if (cmd == WFC_DOWNLOAD_MULTISAMPLE) {
354*4882a593Smuzhiyun 		wfcmd->write_cnt = (unsigned long) rbuf;
355*4882a593Smuzhiyun 		rbuf = NULL;
356*4882a593Smuzhiyun 	}
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_CMD, "0x%x [%s] (%d,%d,%d)\n",
359*4882a593Smuzhiyun 			       cmd, wfcmd->action, wfcmd->read_cnt,
360*4882a593Smuzhiyun 			       wfcmd->write_cnt, wfcmd->need_ack);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	if (wavefront_write (dev, cmd)) {
363*4882a593Smuzhiyun 		DPRINT ((WF_DEBUG_IO|WF_DEBUG_CMD), "cannot request "
364*4882a593Smuzhiyun 						     "0x%x [%s].\n",
365*4882a593Smuzhiyun 						     cmd, wfcmd->action);
366*4882a593Smuzhiyun 		return 1;
367*4882a593Smuzhiyun 	}
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	if (wfcmd->write_cnt > 0) {
370*4882a593Smuzhiyun 		DPRINT (WF_DEBUG_DATA, "writing %d bytes "
371*4882a593Smuzhiyun 					"for 0x%x\n",
372*4882a593Smuzhiyun 					wfcmd->write_cnt, cmd);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 		for (i = 0; i < wfcmd->write_cnt; i++) {
375*4882a593Smuzhiyun 			if (wavefront_write (dev, wbuf[i])) {
376*4882a593Smuzhiyun 				DPRINT (WF_DEBUG_IO, "bad write for byte "
377*4882a593Smuzhiyun 						      "%d of 0x%x [%s].\n",
378*4882a593Smuzhiyun 						      i, cmd, wfcmd->action);
379*4882a593Smuzhiyun 				return 1;
380*4882a593Smuzhiyun 			}
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 			DPRINT (WF_DEBUG_DATA, "write[%d] = 0x%x\n",
383*4882a593Smuzhiyun 						i, wbuf[i]);
384*4882a593Smuzhiyun 		}
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	if (wfcmd->read_cnt > 0) {
388*4882a593Smuzhiyun 		DPRINT (WF_DEBUG_DATA, "reading %d ints "
389*4882a593Smuzhiyun 					"for 0x%x\n",
390*4882a593Smuzhiyun 					wfcmd->read_cnt, cmd);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 		for (i = 0; i < wfcmd->read_cnt; i++) {
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 			if ((c = wavefront_read (dev)) == -1) {
395*4882a593Smuzhiyun 				DPRINT (WF_DEBUG_IO, "bad read for byte "
396*4882a593Smuzhiyun 						      "%d of 0x%x [%s].\n",
397*4882a593Smuzhiyun 						      i, cmd, wfcmd->action);
398*4882a593Smuzhiyun 				return 1;
399*4882a593Smuzhiyun 			}
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 			/* Now handle errors. Lots of special cases here */
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 			if (c == 0xff) {
404*4882a593Smuzhiyun 				if ((c = wavefront_read (dev)) == -1) {
405*4882a593Smuzhiyun 					DPRINT (WF_DEBUG_IO, "bad read for "
406*4882a593Smuzhiyun 							      "error byte at "
407*4882a593Smuzhiyun 							      "read byte %d "
408*4882a593Smuzhiyun 							      "of 0x%x [%s].\n",
409*4882a593Smuzhiyun 							      i, cmd,
410*4882a593Smuzhiyun 							      wfcmd->action);
411*4882a593Smuzhiyun 					return 1;
412*4882a593Smuzhiyun 				}
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 				/* Can you believe this madness ? */
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 				if (c == 1 &&
417*4882a593Smuzhiyun 				    wfcmd->cmd == WFC_IDENTIFY_SAMPLE_TYPE) {
418*4882a593Smuzhiyun 					rbuf[0] = WF_ST_EMPTY;
419*4882a593Smuzhiyun 					return (0);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 				} else if (c == 3 &&
422*4882a593Smuzhiyun 					   wfcmd->cmd == WFC_UPLOAD_PATCH) {
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 					return 3;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 				} else if (c == 1 &&
427*4882a593Smuzhiyun 					   wfcmd->cmd == WFC_UPLOAD_PROGRAM) {
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 					return 1;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 				} else {
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 					DPRINT (WF_DEBUG_IO, "error %d (%s) "
434*4882a593Smuzhiyun 							      "during "
435*4882a593Smuzhiyun 							      "read for byte "
436*4882a593Smuzhiyun 							      "%d of 0x%x "
437*4882a593Smuzhiyun 							      "[%s].\n",
438*4882a593Smuzhiyun 							      c,
439*4882a593Smuzhiyun 							      wavefront_errorstr (c),
440*4882a593Smuzhiyun 							      i, cmd,
441*4882a593Smuzhiyun 							      wfcmd->action);
442*4882a593Smuzhiyun 					return 1;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 				}
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 		} else {
447*4882a593Smuzhiyun 				rbuf[i] = c;
448*4882a593Smuzhiyun 			}
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 			DPRINT (WF_DEBUG_DATA, "read[%d] = 0x%x\n",i, rbuf[i]);
451*4882a593Smuzhiyun 		}
452*4882a593Smuzhiyun 	}
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	if ((wfcmd->read_cnt == 0 && wfcmd->write_cnt == 0) || wfcmd->need_ack) {
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 		DPRINT (WF_DEBUG_CMD, "reading ACK for 0x%x\n", cmd);
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 		/* Some commands need an ACK, but return zero instead
459*4882a593Smuzhiyun 		   of the standard value.
460*4882a593Smuzhiyun 		*/
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 		if ((ack = wavefront_read (dev)) == 0) {
463*4882a593Smuzhiyun 			ack = WF_ACK;
464*4882a593Smuzhiyun 		}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 		if (ack != WF_ACK) {
467*4882a593Smuzhiyun 			if (ack == -1) {
468*4882a593Smuzhiyun 				DPRINT (WF_DEBUG_IO, "cannot read ack for "
469*4882a593Smuzhiyun 						      "0x%x [%s].\n",
470*4882a593Smuzhiyun 						      cmd, wfcmd->action);
471*4882a593Smuzhiyun 				return 1;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 			} else {
474*4882a593Smuzhiyun 				int err = -1; /* something unknown */
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 				if (ack == 0xff) { /* explicit error */
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 					if ((err = wavefront_read (dev)) == -1) {
479*4882a593Smuzhiyun 						DPRINT (WF_DEBUG_DATA,
480*4882a593Smuzhiyun 							"cannot read err "
481*4882a593Smuzhiyun 							"for 0x%x [%s].\n",
482*4882a593Smuzhiyun 							cmd, wfcmd->action);
483*4882a593Smuzhiyun 					}
484*4882a593Smuzhiyun 				}
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 				DPRINT (WF_DEBUG_IO, "0x%x [%s] "
487*4882a593Smuzhiyun 					"failed (0x%x, 0x%x, %s)\n",
488*4882a593Smuzhiyun 					cmd, wfcmd->action, ack, err,
489*4882a593Smuzhiyun 					wavefront_errorstr (err));
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 				return -err;
492*4882a593Smuzhiyun 			}
493*4882a593Smuzhiyun 		}
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 		DPRINT (WF_DEBUG_DATA, "ack received "
496*4882a593Smuzhiyun 					"for 0x%x [%s]\n",
497*4882a593Smuzhiyun 					cmd, wfcmd->action);
498*4882a593Smuzhiyun 	} else {
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 		DPRINT (WF_DEBUG_CMD, "0x%x [%s] does not need "
501*4882a593Smuzhiyun 				       "ACK (%d,%d,%d)\n",
502*4882a593Smuzhiyun 				       cmd, wfcmd->action, wfcmd->read_cnt,
503*4882a593Smuzhiyun 				       wfcmd->write_cnt, wfcmd->need_ack);
504*4882a593Smuzhiyun 	}
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	return 0;
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun /***********************************************************************
511*4882a593Smuzhiyun WaveFront data munging
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun Things here are weird. All data written to the board cannot
514*4882a593Smuzhiyun have its most significant bit set. Any data item with values
515*4882a593Smuzhiyun potentially > 0x7F (127) must be split across multiple bytes.
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun Sometimes, we need to munge numeric values that are represented on
518*4882a593Smuzhiyun the x86 side as 8-32 bit values. Sometimes, we need to munge data
519*4882a593Smuzhiyun that is represented on the x86 side as an array of bytes. The most
520*4882a593Smuzhiyun efficient approach to handling both cases seems to be to use 2
521*4882a593Smuzhiyun different functions for munging and 2 for de-munging. This avoids
522*4882a593Smuzhiyun weird casting and worrying about bit-level offsets.
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun **********************************************************************/
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun static unsigned char *
munge_int32(unsigned int src,unsigned char * dst,unsigned int dst_size)527*4882a593Smuzhiyun munge_int32 (unsigned int src,
528*4882a593Smuzhiyun 	     unsigned char *dst,
529*4882a593Smuzhiyun 	     unsigned int dst_size)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun 	unsigned int i;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	for (i = 0; i < dst_size; i++) {
534*4882a593Smuzhiyun 		*dst = src & 0x7F;  /* Mask high bit of LSB */
535*4882a593Smuzhiyun 		src = src >> 7;     /* Rotate Right 7 bits  */
536*4882a593Smuzhiyun 	                            /* Note: we leave the upper bits in place */
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 		dst++;
539*4882a593Smuzhiyun 	}
540*4882a593Smuzhiyun 	return dst;
541*4882a593Smuzhiyun };
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun static int
demunge_int32(unsigned char * src,int src_size)544*4882a593Smuzhiyun demunge_int32 (unsigned char* src, int src_size)
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun {
547*4882a593Smuzhiyun 	int i;
548*4882a593Smuzhiyun  	int outval = 0;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun  	for (i = src_size - 1; i >= 0; i--) {
551*4882a593Smuzhiyun 		outval=(outval<<7)+src[i];
552*4882a593Smuzhiyun 	}
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	return outval;
555*4882a593Smuzhiyun };
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun static
558*4882a593Smuzhiyun unsigned char *
munge_buf(unsigned char * src,unsigned char * dst,unsigned int dst_size)559*4882a593Smuzhiyun munge_buf (unsigned char *src, unsigned char *dst, unsigned int dst_size)
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	unsigned int i;
563*4882a593Smuzhiyun 	unsigned int last = dst_size / 2;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	for (i = 0; i < last; i++) {
566*4882a593Smuzhiyun 		*dst++ = src[i] & 0x7f;
567*4882a593Smuzhiyun 		*dst++ = src[i] >> 7;
568*4882a593Smuzhiyun 	}
569*4882a593Smuzhiyun 	return dst;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun static
573*4882a593Smuzhiyun unsigned char *
demunge_buf(unsigned char * src,unsigned char * dst,unsigned int src_bytes)574*4882a593Smuzhiyun demunge_buf (unsigned char *src, unsigned char *dst, unsigned int src_bytes)
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun 	int i;
578*4882a593Smuzhiyun 	unsigned char *end = src + src_bytes;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	end = src + src_bytes;
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	/* NOTE: src and dst *CAN* point to the same address */
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	for (i = 0; src != end; i++) {
585*4882a593Smuzhiyun 		dst[i] = *src++;
586*4882a593Smuzhiyun 		dst[i] |= (*src++)<<7;
587*4882a593Smuzhiyun 	}
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	return dst;
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun /***********************************************************************
593*4882a593Smuzhiyun WaveFront: sample, patch and program management.
594*4882a593Smuzhiyun ***********************************************************************/
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun static int
wavefront_delete_sample(snd_wavefront_t * dev,int sample_num)597*4882a593Smuzhiyun wavefront_delete_sample (snd_wavefront_t *dev, int sample_num)
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun {
600*4882a593Smuzhiyun 	unsigned char wbuf[2];
601*4882a593Smuzhiyun 	int x;
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	wbuf[0] = sample_num & 0x7f;
604*4882a593Smuzhiyun 	wbuf[1] = sample_num >> 7;
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	if ((x = snd_wavefront_cmd (dev, WFC_DELETE_SAMPLE, NULL, wbuf)) == 0) {
607*4882a593Smuzhiyun 		dev->sample_status[sample_num] = WF_ST_EMPTY;
608*4882a593Smuzhiyun 	}
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	return x;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun static int
wavefront_get_sample_status(snd_wavefront_t * dev,int assume_rom)614*4882a593Smuzhiyun wavefront_get_sample_status (snd_wavefront_t *dev, int assume_rom)
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun {
617*4882a593Smuzhiyun 	int i;
618*4882a593Smuzhiyun 	unsigned char rbuf[32], wbuf[32];
619*4882a593Smuzhiyun 	unsigned int    sc_real, sc_alias, sc_multi;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	/* check sample status */
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_GET_NSAMPLES, rbuf, wbuf)) {
624*4882a593Smuzhiyun 		snd_printk ("cannot request sample count.\n");
625*4882a593Smuzhiyun 		return -1;
626*4882a593Smuzhiyun 	}
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	sc_real = sc_alias = sc_multi = dev->samples_used = 0;
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	for (i = 0; i < WF_MAX_SAMPLE; i++) {
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 		wbuf[0] = i & 0x7f;
633*4882a593Smuzhiyun 		wbuf[1] = i >> 7;
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 		if (snd_wavefront_cmd (dev, WFC_IDENTIFY_SAMPLE_TYPE, rbuf, wbuf)) {
636*4882a593Smuzhiyun 			snd_printk(KERN_WARNING "cannot identify sample "
637*4882a593Smuzhiyun 				   "type of slot %d\n", i);
638*4882a593Smuzhiyun 			dev->sample_status[i] = WF_ST_EMPTY;
639*4882a593Smuzhiyun 			continue;
640*4882a593Smuzhiyun 		}
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 		dev->sample_status[i] = (WF_SLOT_FILLED|rbuf[0]);
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 		if (assume_rom) {
645*4882a593Smuzhiyun 			dev->sample_status[i] |= WF_SLOT_ROM;
646*4882a593Smuzhiyun 		}
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 		switch (rbuf[0] & WF_ST_MASK) {
649*4882a593Smuzhiyun 		case WF_ST_SAMPLE:
650*4882a593Smuzhiyun 			sc_real++;
651*4882a593Smuzhiyun 			break;
652*4882a593Smuzhiyun 		case WF_ST_MULTISAMPLE:
653*4882a593Smuzhiyun 			sc_multi++;
654*4882a593Smuzhiyun 			break;
655*4882a593Smuzhiyun 		case WF_ST_ALIAS:
656*4882a593Smuzhiyun 			sc_alias++;
657*4882a593Smuzhiyun 			break;
658*4882a593Smuzhiyun 		case WF_ST_EMPTY:
659*4882a593Smuzhiyun 			break;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 		default:
662*4882a593Smuzhiyun 			snd_printk ("unknown sample type for "
663*4882a593Smuzhiyun 				    "slot %d (0x%x)\n",
664*4882a593Smuzhiyun 				    i, rbuf[0]);
665*4882a593Smuzhiyun 		}
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 		if (rbuf[0] != WF_ST_EMPTY) {
668*4882a593Smuzhiyun 			dev->samples_used++;
669*4882a593Smuzhiyun 		}
670*4882a593Smuzhiyun 	}
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	snd_printk ("%d samples used (%d real, %d aliases, %d multi), "
673*4882a593Smuzhiyun 		    "%d empty\n", dev->samples_used, sc_real, sc_alias, sc_multi,
674*4882a593Smuzhiyun 		    WF_MAX_SAMPLE - dev->samples_used);
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	return (0);
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun static int
wavefront_get_patch_status(snd_wavefront_t * dev)682*4882a593Smuzhiyun wavefront_get_patch_status (snd_wavefront_t *dev)
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	unsigned char patchbuf[WF_PATCH_BYTES];
686*4882a593Smuzhiyun 	unsigned char patchnum[2];
687*4882a593Smuzhiyun 	wavefront_patch *p;
688*4882a593Smuzhiyun 	int i, x, cnt, cnt2;
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	for (i = 0; i < WF_MAX_PATCH; i++) {
691*4882a593Smuzhiyun 		patchnum[0] = i & 0x7f;
692*4882a593Smuzhiyun 		patchnum[1] = i >> 7;
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 		if ((x = snd_wavefront_cmd (dev, WFC_UPLOAD_PATCH, patchbuf,
695*4882a593Smuzhiyun 					patchnum)) == 0) {
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 			dev->patch_status[i] |= WF_SLOT_FILLED;
698*4882a593Smuzhiyun 			p = (wavefront_patch *) patchbuf;
699*4882a593Smuzhiyun 			dev->sample_status
700*4882a593Smuzhiyun 				[p->sample_number|(p->sample_msb<<7)] |=
701*4882a593Smuzhiyun 				WF_SLOT_USED;
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 		} else if (x == 3) { /* Bad patch number */
704*4882a593Smuzhiyun 			dev->patch_status[i] = 0;
705*4882a593Smuzhiyun 		} else {
706*4882a593Smuzhiyun 			snd_printk ("upload patch "
707*4882a593Smuzhiyun 				    "error 0x%x\n", x);
708*4882a593Smuzhiyun 			dev->patch_status[i] = 0;
709*4882a593Smuzhiyun 			return 1;
710*4882a593Smuzhiyun 		}
711*4882a593Smuzhiyun 	}
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	/* program status has already filled in slot_used bits */
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	for (i = 0, cnt = 0, cnt2 = 0; i < WF_MAX_PATCH; i++) {
716*4882a593Smuzhiyun 		if (dev->patch_status[i] & WF_SLOT_FILLED) {
717*4882a593Smuzhiyun 			cnt++;
718*4882a593Smuzhiyun 		}
719*4882a593Smuzhiyun 		if (dev->patch_status[i] & WF_SLOT_USED) {
720*4882a593Smuzhiyun 			cnt2++;
721*4882a593Smuzhiyun 		}
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	}
724*4882a593Smuzhiyun 	snd_printk ("%d patch slots filled, %d in use\n", cnt, cnt2);
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	return (0);
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun static int
wavefront_get_program_status(snd_wavefront_t * dev)730*4882a593Smuzhiyun wavefront_get_program_status (snd_wavefront_t *dev)
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun {
733*4882a593Smuzhiyun 	unsigned char progbuf[WF_PROGRAM_BYTES];
734*4882a593Smuzhiyun 	wavefront_program prog;
735*4882a593Smuzhiyun 	unsigned char prognum;
736*4882a593Smuzhiyun 	int i, x, l, cnt;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	for (i = 0; i < WF_MAX_PROGRAM; i++) {
739*4882a593Smuzhiyun 		prognum = i;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 		if ((x = snd_wavefront_cmd (dev, WFC_UPLOAD_PROGRAM, progbuf,
742*4882a593Smuzhiyun 					&prognum)) == 0) {
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 			dev->prog_status[i] |= WF_SLOT_USED;
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 			demunge_buf (progbuf, (unsigned char *) &prog,
747*4882a593Smuzhiyun 				     WF_PROGRAM_BYTES);
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 			for (l = 0; l < WF_NUM_LAYERS; l++) {
750*4882a593Smuzhiyun 				if (prog.layer[l].mute) {
751*4882a593Smuzhiyun 					dev->patch_status
752*4882a593Smuzhiyun 						[prog.layer[l].patch_number] |=
753*4882a593Smuzhiyun 						WF_SLOT_USED;
754*4882a593Smuzhiyun 				}
755*4882a593Smuzhiyun 			}
756*4882a593Smuzhiyun 		} else if (x == 1) { /* Bad program number */
757*4882a593Smuzhiyun 			dev->prog_status[i] = 0;
758*4882a593Smuzhiyun 		} else {
759*4882a593Smuzhiyun 			snd_printk ("upload program "
760*4882a593Smuzhiyun 				    "error 0x%x\n", x);
761*4882a593Smuzhiyun 			dev->prog_status[i] = 0;
762*4882a593Smuzhiyun 		}
763*4882a593Smuzhiyun 	}
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	for (i = 0, cnt = 0; i < WF_MAX_PROGRAM; i++) {
766*4882a593Smuzhiyun 		if (dev->prog_status[i]) {
767*4882a593Smuzhiyun 			cnt++;
768*4882a593Smuzhiyun 		}
769*4882a593Smuzhiyun 	}
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	snd_printk ("%d programs slots in use\n", cnt);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	return (0);
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun static int
wavefront_send_patch(snd_wavefront_t * dev,wavefront_patch_info * header)777*4882a593Smuzhiyun wavefront_send_patch (snd_wavefront_t *dev, wavefront_patch_info *header)
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun {
780*4882a593Smuzhiyun 	unsigned char buf[WF_PATCH_BYTES+2];
781*4882a593Smuzhiyun 	unsigned char *bptr;
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_LOAD_PATCH, "downloading patch %d\n",
784*4882a593Smuzhiyun 				      header->number);
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	if (header->number >= ARRAY_SIZE(dev->patch_status))
787*4882a593Smuzhiyun 		return -EINVAL;
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	dev->patch_status[header->number] |= WF_SLOT_FILLED;
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	bptr = munge_int32 (header->number, buf, 2);
792*4882a593Smuzhiyun 	munge_buf ((unsigned char *)&header->hdr.p, bptr, WF_PATCH_BYTES);
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_PATCH, NULL, buf)) {
795*4882a593Smuzhiyun 		snd_printk ("download patch failed\n");
796*4882a593Smuzhiyun 		return -EIO;
797*4882a593Smuzhiyun 	}
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	return (0);
800*4882a593Smuzhiyun }
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun static int
wavefront_send_program(snd_wavefront_t * dev,wavefront_patch_info * header)803*4882a593Smuzhiyun wavefront_send_program (snd_wavefront_t *dev, wavefront_patch_info *header)
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun {
806*4882a593Smuzhiyun 	unsigned char buf[WF_PROGRAM_BYTES+1];
807*4882a593Smuzhiyun 	int i;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_LOAD_PATCH, "downloading program %d\n",
810*4882a593Smuzhiyun 		header->number);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	if (header->number >= ARRAY_SIZE(dev->prog_status))
813*4882a593Smuzhiyun 		return -EINVAL;
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	dev->prog_status[header->number] = WF_SLOT_USED;
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	/* XXX need to zero existing SLOT_USED bit for program_status[i]
818*4882a593Smuzhiyun 	   where `i' is the program that's being (potentially) overwritten.
819*4882a593Smuzhiyun 	*/
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	for (i = 0; i < WF_NUM_LAYERS; i++) {
822*4882a593Smuzhiyun 		if (header->hdr.pr.layer[i].mute) {
823*4882a593Smuzhiyun 			dev->patch_status[header->hdr.pr.layer[i].patch_number] |=
824*4882a593Smuzhiyun 				WF_SLOT_USED;
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 			/* XXX need to mark SLOT_USED for sample used by
827*4882a593Smuzhiyun 			   patch_number, but this means we have to load it. Ick.
828*4882a593Smuzhiyun 			*/
829*4882a593Smuzhiyun 		}
830*4882a593Smuzhiyun 	}
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	buf[0] = header->number;
833*4882a593Smuzhiyun 	munge_buf ((unsigned char *)&header->hdr.pr, &buf[1], WF_PROGRAM_BYTES);
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_PROGRAM, NULL, buf)) {
836*4882a593Smuzhiyun 		snd_printk ("download patch failed\n");
837*4882a593Smuzhiyun 		return -EIO;
838*4882a593Smuzhiyun 	}
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	return (0);
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun static int
wavefront_freemem(snd_wavefront_t * dev)844*4882a593Smuzhiyun wavefront_freemem (snd_wavefront_t *dev)
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun {
847*4882a593Smuzhiyun 	char rbuf[8];
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_REPORT_FREE_MEMORY, rbuf, NULL)) {
850*4882a593Smuzhiyun 		snd_printk ("can't get memory stats.\n");
851*4882a593Smuzhiyun 		return -1;
852*4882a593Smuzhiyun 	} else {
853*4882a593Smuzhiyun 		return demunge_int32 (rbuf, 4);
854*4882a593Smuzhiyun 	}
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun static int
wavefront_send_sample(snd_wavefront_t * dev,wavefront_patch_info * header,u16 __user * dataptr,int data_is_unsigned)858*4882a593Smuzhiyun wavefront_send_sample (snd_wavefront_t *dev,
859*4882a593Smuzhiyun 		       wavefront_patch_info *header,
860*4882a593Smuzhiyun 		       u16 __user *dataptr,
861*4882a593Smuzhiyun 		       int data_is_unsigned)
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun {
864*4882a593Smuzhiyun 	/* samples are downloaded via a 16-bit wide i/o port
865*4882a593Smuzhiyun 	   (you could think of it as 2 adjacent 8-bit wide ports
866*4882a593Smuzhiyun 	   but its less efficient that way). therefore, all
867*4882a593Smuzhiyun 	   the blocksizes and so forth listed in the documentation,
868*4882a593Smuzhiyun 	   and used conventionally to refer to sample sizes,
869*4882a593Smuzhiyun 	   which are given in 8-bit units (bytes), need to be
870*4882a593Smuzhiyun 	   divided by 2.
871*4882a593Smuzhiyun         */
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	u16 sample_short = 0;
874*4882a593Smuzhiyun 	u32 length;
875*4882a593Smuzhiyun 	u16 __user *data_end = NULL;
876*4882a593Smuzhiyun 	unsigned int i;
877*4882a593Smuzhiyun 	const unsigned int max_blksize = 4096/2;
878*4882a593Smuzhiyun 	unsigned int written;
879*4882a593Smuzhiyun 	unsigned int blocksize;
880*4882a593Smuzhiyun 	int dma_ack;
881*4882a593Smuzhiyun 	int blocknum;
882*4882a593Smuzhiyun 	unsigned char sample_hdr[WF_SAMPLE_HDR_BYTES];
883*4882a593Smuzhiyun 	unsigned char *shptr;
884*4882a593Smuzhiyun 	int skip = 0;
885*4882a593Smuzhiyun 	int initial_skip = 0;
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_LOAD_PATCH, "sample %sdownload for slot %d, "
888*4882a593Smuzhiyun 				      "type %d, %d bytes from 0x%lx\n",
889*4882a593Smuzhiyun 				      header->size ? "" : "header ",
890*4882a593Smuzhiyun 				      header->number, header->subkey,
891*4882a593Smuzhiyun 				      header->size,
892*4882a593Smuzhiyun 				      (unsigned long) header->dataptr);
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 	if (header->number == WAVEFRONT_FIND_FREE_SAMPLE_SLOT) {
895*4882a593Smuzhiyun 		int x;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 		if ((x = wavefront_find_free_sample (dev)) < 0) {
898*4882a593Smuzhiyun 			return -ENOMEM;
899*4882a593Smuzhiyun 		}
900*4882a593Smuzhiyun 		snd_printk ("unspecified sample => %d\n", x);
901*4882a593Smuzhiyun 		header->number = x;
902*4882a593Smuzhiyun 	}
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun 	if (header->number >= WF_MAX_SAMPLE)
905*4882a593Smuzhiyun 		return -EINVAL;
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	if (header->size) {
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 		/* XXX it's a debatable point whether or not RDONLY semantics
910*4882a593Smuzhiyun 		   on the ROM samples should cover just the sample data or
911*4882a593Smuzhiyun 		   the sample header. For now, it only covers the sample data,
912*4882a593Smuzhiyun 		   so anyone is free at all times to rewrite sample headers.
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 		   My reason for this is that we have the sample headers
915*4882a593Smuzhiyun 		   available in the WFB file for General MIDI, and so these
916*4882a593Smuzhiyun 		   can always be reset if needed. The sample data, however,
917*4882a593Smuzhiyun 		   cannot be recovered without a complete reset and firmware
918*4882a593Smuzhiyun 		   reload of the ICS2115, which is a very expensive operation.
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 		   So, doing things this way allows us to honor the notion of
921*4882a593Smuzhiyun 		   "RESETSAMPLES" reasonably cheaply. Note however, that this
922*4882a593Smuzhiyun 		   is done purely at user level: there is no WFB parser in
923*4882a593Smuzhiyun 		   this driver, and so a complete reset (back to General MIDI,
924*4882a593Smuzhiyun 		   or theoretically some other configuration) is the
925*4882a593Smuzhiyun 		   responsibility of the user level library.
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 		   To try to do this in the kernel would be a little
928*4882a593Smuzhiyun 		   crazy: we'd need 158K of kernel space just to hold
929*4882a593Smuzhiyun 		   a copy of the patch/program/sample header data.
930*4882a593Smuzhiyun 		*/
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 		if (dev->rom_samples_rdonly) {
933*4882a593Smuzhiyun 			if (dev->sample_status[header->number] & WF_SLOT_ROM) {
934*4882a593Smuzhiyun 				snd_printk ("sample slot %d "
935*4882a593Smuzhiyun 					    "write protected\n",
936*4882a593Smuzhiyun 					    header->number);
937*4882a593Smuzhiyun 				return -EACCES;
938*4882a593Smuzhiyun 			}
939*4882a593Smuzhiyun 		}
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 		wavefront_delete_sample (dev, header->number);
942*4882a593Smuzhiyun 	}
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	if (header->size) {
945*4882a593Smuzhiyun 		dev->freemem = wavefront_freemem (dev);
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 		if (dev->freemem < (int)header->size) {
948*4882a593Smuzhiyun 			snd_printk ("insufficient memory to "
949*4882a593Smuzhiyun 				    "load %d byte sample.\n",
950*4882a593Smuzhiyun 				    header->size);
951*4882a593Smuzhiyun 			return -ENOMEM;
952*4882a593Smuzhiyun 		}
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	}
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 	skip = WF_GET_CHANNEL(&header->hdr.s);
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	if (skip > 0 && header->hdr.s.SampleResolution != LINEAR_16BIT) {
959*4882a593Smuzhiyun 		snd_printk ("channel selection only "
960*4882a593Smuzhiyun 			    "possible on 16-bit samples");
961*4882a593Smuzhiyun 		return -EINVAL;
962*4882a593Smuzhiyun 	}
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	switch (skip) {
965*4882a593Smuzhiyun 	case 0:
966*4882a593Smuzhiyun 		initial_skip = 0;
967*4882a593Smuzhiyun 		skip = 1;
968*4882a593Smuzhiyun 		break;
969*4882a593Smuzhiyun 	case 1:
970*4882a593Smuzhiyun 		initial_skip = 0;
971*4882a593Smuzhiyun 		skip = 2;
972*4882a593Smuzhiyun 		break;
973*4882a593Smuzhiyun 	case 2:
974*4882a593Smuzhiyun 		initial_skip = 1;
975*4882a593Smuzhiyun 		skip = 2;
976*4882a593Smuzhiyun 		break;
977*4882a593Smuzhiyun 	case 3:
978*4882a593Smuzhiyun 		initial_skip = 2;
979*4882a593Smuzhiyun 		skip = 3;
980*4882a593Smuzhiyun 		break;
981*4882a593Smuzhiyun 	case 4:
982*4882a593Smuzhiyun 		initial_skip = 3;
983*4882a593Smuzhiyun 		skip = 4;
984*4882a593Smuzhiyun 		break;
985*4882a593Smuzhiyun 	case 5:
986*4882a593Smuzhiyun 		initial_skip = 4;
987*4882a593Smuzhiyun 		skip = 5;
988*4882a593Smuzhiyun 		break;
989*4882a593Smuzhiyun 	case 6:
990*4882a593Smuzhiyun 		initial_skip = 5;
991*4882a593Smuzhiyun 		skip = 6;
992*4882a593Smuzhiyun 		break;
993*4882a593Smuzhiyun 	}
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_LOAD_PATCH, "channel selection: %d => "
996*4882a593Smuzhiyun 				      "initial skip = %d, skip = %d\n",
997*4882a593Smuzhiyun 				      WF_GET_CHANNEL (&header->hdr.s),
998*4882a593Smuzhiyun 				      initial_skip, skip);
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	/* Be safe, and zero the "Unused" bits ... */
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 	WF_SET_CHANNEL(&header->hdr.s, 0);
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 	/* adjust size for 16 bit samples by dividing by two.  We always
1005*4882a593Smuzhiyun 	   send 16 bits per write, even for 8 bit samples, so the length
1006*4882a593Smuzhiyun 	   is always half the size of the sample data in bytes.
1007*4882a593Smuzhiyun 	*/
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	length = header->size / 2;
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	/* the data we're sent has not been munged, and in fact, the
1012*4882a593Smuzhiyun 	   header we have to send isn't just a munged copy either.
1013*4882a593Smuzhiyun 	   so, build the sample header right here.
1014*4882a593Smuzhiyun 	*/
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 	shptr = &sample_hdr[0];
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	shptr = munge_int32 (header->number, shptr, 2);
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	if (header->size) {
1021*4882a593Smuzhiyun 		shptr = munge_int32 (length, shptr, 4);
1022*4882a593Smuzhiyun 	}
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	/* Yes, a 4 byte result doesn't contain all of the offset bits,
1025*4882a593Smuzhiyun 	   but the offset only uses 24 bits.
1026*4882a593Smuzhiyun 	*/
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun 	shptr = munge_int32 (*((u32 *) &header->hdr.s.sampleStartOffset),
1029*4882a593Smuzhiyun 			     shptr, 4);
1030*4882a593Smuzhiyun 	shptr = munge_int32 (*((u32 *) &header->hdr.s.loopStartOffset),
1031*4882a593Smuzhiyun 			     shptr, 4);
1032*4882a593Smuzhiyun 	shptr = munge_int32 (*((u32 *) &header->hdr.s.loopEndOffset),
1033*4882a593Smuzhiyun 			     shptr, 4);
1034*4882a593Smuzhiyun 	shptr = munge_int32 (*((u32 *) &header->hdr.s.sampleEndOffset),
1035*4882a593Smuzhiyun 			     shptr, 4);
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 	/* This one is truly weird. What kind of weirdo decided that in
1038*4882a593Smuzhiyun 	   a system dominated by 16 and 32 bit integers, they would use
1039*4882a593Smuzhiyun 	   a just 12 bits ?
1040*4882a593Smuzhiyun 	*/
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	shptr = munge_int32 (header->hdr.s.FrequencyBias, shptr, 3);
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 	/* Why is this nybblified, when the MSB is *always* zero ?
1045*4882a593Smuzhiyun 	   Anyway, we can't take address of bitfield, so make a
1046*4882a593Smuzhiyun 	   good-faith guess at where it starts.
1047*4882a593Smuzhiyun 	*/
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 	shptr = munge_int32 (*(&header->hdr.s.FrequencyBias+1),
1050*4882a593Smuzhiyun 			     shptr, 2);
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev,
1053*4882a593Smuzhiyun 			   header->size ?
1054*4882a593Smuzhiyun 			   WFC_DOWNLOAD_SAMPLE : WFC_DOWNLOAD_SAMPLE_HEADER,
1055*4882a593Smuzhiyun 			   NULL, sample_hdr)) {
1056*4882a593Smuzhiyun 		snd_printk ("sample %sdownload refused.\n",
1057*4882a593Smuzhiyun 			    header->size ? "" : "header ");
1058*4882a593Smuzhiyun 		return -EIO;
1059*4882a593Smuzhiyun 	}
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 	if (header->size == 0) {
1062*4882a593Smuzhiyun 		goto sent; /* Sorry. Just had to have one somewhere */
1063*4882a593Smuzhiyun 	}
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	data_end = dataptr + length;
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	/* Do any initial skip over an unused channel's data */
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	dataptr += initial_skip;
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun 	for (written = 0, blocknum = 0;
1072*4882a593Smuzhiyun 	     written < length; written += max_blksize, blocknum++) {
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun 		if ((length - written) > max_blksize) {
1075*4882a593Smuzhiyun 			blocksize = max_blksize;
1076*4882a593Smuzhiyun 		} else {
1077*4882a593Smuzhiyun 			/* round to nearest 16-byte value */
1078*4882a593Smuzhiyun 			blocksize = ALIGN(length - written, 8);
1079*4882a593Smuzhiyun 		}
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 		if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_BLOCK, NULL, NULL)) {
1082*4882a593Smuzhiyun 			snd_printk ("download block "
1083*4882a593Smuzhiyun 				    "request refused.\n");
1084*4882a593Smuzhiyun 			return -EIO;
1085*4882a593Smuzhiyun 		}
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 		for (i = 0; i < blocksize; i++) {
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 			if (dataptr < data_end) {
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun 				if (get_user(sample_short, dataptr))
1092*4882a593Smuzhiyun 					return -EFAULT;
1093*4882a593Smuzhiyun 				dataptr += skip;
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 				if (data_is_unsigned) { /* GUS ? */
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 					if (WF_SAMPLE_IS_8BIT(&header->hdr.s)) {
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 						/* 8 bit sample
1100*4882a593Smuzhiyun 						 resolution, sign
1101*4882a593Smuzhiyun 						 extend both bytes.
1102*4882a593Smuzhiyun 						*/
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 						((unsigned char*)
1105*4882a593Smuzhiyun 						 &sample_short)[0] += 0x7f;
1106*4882a593Smuzhiyun 						((unsigned char*)
1107*4882a593Smuzhiyun 						 &sample_short)[1] += 0x7f;
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 					} else {
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 						/* 16 bit sample
1112*4882a593Smuzhiyun 						 resolution, sign
1113*4882a593Smuzhiyun 						 extend the MSB.
1114*4882a593Smuzhiyun 						*/
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 						sample_short += 0x7fff;
1117*4882a593Smuzhiyun 					}
1118*4882a593Smuzhiyun 				}
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 			} else {
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 				/* In padding section of final block:
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 				   Don't fetch unsupplied data from
1125*4882a593Smuzhiyun 				   user space, just continue with
1126*4882a593Smuzhiyun 				   whatever the final value was.
1127*4882a593Smuzhiyun 				*/
1128*4882a593Smuzhiyun 			}
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 			if (i < blocksize - 1) {
1131*4882a593Smuzhiyun 				outw (sample_short, dev->block_port);
1132*4882a593Smuzhiyun 			} else {
1133*4882a593Smuzhiyun 				outw (sample_short, dev->last_block_port);
1134*4882a593Smuzhiyun 			}
1135*4882a593Smuzhiyun 		}
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 		/* Get "DMA page acknowledge", even though its really
1138*4882a593Smuzhiyun 		   nothing to do with DMA at all.
1139*4882a593Smuzhiyun 		*/
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 		if ((dma_ack = wavefront_read (dev)) != WF_DMA_ACK) {
1142*4882a593Smuzhiyun 			if (dma_ack == -1) {
1143*4882a593Smuzhiyun 				snd_printk ("upload sample "
1144*4882a593Smuzhiyun 					    "DMA ack timeout\n");
1145*4882a593Smuzhiyun 				return -EIO;
1146*4882a593Smuzhiyun 			} else {
1147*4882a593Smuzhiyun 				snd_printk ("upload sample "
1148*4882a593Smuzhiyun 					    "DMA ack error 0x%x\n",
1149*4882a593Smuzhiyun 					    dma_ack);
1150*4882a593Smuzhiyun 				return -EIO;
1151*4882a593Smuzhiyun 			}
1152*4882a593Smuzhiyun 		}
1153*4882a593Smuzhiyun 	}
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_SAMPLE);
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	/* Note, label is here because sending the sample header shouldn't
1158*4882a593Smuzhiyun 	   alter the sample_status info at all.
1159*4882a593Smuzhiyun 	*/
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun  sent:
1162*4882a593Smuzhiyun 	return (0);
1163*4882a593Smuzhiyun }
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun static int
wavefront_send_alias(snd_wavefront_t * dev,wavefront_patch_info * header)1166*4882a593Smuzhiyun wavefront_send_alias (snd_wavefront_t *dev, wavefront_patch_info *header)
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun {
1169*4882a593Smuzhiyun 	unsigned char alias_hdr[WF_ALIAS_BYTES];
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_LOAD_PATCH, "download alias, %d is "
1172*4882a593Smuzhiyun 				      "alias for %d\n",
1173*4882a593Smuzhiyun 				      header->number,
1174*4882a593Smuzhiyun 				      header->hdr.a.OriginalSample);
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	if (header->number >= WF_MAX_SAMPLE)
1177*4882a593Smuzhiyun 		return -EINVAL;
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun 	munge_int32 (header->number, &alias_hdr[0], 2);
1180*4882a593Smuzhiyun 	munge_int32 (header->hdr.a.OriginalSample, &alias_hdr[2], 2);
1181*4882a593Smuzhiyun 	munge_int32 (*((unsigned int *)&header->hdr.a.sampleStartOffset),
1182*4882a593Smuzhiyun 		     &alias_hdr[4], 4);
1183*4882a593Smuzhiyun 	munge_int32 (*((unsigned int *)&header->hdr.a.loopStartOffset),
1184*4882a593Smuzhiyun 		     &alias_hdr[8], 4);
1185*4882a593Smuzhiyun 	munge_int32 (*((unsigned int *)&header->hdr.a.loopEndOffset),
1186*4882a593Smuzhiyun 		     &alias_hdr[12], 4);
1187*4882a593Smuzhiyun 	munge_int32 (*((unsigned int *)&header->hdr.a.sampleEndOffset),
1188*4882a593Smuzhiyun 		     &alias_hdr[16], 4);
1189*4882a593Smuzhiyun 	munge_int32 (header->hdr.a.FrequencyBias, &alias_hdr[20], 3);
1190*4882a593Smuzhiyun 	munge_int32 (*(&header->hdr.a.FrequencyBias+1), &alias_hdr[23], 2);
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_SAMPLE_ALIAS, NULL, alias_hdr)) {
1193*4882a593Smuzhiyun 		snd_printk ("download alias failed.\n");
1194*4882a593Smuzhiyun 		return -EIO;
1195*4882a593Smuzhiyun 	}
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 	dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_ALIAS);
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	return (0);
1200*4882a593Smuzhiyun }
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun static int
wavefront_send_multisample(snd_wavefront_t * dev,wavefront_patch_info * header)1203*4882a593Smuzhiyun wavefront_send_multisample (snd_wavefront_t *dev, wavefront_patch_info *header)
1204*4882a593Smuzhiyun {
1205*4882a593Smuzhiyun 	int i;
1206*4882a593Smuzhiyun 	int num_samples;
1207*4882a593Smuzhiyun 	unsigned char *msample_hdr;
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 	if (header->number >= WF_MAX_SAMPLE)
1210*4882a593Smuzhiyun 		return -EINVAL;
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	msample_hdr = kmalloc(WF_MSAMPLE_BYTES, GFP_KERNEL);
1213*4882a593Smuzhiyun 	if (! msample_hdr)
1214*4882a593Smuzhiyun 		return -ENOMEM;
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	munge_int32 (header->number, &msample_hdr[0], 2);
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	/* You'll recall at this point that the "number of samples" value
1219*4882a593Smuzhiyun 	   in a wavefront_multisample struct is actually the log2 of the
1220*4882a593Smuzhiyun 	   real number of samples.
1221*4882a593Smuzhiyun 	*/
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun 	num_samples = (1<<(header->hdr.ms.NumberOfSamples&7));
1224*4882a593Smuzhiyun 	msample_hdr[2] = (unsigned char) header->hdr.ms.NumberOfSamples;
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_LOAD_PATCH, "multi %d with %d=%d samples\n",
1227*4882a593Smuzhiyun 				      header->number,
1228*4882a593Smuzhiyun 				      header->hdr.ms.NumberOfSamples,
1229*4882a593Smuzhiyun 				      num_samples);
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 	for (i = 0; i < num_samples; i++) {
1232*4882a593Smuzhiyun 		DPRINT(WF_DEBUG_LOAD_PATCH|WF_DEBUG_DATA, "sample[%d] = %d\n",
1233*4882a593Smuzhiyun 		       i, header->hdr.ms.SampleNumber[i]);
1234*4882a593Smuzhiyun 		munge_int32 (header->hdr.ms.SampleNumber[i],
1235*4882a593Smuzhiyun 		     &msample_hdr[3+(i*2)], 2);
1236*4882a593Smuzhiyun 	}
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun 	/* Need a hack here to pass in the number of bytes
1239*4882a593Smuzhiyun 	   to be written to the synth. This is ugly, and perhaps
1240*4882a593Smuzhiyun 	   one day, I'll fix it.
1241*4882a593Smuzhiyun 	*/
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_MULTISAMPLE,
1244*4882a593Smuzhiyun 			   (unsigned char *) (long) ((num_samples*2)+3),
1245*4882a593Smuzhiyun 			   msample_hdr)) {
1246*4882a593Smuzhiyun 		snd_printk ("download of multisample failed.\n");
1247*4882a593Smuzhiyun 		kfree(msample_hdr);
1248*4882a593Smuzhiyun 		return -EIO;
1249*4882a593Smuzhiyun 	}
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_MULTISAMPLE);
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun 	kfree(msample_hdr);
1254*4882a593Smuzhiyun 	return (0);
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun static int
wavefront_fetch_multisample(snd_wavefront_t * dev,wavefront_patch_info * header)1258*4882a593Smuzhiyun wavefront_fetch_multisample (snd_wavefront_t *dev,
1259*4882a593Smuzhiyun 			     wavefront_patch_info *header)
1260*4882a593Smuzhiyun {
1261*4882a593Smuzhiyun 	int i;
1262*4882a593Smuzhiyun 	unsigned char log_ns[1];
1263*4882a593Smuzhiyun 	unsigned char number[2];
1264*4882a593Smuzhiyun 	int num_samples;
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	munge_int32 (header->number, number, 2);
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_UPLOAD_MULTISAMPLE, log_ns, number)) {
1269*4882a593Smuzhiyun 		snd_printk ("upload multisample failed.\n");
1270*4882a593Smuzhiyun 		return -EIO;
1271*4882a593Smuzhiyun 	}
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_DATA, "msample %d has %d samples\n",
1274*4882a593Smuzhiyun 				header->number, log_ns[0]);
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun 	header->hdr.ms.NumberOfSamples = log_ns[0];
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	/* get the number of samples ... */
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	num_samples = (1 << log_ns[0]);
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 	for (i = 0; i < num_samples; i++) {
1283*4882a593Smuzhiyun 		char d[2];
1284*4882a593Smuzhiyun 		int val;
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 		if ((val = wavefront_read (dev)) == -1) {
1287*4882a593Smuzhiyun 			snd_printk ("upload multisample failed "
1288*4882a593Smuzhiyun 				    "during sample loop.\n");
1289*4882a593Smuzhiyun 			return -EIO;
1290*4882a593Smuzhiyun 		}
1291*4882a593Smuzhiyun 		d[0] = val;
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 		if ((val = wavefront_read (dev)) == -1) {
1294*4882a593Smuzhiyun 			snd_printk ("upload multisample failed "
1295*4882a593Smuzhiyun 				    "during sample loop.\n");
1296*4882a593Smuzhiyun 			return -EIO;
1297*4882a593Smuzhiyun 		}
1298*4882a593Smuzhiyun 		d[1] = val;
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 		header->hdr.ms.SampleNumber[i] =
1301*4882a593Smuzhiyun 			demunge_int32 ((unsigned char *) d, 2);
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun 		DPRINT (WF_DEBUG_DATA, "msample sample[%d] = %d\n",
1304*4882a593Smuzhiyun 					i, header->hdr.ms.SampleNumber[i]);
1305*4882a593Smuzhiyun 	}
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun 	return (0);
1308*4882a593Smuzhiyun }
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 
1311*4882a593Smuzhiyun static int
wavefront_send_drum(snd_wavefront_t * dev,wavefront_patch_info * header)1312*4882a593Smuzhiyun wavefront_send_drum (snd_wavefront_t *dev, wavefront_patch_info *header)
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun {
1315*4882a593Smuzhiyun 	unsigned char drumbuf[WF_DRUM_BYTES];
1316*4882a593Smuzhiyun 	wavefront_drum *drum = &header->hdr.d;
1317*4882a593Smuzhiyun 	int i;
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_LOAD_PATCH, "downloading edrum for MIDI "
1320*4882a593Smuzhiyun 		"note %d, patch = %d\n",
1321*4882a593Smuzhiyun 		header->number, drum->PatchNumber);
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	drumbuf[0] = header->number & 0x7f;
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 	for (i = 0; i < 4; i++) {
1326*4882a593Smuzhiyun 		munge_int32 (((unsigned char *)drum)[i], &drumbuf[1+(i*2)], 2);
1327*4882a593Smuzhiyun 	}
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_EDRUM_PROGRAM, NULL, drumbuf)) {
1330*4882a593Smuzhiyun 		snd_printk ("download drum failed.\n");
1331*4882a593Smuzhiyun 		return -EIO;
1332*4882a593Smuzhiyun 	}
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun 	return (0);
1335*4882a593Smuzhiyun }
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun static int
wavefront_find_free_sample(snd_wavefront_t * dev)1338*4882a593Smuzhiyun wavefront_find_free_sample (snd_wavefront_t *dev)
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun {
1341*4882a593Smuzhiyun 	int i;
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 	for (i = 0; i < WF_MAX_SAMPLE; i++) {
1344*4882a593Smuzhiyun 		if (!(dev->sample_status[i] & WF_SLOT_FILLED)) {
1345*4882a593Smuzhiyun 			return i;
1346*4882a593Smuzhiyun 		}
1347*4882a593Smuzhiyun 	}
1348*4882a593Smuzhiyun 	snd_printk ("no free sample slots!\n");
1349*4882a593Smuzhiyun 	return -1;
1350*4882a593Smuzhiyun }
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun #if 0
1353*4882a593Smuzhiyun static int
1354*4882a593Smuzhiyun wavefront_find_free_patch (snd_wavefront_t *dev)
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun {
1357*4882a593Smuzhiyun 	int i;
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 	for (i = 0; i < WF_MAX_PATCH; i++) {
1360*4882a593Smuzhiyun 		if (!(dev->patch_status[i] & WF_SLOT_FILLED)) {
1361*4882a593Smuzhiyun 			return i;
1362*4882a593Smuzhiyun 		}
1363*4882a593Smuzhiyun 	}
1364*4882a593Smuzhiyun 	snd_printk ("no free patch slots!\n");
1365*4882a593Smuzhiyun 	return -1;
1366*4882a593Smuzhiyun }
1367*4882a593Smuzhiyun #endif
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun static int
wavefront_load_patch(snd_wavefront_t * dev,const char __user * addr)1370*4882a593Smuzhiyun wavefront_load_patch (snd_wavefront_t *dev, const char __user *addr)
1371*4882a593Smuzhiyun {
1372*4882a593Smuzhiyun 	wavefront_patch_info *header;
1373*4882a593Smuzhiyun 	int err;
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	header = kmalloc(sizeof(*header), GFP_KERNEL);
1376*4882a593Smuzhiyun 	if (! header)
1377*4882a593Smuzhiyun 		return -ENOMEM;
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun 	if (copy_from_user (header, addr, sizeof(wavefront_patch_info) -
1380*4882a593Smuzhiyun 			    sizeof(wavefront_any))) {
1381*4882a593Smuzhiyun 		snd_printk ("bad address for load patch.\n");
1382*4882a593Smuzhiyun 		err = -EFAULT;
1383*4882a593Smuzhiyun 		goto __error;
1384*4882a593Smuzhiyun 	}
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_LOAD_PATCH, "download "
1387*4882a593Smuzhiyun 				      "Sample type: %d "
1388*4882a593Smuzhiyun 				      "Sample number: %d "
1389*4882a593Smuzhiyun 				      "Sample size: %d\n",
1390*4882a593Smuzhiyun 				      header->subkey,
1391*4882a593Smuzhiyun 				      header->number,
1392*4882a593Smuzhiyun 				      header->size);
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	switch (header->subkey) {
1395*4882a593Smuzhiyun 	case WF_ST_SAMPLE:  /* sample or sample_header, based on patch->size */
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun 		if (copy_from_user (&header->hdr.s, header->hdrptr,
1398*4882a593Smuzhiyun 				    sizeof (wavefront_sample))) {
1399*4882a593Smuzhiyun 			err = -EFAULT;
1400*4882a593Smuzhiyun 			break;
1401*4882a593Smuzhiyun 		}
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 		err = wavefront_send_sample (dev, header, header->dataptr, 0);
1404*4882a593Smuzhiyun 		break;
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	case WF_ST_MULTISAMPLE:
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 		if (copy_from_user (&header->hdr.s, header->hdrptr,
1409*4882a593Smuzhiyun 				    sizeof (wavefront_multisample))) {
1410*4882a593Smuzhiyun 			err = -EFAULT;
1411*4882a593Smuzhiyun 			break;
1412*4882a593Smuzhiyun 		}
1413*4882a593Smuzhiyun 
1414*4882a593Smuzhiyun 		err = wavefront_send_multisample (dev, header);
1415*4882a593Smuzhiyun 		break;
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun 	case WF_ST_ALIAS:
1418*4882a593Smuzhiyun 
1419*4882a593Smuzhiyun 		if (copy_from_user (&header->hdr.a, header->hdrptr,
1420*4882a593Smuzhiyun 				    sizeof (wavefront_alias))) {
1421*4882a593Smuzhiyun 			err = -EFAULT;
1422*4882a593Smuzhiyun 			break;
1423*4882a593Smuzhiyun 		}
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun 		err = wavefront_send_alias (dev, header);
1426*4882a593Smuzhiyun 		break;
1427*4882a593Smuzhiyun 
1428*4882a593Smuzhiyun 	case WF_ST_DRUM:
1429*4882a593Smuzhiyun 		if (copy_from_user (&header->hdr.d, header->hdrptr,
1430*4882a593Smuzhiyun 				    sizeof (wavefront_drum))) {
1431*4882a593Smuzhiyun 			err = -EFAULT;
1432*4882a593Smuzhiyun 			break;
1433*4882a593Smuzhiyun 		}
1434*4882a593Smuzhiyun 
1435*4882a593Smuzhiyun 		err = wavefront_send_drum (dev, header);
1436*4882a593Smuzhiyun 		break;
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 	case WF_ST_PATCH:
1439*4882a593Smuzhiyun 		if (copy_from_user (&header->hdr.p, header->hdrptr,
1440*4882a593Smuzhiyun 				    sizeof (wavefront_patch))) {
1441*4882a593Smuzhiyun 			err = -EFAULT;
1442*4882a593Smuzhiyun 			break;
1443*4882a593Smuzhiyun 		}
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 		err = wavefront_send_patch (dev, header);
1446*4882a593Smuzhiyun 		break;
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 	case WF_ST_PROGRAM:
1449*4882a593Smuzhiyun 		if (copy_from_user (&header->hdr.pr, header->hdrptr,
1450*4882a593Smuzhiyun 				    sizeof (wavefront_program))) {
1451*4882a593Smuzhiyun 			err = -EFAULT;
1452*4882a593Smuzhiyun 			break;
1453*4882a593Smuzhiyun 		}
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun 		err = wavefront_send_program (dev, header);
1456*4882a593Smuzhiyun 		break;
1457*4882a593Smuzhiyun 
1458*4882a593Smuzhiyun 	default:
1459*4882a593Smuzhiyun 		snd_printk ("unknown patch type %d.\n",
1460*4882a593Smuzhiyun 			    header->subkey);
1461*4882a593Smuzhiyun 		err = -EINVAL;
1462*4882a593Smuzhiyun 		break;
1463*4882a593Smuzhiyun 	}
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun  __error:
1466*4882a593Smuzhiyun 	kfree(header);
1467*4882a593Smuzhiyun 	return err;
1468*4882a593Smuzhiyun }
1469*4882a593Smuzhiyun 
1470*4882a593Smuzhiyun /***********************************************************************
1471*4882a593Smuzhiyun WaveFront: hardware-dependent interface
1472*4882a593Smuzhiyun ***********************************************************************/
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun static void
process_sample_hdr(u8 * buf)1475*4882a593Smuzhiyun process_sample_hdr (u8 *buf)
1476*4882a593Smuzhiyun 
1477*4882a593Smuzhiyun {
1478*4882a593Smuzhiyun 	wavefront_sample s;
1479*4882a593Smuzhiyun 	u8 *ptr;
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun 	ptr = buf;
1482*4882a593Smuzhiyun 
1483*4882a593Smuzhiyun 	/* The board doesn't send us an exact copy of a "wavefront_sample"
1484*4882a593Smuzhiyun 	   in response to an Upload Sample Header command. Instead, we
1485*4882a593Smuzhiyun 	   have to convert the data format back into our data structure,
1486*4882a593Smuzhiyun 	   just as in the Download Sample command, where we have to do
1487*4882a593Smuzhiyun 	   something very similar in the reverse direction.
1488*4882a593Smuzhiyun 	*/
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 	*((u32 *) &s.sampleStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
1491*4882a593Smuzhiyun 	*((u32 *) &s.loopStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
1492*4882a593Smuzhiyun 	*((u32 *) &s.loopEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
1493*4882a593Smuzhiyun 	*((u32 *) &s.sampleEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
1494*4882a593Smuzhiyun 	*((u32 *) &s.FrequencyBias) = demunge_int32 (ptr, 3); ptr += 3;
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	s.SampleResolution = *ptr & 0x3;
1497*4882a593Smuzhiyun 	s.Loop = *ptr & 0x8;
1498*4882a593Smuzhiyun 	s.Bidirectional = *ptr & 0x10;
1499*4882a593Smuzhiyun 	s.Reverse = *ptr & 0x40;
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	/* Now copy it back to where it came from */
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	memcpy (buf, (unsigned char *) &s, sizeof (wavefront_sample));
1504*4882a593Smuzhiyun }
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun static int
wavefront_synth_control(snd_wavefront_card_t * acard,wavefront_control * wc)1507*4882a593Smuzhiyun wavefront_synth_control (snd_wavefront_card_t *acard,
1508*4882a593Smuzhiyun 			 wavefront_control *wc)
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun {
1511*4882a593Smuzhiyun 	snd_wavefront_t *dev = &acard->wavefront;
1512*4882a593Smuzhiyun 	unsigned char patchnumbuf[2];
1513*4882a593Smuzhiyun 	int i;
1514*4882a593Smuzhiyun 
1515*4882a593Smuzhiyun 	DPRINT (WF_DEBUG_CMD, "synth control with "
1516*4882a593Smuzhiyun 		"cmd 0x%x\n", wc->cmd);
1517*4882a593Smuzhiyun 
1518*4882a593Smuzhiyun 	/* Pre-handling of or for various commands */
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun 	switch (wc->cmd) {
1521*4882a593Smuzhiyun 
1522*4882a593Smuzhiyun 	case WFC_DISABLE_INTERRUPTS:
1523*4882a593Smuzhiyun 		snd_printk ("interrupts disabled.\n");
1524*4882a593Smuzhiyun 		outb (0x80|0x20, dev->control_port);
1525*4882a593Smuzhiyun 		dev->interrupts_are_midi = 1;
1526*4882a593Smuzhiyun 		return 0;
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun 	case WFC_ENABLE_INTERRUPTS:
1529*4882a593Smuzhiyun 		snd_printk ("interrupts enabled.\n");
1530*4882a593Smuzhiyun 		outb (0x80|0x40|0x20, dev->control_port);
1531*4882a593Smuzhiyun 		dev->interrupts_are_midi = 1;
1532*4882a593Smuzhiyun 		return 0;
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	case WFC_INTERRUPT_STATUS:
1535*4882a593Smuzhiyun 		wc->rbuf[0] = dev->interrupts_are_midi;
1536*4882a593Smuzhiyun 		return 0;
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun 	case WFC_ROMSAMPLES_RDONLY:
1539*4882a593Smuzhiyun 		dev->rom_samples_rdonly = wc->wbuf[0];
1540*4882a593Smuzhiyun 		wc->status = 0;
1541*4882a593Smuzhiyun 		return 0;
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun 	case WFC_IDENTIFY_SLOT_TYPE:
1544*4882a593Smuzhiyun 		i = wc->wbuf[0] | (wc->wbuf[1] << 7);
1545*4882a593Smuzhiyun 		if (i <0 || i >= WF_MAX_SAMPLE) {
1546*4882a593Smuzhiyun 			snd_printk ("invalid slot ID %d\n",
1547*4882a593Smuzhiyun 				i);
1548*4882a593Smuzhiyun 			wc->status = EINVAL;
1549*4882a593Smuzhiyun 			return -EINVAL;
1550*4882a593Smuzhiyun 		}
1551*4882a593Smuzhiyun 		wc->rbuf[0] = dev->sample_status[i];
1552*4882a593Smuzhiyun 		wc->status = 0;
1553*4882a593Smuzhiyun 		return 0;
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun 	case WFC_DEBUG_DRIVER:
1556*4882a593Smuzhiyun 		dev->debug = wc->wbuf[0];
1557*4882a593Smuzhiyun 		snd_printk ("debug = 0x%x\n", dev->debug);
1558*4882a593Smuzhiyun 		return 0;
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun 	case WFC_UPLOAD_PATCH:
1561*4882a593Smuzhiyun 		munge_int32 (*((u32 *) wc->wbuf), patchnumbuf, 2);
1562*4882a593Smuzhiyun 		memcpy (wc->wbuf, patchnumbuf, 2);
1563*4882a593Smuzhiyun 		break;
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun 	case WFC_UPLOAD_MULTISAMPLE:
1566*4882a593Smuzhiyun 		/* multisamples have to be handled differently, and
1567*4882a593Smuzhiyun 		   cannot be dealt with properly by snd_wavefront_cmd() alone.
1568*4882a593Smuzhiyun 		*/
1569*4882a593Smuzhiyun 		wc->status = wavefront_fetch_multisample
1570*4882a593Smuzhiyun 			(dev, (wavefront_patch_info *) wc->rbuf);
1571*4882a593Smuzhiyun 		return 0;
1572*4882a593Smuzhiyun 
1573*4882a593Smuzhiyun 	case WFC_UPLOAD_SAMPLE_ALIAS:
1574*4882a593Smuzhiyun 		snd_printk ("support for sample alias upload "
1575*4882a593Smuzhiyun 			"being considered.\n");
1576*4882a593Smuzhiyun 		wc->status = EINVAL;
1577*4882a593Smuzhiyun 		return -EINVAL;
1578*4882a593Smuzhiyun 	}
1579*4882a593Smuzhiyun 
1580*4882a593Smuzhiyun 	wc->status = snd_wavefront_cmd (dev, wc->cmd, wc->rbuf, wc->wbuf);
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun 	/* Post-handling of certain commands.
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun 	   In particular, if the command was an upload, demunge the data
1585*4882a593Smuzhiyun 	   so that the user-level doesn't have to think about it.
1586*4882a593Smuzhiyun 	*/
1587*4882a593Smuzhiyun 
1588*4882a593Smuzhiyun 	if (wc->status == 0) {
1589*4882a593Smuzhiyun 		switch (wc->cmd) {
1590*4882a593Smuzhiyun 			/* intercept any freemem requests so that we know
1591*4882a593Smuzhiyun 			   we are always current with the user-level view
1592*4882a593Smuzhiyun 			   of things.
1593*4882a593Smuzhiyun 			*/
1594*4882a593Smuzhiyun 
1595*4882a593Smuzhiyun 		case WFC_REPORT_FREE_MEMORY:
1596*4882a593Smuzhiyun 			dev->freemem = demunge_int32 (wc->rbuf, 4);
1597*4882a593Smuzhiyun 			break;
1598*4882a593Smuzhiyun 
1599*4882a593Smuzhiyun 		case WFC_UPLOAD_PATCH:
1600*4882a593Smuzhiyun 			demunge_buf (wc->rbuf, wc->rbuf, WF_PATCH_BYTES);
1601*4882a593Smuzhiyun 			break;
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 		case WFC_UPLOAD_PROGRAM:
1604*4882a593Smuzhiyun 			demunge_buf (wc->rbuf, wc->rbuf, WF_PROGRAM_BYTES);
1605*4882a593Smuzhiyun 			break;
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 		case WFC_UPLOAD_EDRUM_PROGRAM:
1608*4882a593Smuzhiyun 			demunge_buf (wc->rbuf, wc->rbuf, WF_DRUM_BYTES - 1);
1609*4882a593Smuzhiyun 			break;
1610*4882a593Smuzhiyun 
1611*4882a593Smuzhiyun 		case WFC_UPLOAD_SAMPLE_HEADER:
1612*4882a593Smuzhiyun 			process_sample_hdr (wc->rbuf);
1613*4882a593Smuzhiyun 			break;
1614*4882a593Smuzhiyun 
1615*4882a593Smuzhiyun 		case WFC_UPLOAD_SAMPLE_ALIAS:
1616*4882a593Smuzhiyun 			snd_printk ("support for "
1617*4882a593Smuzhiyun 				    "sample aliases still "
1618*4882a593Smuzhiyun 				    "being considered.\n");
1619*4882a593Smuzhiyun 			break;
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun 		case WFC_VMIDI_OFF:
1622*4882a593Smuzhiyun 			snd_wavefront_midi_disable_virtual (acard);
1623*4882a593Smuzhiyun 			break;
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun 		case WFC_VMIDI_ON:
1626*4882a593Smuzhiyun 			snd_wavefront_midi_enable_virtual (acard);
1627*4882a593Smuzhiyun 			break;
1628*4882a593Smuzhiyun 		}
1629*4882a593Smuzhiyun 	}
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun 	return 0;
1632*4882a593Smuzhiyun }
1633*4882a593Smuzhiyun 
1634*4882a593Smuzhiyun int
snd_wavefront_synth_open(struct snd_hwdep * hw,struct file * file)1635*4882a593Smuzhiyun snd_wavefront_synth_open (struct snd_hwdep *hw, struct file *file)
1636*4882a593Smuzhiyun 
1637*4882a593Smuzhiyun {
1638*4882a593Smuzhiyun 	if (!try_module_get(hw->card->module))
1639*4882a593Smuzhiyun 		return -EFAULT;
1640*4882a593Smuzhiyun 	file->private_data = hw;
1641*4882a593Smuzhiyun 	return 0;
1642*4882a593Smuzhiyun }
1643*4882a593Smuzhiyun 
1644*4882a593Smuzhiyun int
snd_wavefront_synth_release(struct snd_hwdep * hw,struct file * file)1645*4882a593Smuzhiyun snd_wavefront_synth_release (struct snd_hwdep *hw, struct file *file)
1646*4882a593Smuzhiyun 
1647*4882a593Smuzhiyun {
1648*4882a593Smuzhiyun 	module_put(hw->card->module);
1649*4882a593Smuzhiyun 	return 0;
1650*4882a593Smuzhiyun }
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun int
snd_wavefront_synth_ioctl(struct snd_hwdep * hw,struct file * file,unsigned int cmd,unsigned long arg)1653*4882a593Smuzhiyun snd_wavefront_synth_ioctl (struct snd_hwdep *hw, struct file *file,
1654*4882a593Smuzhiyun 			   unsigned int cmd, unsigned long arg)
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun {
1657*4882a593Smuzhiyun 	struct snd_card *card;
1658*4882a593Smuzhiyun 	snd_wavefront_t *dev;
1659*4882a593Smuzhiyun 	snd_wavefront_card_t *acard;
1660*4882a593Smuzhiyun 	wavefront_control *wc;
1661*4882a593Smuzhiyun 	void __user *argp = (void __user *)arg;
1662*4882a593Smuzhiyun 	int err;
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 	card = (struct snd_card *) hw->card;
1665*4882a593Smuzhiyun 
1666*4882a593Smuzhiyun 	if (snd_BUG_ON(!card))
1667*4882a593Smuzhiyun 		return -ENODEV;
1668*4882a593Smuzhiyun 	if (snd_BUG_ON(!card->private_data))
1669*4882a593Smuzhiyun 		return -ENODEV;
1670*4882a593Smuzhiyun 
1671*4882a593Smuzhiyun 	acard = card->private_data;
1672*4882a593Smuzhiyun 	dev = &acard->wavefront;
1673*4882a593Smuzhiyun 
1674*4882a593Smuzhiyun 	switch (cmd) {
1675*4882a593Smuzhiyun 	case WFCTL_LOAD_SPP:
1676*4882a593Smuzhiyun 		if (wavefront_load_patch (dev, argp) != 0) {
1677*4882a593Smuzhiyun 			return -EIO;
1678*4882a593Smuzhiyun 		}
1679*4882a593Smuzhiyun 		break;
1680*4882a593Smuzhiyun 
1681*4882a593Smuzhiyun 	case WFCTL_WFCMD:
1682*4882a593Smuzhiyun 		wc = memdup_user(argp, sizeof(*wc));
1683*4882a593Smuzhiyun 		if (IS_ERR(wc))
1684*4882a593Smuzhiyun 			return PTR_ERR(wc);
1685*4882a593Smuzhiyun 
1686*4882a593Smuzhiyun 		if (wavefront_synth_control (acard, wc) < 0)
1687*4882a593Smuzhiyun 			err = -EIO;
1688*4882a593Smuzhiyun 		else if (copy_to_user (argp, wc, sizeof (*wc)))
1689*4882a593Smuzhiyun 			err = -EFAULT;
1690*4882a593Smuzhiyun 		else
1691*4882a593Smuzhiyun 			err = 0;
1692*4882a593Smuzhiyun 		kfree(wc);
1693*4882a593Smuzhiyun 		return err;
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun 	default:
1696*4882a593Smuzhiyun 		return -EINVAL;
1697*4882a593Smuzhiyun 	}
1698*4882a593Smuzhiyun 
1699*4882a593Smuzhiyun 	return 0;
1700*4882a593Smuzhiyun }
1701*4882a593Smuzhiyun 
1702*4882a593Smuzhiyun 
1703*4882a593Smuzhiyun /***********************************************************************/
1704*4882a593Smuzhiyun /*  WaveFront: interface for card-level wavefront module               */
1705*4882a593Smuzhiyun /***********************************************************************/
1706*4882a593Smuzhiyun 
1707*4882a593Smuzhiyun void
snd_wavefront_internal_interrupt(snd_wavefront_card_t * card)1708*4882a593Smuzhiyun snd_wavefront_internal_interrupt (snd_wavefront_card_t *card)
1709*4882a593Smuzhiyun {
1710*4882a593Smuzhiyun 	snd_wavefront_t *dev = &card->wavefront;
1711*4882a593Smuzhiyun 
1712*4882a593Smuzhiyun 	/*
1713*4882a593Smuzhiyun 	   Some comments on interrupts. I attempted a version of this
1714*4882a593Smuzhiyun 	   driver that used interrupts throughout the code instead of
1715*4882a593Smuzhiyun 	   doing busy and/or sleep-waiting. Alas, it appears that once
1716*4882a593Smuzhiyun 	   the Motorola firmware is downloaded, the card *never*
1717*4882a593Smuzhiyun 	   generates an RX interrupt. These are successfully generated
1718*4882a593Smuzhiyun 	   during firmware loading, and after that wavefront_status()
1719*4882a593Smuzhiyun 	   reports that an interrupt is pending on the card from time
1720*4882a593Smuzhiyun 	   to time, but it never seems to be delivered to this
1721*4882a593Smuzhiyun 	   driver. Note also that wavefront_status() continues to
1722*4882a593Smuzhiyun 	   report that RX interrupts are enabled, suggesting that I
1723*4882a593Smuzhiyun 	   didn't goof up and disable them by mistake.
1724*4882a593Smuzhiyun 
1725*4882a593Smuzhiyun 	   Thus, I stepped back to a prior version of
1726*4882a593Smuzhiyun 	   wavefront_wait(), the only place where this really
1727*4882a593Smuzhiyun 	   matters. Its sad, but I've looked through the code to check
1728*4882a593Smuzhiyun 	   on things, and I really feel certain that the Motorola
1729*4882a593Smuzhiyun 	   firmware prevents RX-ready interrupts.
1730*4882a593Smuzhiyun 	*/
1731*4882a593Smuzhiyun 
1732*4882a593Smuzhiyun 	if ((wavefront_status(dev) & (STAT_INTR_READ|STAT_INTR_WRITE)) == 0) {
1733*4882a593Smuzhiyun 		return;
1734*4882a593Smuzhiyun 	}
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun 	spin_lock(&dev->irq_lock);
1737*4882a593Smuzhiyun 	dev->irq_ok = 1;
1738*4882a593Smuzhiyun 	dev->irq_cnt++;
1739*4882a593Smuzhiyun 	spin_unlock(&dev->irq_lock);
1740*4882a593Smuzhiyun 	wake_up(&dev->interrupt_sleeper);
1741*4882a593Smuzhiyun }
1742*4882a593Smuzhiyun 
1743*4882a593Smuzhiyun /* STATUS REGISTER
1744*4882a593Smuzhiyun 
1745*4882a593Smuzhiyun 0 Host Rx Interrupt Enable (1=Enabled)
1746*4882a593Smuzhiyun 1 Host Rx Register Full (1=Full)
1747*4882a593Smuzhiyun 2 Host Rx Interrupt Pending (1=Interrupt)
1748*4882a593Smuzhiyun 3 Unused
1749*4882a593Smuzhiyun 4 Host Tx Interrupt (1=Enabled)
1750*4882a593Smuzhiyun 5 Host Tx Register empty (1=Empty)
1751*4882a593Smuzhiyun 6 Host Tx Interrupt Pending (1=Interrupt)
1752*4882a593Smuzhiyun 7 Unused
1753*4882a593Smuzhiyun */
1754*4882a593Smuzhiyun 
1755*4882a593Smuzhiyun static int
snd_wavefront_interrupt_bits(int irq)1756*4882a593Smuzhiyun snd_wavefront_interrupt_bits (int irq)
1757*4882a593Smuzhiyun 
1758*4882a593Smuzhiyun {
1759*4882a593Smuzhiyun 	int bits;
1760*4882a593Smuzhiyun 
1761*4882a593Smuzhiyun 	switch (irq) {
1762*4882a593Smuzhiyun 	case 9:
1763*4882a593Smuzhiyun 		bits = 0x00;
1764*4882a593Smuzhiyun 		break;
1765*4882a593Smuzhiyun 	case 5:
1766*4882a593Smuzhiyun 		bits = 0x08;
1767*4882a593Smuzhiyun 		break;
1768*4882a593Smuzhiyun 	case 12:
1769*4882a593Smuzhiyun 		bits = 0x10;
1770*4882a593Smuzhiyun 		break;
1771*4882a593Smuzhiyun 	case 15:
1772*4882a593Smuzhiyun 		bits = 0x18;
1773*4882a593Smuzhiyun 		break;
1774*4882a593Smuzhiyun 
1775*4882a593Smuzhiyun 	default:
1776*4882a593Smuzhiyun 		snd_printk ("invalid IRQ %d\n", irq);
1777*4882a593Smuzhiyun 		bits = -1;
1778*4882a593Smuzhiyun 	}
1779*4882a593Smuzhiyun 
1780*4882a593Smuzhiyun 	return bits;
1781*4882a593Smuzhiyun }
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun static void
wavefront_should_cause_interrupt(snd_wavefront_t * dev,int val,int port,unsigned long timeout)1784*4882a593Smuzhiyun wavefront_should_cause_interrupt (snd_wavefront_t *dev,
1785*4882a593Smuzhiyun 				  int val, int port, unsigned long timeout)
1786*4882a593Smuzhiyun 
1787*4882a593Smuzhiyun {
1788*4882a593Smuzhiyun 	wait_queue_entry_t wait;
1789*4882a593Smuzhiyun 
1790*4882a593Smuzhiyun 	init_waitqueue_entry(&wait, current);
1791*4882a593Smuzhiyun 	spin_lock_irq(&dev->irq_lock);
1792*4882a593Smuzhiyun 	add_wait_queue(&dev->interrupt_sleeper, &wait);
1793*4882a593Smuzhiyun 	dev->irq_ok = 0;
1794*4882a593Smuzhiyun 	outb (val,port);
1795*4882a593Smuzhiyun 	spin_unlock_irq(&dev->irq_lock);
1796*4882a593Smuzhiyun 	while (!dev->irq_ok && time_before(jiffies, timeout)) {
1797*4882a593Smuzhiyun 		schedule_timeout_uninterruptible(1);
1798*4882a593Smuzhiyun 		barrier();
1799*4882a593Smuzhiyun 	}
1800*4882a593Smuzhiyun }
1801*4882a593Smuzhiyun 
1802*4882a593Smuzhiyun static int
wavefront_reset_to_cleanliness(snd_wavefront_t * dev)1803*4882a593Smuzhiyun wavefront_reset_to_cleanliness (snd_wavefront_t *dev)
1804*4882a593Smuzhiyun 
1805*4882a593Smuzhiyun {
1806*4882a593Smuzhiyun 	int bits;
1807*4882a593Smuzhiyun 	int hwv[2];
1808*4882a593Smuzhiyun 
1809*4882a593Smuzhiyun 	/* IRQ already checked */
1810*4882a593Smuzhiyun 
1811*4882a593Smuzhiyun 	bits = snd_wavefront_interrupt_bits (dev->irq);
1812*4882a593Smuzhiyun 
1813*4882a593Smuzhiyun 	/* try reset of port */
1814*4882a593Smuzhiyun 
1815*4882a593Smuzhiyun 	outb (0x0, dev->control_port);
1816*4882a593Smuzhiyun 
1817*4882a593Smuzhiyun 	/* At this point, the board is in reset, and the H/W initialization
1818*4882a593Smuzhiyun 	   register is accessed at the same address as the data port.
1819*4882a593Smuzhiyun 
1820*4882a593Smuzhiyun 	   Bit 7 - Enable IRQ Driver
1821*4882a593Smuzhiyun 	   0 - Tri-state the Wave-Board drivers for the PC Bus IRQs
1822*4882a593Smuzhiyun 	   1 - Enable IRQ selected by bits 5:3 to be driven onto the PC Bus.
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun 	   Bit 6 - MIDI Interface Select
1825*4882a593Smuzhiyun 
1826*4882a593Smuzhiyun 	   0 - Use the MIDI Input from the 26-pin WaveBlaster
1827*4882a593Smuzhiyun 	   compatible header as the serial MIDI source
1828*4882a593Smuzhiyun 	   1 - Use the MIDI Input from the 9-pin D connector as the
1829*4882a593Smuzhiyun 	   serial MIDI source.
1830*4882a593Smuzhiyun 
1831*4882a593Smuzhiyun 	   Bits 5:3 - IRQ Selection
1832*4882a593Smuzhiyun 	   0 0 0 - IRQ 2/9
1833*4882a593Smuzhiyun 	   0 0 1 - IRQ 5
1834*4882a593Smuzhiyun 	   0 1 0 - IRQ 12
1835*4882a593Smuzhiyun 	   0 1 1 - IRQ 15
1836*4882a593Smuzhiyun 	   1 0 0 - Reserved
1837*4882a593Smuzhiyun 	   1 0 1 - Reserved
1838*4882a593Smuzhiyun 	   1 1 0 - Reserved
1839*4882a593Smuzhiyun 	   1 1 1 - Reserved
1840*4882a593Smuzhiyun 
1841*4882a593Smuzhiyun 	   Bits 2:1 - Reserved
1842*4882a593Smuzhiyun 	   Bit 0 - Disable Boot ROM
1843*4882a593Smuzhiyun 	   0 - memory accesses to 03FC30-03FFFFH utilize the internal Boot ROM
1844*4882a593Smuzhiyun 	   1 - memory accesses to 03FC30-03FFFFH are directed to external
1845*4882a593Smuzhiyun 	   storage.
1846*4882a593Smuzhiyun 
1847*4882a593Smuzhiyun 	*/
1848*4882a593Smuzhiyun 
1849*4882a593Smuzhiyun 	/* configure hardware: IRQ, enable interrupts,
1850*4882a593Smuzhiyun 	   plus external 9-pin MIDI interface selected
1851*4882a593Smuzhiyun 	*/
1852*4882a593Smuzhiyun 
1853*4882a593Smuzhiyun 	outb (0x80 | 0x40 | bits, dev->data_port);
1854*4882a593Smuzhiyun 
1855*4882a593Smuzhiyun 	/* CONTROL REGISTER
1856*4882a593Smuzhiyun 
1857*4882a593Smuzhiyun 	   0 Host Rx Interrupt Enable (1=Enabled)      0x1
1858*4882a593Smuzhiyun 	   1 Unused                                    0x2
1859*4882a593Smuzhiyun 	   2 Unused                                    0x4
1860*4882a593Smuzhiyun 	   3 Unused                                    0x8
1861*4882a593Smuzhiyun 	   4 Host Tx Interrupt Enable                 0x10
1862*4882a593Smuzhiyun 	   5 Mute (0=Mute; 1=Play)                    0x20
1863*4882a593Smuzhiyun 	   6 Master Interrupt Enable (1=Enabled)      0x40
1864*4882a593Smuzhiyun 	   7 Master Reset (0=Reset; 1=Run)            0x80
1865*4882a593Smuzhiyun 
1866*4882a593Smuzhiyun 	   Take us out of reset, mute output, master + TX + RX interrupts on.
1867*4882a593Smuzhiyun 
1868*4882a593Smuzhiyun 	   We'll get an interrupt presumably to tell us that the TX
1869*4882a593Smuzhiyun 	   register is clear.
1870*4882a593Smuzhiyun 	*/
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun 	wavefront_should_cause_interrupt(dev, 0x80|0x40|0x10|0x1,
1873*4882a593Smuzhiyun 					 dev->control_port,
1874*4882a593Smuzhiyun 					 (reset_time*HZ)/100);
1875*4882a593Smuzhiyun 
1876*4882a593Smuzhiyun 	/* Note: data port is now the data port, not the h/w initialization
1877*4882a593Smuzhiyun 	   port.
1878*4882a593Smuzhiyun 	 */
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun 	if (!dev->irq_ok) {
1881*4882a593Smuzhiyun 		snd_printk ("intr not received after h/w un-reset.\n");
1882*4882a593Smuzhiyun 		goto gone_bad;
1883*4882a593Smuzhiyun 	}
1884*4882a593Smuzhiyun 
1885*4882a593Smuzhiyun 	/* Note: data port is now the data port, not the h/w initialization
1886*4882a593Smuzhiyun 	   port.
1887*4882a593Smuzhiyun 
1888*4882a593Smuzhiyun 	   At this point, only "HW VERSION" or "DOWNLOAD OS" commands
1889*4882a593Smuzhiyun 	   will work. So, issue one of them, and wait for TX
1890*4882a593Smuzhiyun 	   interrupt. This can take a *long* time after a cold boot,
1891*4882a593Smuzhiyun 	   while the ISC ROM does its RAM test. The SDK says up to 4
1892*4882a593Smuzhiyun 	   seconds - with 12MB of RAM on a Tropez+, it takes a lot
1893*4882a593Smuzhiyun 	   longer than that (~16secs). Note that the card understands
1894*4882a593Smuzhiyun 	   the difference between a warm and a cold boot, so
1895*4882a593Smuzhiyun 	   subsequent ISC2115 reboots (say, caused by module
1896*4882a593Smuzhiyun 	   reloading) will get through this much faster.
1897*4882a593Smuzhiyun 
1898*4882a593Smuzhiyun 	   XXX Interesting question: why is no RX interrupt received first ?
1899*4882a593Smuzhiyun 	*/
1900*4882a593Smuzhiyun 
1901*4882a593Smuzhiyun 	wavefront_should_cause_interrupt(dev, WFC_HARDWARE_VERSION,
1902*4882a593Smuzhiyun 					 dev->data_port, ramcheck_time*HZ);
1903*4882a593Smuzhiyun 
1904*4882a593Smuzhiyun 	if (!dev->irq_ok) {
1905*4882a593Smuzhiyun 		snd_printk ("post-RAM-check interrupt not received.\n");
1906*4882a593Smuzhiyun 		goto gone_bad;
1907*4882a593Smuzhiyun 	}
1908*4882a593Smuzhiyun 
1909*4882a593Smuzhiyun 	if (!wavefront_wait (dev, STAT_CAN_READ)) {
1910*4882a593Smuzhiyun 		snd_printk ("no response to HW version cmd.\n");
1911*4882a593Smuzhiyun 		goto gone_bad;
1912*4882a593Smuzhiyun 	}
1913*4882a593Smuzhiyun 
1914*4882a593Smuzhiyun 	if ((hwv[0] = wavefront_read (dev)) == -1) {
1915*4882a593Smuzhiyun 		snd_printk ("board not responding correctly.\n");
1916*4882a593Smuzhiyun 		goto gone_bad;
1917*4882a593Smuzhiyun 	}
1918*4882a593Smuzhiyun 
1919*4882a593Smuzhiyun 	if (hwv[0] == 0xFF) { /* NAK */
1920*4882a593Smuzhiyun 
1921*4882a593Smuzhiyun 		/* Board's RAM test failed. Try to read error code,
1922*4882a593Smuzhiyun 		   and tell us about it either way.
1923*4882a593Smuzhiyun 		*/
1924*4882a593Smuzhiyun 
1925*4882a593Smuzhiyun 		if ((hwv[0] = wavefront_read (dev)) == -1) {
1926*4882a593Smuzhiyun 			snd_printk ("on-board RAM test failed "
1927*4882a593Smuzhiyun 				    "(bad error code).\n");
1928*4882a593Smuzhiyun 		} else {
1929*4882a593Smuzhiyun 			snd_printk ("on-board RAM test failed "
1930*4882a593Smuzhiyun 				    "(error code: 0x%x).\n",
1931*4882a593Smuzhiyun 				hwv[0]);
1932*4882a593Smuzhiyun 		}
1933*4882a593Smuzhiyun 		goto gone_bad;
1934*4882a593Smuzhiyun 	}
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	/* We're OK, just get the next byte of the HW version response */
1937*4882a593Smuzhiyun 
1938*4882a593Smuzhiyun 	if ((hwv[1] = wavefront_read (dev)) == -1) {
1939*4882a593Smuzhiyun 		snd_printk ("incorrect h/w response.\n");
1940*4882a593Smuzhiyun 		goto gone_bad;
1941*4882a593Smuzhiyun 	}
1942*4882a593Smuzhiyun 
1943*4882a593Smuzhiyun 	snd_printk ("hardware version %d.%d\n",
1944*4882a593Smuzhiyun 		    hwv[0], hwv[1]);
1945*4882a593Smuzhiyun 
1946*4882a593Smuzhiyun 	return 0;
1947*4882a593Smuzhiyun 
1948*4882a593Smuzhiyun 
1949*4882a593Smuzhiyun      gone_bad:
1950*4882a593Smuzhiyun 	return (1);
1951*4882a593Smuzhiyun }
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun static int
wavefront_download_firmware(snd_wavefront_t * dev,char * path)1954*4882a593Smuzhiyun wavefront_download_firmware (snd_wavefront_t *dev, char *path)
1955*4882a593Smuzhiyun 
1956*4882a593Smuzhiyun {
1957*4882a593Smuzhiyun 	const unsigned char *buf;
1958*4882a593Smuzhiyun 	int len, err;
1959*4882a593Smuzhiyun 	int section_cnt_downloaded = 0;
1960*4882a593Smuzhiyun 	const struct firmware *firmware;
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 	err = request_firmware(&firmware, path, dev->card->dev);
1963*4882a593Smuzhiyun 	if (err < 0) {
1964*4882a593Smuzhiyun 		snd_printk(KERN_ERR "firmware (%s) download failed!!!\n", path);
1965*4882a593Smuzhiyun 		return 1;
1966*4882a593Smuzhiyun 	}
1967*4882a593Smuzhiyun 
1968*4882a593Smuzhiyun 	len = 0;
1969*4882a593Smuzhiyun 	buf = firmware->data;
1970*4882a593Smuzhiyun 	for (;;) {
1971*4882a593Smuzhiyun 		int section_length = *(signed char *)buf;
1972*4882a593Smuzhiyun 		if (section_length == 0)
1973*4882a593Smuzhiyun 			break;
1974*4882a593Smuzhiyun 		if (section_length < 0 || section_length > WF_SECTION_MAX) {
1975*4882a593Smuzhiyun 			snd_printk(KERN_ERR
1976*4882a593Smuzhiyun 				   "invalid firmware section length %d\n",
1977*4882a593Smuzhiyun 				   section_length);
1978*4882a593Smuzhiyun 			goto failure;
1979*4882a593Smuzhiyun 		}
1980*4882a593Smuzhiyun 		buf++;
1981*4882a593Smuzhiyun 		len++;
1982*4882a593Smuzhiyun 
1983*4882a593Smuzhiyun 		if (firmware->size < len + section_length) {
1984*4882a593Smuzhiyun 			snd_printk(KERN_ERR "firmware section read error.\n");
1985*4882a593Smuzhiyun 			goto failure;
1986*4882a593Smuzhiyun 		}
1987*4882a593Smuzhiyun 
1988*4882a593Smuzhiyun 		/* Send command */
1989*4882a593Smuzhiyun 		if (wavefront_write(dev, WFC_DOWNLOAD_OS))
1990*4882a593Smuzhiyun 			goto failure;
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun 		for (; section_length; section_length--) {
1993*4882a593Smuzhiyun 			if (wavefront_write(dev, *buf))
1994*4882a593Smuzhiyun 				goto failure;
1995*4882a593Smuzhiyun 			buf++;
1996*4882a593Smuzhiyun 			len++;
1997*4882a593Smuzhiyun 		}
1998*4882a593Smuzhiyun 
1999*4882a593Smuzhiyun 		/* get ACK */
2000*4882a593Smuzhiyun 		if (!wavefront_wait(dev, STAT_CAN_READ)) {
2001*4882a593Smuzhiyun 			snd_printk(KERN_ERR "time out for firmware ACK.\n");
2002*4882a593Smuzhiyun 			goto failure;
2003*4882a593Smuzhiyun 		}
2004*4882a593Smuzhiyun 		err = inb(dev->data_port);
2005*4882a593Smuzhiyun 		if (err != WF_ACK) {
2006*4882a593Smuzhiyun 			snd_printk(KERN_ERR
2007*4882a593Smuzhiyun 				   "download of section #%d not "
2008*4882a593Smuzhiyun 				   "acknowledged, ack = 0x%x\n",
2009*4882a593Smuzhiyun 				   section_cnt_downloaded + 1, err);
2010*4882a593Smuzhiyun 			goto failure;
2011*4882a593Smuzhiyun 		}
2012*4882a593Smuzhiyun 
2013*4882a593Smuzhiyun 		section_cnt_downloaded++;
2014*4882a593Smuzhiyun 	}
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 	release_firmware(firmware);
2017*4882a593Smuzhiyun 	return 0;
2018*4882a593Smuzhiyun 
2019*4882a593Smuzhiyun  failure:
2020*4882a593Smuzhiyun 	release_firmware(firmware);
2021*4882a593Smuzhiyun 	snd_printk(KERN_ERR "firmware download failed!!!\n");
2022*4882a593Smuzhiyun 	return 1;
2023*4882a593Smuzhiyun }
2024*4882a593Smuzhiyun 
2025*4882a593Smuzhiyun 
2026*4882a593Smuzhiyun static int
wavefront_do_reset(snd_wavefront_t * dev)2027*4882a593Smuzhiyun wavefront_do_reset (snd_wavefront_t *dev)
2028*4882a593Smuzhiyun 
2029*4882a593Smuzhiyun {
2030*4882a593Smuzhiyun 	char voices[1];
2031*4882a593Smuzhiyun 
2032*4882a593Smuzhiyun 	if (wavefront_reset_to_cleanliness (dev)) {
2033*4882a593Smuzhiyun 		snd_printk ("hw reset failed.\n");
2034*4882a593Smuzhiyun 		goto gone_bad;
2035*4882a593Smuzhiyun 	}
2036*4882a593Smuzhiyun 
2037*4882a593Smuzhiyun 	if (dev->israw) {
2038*4882a593Smuzhiyun 		if (wavefront_download_firmware (dev, ospath)) {
2039*4882a593Smuzhiyun 			goto gone_bad;
2040*4882a593Smuzhiyun 		}
2041*4882a593Smuzhiyun 
2042*4882a593Smuzhiyun 		dev->israw = 0;
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun 		/* Wait for the OS to get running. The protocol for
2045*4882a593Smuzhiyun 		   this is non-obvious, and was determined by
2046*4882a593Smuzhiyun 		   using port-IO tracing in DOSemu and some
2047*4882a593Smuzhiyun 		   experimentation here.
2048*4882a593Smuzhiyun 
2049*4882a593Smuzhiyun 		   Rather than using timed waits, use interrupts creatively.
2050*4882a593Smuzhiyun 		*/
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun 		wavefront_should_cause_interrupt (dev, WFC_NOOP,
2053*4882a593Smuzhiyun 						  dev->data_port,
2054*4882a593Smuzhiyun 						  (osrun_time*HZ));
2055*4882a593Smuzhiyun 
2056*4882a593Smuzhiyun 		if (!dev->irq_ok) {
2057*4882a593Smuzhiyun 			snd_printk ("no post-OS interrupt.\n");
2058*4882a593Smuzhiyun 			goto gone_bad;
2059*4882a593Smuzhiyun 		}
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun 		/* Now, do it again ! */
2062*4882a593Smuzhiyun 
2063*4882a593Smuzhiyun 		wavefront_should_cause_interrupt (dev, WFC_NOOP,
2064*4882a593Smuzhiyun 						  dev->data_port, (10*HZ));
2065*4882a593Smuzhiyun 
2066*4882a593Smuzhiyun 		if (!dev->irq_ok) {
2067*4882a593Smuzhiyun 			snd_printk ("no post-OS interrupt(2).\n");
2068*4882a593Smuzhiyun 			goto gone_bad;
2069*4882a593Smuzhiyun 		}
2070*4882a593Smuzhiyun 
2071*4882a593Smuzhiyun 		/* OK, no (RX/TX) interrupts any more, but leave mute
2072*4882a593Smuzhiyun 		   in effect.
2073*4882a593Smuzhiyun 		*/
2074*4882a593Smuzhiyun 
2075*4882a593Smuzhiyun 		outb (0x80|0x40, dev->control_port);
2076*4882a593Smuzhiyun 	}
2077*4882a593Smuzhiyun 
2078*4882a593Smuzhiyun 	/* SETUPSND.EXE asks for sample memory config here, but since i
2079*4882a593Smuzhiyun 	   have no idea how to interpret the result, we'll forget
2080*4882a593Smuzhiyun 	   about it.
2081*4882a593Smuzhiyun 	*/
2082*4882a593Smuzhiyun 
2083*4882a593Smuzhiyun 	if ((dev->freemem = wavefront_freemem (dev)) < 0) {
2084*4882a593Smuzhiyun 		goto gone_bad;
2085*4882a593Smuzhiyun 	}
2086*4882a593Smuzhiyun 
2087*4882a593Smuzhiyun 	snd_printk ("available DRAM %dk\n", dev->freemem / 1024);
2088*4882a593Smuzhiyun 
2089*4882a593Smuzhiyun 	if (wavefront_write (dev, 0xf0) ||
2090*4882a593Smuzhiyun 	    wavefront_write (dev, 1) ||
2091*4882a593Smuzhiyun 	    (wavefront_read (dev) < 0)) {
2092*4882a593Smuzhiyun 		dev->debug = 0;
2093*4882a593Smuzhiyun 		snd_printk ("MPU emulation mode not set.\n");
2094*4882a593Smuzhiyun 		goto gone_bad;
2095*4882a593Smuzhiyun 	}
2096*4882a593Smuzhiyun 
2097*4882a593Smuzhiyun 	voices[0] = 32;
2098*4882a593Smuzhiyun 
2099*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_SET_NVOICES, NULL, voices)) {
2100*4882a593Smuzhiyun 		snd_printk ("cannot set number of voices to 32.\n");
2101*4882a593Smuzhiyun 		goto gone_bad;
2102*4882a593Smuzhiyun 	}
2103*4882a593Smuzhiyun 
2104*4882a593Smuzhiyun 
2105*4882a593Smuzhiyun 	return 0;
2106*4882a593Smuzhiyun 
2107*4882a593Smuzhiyun  gone_bad:
2108*4882a593Smuzhiyun 	/* reset that sucker so that it doesn't bother us. */
2109*4882a593Smuzhiyun 
2110*4882a593Smuzhiyun 	outb (0x0, dev->control_port);
2111*4882a593Smuzhiyun 	dev->interrupts_are_midi = 0;
2112*4882a593Smuzhiyun 	return 1;
2113*4882a593Smuzhiyun }
2114*4882a593Smuzhiyun 
2115*4882a593Smuzhiyun int
snd_wavefront_start(snd_wavefront_t * dev)2116*4882a593Smuzhiyun snd_wavefront_start (snd_wavefront_t *dev)
2117*4882a593Smuzhiyun 
2118*4882a593Smuzhiyun {
2119*4882a593Smuzhiyun 	int samples_are_from_rom;
2120*4882a593Smuzhiyun 
2121*4882a593Smuzhiyun 	/* IMPORTANT: assumes that snd_wavefront_detect() and/or
2122*4882a593Smuzhiyun 	   wavefront_reset_to_cleanliness() has already been called
2123*4882a593Smuzhiyun 	*/
2124*4882a593Smuzhiyun 
2125*4882a593Smuzhiyun 	if (dev->israw) {
2126*4882a593Smuzhiyun 		samples_are_from_rom = 1;
2127*4882a593Smuzhiyun 	} else {
2128*4882a593Smuzhiyun 		/* XXX is this always true ? */
2129*4882a593Smuzhiyun 		samples_are_from_rom = 0;
2130*4882a593Smuzhiyun 	}
2131*4882a593Smuzhiyun 
2132*4882a593Smuzhiyun 	if (dev->israw || fx_raw) {
2133*4882a593Smuzhiyun 		if (wavefront_do_reset (dev)) {
2134*4882a593Smuzhiyun 			return -1;
2135*4882a593Smuzhiyun 		}
2136*4882a593Smuzhiyun 	}
2137*4882a593Smuzhiyun 	/* Check for FX device, present only on Tropez+ */
2138*4882a593Smuzhiyun 
2139*4882a593Smuzhiyun 	dev->has_fx = (snd_wavefront_fx_detect (dev) == 0);
2140*4882a593Smuzhiyun 
2141*4882a593Smuzhiyun 	if (dev->has_fx && fx_raw) {
2142*4882a593Smuzhiyun 		snd_wavefront_fx_start (dev);
2143*4882a593Smuzhiyun 	}
2144*4882a593Smuzhiyun 
2145*4882a593Smuzhiyun 	wavefront_get_sample_status (dev, samples_are_from_rom);
2146*4882a593Smuzhiyun 	wavefront_get_program_status (dev);
2147*4882a593Smuzhiyun 	wavefront_get_patch_status (dev);
2148*4882a593Smuzhiyun 
2149*4882a593Smuzhiyun 	/* Start normal operation: unreset, master interrupt enabled, no mute
2150*4882a593Smuzhiyun 	*/
2151*4882a593Smuzhiyun 
2152*4882a593Smuzhiyun 	outb (0x80|0x40|0x20, dev->control_port);
2153*4882a593Smuzhiyun 
2154*4882a593Smuzhiyun 	return (0);
2155*4882a593Smuzhiyun }
2156*4882a593Smuzhiyun 
2157*4882a593Smuzhiyun int
snd_wavefront_detect(snd_wavefront_card_t * card)2158*4882a593Smuzhiyun snd_wavefront_detect (snd_wavefront_card_t *card)
2159*4882a593Smuzhiyun 
2160*4882a593Smuzhiyun {
2161*4882a593Smuzhiyun 	unsigned char   rbuf[4], wbuf[4];
2162*4882a593Smuzhiyun 	snd_wavefront_t *dev = &card->wavefront;
2163*4882a593Smuzhiyun 
2164*4882a593Smuzhiyun 	/* returns zero if a WaveFront card is successfully detected.
2165*4882a593Smuzhiyun 	   negative otherwise.
2166*4882a593Smuzhiyun 	*/
2167*4882a593Smuzhiyun 
2168*4882a593Smuzhiyun 	dev->israw = 0;
2169*4882a593Smuzhiyun 	dev->has_fx = 0;
2170*4882a593Smuzhiyun 	dev->debug = debug_default;
2171*4882a593Smuzhiyun 	dev->interrupts_are_midi = 0;
2172*4882a593Smuzhiyun 	dev->irq_cnt = 0;
2173*4882a593Smuzhiyun 	dev->rom_samples_rdonly = 1;
2174*4882a593Smuzhiyun 
2175*4882a593Smuzhiyun 	if (snd_wavefront_cmd (dev, WFC_FIRMWARE_VERSION, rbuf, wbuf) == 0) {
2176*4882a593Smuzhiyun 
2177*4882a593Smuzhiyun 		dev->fw_version[0] = rbuf[0];
2178*4882a593Smuzhiyun 		dev->fw_version[1] = rbuf[1];
2179*4882a593Smuzhiyun 
2180*4882a593Smuzhiyun 		snd_printk ("firmware %d.%d already loaded.\n",
2181*4882a593Smuzhiyun 			    rbuf[0], rbuf[1]);
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun 		/* check that a command actually works */
2184*4882a593Smuzhiyun 
2185*4882a593Smuzhiyun 		if (snd_wavefront_cmd (dev, WFC_HARDWARE_VERSION,
2186*4882a593Smuzhiyun 				       rbuf, wbuf) == 0) {
2187*4882a593Smuzhiyun 			dev->hw_version[0] = rbuf[0];
2188*4882a593Smuzhiyun 			dev->hw_version[1] = rbuf[1];
2189*4882a593Smuzhiyun 		} else {
2190*4882a593Smuzhiyun 			snd_printk ("not raw, but no "
2191*4882a593Smuzhiyun 				    "hardware version!\n");
2192*4882a593Smuzhiyun 			return -1;
2193*4882a593Smuzhiyun 		}
2194*4882a593Smuzhiyun 
2195*4882a593Smuzhiyun 		if (!wf_raw) {
2196*4882a593Smuzhiyun 			return 0;
2197*4882a593Smuzhiyun 		} else {
2198*4882a593Smuzhiyun 			snd_printk ("reloading firmware as you requested.\n");
2199*4882a593Smuzhiyun 			dev->israw = 1;
2200*4882a593Smuzhiyun 		}
2201*4882a593Smuzhiyun 
2202*4882a593Smuzhiyun 	} else {
2203*4882a593Smuzhiyun 
2204*4882a593Smuzhiyun 		dev->israw = 1;
2205*4882a593Smuzhiyun 		snd_printk ("no response to firmware probe, assume raw.\n");
2206*4882a593Smuzhiyun 
2207*4882a593Smuzhiyun 	}
2208*4882a593Smuzhiyun 
2209*4882a593Smuzhiyun 	return 0;
2210*4882a593Smuzhiyun }
2211*4882a593Smuzhiyun 
2212*4882a593Smuzhiyun MODULE_FIRMWARE(DEFAULT_OSPATH);
2213