Remove all inclusions of <linux/config.h>
[pandora-kernel.git] / sound / oss / wavfront.c
1 /*  -*- linux-c -*-
2  *
3  * sound/wavfront.c
4  *
5  * A Linux driver for Turtle Beach WaveFront Series (Maui, Tropez, Tropez Plus)
6  *
7  * This driver supports the onboard wavetable synthesizer (an ICS2115),
8  * including patch, sample and program loading and unloading, conversion
9  * of GUS patches during loading, and full user-level access to all
10  * WaveFront commands. It tries to provide semi-intelligent patch and
11  * sample management as well.
12  *
13  * It also provides support for the ICS emulation of an MPU-401.  Full
14  * support for the ICS emulation's "virtual MIDI mode" is provided in
15  * wf_midi.c.
16  *
17  * Support is also provided for the Tropez Plus' onboard FX processor,
18  * a Yamaha YSS225. Currently, code exists to configure the YSS225,
19  * and there is an interface allowing tweaking of any of its memory
20  * addresses. However, I have been unable to decipher the logical
21  * positioning of the configuration info for various effects, so for
22  * now, you just get the YSS225 in the same state as Turtle Beach's
23  * "SETUPSND.EXE" utility leaves it.
24  *
25  * The boards' DAC/ADC (a Crystal CS4232) is supported by cs4232.[co],
26  * This chip also controls the configuration of the card: the wavefront
27  * synth is logical unit 4.
28  *
29  *
30  * Supported devices:
31  *
32  *   /dev/dsp                      - using cs4232+ad1848 modules, OSS compatible
33  *   /dev/midiNN and /dev/midiNN+1 - using wf_midi code, OSS compatible
34  *   /dev/synth00                  - raw synth interface
35  * 
36  **********************************************************************
37  *
38  * Copyright (C) by Paul Barton-Davis 1998
39  *
40  * Some portions of this file are taken from work that is
41  * copyright (C) by Hannu Savolainen 1993-1996
42  *
43  * Although the relevant code here is all new, the handling of
44  * sample/alias/multi- samples is entirely based on a driver by Matt
45  * Martin and Rutger Nijlunsing which demonstrated how to get things
46  * to work correctly. The GUS patch loading code has been almost
47  * unaltered by me, except to fit formatting and function names in the
48  * rest of the file. Many thanks to them.
49  *
50  * Appreciation and thanks to Hannu Savolainen for his early work on the Maui
51  * driver, and answering a few questions while this one was developed.
52  *
53  * Absolutely NO thanks to Turtle Beach/Voyetra and Yamaha for their
54  * complete lack of help in developing this driver, and in particular
55  * for their utter silence in response to questions about undocumented
56  * aspects of configuring a WaveFront soundcard, particularly the
57  * effects processor.
58  *
59  * $Id: wavfront.c,v 0.7 1998/09/09 15:47:36 pbd Exp $
60  *
61  * This program is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
62  * Version 2 (June 1991). See the "COPYING" file distributed with this software
63  * for more info.
64  *
65  * Changes:
66  * 11-10-2000   Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
67  *              Added some __init and __initdata to entries in yss225.c
68  */
69
70 #include <linux/module.h>
71
72 #include <linux/kernel.h>
73 #include <linux/init.h>
74 #include <linux/sched.h>
75 #include <linux/smp_lock.h>
76 #include <linux/ptrace.h>
77 #include <linux/fcntl.h>
78 #include <linux/syscalls.h>
79 #include <linux/ioport.h>
80 #include <linux/spinlock.h>
81 #include <linux/interrupt.h>
82
83 #include <linux/delay.h>
84
85 #include "sound_config.h"
86
87 #include <linux/wavefront.h>
88
89 #define _MIDI_SYNTH_C_
90 #define MIDI_SYNTH_NAME "WaveFront MIDI"
91 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
92 #include "midi_synth.h"
93
94 /* Compile-time control of the extent to which OSS is supported.
95
96    I consider /dev/sequencer to be an anachronism, but given its
97    widespread usage by various Linux MIDI software, it seems worth
98    offering support to it if it's not too painful. Instead of using
99    /dev/sequencer, I recommend:
100
101      for synth programming and patch loading: /dev/synthNN
102      for kernel-synchronized MIDI sequencing: the ALSA sequencer
103      for direct MIDI control: /dev/midiNN
104
105    I have never tried static compilation into the kernel. The #if's
106    for this are really just notes to myself about what the code is
107    for.
108 */
109
110 #define OSS_SUPPORT_SEQ            0x1  /* use of /dev/sequencer */
111 #define OSS_SUPPORT_STATIC_INSTALL 0x2  /* static compilation into kernel */
112
113 #define OSS_SUPPORT_LEVEL          0x1  /* just /dev/sequencer for now */
114
115 #if    OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
116 static int (*midi_load_patch) (int devno, int format, const char __user *addr,
117                                int offs, int count, int pmgr_flag) = NULL;
118 #endif /* OSS_SUPPORT_SEQ */
119
120 /* if WF_DEBUG not defined, no run-time debugging messages will
121    be available via the debug flag setting. Given the current
122    beta state of the driver, this will remain set until a future 
123    version.
124 */
125
126 #define WF_DEBUG 1
127
128 #ifdef WF_DEBUG
129
130 /* Thank goodness for gcc's preprocessor ... */
131
132 #define DPRINT(cond, format, args...) \
133        if ((dev.debug & (cond)) == (cond)) { \
134              printk (KERN_DEBUG LOGNAME format, ## args); \
135        }
136 #else
137 #define DPRINT(cond, format, args...)
138 #endif
139
140 #define LOGNAME "WaveFront: "
141
142 /* bitmasks for WaveFront status port value */
143
144 #define STAT_RINTR_ENABLED      0x01
145 #define STAT_CAN_READ           0x02
146 #define STAT_INTR_READ          0x04
147 #define STAT_WINTR_ENABLED      0x10
148 #define STAT_CAN_WRITE          0x20
149 #define STAT_INTR_WRITE         0x40
150
151 /*** Module-accessible parameters ***************************************/
152
153 static int wf_raw;     /* we normally check for "raw state" to firmware
154                            loading. if set, then during driver loading, the
155                            state of the board is ignored, and we reset the
156                            board and load the firmware anyway.
157                         */
158                    
159 static int fx_raw = 1; /* if this is zero, we'll leave the FX processor in
160                           whatever state it is when the driver is loaded.
161                           The default is to download the microprogram and
162                           associated coefficients to set it up for "default"
163                           operation, whatever that means.
164                        */
165
166 static int debug_default;  /* you can set this to control debugging
167                               during driver loading. it takes any combination
168                               of the WF_DEBUG_* flags defined in
169                               wavefront.h
170                            */
171
172 /* XXX this needs to be made firmware and hardware version dependent */
173
174 static char *ospath = "/etc/sound/wavefront.os"; /* where to find a processed
175                                                     version of the WaveFront OS
176                                                   */
177
178 static int wait_polls = 2000; /* This is a number of tries we poll the
179                                  status register before resorting to sleeping.
180                                  WaveFront being an ISA card each poll takes
181                                  about 1.2us. So before going to
182                                  sleep we wait up to 2.4ms in a loop.
183                              */
184
185 static int sleep_length = HZ/100; /* This says how long we're going to
186                                      sleep between polls.
187                                      10ms sounds reasonable for fast response.
188                                   */
189
190 static int sleep_tries = 50;       /* Wait for status 0.5 seconds total. */
191
192 static int reset_time = 2; /* hundreths of a second we wait after a HW reset for
193                               the expected interrupt.
194                            */
195
196 static int ramcheck_time = 20;    /* time in seconds to wait while ROM code
197                                      checks on-board RAM.
198                                   */
199
200 static int osrun_time = 10;  /* time in seconds we wait for the OS to
201                                 start running.
202                              */
203
204 module_param(wf_raw, int, 0);
205 module_param(fx_raw, int, 0);
206 module_param(debug_default, int, 0);
207 module_param(wait_polls, int, 0);
208 module_param(sleep_length, int, 0);
209 module_param(sleep_tries, int, 0);
210 module_param(ospath, charp, 0);
211 module_param(reset_time, int, 0);
212 module_param(ramcheck_time, int, 0);
213 module_param(osrun_time, int, 0);
214
215 /***************************************************************************/
216
217 /* Note: because this module doesn't export any symbols, this really isn't
218    a global variable, even if it looks like one. I was quite confused by
219    this when I started writing this as a (newer) module -- pbd.
220 */
221
222 struct wf_config {
223         int devno;            /* device number from kernel */
224         int irq;              /* "you were one, one of the few ..." */
225         int base;             /* low i/o port address */
226
227 #define mpu_data_port    base 
228 #define mpu_command_port base + 1 /* write semantics */
229 #define mpu_status_port  base + 1 /* read semantics */
230 #define data_port        base + 2 
231 #define status_port      base + 3 /* read semantics */
232 #define control_port     base + 3 /* write semantics  */
233 #define block_port       base + 4 /* 16 bit, writeonly */
234 #define last_block_port  base + 6 /* 16 bit, writeonly */
235
236         /* FX ports. These are mapped through the ICS2115 to the YS225.
237            The ICS2115 takes care of flipping the relevant pins on the
238            YS225 so that access to each of these ports does the right
239            thing. Note: these are NOT documented by Turtle Beach.
240         */
241
242 #define fx_status       base + 8 
243 #define fx_op           base + 8 
244 #define fx_lcr          base + 9 
245 #define fx_dsp_addr     base + 0xa
246 #define fx_dsp_page     base + 0xb 
247 #define fx_dsp_lsb      base + 0xc 
248 #define fx_dsp_msb      base + 0xd 
249 #define fx_mod_addr     base + 0xe
250 #define fx_mod_data     base + 0xf 
251
252         volatile int irq_ok;               /* set by interrupt handler */
253         volatile int irq_cnt;              /* ditto */
254         int opened;                        /* flag, holds open(2) mode */
255         char debug;                        /* debugging flags */
256         int freemem;                       /* installed RAM, in bytes */ 
257
258         int synth_dev;                     /* devno for "raw" synth */
259         int mididev;                       /* devno for internal MIDI */
260         int ext_mididev;                   /* devno for external MIDI */ 
261         int fx_mididev;                    /* devno for FX MIDI interface */
262 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
263         int oss_dev;                      /* devno for OSS sequencer synth */
264 #endif /* OSS_SUPPORT_SEQ */
265
266         char fw_version[2];                /* major = [0], minor = [1] */
267         char hw_version[2];                /* major = [0], minor = [1] */
268         char israw;                        /* needs Motorola microcode */
269         char has_fx;                       /* has FX processor (Tropez+) */
270         char prog_status[WF_MAX_PROGRAM];  /* WF_SLOT_* */
271         char patch_status[WF_MAX_PATCH];   /* WF_SLOT_* */
272         char sample_status[WF_MAX_SAMPLE]; /* WF_ST_* | WF_SLOT_* */
273         int samples_used;                  /* how many */
274         char interrupts_on;                /* h/w MPU interrupts enabled ? */
275         char rom_samples_rdonly;           /* can we write on ROM samples */
276         wait_queue_head_t interrupt_sleeper; 
277 } dev;
278
279 static DEFINE_SPINLOCK(lock);
280 static int  detect_wffx(void);
281 static int  wffx_ioctl (wavefront_fx_info *);
282 static int  wffx_init (void);
283
284 static int wavefront_delete_sample (int sampnum);
285 static int wavefront_find_free_sample (void);
286
287 /* From wf_midi.c */
288
289 extern int  virtual_midi_enable  (void);
290 extern int  virtual_midi_disable (void);
291 extern int  detect_wf_mpu (int, int);
292 extern int  install_wf_mpu (void);
293 extern int  uninstall_wf_mpu (void);
294
295 typedef struct {
296         int cmd;
297         char *action;
298         unsigned int read_cnt;
299         unsigned int write_cnt;
300         int need_ack;
301 } wavefront_command;
302
303 static struct {
304         int errno;
305         const char *errstr;
306 } wavefront_errors[] = {
307         { 0x01, "Bad sample number" },
308         { 0x02, "Out of sample memory" },
309         { 0x03, "Bad patch number" },
310         { 0x04, "Error in number of voices" },
311         { 0x06, "Sample load already in progress" },
312         { 0x0B, "No sample load request pending" },
313         { 0x0E, "Bad MIDI channel number" },
314         { 0x10, "Download Record Error" },
315         { 0x80, "Success" },
316         { 0 }
317 };
318
319 #define NEEDS_ACK 1
320
321 static wavefront_command wavefront_commands[] = {
322         { WFC_SET_SYNTHVOL, "set synthesizer volume", 0, 1, NEEDS_ACK },
323         { WFC_GET_SYNTHVOL, "get synthesizer volume", 1, 0, 0},
324         { WFC_SET_NVOICES, "set number of voices", 0, 1, NEEDS_ACK },
325         { WFC_GET_NVOICES, "get number of voices", 1, 0, 0 },
326         { WFC_SET_TUNING, "set synthesizer tuning", 0, 2, NEEDS_ACK },
327         { WFC_GET_TUNING, "get synthesizer tuning", 2, 0, 0 },
328         { WFC_DISABLE_CHANNEL, "disable synth channel", 0, 1, NEEDS_ACK },
329         { WFC_ENABLE_CHANNEL, "enable synth channel", 0, 1, NEEDS_ACK },
330         { WFC_GET_CHANNEL_STATUS, "get synth channel status", 3, 0, 0 },
331         { WFC_MISYNTH_OFF, "disable midi-in to synth", 0, 0, NEEDS_ACK },
332         { WFC_MISYNTH_ON, "enable midi-in to synth", 0, 0, NEEDS_ACK },
333         { WFC_VMIDI_ON, "enable virtual midi mode", 0, 0, NEEDS_ACK },
334         { WFC_VMIDI_OFF, "disable virtual midi mode", 0, 0, NEEDS_ACK },
335         { WFC_MIDI_STATUS, "report midi status", 1, 0, 0 },
336         { WFC_FIRMWARE_VERSION, "report firmware version", 2, 0, 0 },
337         { WFC_HARDWARE_VERSION, "report hardware version", 2, 0, 0 },
338         { WFC_GET_NSAMPLES, "report number of samples", 2, 0, 0 },
339         { WFC_INSTOUT_LEVELS, "report instantaneous output levels", 7, 0, 0 },
340         { WFC_PEAKOUT_LEVELS, "report peak output levels", 7, 0, 0 },
341         { WFC_DOWNLOAD_SAMPLE, "download sample",
342           0, WF_SAMPLE_BYTES, NEEDS_ACK },
343         { WFC_DOWNLOAD_BLOCK, "download block", 0, 0, NEEDS_ACK},
344         { WFC_DOWNLOAD_SAMPLE_HEADER, "download sample header",
345           0, WF_SAMPLE_HDR_BYTES, NEEDS_ACK },
346         { WFC_UPLOAD_SAMPLE_HEADER, "upload sample header", 13, 2, 0 },
347
348         /* This command requires a variable number of bytes to be written.
349            There is a hack in wavefront_cmd() to support this. The actual
350            count is passed in as the read buffer ptr, cast appropriately.
351            Ugh.
352         */
353
354         { WFC_DOWNLOAD_MULTISAMPLE, "download multisample", 0, 0, NEEDS_ACK },
355
356         /* This one is a hack as well. We just read the first byte of the
357            response, don't fetch an ACK, and leave the rest to the 
358            calling function. Ugly, ugly, ugly.
359         */
360
361         { WFC_UPLOAD_MULTISAMPLE, "upload multisample", 2, 1, 0 },
362         { WFC_DOWNLOAD_SAMPLE_ALIAS, "download sample alias",
363           0, WF_ALIAS_BYTES, NEEDS_ACK },
364         { WFC_UPLOAD_SAMPLE_ALIAS, "upload sample alias", WF_ALIAS_BYTES, 2, 0},
365         { WFC_DELETE_SAMPLE, "delete sample", 0, 2, NEEDS_ACK },
366         { WFC_IDENTIFY_SAMPLE_TYPE, "identify sample type", 5, 2, 0 },
367         { WFC_UPLOAD_SAMPLE_PARAMS, "upload sample parameters" },
368         { WFC_REPORT_FREE_MEMORY, "report free memory", 4, 0, 0 },
369         { WFC_DOWNLOAD_PATCH, "download patch", 0, 134, NEEDS_ACK },
370         { WFC_UPLOAD_PATCH, "upload patch", 132, 2, 0 },
371         { WFC_DOWNLOAD_PROGRAM, "download program", 0, 33, NEEDS_ACK },
372         { WFC_UPLOAD_PROGRAM, "upload program", 32, 1, 0 },
373         { WFC_DOWNLOAD_EDRUM_PROGRAM, "download enhanced drum program", 0, 9,
374           NEEDS_ACK},
375         { WFC_UPLOAD_EDRUM_PROGRAM, "upload enhanced drum program", 8, 1, 0},
376         { WFC_SET_EDRUM_CHANNEL, "set enhanced drum program channel",
377           0, 1, NEEDS_ACK },
378         { WFC_DISABLE_DRUM_PROGRAM, "disable drum program", 0, 1, NEEDS_ACK },
379         { WFC_REPORT_CHANNEL_PROGRAMS, "report channel program numbers",
380           32, 0, 0 },
381         { WFC_NOOP, "the no-op command", 0, 0, NEEDS_ACK },
382         { 0x00 }
383 };
384
385 static const char *
386 wavefront_errorstr (int errnum)
387
388 {
389         int i;
390
391         for (i = 0; wavefront_errors[i].errstr; i++) {
392                 if (wavefront_errors[i].errno == errnum) {
393                         return wavefront_errors[i].errstr;
394                 }
395         }
396
397         return "Unknown WaveFront error";
398 }
399
400 static wavefront_command *
401 wavefront_get_command (int cmd) 
402
403 {
404         int i;
405
406         for (i = 0; wavefront_commands[i].cmd != 0; i++) {
407                 if (cmd == wavefront_commands[i].cmd) {
408                         return &wavefront_commands[i];
409                 }
410         }
411
412         return (wavefront_command *) 0;
413 }
414
415 static inline int
416 wavefront_status (void) 
417
418 {
419         return inb (dev.status_port);
420 }
421
422 static int
423 wavefront_wait (int mask)
424
425 {
426         int i;
427
428         for (i = 0; i < wait_polls; i++)
429                 if (wavefront_status() & mask)
430                         return 1;
431
432         for (i = 0; i < sleep_tries; i++) {
433
434                 if (wavefront_status() & mask) {
435                         set_current_state(TASK_RUNNING);
436                         return 1;
437                 }
438
439                 set_current_state(TASK_INTERRUPTIBLE);
440                 schedule_timeout(sleep_length);
441                 if (signal_pending(current))
442                         break;
443         }
444
445         set_current_state(TASK_RUNNING);
446         return 0;
447 }
448
449 static int
450 wavefront_read (void)
451
452 {
453         if (wavefront_wait (STAT_CAN_READ))
454                 return inb (dev.data_port);
455
456         DPRINT (WF_DEBUG_DATA, "read timeout.\n");
457
458         return -1;
459 }
460
461 static int
462 wavefront_write (unsigned char data)
463
464 {
465         if (wavefront_wait (STAT_CAN_WRITE)) {
466                 outb (data, dev.data_port);
467                 return 0;
468         }
469
470         DPRINT (WF_DEBUG_DATA, "write timeout.\n");
471
472         return -1;
473 }
474
475 static int
476 wavefront_cmd (int cmd, unsigned char *rbuf, unsigned char *wbuf)
477
478 {
479         int ack;
480         int i;
481         int c;
482         wavefront_command *wfcmd;
483
484         if ((wfcmd = wavefront_get_command (cmd)) == (wavefront_command *) 0) {
485                 printk (KERN_WARNING LOGNAME "command 0x%x not supported.\n",
486                         cmd);
487                 return 1;
488         }
489
490         /* Hack to handle the one variable-size write command. See
491            wavefront_send_multisample() for the other half of this
492            gross and ugly strategy.
493         */
494
495         if (cmd == WFC_DOWNLOAD_MULTISAMPLE) {
496                 wfcmd->write_cnt = (unsigned int) rbuf;
497                 rbuf = NULL;
498         }
499
500         DPRINT (WF_DEBUG_CMD, "0x%x [%s] (%d,%d,%d)\n",
501                                cmd, wfcmd->action, wfcmd->read_cnt,
502                                wfcmd->write_cnt, wfcmd->need_ack);
503     
504         if (wavefront_write (cmd)) { 
505                 DPRINT ((WF_DEBUG_IO|WF_DEBUG_CMD), "cannot request "
506                                                      "0x%x [%s].\n",
507                                                      cmd, wfcmd->action);
508                 return 1;
509         } 
510
511         if (wfcmd->write_cnt > 0) {
512                 DPRINT (WF_DEBUG_DATA, "writing %d bytes "
513                                         "for 0x%x\n",
514                                         wfcmd->write_cnt, cmd);
515
516                 for (i = 0; i < wfcmd->write_cnt; i++) {
517                         if (wavefront_write (wbuf[i])) {
518                                 DPRINT (WF_DEBUG_IO, "bad write for byte "
519                                                       "%d of 0x%x [%s].\n",
520                                                       i, cmd, wfcmd->action);
521                                 return 1;
522                         }
523
524                         DPRINT (WF_DEBUG_DATA, "write[%d] = 0x%x\n",
525                                                 i, wbuf[i]);
526                 }
527         }
528
529         if (wfcmd->read_cnt > 0) {
530                 DPRINT (WF_DEBUG_DATA, "reading %d ints "
531                                         "for 0x%x\n",
532                                         wfcmd->read_cnt, cmd);
533
534                 for (i = 0; i < wfcmd->read_cnt; i++) {
535
536                         if ((c = wavefront_read()) == -1) {
537                                 DPRINT (WF_DEBUG_IO, "bad read for byte "
538                                                       "%d of 0x%x [%s].\n",
539                                                       i, cmd, wfcmd->action);
540                                 return 1;
541                         }
542
543                         /* Now handle errors. Lots of special cases here */
544             
545                         if (c == 0xff) { 
546                                 if ((c = wavefront_read ()) == -1) {
547                                         DPRINT (WF_DEBUG_IO, "bad read for "
548                                                               "error byte at "
549                                                               "read byte %d "
550                                                               "of 0x%x [%s].\n",
551                                                               i, cmd,
552                                                               wfcmd->action);
553                                         return 1;
554                                 }
555
556                                 /* Can you believe this madness ? */
557
558                                 if (c == 1 &&
559                                     wfcmd->cmd == WFC_IDENTIFY_SAMPLE_TYPE) {
560                                         rbuf[0] = WF_ST_EMPTY;
561                                         return (0);
562
563                                 } else if (c == 3 &&
564                                            wfcmd->cmd == WFC_UPLOAD_PATCH) {
565
566                                         return 3;
567
568                                 } else if (c == 1 &&
569                                            wfcmd->cmd == WFC_UPLOAD_PROGRAM) {
570
571                                         return 1;
572
573                                 } else {
574
575                                         DPRINT (WF_DEBUG_IO, "error %d (%s) "
576                                                               "during "
577                                                               "read for byte "
578                                                               "%d of 0x%x "
579                                                               "[%s].\n",
580                                                               c,
581                                                               wavefront_errorstr (c),
582                                                               i, cmd,
583                                                               wfcmd->action);
584                                         return 1;
585
586                                 }
587                 
588                 } else {
589                                 rbuf[i] = c;
590                         }
591                         
592                         DPRINT (WF_DEBUG_DATA, "read[%d] = 0x%x\n",i, rbuf[i]);
593                 }
594         }
595         
596         if ((wfcmd->read_cnt == 0 && wfcmd->write_cnt == 0) || wfcmd->need_ack) {
597
598                 DPRINT (WF_DEBUG_CMD, "reading ACK for 0x%x\n", cmd);
599
600                 /* Some commands need an ACK, but return zero instead
601                    of the standard value.
602                 */
603             
604                 if ((ack = wavefront_read()) == 0) {
605                         ack = WF_ACK;
606                 }
607         
608                 if (ack != WF_ACK) {
609                         if (ack == -1) {
610                                 DPRINT (WF_DEBUG_IO, "cannot read ack for "
611                                                       "0x%x [%s].\n",
612                                                       cmd, wfcmd->action);
613                                 return 1;
614                 
615                         } else {
616                                 int err = -1; /* something unknown */
617
618                                 if (ack == 0xff) { /* explicit error */
619                     
620                                         if ((err = wavefront_read ()) == -1) {
621                                                 DPRINT (WF_DEBUG_DATA,
622                                                         "cannot read err "
623                                                         "for 0x%x [%s].\n",
624                                                         cmd, wfcmd->action);
625                                         }
626                                 }
627                                 
628                                 DPRINT (WF_DEBUG_IO, "0x%x [%s] "
629                                         "failed (0x%x, 0x%x, %s)\n",
630                                         cmd, wfcmd->action, ack, err,
631                                         wavefront_errorstr (err));
632                                 
633                                 return -err;
634                         }
635                 }
636                 
637                 DPRINT (WF_DEBUG_DATA, "ack received "
638                                         "for 0x%x [%s]\n",
639                                         cmd, wfcmd->action);
640         } else {
641
642                 DPRINT (WF_DEBUG_CMD, "0x%x [%s] does not need "
643                                        "ACK (%d,%d,%d)\n",
644                                        cmd, wfcmd->action, wfcmd->read_cnt,
645                                        wfcmd->write_cnt, wfcmd->need_ack);
646         }
647
648         return 0;
649         
650 }
651 \f
652 /***********************************************************************
653 WaveFront: data munging   
654
655 Things here are weird. All data written to the board cannot 
656 have its most significant bit set. Any data item with values 
657 potentially > 0x7F (127) must be split across multiple bytes.
658
659 Sometimes, we need to munge numeric values that are represented on
660 the x86 side as 8-32 bit values. Sometimes, we need to munge data
661 that is represented on the x86 side as an array of bytes. The most
662 efficient approach to handling both cases seems to be to use 2
663 different functions for munging and 2 for de-munging. This avoids
664 weird casting and worrying about bit-level offsets.
665
666 **********************************************************************/
667
668 static 
669 unsigned char *
670 munge_int32 (unsigned int src,
671              unsigned char *dst,
672              unsigned int dst_size)
673 {
674         int i;
675
676         for (i = 0;i < dst_size; i++) {
677                 *dst = src & 0x7F;  /* Mask high bit of LSB */
678                 src = src >> 7;     /* Rotate Right 7 bits  */
679                                     /* Note: we leave the upper bits in place */ 
680
681                 dst++;
682         };
683         return dst;
684 };
685
686 static int 
687 demunge_int32 (unsigned char* src, int src_size)
688
689 {
690         int i;
691         int outval = 0;
692         
693         for (i = src_size - 1; i >= 0; i--) {
694                 outval=(outval<<7)+src[i];
695         }
696
697         return outval;
698 };
699
700 static 
701 unsigned char *
702 munge_buf (unsigned char *src, unsigned char *dst, unsigned int dst_size)
703
704 {
705         int i;
706         unsigned int last = dst_size / 2;
707
708         for (i = 0; i < last; i++) {
709                 *dst++ = src[i] & 0x7f;
710                 *dst++ = src[i] >> 7;
711         }
712         return dst;
713 }
714
715 static 
716 unsigned char *
717 demunge_buf (unsigned char *src, unsigned char *dst, unsigned int src_bytes)
718
719 {
720         int i;
721         unsigned char *end = src + src_bytes;
722     
723         end = src + src_bytes;
724
725         /* NOTE: src and dst *CAN* point to the same address */
726
727         for (i = 0; src != end; i++) {
728                 dst[i] = *src++;
729                 dst[i] |= (*src++)<<7;
730         }
731
732         return dst;
733 }
734 \f
735 /***********************************************************************
736 WaveFront: sample, patch and program management.
737 ***********************************************************************/
738
739 static int
740 wavefront_delete_sample (int sample_num)
741
742 {
743         unsigned char wbuf[2];
744         int x;
745
746         wbuf[0] = sample_num & 0x7f;
747         wbuf[1] = sample_num >> 7;
748
749         if ((x = wavefront_cmd (WFC_DELETE_SAMPLE, NULL, wbuf)) == 0) {
750                 dev.sample_status[sample_num] = WF_ST_EMPTY;
751         }
752
753         return x;
754 }
755
756 static int
757 wavefront_get_sample_status (int assume_rom)
758
759 {
760         int i;
761         unsigned char rbuf[32], wbuf[32];
762         unsigned int    sc_real, sc_alias, sc_multi;
763
764         /* check sample status */
765     
766         if (wavefront_cmd (WFC_GET_NSAMPLES, rbuf, wbuf)) {
767                 printk (KERN_WARNING LOGNAME "cannot request sample count.\n");
768                 return -1;
769         } 
770     
771         sc_real = sc_alias = sc_multi = dev.samples_used = 0;
772     
773         for (i = 0; i < WF_MAX_SAMPLE; i++) {
774         
775                 wbuf[0] = i & 0x7f;
776                 wbuf[1] = i >> 7;
777
778                 if (wavefront_cmd (WFC_IDENTIFY_SAMPLE_TYPE, rbuf, wbuf)) {
779                         printk (KERN_WARNING LOGNAME
780                                 "cannot identify sample "
781                                 "type of slot %d\n", i);
782                         dev.sample_status[i] = WF_ST_EMPTY;
783                         continue;
784                 }
785
786                 dev.sample_status[i] = (WF_SLOT_FILLED|rbuf[0]);
787
788                 if (assume_rom) {
789                         dev.sample_status[i] |= WF_SLOT_ROM;
790                 }
791
792                 switch (rbuf[0] & WF_ST_MASK) {
793                 case WF_ST_SAMPLE:
794                         sc_real++;
795                         break;
796                 case WF_ST_MULTISAMPLE:
797                         sc_multi++;
798                         break;
799                 case WF_ST_ALIAS:
800                         sc_alias++;
801                         break;
802                 case WF_ST_EMPTY:
803                         break;
804
805                 default:
806                         printk (KERN_WARNING LOGNAME "unknown sample type for "
807                                 "slot %d (0x%x)\n", 
808                                 i, rbuf[0]);
809                 }
810
811                 if (rbuf[0] != WF_ST_EMPTY) {
812                         dev.samples_used++;
813                 } 
814         }
815
816         printk (KERN_INFO LOGNAME
817                 "%d samples used (%d real, %d aliases, %d multi), "
818                 "%d empty\n", dev.samples_used, sc_real, sc_alias, sc_multi,
819                 WF_MAX_SAMPLE - dev.samples_used);
820
821
822         return (0);
823
824 }
825
826 static int
827 wavefront_get_patch_status (void)
828
829 {
830         unsigned char patchbuf[WF_PATCH_BYTES];
831         unsigned char patchnum[2];
832         wavefront_patch *p;
833         int i, x, cnt, cnt2;
834
835         for (i = 0; i < WF_MAX_PATCH; i++) {
836                 patchnum[0] = i & 0x7f;
837                 patchnum[1] = i >> 7;
838
839                 if ((x = wavefront_cmd (WFC_UPLOAD_PATCH, patchbuf,
840                                         patchnum)) == 0) {
841
842                         dev.patch_status[i] |= WF_SLOT_FILLED;
843                         p = (wavefront_patch *) patchbuf;
844                         dev.sample_status
845                                 [p->sample_number|(p->sample_msb<<7)] |=
846                                 WF_SLOT_USED;
847             
848                 } else if (x == 3) { /* Bad patch number */
849                         dev.patch_status[i] = 0;
850                 } else {
851                         printk (KERN_ERR LOGNAME "upload patch "
852                                 "error 0x%x\n", x);
853                         dev.patch_status[i] = 0;
854                         return 1;
855                 }
856         }
857
858         /* program status has already filled in slot_used bits */
859
860         for (i = 0, cnt = 0, cnt2 = 0; i < WF_MAX_PATCH; i++) {
861                 if (dev.patch_status[i] & WF_SLOT_FILLED) {
862                         cnt++;
863                 }
864                 if (dev.patch_status[i] & WF_SLOT_USED) {
865                         cnt2++;
866                 }
867         
868         }
869         printk (KERN_INFO LOGNAME
870                 "%d patch slots filled, %d in use\n", cnt, cnt2);
871
872         return (0);
873 }
874
875 static int
876 wavefront_get_program_status (void)
877
878 {
879         unsigned char progbuf[WF_PROGRAM_BYTES];
880         wavefront_program prog;
881         unsigned char prognum;
882         int i, x, l, cnt;
883
884         for (i = 0; i < WF_MAX_PROGRAM; i++) {
885                 prognum = i;
886
887                 if ((x = wavefront_cmd (WFC_UPLOAD_PROGRAM, progbuf,
888                                         &prognum)) == 0) {
889
890                         dev.prog_status[i] |= WF_SLOT_USED;
891
892                         demunge_buf (progbuf, (unsigned char *) &prog,
893                                      WF_PROGRAM_BYTES);
894
895                         for (l = 0; l < WF_NUM_LAYERS; l++) {
896                                 if (prog.layer[l].mute) {
897                                         dev.patch_status
898                                                 [prog.layer[l].patch_number] |=
899                                                 WF_SLOT_USED;
900                                 }
901                         }
902                 } else if (x == 1) { /* Bad program number */
903                         dev.prog_status[i] = 0;
904                 } else {
905                         printk (KERN_ERR LOGNAME "upload program "
906                                 "error 0x%x\n", x);
907                         dev.prog_status[i] = 0;
908                 }
909         }
910
911         for (i = 0, cnt = 0; i < WF_MAX_PROGRAM; i++) {
912                 if (dev.prog_status[i]) {
913                         cnt++;
914                 }
915         }
916
917         printk (KERN_INFO LOGNAME "%d programs slots in use\n", cnt);
918
919         return (0);
920 }
921
922 static int
923 wavefront_send_patch (wavefront_patch_info *header)
924
925 {
926         unsigned char buf[WF_PATCH_BYTES+2];
927         unsigned char *bptr;
928
929         DPRINT (WF_DEBUG_LOAD_PATCH, "downloading patch %d\n",
930                                       header->number);
931
932         dev.patch_status[header->number] |= WF_SLOT_FILLED;
933
934         bptr = buf;
935         bptr = munge_int32 (header->number, buf, 2);
936         munge_buf ((unsigned char *)&header->hdr.p, bptr, WF_PATCH_BYTES);
937     
938         if (wavefront_cmd (WFC_DOWNLOAD_PATCH, NULL, buf)) {
939                 printk (KERN_ERR LOGNAME "download patch failed\n");
940                 return -(EIO);
941         }
942
943         return (0);
944 }
945
946 static int
947 wavefront_send_program (wavefront_patch_info *header)
948
949 {
950         unsigned char buf[WF_PROGRAM_BYTES+1];
951         int i;
952
953         DPRINT (WF_DEBUG_LOAD_PATCH, "downloading program %d\n",
954                 header->number);
955
956         dev.prog_status[header->number] = WF_SLOT_USED;
957
958         /* XXX need to zero existing SLOT_USED bit for program_status[i]
959            where `i' is the program that's being (potentially) overwritten.
960         */
961     
962         for (i = 0; i < WF_NUM_LAYERS; i++) {
963                 if (header->hdr.pr.layer[i].mute) {
964                         dev.patch_status[header->hdr.pr.layer[i].patch_number] |=
965                                 WF_SLOT_USED;
966
967                         /* XXX need to mark SLOT_USED for sample used by
968                            patch_number, but this means we have to load it. Ick.
969                         */
970                 }
971         }
972
973         buf[0] = header->number;
974         munge_buf ((unsigned char *)&header->hdr.pr, &buf[1], WF_PROGRAM_BYTES);
975     
976         if (wavefront_cmd (WFC_DOWNLOAD_PROGRAM, NULL, buf)) {
977                 printk (KERN_WARNING LOGNAME "download patch failed\n");        
978                 return -(EIO);
979         }
980
981         return (0);
982 }
983
984 static int
985 wavefront_freemem (void)
986
987 {
988         char rbuf[8];
989
990         if (wavefront_cmd (WFC_REPORT_FREE_MEMORY, rbuf, NULL)) {
991                 printk (KERN_WARNING LOGNAME "can't get memory stats.\n");
992                 return -1;
993         } else {
994                 return demunge_int32 (rbuf, 4);
995         }
996 }
997
998 static int
999 wavefront_send_sample (wavefront_patch_info *header,
1000                        UINT16 __user *dataptr,
1001                        int data_is_unsigned)
1002
1003 {
1004         /* samples are downloaded via a 16-bit wide i/o port
1005            (you could think of it as 2 adjacent 8-bit wide ports
1006            but its less efficient that way). therefore, all
1007            the blocksizes and so forth listed in the documentation,
1008            and used conventionally to refer to sample sizes,
1009            which are given in 8-bit units (bytes), need to be
1010            divided by 2.
1011         */
1012
1013         UINT16 sample_short;
1014         UINT32 length;
1015         UINT16 __user *data_end = NULL;
1016         unsigned int i;
1017         const int max_blksize = 4096/2;
1018         unsigned int written;
1019         unsigned int blocksize;
1020         int dma_ack;
1021         int blocknum;
1022         unsigned char sample_hdr[WF_SAMPLE_HDR_BYTES];
1023         unsigned char *shptr;
1024         int skip = 0;
1025         int initial_skip = 0;
1026
1027         DPRINT (WF_DEBUG_LOAD_PATCH, "sample %sdownload for slot %d, "
1028                                       "type %d, %d bytes from %p\n",
1029                                       header->size ? "" : "header ", 
1030                                       header->number, header->subkey,
1031                                       header->size,
1032                                       header->dataptr);
1033
1034         if (header->number == WAVEFRONT_FIND_FREE_SAMPLE_SLOT) {
1035                 int x;
1036
1037                 if ((x = wavefront_find_free_sample ()) < 0) {
1038                         return -ENOMEM;
1039                 }
1040                 printk (KERN_DEBUG LOGNAME "unspecified sample => %d\n", x);
1041                 header->number = x;
1042         }
1043
1044         if (header->size) {
1045
1046                 /* XXX it's a debatable point whether or not RDONLY semantics
1047                    on the ROM samples should cover just the sample data or
1048                    the sample header. For now, it only covers the sample data,
1049                    so anyone is free at all times to rewrite sample headers.
1050
1051                    My reason for this is that we have the sample headers
1052                    available in the WFB file for General MIDI, and so these
1053                    can always be reset if needed. The sample data, however,
1054                    cannot be recovered without a complete reset and firmware
1055                    reload of the ICS2115, which is a very expensive operation.
1056
1057                    So, doing things this way allows us to honor the notion of
1058                    "RESETSAMPLES" reasonably cheaply. Note however, that this
1059                    is done purely at user level: there is no WFB parser in
1060                    this driver, and so a complete reset (back to General MIDI,
1061                    or theoretically some other configuration) is the
1062                    responsibility of the user level library. 
1063
1064                    To try to do this in the kernel would be a little
1065                    crazy: we'd need 158K of kernel space just to hold
1066                    a copy of the patch/program/sample header data.
1067                 */
1068
1069                 if (dev.rom_samples_rdonly) {
1070                         if (dev.sample_status[header->number] & WF_SLOT_ROM) {
1071                                 printk (KERN_ERR LOGNAME "sample slot %d "
1072                                         "write protected\n",
1073                                         header->number);
1074                                 return -EACCES;
1075                         }
1076                 }
1077
1078                 wavefront_delete_sample (header->number);
1079         }
1080
1081         if (header->size) {
1082                 dev.freemem = wavefront_freemem ();
1083
1084                 if (dev.freemem < header->size) {
1085                         printk (KERN_ERR LOGNAME
1086                                 "insufficient memory to "
1087                                 "load %d byte sample.\n",
1088                                 header->size);
1089                         return -ENOMEM;
1090                 }
1091         
1092         }
1093
1094         skip = WF_GET_CHANNEL(&header->hdr.s);
1095
1096         if (skip > 0 && header->hdr.s.SampleResolution != LINEAR_16BIT) {
1097                 printk (KERN_ERR LOGNAME "channel selection only "
1098                         "possible on 16-bit samples");
1099                 return -(EINVAL);
1100         }
1101
1102         switch (skip) {
1103         case 0:
1104                 initial_skip = 0;
1105                 skip = 1;
1106                 break;
1107         case 1:
1108                 initial_skip = 0;
1109                 skip = 2;
1110                 break;
1111         case 2:
1112                 initial_skip = 1;
1113                 skip = 2;
1114                 break;
1115         case 3:
1116                 initial_skip = 2;
1117                 skip = 3;
1118                 break;
1119         case 4:
1120                 initial_skip = 3;
1121                 skip = 4;
1122                 break;
1123         case 5:
1124                 initial_skip = 4;
1125                 skip = 5;
1126                 break;
1127         case 6:
1128                 initial_skip = 5;
1129                 skip = 6;
1130                 break;
1131         }
1132
1133         DPRINT (WF_DEBUG_LOAD_PATCH, "channel selection: %d => "
1134                                       "initial skip = %d, skip = %d\n",
1135                                       WF_GET_CHANNEL (&header->hdr.s),
1136                                       initial_skip, skip);
1137     
1138         /* Be safe, and zero the "Unused" bits ... */
1139
1140         WF_SET_CHANNEL(&header->hdr.s, 0);
1141
1142         /* adjust size for 16 bit samples by dividing by two.  We always
1143            send 16 bits per write, even for 8 bit samples, so the length
1144            is always half the size of the sample data in bytes.
1145         */
1146
1147         length = header->size / 2;
1148
1149         /* the data we're sent has not been munged, and in fact, the
1150            header we have to send isn't just a munged copy either.
1151            so, build the sample header right here.
1152         */
1153
1154         shptr = &sample_hdr[0];
1155
1156         shptr = munge_int32 (header->number, shptr, 2);
1157
1158         if (header->size) {
1159                 shptr = munge_int32 (length, shptr, 4);
1160         }
1161
1162         /* Yes, a 4 byte result doesn't contain all of the offset bits,
1163            but the offset only uses 24 bits.
1164         */
1165
1166         shptr = munge_int32 (*((UINT32 *) &header->hdr.s.sampleStartOffset),
1167                              shptr, 4);
1168         shptr = munge_int32 (*((UINT32 *) &header->hdr.s.loopStartOffset),
1169                              shptr, 4);
1170         shptr = munge_int32 (*((UINT32 *) &header->hdr.s.loopEndOffset),
1171                              shptr, 4);
1172         shptr = munge_int32 (*((UINT32 *) &header->hdr.s.sampleEndOffset),
1173                              shptr, 4);
1174         
1175         /* This one is truly weird. What kind of weirdo decided that in
1176            a system dominated by 16 and 32 bit integers, they would use
1177            a just 12 bits ?
1178         */
1179         
1180         shptr = munge_int32 (header->hdr.s.FrequencyBias, shptr, 3);
1181         
1182         /* Why is this nybblified, when the MSB is *always* zero ? 
1183            Anyway, we can't take address of bitfield, so make a
1184            good-faith guess at where it starts.
1185         */
1186         
1187         shptr = munge_int32 (*(&header->hdr.s.FrequencyBias+1),
1188                              shptr, 2);
1189
1190         if (wavefront_cmd (header->size ?
1191                            WFC_DOWNLOAD_SAMPLE : WFC_DOWNLOAD_SAMPLE_HEADER,
1192                            NULL, sample_hdr)) {
1193                 printk (KERN_WARNING LOGNAME "sample %sdownload refused.\n",
1194                         header->size ? "" : "header ");
1195                 return -(EIO);
1196         }
1197
1198         if (header->size == 0) {
1199                 goto sent; /* Sorry. Just had to have one somewhere */
1200         }
1201     
1202         data_end = dataptr + length;
1203
1204         /* Do any initial skip over an unused channel's data */
1205
1206         dataptr += initial_skip;
1207     
1208         for (written = 0, blocknum = 0;
1209              written < length; written += max_blksize, blocknum++) {
1210         
1211                 if ((length - written) > max_blksize) {
1212                         blocksize = max_blksize;
1213                 } else {
1214                         /* round to nearest 16-byte value */
1215                         blocksize = ((length-written+7)&~0x7);
1216                 }
1217
1218                 if (wavefront_cmd (WFC_DOWNLOAD_BLOCK, NULL, NULL)) {
1219                         printk (KERN_WARNING LOGNAME "download block "
1220                                 "request refused.\n");
1221                         return -(EIO);
1222                 }
1223
1224                 for (i = 0; i < blocksize; i++) {
1225
1226                         if (dataptr < data_end) {
1227                 
1228                                 __get_user (sample_short, dataptr);
1229                                 dataptr += skip;
1230                 
1231                                 if (data_is_unsigned) { /* GUS ? */
1232
1233                                         if (WF_SAMPLE_IS_8BIT(&header->hdr.s)) {
1234                         
1235                                                 /* 8 bit sample
1236                                                  resolution, sign
1237                                                  extend both bytes.
1238                                                 */
1239                         
1240                                                 ((unsigned char*)
1241                                                  &sample_short)[0] += 0x7f;
1242                                                 ((unsigned char*)
1243                                                  &sample_short)[1] += 0x7f;
1244                         
1245                                         } else {
1246                         
1247                                                 /* 16 bit sample
1248                                                  resolution, sign
1249                                                  extend the MSB.
1250                                                 */
1251                         
1252                                                 sample_short += 0x7fff;
1253                                         }
1254                                 }
1255
1256                         } else {
1257
1258                                 /* In padding section of final block:
1259
1260                                    Don't fetch unsupplied data from
1261                                    user space, just continue with
1262                                    whatever the final value was.
1263                                 */
1264                         }
1265             
1266                         if (i < blocksize - 1) {
1267                                 outw (sample_short, dev.block_port);
1268                         } else {
1269                                 outw (sample_short, dev.last_block_port);
1270                         }
1271                 }
1272
1273                 /* Get "DMA page acknowledge", even though its really
1274                    nothing to do with DMA at all.
1275                 */
1276         
1277                 if ((dma_ack = wavefront_read ()) != WF_DMA_ACK) {
1278                         if (dma_ack == -1) {
1279                                 printk (KERN_ERR LOGNAME "upload sample "
1280                                         "DMA ack timeout\n");
1281                                 return -(EIO);
1282                         } else {
1283                                 printk (KERN_ERR LOGNAME "upload sample "
1284                                         "DMA ack error 0x%x\n",
1285                                         dma_ack);
1286                                 return -(EIO);
1287                         }
1288                 }
1289         }
1290
1291         dev.sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_SAMPLE);
1292
1293         /* Note, label is here because sending the sample header shouldn't
1294            alter the sample_status info at all.
1295         */
1296
1297  sent:
1298         return (0);
1299 }
1300
1301 static int
1302 wavefront_send_alias (wavefront_patch_info *header)
1303
1304 {
1305         unsigned char alias_hdr[WF_ALIAS_BYTES];
1306
1307         DPRINT (WF_DEBUG_LOAD_PATCH, "download alias, %d is "
1308                                       "alias for %d\n",
1309                                       header->number,
1310                                       header->hdr.a.OriginalSample);
1311     
1312         munge_int32 (header->number, &alias_hdr[0], 2);
1313         munge_int32 (header->hdr.a.OriginalSample, &alias_hdr[2], 2);
1314         munge_int32 (*((unsigned int *)&header->hdr.a.sampleStartOffset),
1315                      &alias_hdr[4], 4);
1316         munge_int32 (*((unsigned int *)&header->hdr.a.loopStartOffset),
1317                      &alias_hdr[8], 4);
1318         munge_int32 (*((unsigned int *)&header->hdr.a.loopEndOffset),
1319                      &alias_hdr[12], 4);
1320         munge_int32 (*((unsigned int *)&header->hdr.a.sampleEndOffset),
1321                      &alias_hdr[16], 4);
1322         munge_int32 (header->hdr.a.FrequencyBias, &alias_hdr[20], 3);
1323         munge_int32 (*(&header->hdr.a.FrequencyBias+1), &alias_hdr[23], 2);
1324
1325         if (wavefront_cmd (WFC_DOWNLOAD_SAMPLE_ALIAS, NULL, alias_hdr)) {
1326                 printk (KERN_ERR LOGNAME "download alias failed.\n");
1327                 return -(EIO);
1328         }
1329
1330         dev.sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_ALIAS);
1331
1332         return (0);
1333 }
1334
1335 static int
1336 wavefront_send_multisample (wavefront_patch_info *header)
1337 {
1338         int i;
1339         int num_samples;
1340         unsigned char msample_hdr[WF_MSAMPLE_BYTES];
1341
1342         munge_int32 (header->number, &msample_hdr[0], 2);
1343
1344         /* You'll recall at this point that the "number of samples" value
1345            in a wavefront_multisample struct is actually the log2 of the
1346            real number of samples.
1347         */
1348
1349         num_samples = (1<<(header->hdr.ms.NumberOfSamples&7));
1350         msample_hdr[2] = (unsigned char) header->hdr.ms.NumberOfSamples;
1351
1352         DPRINT (WF_DEBUG_LOAD_PATCH, "multi %d with %d=%d samples\n",
1353                                       header->number,
1354                                       header->hdr.ms.NumberOfSamples,
1355                                       num_samples);
1356
1357         for (i = 0; i < num_samples; i++) {
1358                 DPRINT(WF_DEBUG_LOAD_PATCH|WF_DEBUG_DATA, "sample[%d] = %d\n",
1359                        i, header->hdr.ms.SampleNumber[i]);
1360                 munge_int32 (header->hdr.ms.SampleNumber[i],
1361                      &msample_hdr[3+(i*2)], 2);
1362         }
1363     
1364         /* Need a hack here to pass in the number of bytes
1365            to be written to the synth. This is ugly, and perhaps
1366            one day, I'll fix it.
1367         */
1368
1369         if (wavefront_cmd (WFC_DOWNLOAD_MULTISAMPLE, 
1370                            (unsigned char *) ((num_samples*2)+3),
1371                            msample_hdr)) {
1372                 printk (KERN_ERR LOGNAME "download of multisample failed.\n");
1373                 return -(EIO);
1374         }
1375
1376         dev.sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_MULTISAMPLE);
1377
1378         return (0);
1379 }
1380
1381 static int
1382 wavefront_fetch_multisample (wavefront_patch_info *header)
1383 {
1384         int i;
1385         unsigned char log_ns[1];
1386         unsigned char number[2];
1387         int num_samples;
1388
1389         munge_int32 (header->number, number, 2);
1390     
1391         if (wavefront_cmd (WFC_UPLOAD_MULTISAMPLE, log_ns, number)) {
1392                 printk (KERN_ERR LOGNAME "upload multisample failed.\n");
1393                 return -(EIO);
1394         }
1395     
1396         DPRINT (WF_DEBUG_DATA, "msample %d has %d samples\n",
1397                                 header->number, log_ns[0]);
1398
1399         header->hdr.ms.NumberOfSamples = log_ns[0];
1400
1401         /* get the number of samples ... */
1402
1403         num_samples = (1 << log_ns[0]);
1404     
1405         for (i = 0; i < num_samples; i++) {
1406                 s8 d[2];
1407         
1408                 if ((d[0] = wavefront_read ()) == -1) {
1409                         printk (KERN_ERR LOGNAME "upload multisample failed "
1410                                 "during sample loop.\n");
1411                         return -(EIO);
1412                 }
1413
1414                 if ((d[1] = wavefront_read ()) == -1) {
1415                         printk (KERN_ERR LOGNAME "upload multisample failed "
1416                                 "during sample loop.\n");
1417                         return -(EIO);
1418                 }
1419         
1420                 header->hdr.ms.SampleNumber[i] =
1421                         demunge_int32 ((unsigned char *) d, 2);
1422         
1423                 DPRINT (WF_DEBUG_DATA, "msample sample[%d] = %d\n",
1424                                         i, header->hdr.ms.SampleNumber[i]);
1425         }
1426
1427         return (0);
1428 }
1429
1430
1431 static int
1432 wavefront_send_drum (wavefront_patch_info *header)
1433
1434 {
1435         unsigned char drumbuf[WF_DRUM_BYTES];
1436         wavefront_drum *drum = &header->hdr.d;
1437         int i;
1438
1439         DPRINT (WF_DEBUG_LOAD_PATCH, "downloading edrum for MIDI "
1440                 "note %d, patch = %d\n", 
1441                 header->number, drum->PatchNumber);
1442
1443         drumbuf[0] = header->number & 0x7f;
1444
1445         for (i = 0; i < 4; i++) {
1446                 munge_int32 (((unsigned char *)drum)[i], &drumbuf[1+(i*2)], 2);
1447         }
1448
1449         if (wavefront_cmd (WFC_DOWNLOAD_EDRUM_PROGRAM, NULL, drumbuf)) {
1450                 printk (KERN_ERR LOGNAME "download drum failed.\n");
1451                 return -(EIO);
1452         }
1453
1454         return (0);
1455 }
1456
1457 static int 
1458 wavefront_find_free_sample (void)
1459
1460 {
1461         int i;
1462
1463         for (i = 0; i < WF_MAX_SAMPLE; i++) {
1464                 if (!(dev.sample_status[i] & WF_SLOT_FILLED)) {
1465                         return i;
1466                 }
1467         }
1468         printk (KERN_WARNING LOGNAME "no free sample slots!\n");
1469         return -1;
1470 }
1471
1472 static int 
1473 wavefront_find_free_patch (void)
1474
1475 {
1476         int i;
1477
1478         for (i = 0; i < WF_MAX_PATCH; i++) {
1479                 if (!(dev.patch_status[i] & WF_SLOT_FILLED)) {
1480                         return i;
1481                 }
1482         }
1483         printk (KERN_WARNING LOGNAME "no free patch slots!\n");
1484         return -1;
1485 }
1486
1487 static int 
1488 log2_2048(int n)
1489
1490 {
1491         int tbl[]={0, 0, 2048, 3246, 4096, 4755, 5294, 5749, 6143,
1492                    6492, 6803, 7084, 7342, 7578, 7797, 8001, 8192,
1493                    8371, 8540, 8699, 8851, 8995, 9132, 9264, 9390,
1494                    9510, 9626, 9738, 9845, 9949, 10049, 10146};
1495         int i;
1496
1497         /* Returns 2048*log2(n) */
1498
1499         /* FIXME: this is like doing integer math
1500            on quantum particles (RuN) */
1501
1502         i=0;
1503         while(n>=32*256) {
1504                 n>>=8;
1505                 i+=2048*8;
1506         }
1507         while(n>=32) {
1508                 n>>=1;
1509                 i+=2048;
1510         }
1511         i+=tbl[n];
1512         return(i);
1513 }
1514
1515 static int
1516 wavefront_load_gus_patch (int devno, int format, const char __user *addr,
1517                           int offs, int count, int pmgr_flag)
1518 {
1519         struct patch_info guspatch;
1520         wavefront_patch_info *samp, *pat, *prog;
1521         wavefront_patch *patp;
1522         wavefront_sample *sampp;
1523         wavefront_program *progp;
1524
1525         int i,base_note;
1526         long sizeof_patch;
1527         int rc = -ENOMEM;
1528
1529         samp = kmalloc(3 * sizeof(wavefront_patch_info), GFP_KERNEL);
1530         if (!samp)
1531                 goto free_fail;
1532         pat = samp + 1;
1533         prog = pat + 1;
1534
1535         /* Copy in the header of the GUS patch */
1536
1537         sizeof_patch = (long) &guspatch.data[0] - (long) &guspatch; 
1538         if (copy_from_user(&((char *) &guspatch)[offs],
1539                            &(addr)[offs], sizeof_patch - offs)) {
1540                 rc = -EFAULT;
1541                 goto free_fail;
1542         }
1543
1544         if ((i = wavefront_find_free_patch ()) == -1) {
1545                 rc = -EBUSY;
1546                 goto free_fail;
1547         }
1548         pat->number = i;
1549         pat->subkey = WF_ST_PATCH;
1550         patp = &pat->hdr.p;
1551
1552         if ((i = wavefront_find_free_sample ()) == -1) {
1553                 rc = -EBUSY;
1554                 goto free_fail;
1555         }
1556         samp->number = i;
1557         samp->subkey = WF_ST_SAMPLE;
1558         samp->size = guspatch.len;
1559         sampp = &samp->hdr.s;
1560
1561         prog->number = guspatch.instr_no;
1562         progp = &prog->hdr.pr;
1563
1564         /* Setup the patch structure */
1565
1566         patp->amplitude_bias=guspatch.volume;
1567         patp->portamento=0;
1568         patp->sample_number= samp->number & 0xff;
1569         patp->sample_msb= samp->number >> 8;
1570         patp->pitch_bend= /*12*/ 0;
1571         patp->mono=1;
1572         patp->retrigger=1;
1573         patp->nohold=(guspatch.mode & WAVE_SUSTAIN_ON) ? 0:1;
1574         patp->frequency_bias=0;
1575         patp->restart=0;
1576         patp->reuse=0;
1577         patp->reset_lfo=1;
1578         patp->fm_src2=0;
1579         patp->fm_src1=WF_MOD_MOD_WHEEL;
1580         patp->am_src=WF_MOD_PRESSURE;
1581         patp->am_amount=127;
1582         patp->fc1_mod_amount=0;
1583         patp->fc2_mod_amount=0; 
1584         patp->fm_amount1=0;
1585         patp->fm_amount2=0;
1586         patp->envelope1.attack_level=127;
1587         patp->envelope1.decay1_level=127;
1588         patp->envelope1.decay2_level=127;
1589         patp->envelope1.sustain_level=127;
1590         patp->envelope1.release_level=0;
1591         patp->envelope2.attack_velocity=127;
1592         patp->envelope2.attack_level=127;
1593         patp->envelope2.decay1_level=127;
1594         patp->envelope2.decay2_level=127;
1595         patp->envelope2.sustain_level=127;
1596         patp->envelope2.release_level=0;
1597         patp->envelope2.attack_velocity=127;
1598         patp->randomizer=0;
1599
1600         /* Program for this patch */
1601
1602         progp->layer[0].patch_number= pat->number; /* XXX is this right ? */
1603         progp->layer[0].mute=1;
1604         progp->layer[0].pan_or_mod=1;
1605         progp->layer[0].pan=7;
1606         progp->layer[0].mix_level=127  /* guspatch.volume */;
1607         progp->layer[0].split_type=0;
1608         progp->layer[0].split_point=0;
1609         progp->layer[0].play_below=0;
1610
1611         for (i = 1; i < 4; i++) {
1612                 progp->layer[i].mute=0;
1613         }
1614
1615         /* Sample data */
1616
1617         sampp->SampleResolution=((~guspatch.mode & WAVE_16_BITS)<<1);
1618
1619         for (base_note=0;
1620              note_to_freq (base_note) < guspatch.base_note;
1621              base_note++);
1622
1623         if ((guspatch.base_note-note_to_freq(base_note))
1624             >(note_to_freq(base_note)-guspatch.base_note))
1625                 base_note++;
1626
1627         printk(KERN_DEBUG "ref freq=%d,base note=%d\n",
1628                guspatch.base_freq,
1629                base_note);
1630
1631         sampp->FrequencyBias = (29550 - log2_2048(guspatch.base_freq)
1632                                 + base_note*171);
1633         printk(KERN_DEBUG "Freq Bias is %d\n", sampp->FrequencyBias);
1634         sampp->Loop=(guspatch.mode & WAVE_LOOPING) ? 1:0;
1635         sampp->sampleStartOffset.Fraction=0;
1636         sampp->sampleStartOffset.Integer=0;
1637         sampp->loopStartOffset.Fraction=0;
1638         sampp->loopStartOffset.Integer=guspatch.loop_start
1639                 >>((guspatch.mode&WAVE_16_BITS) ? 1:0);
1640         sampp->loopEndOffset.Fraction=0;
1641         sampp->loopEndOffset.Integer=guspatch.loop_end
1642                 >>((guspatch.mode&WAVE_16_BITS) ? 1:0);
1643         sampp->sampleEndOffset.Fraction=0;
1644         sampp->sampleEndOffset.Integer=guspatch.len >> (guspatch.mode&1);
1645         sampp->Bidirectional=(guspatch.mode&WAVE_BIDIR_LOOP) ? 1:0;
1646         sampp->Reverse=(guspatch.mode&WAVE_LOOP_BACK) ? 1:0;
1647
1648         /* Now ship it down */
1649
1650         wavefront_send_sample (samp,
1651                                (unsigned short __user *) &(addr)[sizeof_patch],
1652                                (guspatch.mode & WAVE_UNSIGNED) ? 1:0);
1653         wavefront_send_patch (pat);
1654         wavefront_send_program (prog);
1655
1656         /* Now pan as best we can ... use the slave/internal MIDI device
1657            number if it exists (since it talks to the WaveFront), or the
1658            master otherwise.
1659         */
1660
1661         if (dev.mididev > 0) {
1662                 midi_synth_controller (dev.mididev, guspatch.instr_no, 10,
1663                                        ((guspatch.panning << 4) > 127) ?
1664                                        127 : (guspatch.panning << 4));
1665         }
1666         rc = 0;
1667
1668 free_fail:
1669         kfree(samp);
1670         return rc;
1671 }
1672
1673 static int
1674 wavefront_load_patch (const char __user *addr)
1675
1676
1677 {
1678         wavefront_patch_info header;
1679         
1680         if (copy_from_user (&header, addr, sizeof(wavefront_patch_info) -
1681                             sizeof(wavefront_any))) {
1682                 printk (KERN_WARNING LOGNAME "bad address for load patch.\n");
1683                 return -EFAULT;
1684         }
1685
1686         DPRINT (WF_DEBUG_LOAD_PATCH, "download "
1687                                       "Sample type: %d "
1688                                       "Sample number: %d "
1689                                       "Sample size: %d\n",
1690                                       header.subkey,
1691                                       header.number,
1692                                       header.size);
1693
1694         switch (header.subkey) {
1695         case WF_ST_SAMPLE:  /* sample or sample_header, based on patch->size */
1696
1697                 if (copy_from_user((unsigned char *) &header.hdr.s,
1698                                    (unsigned char __user *) header.hdrptr,
1699                                    sizeof (wavefront_sample)))
1700                         return -EFAULT;
1701
1702                 return wavefront_send_sample (&header, header.dataptr, 0);
1703
1704         case WF_ST_MULTISAMPLE:
1705
1706                 if (copy_from_user(&header.hdr.s, header.hdrptr,
1707                                    sizeof(wavefront_multisample)))
1708                         return -EFAULT;
1709
1710                 return wavefront_send_multisample (&header);
1711
1712
1713         case WF_ST_ALIAS:
1714
1715                 if (copy_from_user(&header.hdr.a, header.hdrptr,
1716                                    sizeof (wavefront_alias)))
1717                         return -EFAULT;
1718
1719                 return wavefront_send_alias (&header);
1720
1721         case WF_ST_DRUM:
1722                 if (copy_from_user(&header.hdr.d, header.hdrptr,
1723                                    sizeof (wavefront_drum)))
1724                         return -EFAULT;
1725
1726                 return wavefront_send_drum (&header);
1727
1728         case WF_ST_PATCH:
1729                 if (copy_from_user(&header.hdr.p, header.hdrptr,
1730                                    sizeof (wavefront_patch)))
1731                         return -EFAULT;
1732
1733                 return wavefront_send_patch (&header);
1734
1735         case WF_ST_PROGRAM:
1736                 if (copy_from_user(&header.hdr.pr, header.hdrptr,
1737                                    sizeof (wavefront_program)))
1738                         return -EFAULT;
1739
1740                 return wavefront_send_program (&header);
1741
1742         default:
1743                 printk (KERN_ERR LOGNAME "unknown patch type %d.\n",
1744                         header.subkey);
1745                 return -(EINVAL);
1746         }
1747
1748         return 0;
1749 }
1750 \f
1751 /***********************************************************************
1752 WaveFront: /dev/sequencer{,2} and other hardware-dependent interfaces
1753 ***********************************************************************/
1754
1755 static void
1756 process_sample_hdr (UCHAR8 *buf)
1757
1758 {
1759         wavefront_sample s;
1760         UCHAR8 *ptr;
1761
1762         ptr = buf;
1763
1764         /* The board doesn't send us an exact copy of a "wavefront_sample"
1765            in response to an Upload Sample Header command. Instead, we 
1766            have to convert the data format back into our data structure,
1767            just as in the Download Sample command, where we have to do
1768            something very similar in the reverse direction.
1769         */
1770
1771         *((UINT32 *) &s.sampleStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
1772         *((UINT32 *) &s.loopStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
1773         *((UINT32 *) &s.loopEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
1774         *((UINT32 *) &s.sampleEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
1775         *((UINT32 *) &s.FrequencyBias) = demunge_int32 (ptr, 3); ptr += 3;
1776
1777         s.SampleResolution = *ptr & 0x3;
1778         s.Loop = *ptr & 0x8;
1779         s.Bidirectional = *ptr & 0x10;
1780         s.Reverse = *ptr & 0x40;
1781
1782         /* Now copy it back to where it came from */
1783
1784         memcpy (buf, (unsigned char *) &s, sizeof (wavefront_sample));
1785 }
1786
1787 static int
1788 wavefront_synth_control (int cmd, wavefront_control *wc)
1789
1790 {
1791         unsigned char patchnumbuf[2];
1792         int i;
1793
1794         DPRINT (WF_DEBUG_CMD, "synth control with "
1795                 "cmd 0x%x\n", wc->cmd);
1796
1797         /* Pre-handling of or for various commands */
1798
1799         switch (wc->cmd) {
1800         case WFC_DISABLE_INTERRUPTS:
1801                 printk (KERN_INFO LOGNAME "interrupts disabled.\n");
1802                 outb (0x80|0x20, dev.control_port);
1803                 dev.interrupts_on = 0;
1804                 return 0;
1805
1806         case WFC_ENABLE_INTERRUPTS:
1807                 printk (KERN_INFO LOGNAME "interrupts enabled.\n");
1808                 outb (0x80|0x40|0x20, dev.control_port);
1809                 dev.interrupts_on = 1;
1810                 return 0;
1811
1812         case WFC_INTERRUPT_STATUS:
1813                 wc->rbuf[0] = dev.interrupts_on;
1814                 return 0;
1815
1816         case WFC_ROMSAMPLES_RDONLY:
1817                 dev.rom_samples_rdonly = wc->wbuf[0];
1818                 wc->status = 0;
1819                 return 0;
1820
1821         case WFC_IDENTIFY_SLOT_TYPE:
1822                 i = wc->wbuf[0] | (wc->wbuf[1] << 7);
1823                 if (i <0 || i >= WF_MAX_SAMPLE) {
1824                         printk (KERN_WARNING LOGNAME "invalid slot ID %d\n",
1825                                 i);
1826                         wc->status = EINVAL;
1827                         return 0;
1828                 }
1829                 wc->rbuf[0] = dev.sample_status[i];
1830                 wc->status = 0;
1831                 return 0;
1832
1833         case WFC_DEBUG_DRIVER:
1834                 dev.debug = wc->wbuf[0];
1835                 printk (KERN_INFO LOGNAME "debug = 0x%x\n", dev.debug);
1836                 return 0;
1837
1838         case WFC_FX_IOCTL:
1839                 wffx_ioctl ((wavefront_fx_info *) &wc->wbuf[0]);
1840                 return 0;
1841
1842         case WFC_UPLOAD_PATCH:
1843                 munge_int32 (*((UINT32 *) wc->wbuf), patchnumbuf, 2);
1844                 memcpy (wc->wbuf, patchnumbuf, 2);
1845                 break;
1846
1847         case WFC_UPLOAD_MULTISAMPLE:
1848                 /* multisamples have to be handled differently, and
1849                    cannot be dealt with properly by wavefront_cmd() alone.
1850                 */
1851                 wc->status = wavefront_fetch_multisample
1852                         ((wavefront_patch_info *) wc->rbuf);
1853                 return 0;
1854
1855         case WFC_UPLOAD_SAMPLE_ALIAS:
1856                 printk (KERN_INFO LOGNAME "support for sample alias upload "
1857                         "being considered.\n");
1858                 wc->status = EINVAL;
1859                 return -EINVAL;
1860         }
1861
1862         wc->status = wavefront_cmd (wc->cmd, wc->rbuf, wc->wbuf);
1863
1864         /* Post-handling of certain commands.
1865
1866            In particular, if the command was an upload, demunge the data
1867            so that the user-level doesn't have to think about it.
1868         */
1869
1870         if (wc->status == 0) {
1871                 switch (wc->cmd) {
1872                         /* intercept any freemem requests so that we know
1873                            we are always current with the user-level view
1874                            of things.
1875                         */
1876
1877                 case WFC_REPORT_FREE_MEMORY:
1878                         dev.freemem = demunge_int32 (wc->rbuf, 4);
1879                         break;
1880
1881                 case WFC_UPLOAD_PATCH:
1882                         demunge_buf (wc->rbuf, wc->rbuf, WF_PATCH_BYTES);
1883                         break;
1884
1885                 case WFC_UPLOAD_PROGRAM:
1886                         demunge_buf (wc->rbuf, wc->rbuf, WF_PROGRAM_BYTES);
1887                         break;
1888
1889                 case WFC_UPLOAD_EDRUM_PROGRAM:
1890                         demunge_buf (wc->rbuf, wc->rbuf, WF_DRUM_BYTES - 1);
1891                         break;
1892
1893                 case WFC_UPLOAD_SAMPLE_HEADER:
1894                         process_sample_hdr (wc->rbuf);
1895                         break;
1896
1897                 case WFC_UPLOAD_SAMPLE_ALIAS:
1898                         printk (KERN_INFO LOGNAME "support for "
1899                                 "sample aliases still "
1900                                 "being considered.\n");
1901                         break;
1902
1903                 case WFC_VMIDI_OFF:
1904                         if (virtual_midi_disable () < 0) {
1905                                 return -(EIO);
1906                         }
1907                         break;
1908
1909                 case WFC_VMIDI_ON:
1910                         if (virtual_midi_enable () < 0) {
1911                                 return -(EIO);
1912                         }
1913                         break;
1914                 }
1915         }
1916
1917         return 0;
1918 }
1919
1920 \f
1921 /***********************************************************************/
1922 /* WaveFront: Linux file system interface (for access via raw synth)    */
1923 /***********************************************************************/
1924
1925 static int 
1926 wavefront_open (struct inode *inode, struct file *file)
1927 {
1928         /* XXX fix me */
1929         dev.opened = file->f_flags;
1930         return 0;
1931 }
1932
1933 static int
1934 wavefront_release(struct inode *inode, struct file *file)
1935 {
1936         lock_kernel();
1937         dev.opened = 0;
1938         dev.debug = 0;
1939         unlock_kernel();
1940         return 0;
1941 }
1942
1943 static int
1944 wavefront_ioctl(struct inode *inode, struct file *file,
1945                 unsigned int cmd, unsigned long arg)
1946 {
1947         wavefront_control wc;
1948         int err;
1949
1950         switch (cmd) {
1951
1952         case WFCTL_WFCMD:
1953                 if (copy_from_user(&wc, (void __user *) arg, sizeof (wc)))
1954                         return -EFAULT;
1955                 
1956                 if ((err = wavefront_synth_control (cmd, &wc)) == 0) {
1957                         if (copy_to_user ((void __user *) arg, &wc, sizeof (wc)))
1958                                 return -EFAULT;
1959                 }
1960
1961                 return err;
1962                 
1963         case WFCTL_LOAD_SPP:
1964                 return wavefront_load_patch ((const char __user *) arg);
1965                 
1966         default:
1967                 printk (KERN_WARNING LOGNAME "invalid ioctl %#x\n", cmd);
1968                 return -(EINVAL);
1969
1970         }
1971         return 0;
1972 }
1973
1974 static /*const*/ struct file_operations wavefront_fops = {
1975         .owner          = THIS_MODULE,
1976         .llseek         = no_llseek,
1977         .ioctl          = wavefront_ioctl,
1978         .open           = wavefront_open,
1979         .release        = wavefront_release,
1980 };
1981
1982 \f
1983 /***********************************************************************/
1984 /* WaveFront: OSS installation and support interface                   */
1985 /***********************************************************************/
1986
1987 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
1988
1989 static struct synth_info wavefront_info =
1990 {"Turtle Beach WaveFront", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_WAVEFRONT,
1991  0, 32, 0, 0, SYNTH_CAP_INPUT};
1992
1993 static int
1994 wavefront_oss_open (int devno, int mode)
1995
1996 {
1997         dev.opened = mode;
1998         return 0;
1999 }
2000
2001 static void
2002 wavefront_oss_close (int devno)
2003
2004 {
2005         dev.opened = 0;
2006         dev.debug = 0;
2007         return;
2008 }
2009
2010 static int
2011 wavefront_oss_ioctl (int devno, unsigned int cmd, void __user * arg)
2012
2013 {
2014         wavefront_control wc;
2015         int err;
2016
2017         switch (cmd) {
2018         case SNDCTL_SYNTH_INFO:
2019                 if(copy_to_user(arg, &wavefront_info, sizeof (wavefront_info)))
2020                         return -EFAULT;
2021                 return 0;
2022
2023         case SNDCTL_SEQ_RESETSAMPLES:
2024 //              printk (KERN_WARNING LOGNAME "driver cannot reset samples.\n");
2025                 return 0; /* don't force an error */
2026
2027         case SNDCTL_SEQ_PERCMODE:
2028                 return 0; /* don't force an error */
2029
2030         case SNDCTL_SYNTH_MEMAVL:
2031                 if ((dev.freemem = wavefront_freemem ()) < 0) {
2032                         printk (KERN_ERR LOGNAME "cannot get memory size\n");
2033                         return -EIO;
2034                 } else {
2035                         return dev.freemem;
2036                 }
2037                 break;
2038
2039         case SNDCTL_SYNTH_CONTROL:
2040                 if(copy_from_user (&wc, arg, sizeof (wc)))
2041                         err = -EFAULT;
2042                 else if ((err = wavefront_synth_control (cmd, &wc)) == 0) {
2043                         if(copy_to_user (arg, &wc, sizeof (wc)))
2044                                 err = -EFAULT;
2045                 }
2046
2047                 return err;
2048
2049         default:
2050                 return -(EINVAL);
2051         }
2052 }
2053
2054 static int
2055 wavefront_oss_load_patch (int devno, int format, const char __user *addr,
2056                           int offs, int count, int pmgr_flag)
2057 {
2058
2059         if (format == SYSEX_PATCH) {    /* Handled by midi_synth.c */
2060                 if (midi_load_patch == NULL) {
2061                         printk (KERN_ERR LOGNAME
2062                                 "SYSEX not loadable: "
2063                                 "no midi patch loader!\n");
2064                         return -(EINVAL);
2065                 }
2066
2067                 return midi_load_patch (devno, format, addr,
2068                                         offs, count, pmgr_flag);
2069
2070         } else if (format == GUS_PATCH) {
2071                 return wavefront_load_gus_patch (devno, format,
2072                                                  addr, offs, count, pmgr_flag);
2073
2074         } else if (format != WAVEFRONT_PATCH) {
2075                 printk (KERN_ERR LOGNAME "unknown patch format %d\n", format);
2076                 return -(EINVAL);
2077         }
2078
2079         if (count < sizeof (wavefront_patch_info)) {
2080                 printk (KERN_ERR LOGNAME "sample header too short\n");
2081                 return -(EINVAL);
2082         }
2083
2084         /* "addr" points to a user-space wavefront_patch_info */
2085
2086         return wavefront_load_patch (addr);
2087 }       
2088
2089 static struct synth_operations wavefront_operations =
2090 {
2091         .owner          = THIS_MODULE,
2092         .id             = "WaveFront",
2093         .info           = &wavefront_info,
2094         .midi_dev       = 0,
2095         .synth_type     = SYNTH_TYPE_SAMPLE,
2096         .synth_subtype  = SAMPLE_TYPE_WAVEFRONT,
2097         .open           = wavefront_oss_open,
2098         .close          = wavefront_oss_close,
2099         .ioctl          = wavefront_oss_ioctl,
2100         .kill_note      = midi_synth_kill_note,
2101         .start_note     = midi_synth_start_note,
2102         .set_instr      = midi_synth_set_instr,
2103         .reset          = midi_synth_reset,
2104         .load_patch     = midi_synth_load_patch,
2105         .aftertouch     = midi_synth_aftertouch,
2106         .controller     = midi_synth_controller,
2107         .panning        = midi_synth_panning,
2108         .bender         = midi_synth_bender,
2109         .setup_voice    = midi_synth_setup_voice
2110 };
2111 #endif /* OSS_SUPPORT_SEQ */
2112
2113 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_STATIC_INSTALL
2114
2115 static void __init attach_wavefront (struct address_info *hw_config)
2116 {
2117     (void) install_wavefront ();
2118 }
2119
2120 static int __init probe_wavefront (struct address_info *hw_config)
2121 {
2122     return !detect_wavefront (hw_config->irq, hw_config->io_base);
2123 }
2124
2125 static void __exit unload_wavefront (struct address_info *hw_config) 
2126 {
2127     (void) uninstall_wavefront ();
2128 }
2129
2130 #endif /* OSS_SUPPORT_STATIC_INSTALL */
2131
2132 /***********************************************************************/
2133 /* WaveFront: Linux modular sound kernel installation interface        */
2134 /***********************************************************************/
2135
2136 static irqreturn_t
2137 wavefrontintr(int irq, void *dev_id, struct pt_regs *dummy)
2138 {
2139         struct wf_config *hw = dev_id;
2140
2141         /*
2142            Some comments on interrupts. I attempted a version of this
2143            driver that used interrupts throughout the code instead of
2144            doing busy and/or sleep-waiting. Alas, it appears that once
2145            the Motorola firmware is downloaded, the card *never*
2146            generates an RX interrupt. These are successfully generated
2147            during firmware loading, and after that wavefront_status()
2148            reports that an interrupt is pending on the card from time
2149            to time, but it never seems to be delivered to this
2150            driver. Note also that wavefront_status() continues to
2151            report that RX interrupts are enabled, suggesting that I
2152            didn't goof up and disable them by mistake.
2153
2154            Thus, I stepped back to a prior version of
2155            wavefront_wait(), the only place where this really
2156            matters. Its sad, but I've looked through the code to check
2157            on things, and I really feel certain that the Motorola
2158            firmware prevents RX-ready interrupts.
2159         */
2160
2161         if ((wavefront_status() & (STAT_INTR_READ|STAT_INTR_WRITE)) == 0) {
2162                 return IRQ_NONE;
2163         }
2164
2165         hw->irq_ok = 1;
2166         hw->irq_cnt++;
2167         wake_up_interruptible (&hw->interrupt_sleeper);
2168         return IRQ_HANDLED;
2169 }
2170
2171 /* STATUS REGISTER 
2172
2173 0 Host Rx Interrupt Enable (1=Enabled)
2174 1 Host Rx Register Full (1=Full)
2175 2 Host Rx Interrupt Pending (1=Interrupt)
2176 3 Unused
2177 4 Host Tx Interrupt (1=Enabled)
2178 5 Host Tx Register empty (1=Empty)
2179 6 Host Tx Interrupt Pending (1=Interrupt)
2180 7 Unused
2181 */
2182
2183 static int
2184 wavefront_interrupt_bits (int irq)
2185
2186 {
2187         int bits;
2188
2189         switch (irq) {
2190         case 9:
2191                 bits = 0x00;
2192                 break;
2193         case 5:
2194                 bits = 0x08;
2195                 break;
2196         case 12:
2197                 bits = 0x10;
2198                 break;
2199         case 15:
2200                 bits = 0x18;
2201                 break;
2202         
2203         default:
2204                 printk (KERN_WARNING LOGNAME "invalid IRQ %d\n", irq);
2205                 bits = -1;
2206         }
2207
2208         return bits;
2209 }
2210
2211 static void
2212 wavefront_should_cause_interrupt (int val, int port, int timeout)
2213
2214 {
2215         unsigned long flags;
2216
2217         /* this will not help on SMP - but at least it compiles */
2218         spin_lock_irqsave(&lock, flags);
2219         dev.irq_ok = 0;
2220         outb (val,port);
2221         interruptible_sleep_on_timeout (&dev.interrupt_sleeper, timeout);
2222         spin_unlock_irqrestore(&lock,flags);
2223 }
2224
2225 static int __init wavefront_hw_reset (void)
2226 {
2227         int bits;
2228         int hwv[2];
2229         unsigned long irq_mask;
2230         short reported_irq;
2231
2232         /* IRQ already checked in init_module() */
2233
2234         bits = wavefront_interrupt_bits (dev.irq);
2235
2236         printk (KERN_DEBUG LOGNAME "autodetecting WaveFront IRQ\n");
2237
2238         irq_mask = probe_irq_on ();
2239
2240         outb (0x0, dev.control_port); 
2241         outb (0x80 | 0x40 | bits, dev.data_port);       
2242         wavefront_should_cause_interrupt(0x80|0x40|0x10|0x1,
2243                                          dev.control_port,
2244                                          (reset_time*HZ)/100);
2245
2246         reported_irq = probe_irq_off (irq_mask);
2247
2248         if (reported_irq != dev.irq) {
2249                 if (reported_irq == 0) {
2250                         printk (KERN_ERR LOGNAME
2251                                 "No unassigned interrupts detected "
2252                                 "after h/w reset\n");
2253                 } else if (reported_irq < 0) {
2254                         printk (KERN_ERR LOGNAME
2255                                 "Multiple unassigned interrupts detected "
2256                                 "after h/w reset\n");
2257                 } else {
2258                         printk (KERN_ERR LOGNAME "autodetected IRQ %d not the "
2259                                 "value provided (%d)\n", reported_irq,
2260                                 dev.irq);
2261                 }
2262                 dev.irq = -1;
2263                 return 1;
2264         } else {
2265                 printk (KERN_INFO LOGNAME "autodetected IRQ at %d\n",
2266                         reported_irq);
2267         }
2268
2269         if (request_irq (dev.irq, wavefrontintr,
2270                          IRQF_DISABLED|IRQF_SHARED,
2271                          "wavefront synth", &dev) < 0) {
2272                 printk (KERN_WARNING LOGNAME "IRQ %d not available!\n",
2273                         dev.irq);
2274                 return 1;
2275         }
2276
2277         /* try reset of port */
2278       
2279         outb (0x0, dev.control_port); 
2280   
2281         /* At this point, the board is in reset, and the H/W initialization
2282            register is accessed at the same address as the data port.
2283      
2284            Bit 7 - Enable IRQ Driver    
2285            0 - Tri-state the Wave-Board drivers for the PC Bus IRQs
2286            1 - Enable IRQ selected by bits 5:3 to be driven onto the PC Bus.
2287      
2288            Bit 6 - MIDI Interface Select
2289
2290            0 - Use the MIDI Input from the 26-pin WaveBlaster
2291            compatible header as the serial MIDI source
2292            1 - Use the MIDI Input from the 9-pin D connector as the
2293            serial MIDI source.
2294      
2295            Bits 5:3 - IRQ Selection
2296            0 0 0 - IRQ 2/9
2297            0 0 1 - IRQ 5
2298            0 1 0 - IRQ 12
2299            0 1 1 - IRQ 15
2300            1 0 0 - Reserved
2301            1 0 1 - Reserved
2302            1 1 0 - Reserved
2303            1 1 1 - Reserved
2304      
2305            Bits 2:1 - Reserved
2306            Bit 0 - Disable Boot ROM
2307            0 - memory accesses to 03FC30-03FFFFH utilize the internal Boot ROM
2308            1 - memory accesses to 03FC30-03FFFFH are directed to external 
2309            storage.
2310      
2311         */
2312
2313         /* configure hardware: IRQ, enable interrupts, 
2314            plus external 9-pin MIDI interface selected
2315         */
2316
2317         outb (0x80 | 0x40 | bits, dev.data_port);       
2318   
2319         /* CONTROL REGISTER
2320
2321            0 Host Rx Interrupt Enable (1=Enabled)      0x1
2322            1 Unused                                    0x2
2323            2 Unused                                    0x4
2324            3 Unused                                    0x8
2325            4 Host Tx Interrupt Enable                 0x10
2326            5 Mute (0=Mute; 1=Play)                    0x20
2327            6 Master Interrupt Enable (1=Enabled)      0x40
2328            7 Master Reset (0=Reset; 1=Run)            0x80
2329
2330            Take us out of reset, mute output, master + TX + RX interrupts on.
2331            
2332            We'll get an interrupt presumably to tell us that the TX
2333            register is clear.
2334         */
2335
2336         wavefront_should_cause_interrupt(0x80|0x40|0x10|0x1,
2337                                          dev.control_port,
2338                                          (reset_time*HZ)/100);
2339
2340         /* Note: data port is now the data port, not the h/w initialization
2341            port.
2342          */
2343
2344         if (!dev.irq_ok) {
2345                 printk (KERN_WARNING LOGNAME
2346                         "intr not received after h/w un-reset.\n");
2347                 goto gone_bad;
2348         } 
2349
2350         dev.interrupts_on = 1;
2351         
2352         /* Note: data port is now the data port, not the h/w initialization
2353            port.
2354
2355            At this point, only "HW VERSION" or "DOWNLOAD OS" commands
2356            will work. So, issue one of them, and wait for TX
2357            interrupt. This can take a *long* time after a cold boot,
2358            while the ISC ROM does its RAM test. The SDK says up to 4
2359            seconds - with 12MB of RAM on a Tropez+, it takes a lot
2360            longer than that (~16secs). Note that the card understands
2361            the difference between a warm and a cold boot, so
2362            subsequent ISC2115 reboots (say, caused by module
2363            reloading) will get through this much faster.
2364
2365            XXX Interesting question: why is no RX interrupt received first ?
2366         */
2367
2368         wavefront_should_cause_interrupt(WFC_HARDWARE_VERSION, 
2369                                          dev.data_port, ramcheck_time*HZ);
2370
2371         if (!dev.irq_ok) {
2372                 printk (KERN_WARNING LOGNAME
2373                         "post-RAM-check interrupt not received.\n");
2374                 goto gone_bad;
2375         } 
2376
2377         if (!wavefront_wait (STAT_CAN_READ)) {
2378                 printk (KERN_WARNING LOGNAME
2379                         "no response to HW version cmd.\n");
2380                 goto gone_bad;
2381         }
2382         
2383         if ((hwv[0] = wavefront_read ()) == -1) {
2384                 printk (KERN_WARNING LOGNAME
2385                         "board not responding correctly.\n");
2386                 goto gone_bad;
2387         }
2388
2389         if (hwv[0] == 0xFF) { /* NAK */
2390
2391                 /* Board's RAM test failed. Try to read error code,
2392                    and tell us about it either way.
2393                 */
2394                 
2395                 if ((hwv[0] = wavefront_read ()) == -1) {
2396                         printk (KERN_WARNING LOGNAME "on-board RAM test failed "
2397                                 "(bad error code).\n");
2398                 } else {
2399                         printk (KERN_WARNING LOGNAME "on-board RAM test failed "
2400                                 "(error code: 0x%x).\n",
2401                                 hwv[0]);
2402                 }
2403                 goto gone_bad;
2404         }
2405
2406         /* We're OK, just get the next byte of the HW version response */
2407
2408         if ((hwv[1] = wavefront_read ()) == -1) {
2409                 printk (KERN_WARNING LOGNAME "incorrect h/w response.\n");
2410                 goto gone_bad;
2411         }
2412
2413         printk (KERN_INFO LOGNAME "hardware version %d.%d\n",
2414                 hwv[0], hwv[1]);
2415
2416         return 0;
2417
2418
2419      gone_bad:
2420         if (dev.irq >= 0) {
2421                 free_irq (dev.irq, &dev);
2422                 dev.irq = -1;
2423         }
2424         return (1);
2425 }
2426
2427 static int __init detect_wavefront (int irq, int io_base)
2428 {
2429         unsigned char   rbuf[4], wbuf[4];
2430
2431         /* TB docs say the device takes up 8 ports, but we know that
2432            if there is an FX device present (i.e. a Tropez+) it really
2433            consumes 16.
2434         */
2435
2436         if (!request_region (io_base, 16, "wavfront")) {
2437                 printk (KERN_ERR LOGNAME "IO address range 0x%x - 0x%x "
2438                         "already in use - ignored\n", dev.base,
2439                         dev.base+15);
2440                 return -1;
2441         }
2442   
2443         dev.irq = irq;
2444         dev.base = io_base;
2445         dev.israw = 0;
2446         dev.debug = debug_default;
2447         dev.interrupts_on = 0;
2448         dev.irq_cnt = 0;
2449         dev.rom_samples_rdonly = 1; /* XXX default lock on ROM sample slots */
2450
2451         if (wavefront_cmd (WFC_FIRMWARE_VERSION, rbuf, wbuf) == 0) {
2452
2453                 dev.fw_version[0] = rbuf[0];
2454                 dev.fw_version[1] = rbuf[1];
2455                 printk (KERN_INFO LOGNAME
2456                         "firmware %d.%d already loaded.\n",
2457                         rbuf[0], rbuf[1]);
2458
2459                 /* check that a command actually works */
2460       
2461                 if (wavefront_cmd (WFC_HARDWARE_VERSION,
2462                                    rbuf, wbuf) == 0) {
2463                         dev.hw_version[0] = rbuf[0];
2464                         dev.hw_version[1] = rbuf[1];
2465                 } else {
2466                         printk (KERN_WARNING LOGNAME "not raw, but no "
2467                                 "hardware version!\n");
2468                         release_region (io_base, 16);
2469                         return 0;
2470                 }
2471
2472                 if (!wf_raw) {
2473                         /* will re-acquire region in install_wavefront() */
2474                         release_region (io_base, 16);
2475                         return 1;
2476                 } else {
2477                         printk (KERN_INFO LOGNAME
2478                                 "reloading firmware anyway.\n");
2479                         dev.israw = 1;
2480                 }
2481
2482         } else {
2483
2484                 dev.israw = 1;
2485                 printk (KERN_INFO LOGNAME
2486                         "no response to firmware probe, assume raw.\n");
2487
2488         }
2489
2490         init_waitqueue_head (&dev.interrupt_sleeper);
2491
2492         if (wavefront_hw_reset ()) {
2493                 printk (KERN_WARNING LOGNAME "hardware reset failed\n");
2494                 release_region (io_base, 16);
2495                 return 0;
2496         }
2497
2498         /* Check for FX device, present only on Tropez+ */
2499
2500         dev.has_fx = (detect_wffx () == 0);
2501
2502         /* will re-acquire region in install_wavefront() */
2503         release_region (io_base, 16);
2504         return 1;
2505 }
2506
2507 #include "os.h"
2508 #include <linux/fs.h>
2509 #include <linux/mm.h>
2510 #include <linux/slab.h>
2511 #include <asm/uaccess.h>
2512
2513
2514 static int
2515 wavefront_download_firmware (char *path)
2516
2517 {
2518         unsigned char section[WF_SECTION_MAX];
2519         char section_length; /* yes, just a char; max value is WF_SECTION_MAX */
2520         int section_cnt_downloaded = 0;
2521         int fd;
2522         int c;
2523         int i;
2524         mm_segment_t fs;
2525
2526         /* This tries to be a bit cleverer than the stuff Alan Cox did for
2527            the generic sound firmware, in that it actually knows
2528            something about the structure of the Motorola firmware. In
2529            particular, it uses a version that has been stripped of the
2530            20K of useless header information, and had section lengths
2531            added, making it possible to load the entire OS without any
2532            [kv]malloc() activity, since the longest entity we ever read is
2533            42 bytes (well, WF_SECTION_MAX) long.
2534         */
2535
2536         fs = get_fs();
2537         set_fs (get_ds());
2538
2539         if ((fd = sys_open (path, 0, 0)) < 0) {
2540                 printk (KERN_WARNING LOGNAME "Unable to load \"%s\".\n",
2541                         path);
2542                 return 1;
2543         }
2544
2545         while (1) {
2546                 int x;
2547
2548                 if ((x = sys_read (fd, &section_length, sizeof (section_length))) !=
2549                     sizeof (section_length)) {
2550                         printk (KERN_ERR LOGNAME "firmware read error.\n");
2551                         goto failure;
2552                 }
2553
2554                 if (section_length == 0) {
2555                         break;
2556                 }
2557
2558                 if (sys_read (fd, section, section_length) != section_length) {
2559                         printk (KERN_ERR LOGNAME "firmware section "
2560                                 "read error.\n");
2561                         goto failure;
2562                 }
2563
2564                 /* Send command */
2565         
2566                 if (wavefront_write (WFC_DOWNLOAD_OS)) {
2567                         goto failure;
2568                 }
2569         
2570                 for (i = 0; i < section_length; i++) {
2571                         if (wavefront_write (section[i])) {
2572                                 goto failure;
2573                         }
2574                 }
2575         
2576                 /* get ACK */
2577         
2578                 if (wavefront_wait (STAT_CAN_READ)) {
2579
2580                         if ((c = inb (dev.data_port)) != WF_ACK) {
2581
2582                                 printk (KERN_ERR LOGNAME "download "
2583                                         "of section #%d not "
2584                                         "acknowledged, ack = 0x%x\n",
2585                                         section_cnt_downloaded + 1, c);
2586                                 goto failure;
2587                 
2588                         }
2589
2590                 } else {
2591                         printk (KERN_ERR LOGNAME "time out for firmware ACK.\n");
2592                         goto failure;
2593                 }
2594
2595         }
2596
2597         sys_close (fd);
2598         set_fs (fs);
2599         return 0;
2600
2601  failure:
2602         sys_close (fd);
2603         set_fs (fs);
2604         printk (KERN_ERR "\nWaveFront: firmware download failed!!!\n");
2605         return 1;
2606 }
2607
2608 static int __init wavefront_config_midi (void)
2609 {
2610         unsigned char rbuf[4], wbuf[4];
2611     
2612         if (detect_wf_mpu (dev.irq, dev.base) < 0) {
2613                 printk (KERN_WARNING LOGNAME
2614                         "could not find working MIDI device\n");
2615                 return -1;
2616         } 
2617
2618         if ((dev.mididev = install_wf_mpu ()) < 0) {
2619                 printk (KERN_WARNING LOGNAME
2620                         "MIDI interfaces not configured\n");
2621                 return -1;
2622         }
2623
2624         /* Route external MIDI to WaveFront synth (by default) */
2625     
2626         if (wavefront_cmd (WFC_MISYNTH_ON, rbuf, wbuf)) {
2627                 printk (KERN_WARNING LOGNAME
2628                         "cannot enable MIDI-IN to synth routing.\n");
2629                 /* XXX error ? */
2630         }
2631
2632
2633 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2634         /* Get the regular MIDI patch loading function, so we can
2635            use it if we ever get handed a SYSEX patch. This is
2636            unlikely, because its so damn slow, but we may as well
2637            leave this functionality from maui.c behind, since it
2638            could be useful for sequencer applications that can
2639            only use MIDI to do patch loading.
2640         */
2641
2642         if (midi_devs[dev.mididev]->converter != NULL) {
2643                 midi_load_patch = midi_devs[dev.mididev]->converter->load_patch;
2644                 midi_devs[dev.mididev]->converter->load_patch =
2645                     &wavefront_oss_load_patch;
2646         }
2647
2648 #endif /* OSS_SUPPORT_SEQ */
2649         
2650         /* Turn on Virtual MIDI, but first *always* turn it off,
2651            since otherwise consectutive reloads of the driver will
2652            never cause the hardware to generate the initial "internal" or 
2653            "external" source bytes in the MIDI data stream. This
2654            is pretty important, since the internal hardware generally will
2655            be used to generate none or very little MIDI output, and
2656            thus the only source of MIDI data is actually external. Without
2657            the switch bytes, the driver will think it all comes from
2658            the internal interface. Duh.
2659         */
2660
2661         if (wavefront_cmd (WFC_VMIDI_OFF, rbuf, wbuf)) { 
2662                 printk (KERN_WARNING LOGNAME
2663                         "virtual MIDI mode not disabled\n");
2664                 return 0; /* We're OK, but missing the external MIDI dev */
2665         }
2666
2667         if ((dev.ext_mididev = virtual_midi_enable ()) < 0) {
2668                 printk (KERN_WARNING LOGNAME "no virtual MIDI access.\n");
2669         } else {
2670                 if (wavefront_cmd (WFC_VMIDI_ON, rbuf, wbuf)) {
2671                         printk (KERN_WARNING LOGNAME
2672                                 "cannot enable virtual MIDI mode.\n");
2673                         virtual_midi_disable ();
2674                 } 
2675         }
2676     
2677         return 0;
2678 }
2679
2680 static int __init wavefront_do_reset (int atboot)
2681 {
2682         char voices[1];
2683
2684         if (!atboot && wavefront_hw_reset ()) {
2685                 printk (KERN_WARNING LOGNAME "hw reset failed.\n");
2686                 goto gone_bad;
2687         }
2688
2689         if (dev.israw) {
2690                 if (wavefront_download_firmware (ospath)) {
2691                         goto gone_bad;
2692                 }
2693
2694                 dev.israw = 0;
2695
2696                 /* Wait for the OS to get running. The protocol for
2697                    this is non-obvious, and was determined by
2698                    using port-IO tracing in DOSemu and some
2699                    experimentation here.
2700                    
2701                    Rather than using timed waits, use interrupts creatively.
2702                 */
2703
2704                 wavefront_should_cause_interrupt (WFC_NOOP,
2705                                                   dev.data_port,
2706                                                   (osrun_time*HZ));
2707
2708                 if (!dev.irq_ok) {
2709                         printk (KERN_WARNING LOGNAME
2710                                 "no post-OS interrupt.\n");
2711                         goto gone_bad;
2712                 }
2713                 
2714                 /* Now, do it again ! */
2715                 
2716                 wavefront_should_cause_interrupt (WFC_NOOP,
2717                                                   dev.data_port, (10*HZ));
2718                 
2719                 if (!dev.irq_ok) {
2720                         printk (KERN_WARNING LOGNAME
2721                                 "no post-OS interrupt(2).\n");
2722                         goto gone_bad;
2723                 }
2724
2725                 /* OK, no (RX/TX) interrupts any more, but leave mute
2726                    in effect. 
2727                 */
2728                 
2729                 outb (0x80|0x40, dev.control_port); 
2730
2731                 /* No need for the IRQ anymore */
2732                 
2733                 free_irq (dev.irq, &dev);
2734
2735         }
2736
2737         if (dev.has_fx && fx_raw) {
2738                 wffx_init ();
2739         }
2740
2741         /* SETUPSND.EXE asks for sample memory config here, but since i
2742            have no idea how to interpret the result, we'll forget
2743            about it.
2744         */
2745         
2746         if ((dev.freemem = wavefront_freemem ()) < 0) {
2747                 goto gone_bad;
2748         }
2749                 
2750         printk (KERN_INFO LOGNAME "available DRAM %dk\n", dev.freemem / 1024);
2751
2752         if (wavefront_write (0xf0) ||
2753             wavefront_write (1) ||
2754             (wavefront_read () < 0)) {
2755                 dev.debug = 0;
2756                 printk (KERN_WARNING LOGNAME "MPU emulation mode not set.\n");
2757                 goto gone_bad;
2758         }
2759
2760         voices[0] = 32;
2761
2762         if (wavefront_cmd (WFC_SET_NVOICES, NULL, voices)) {
2763                 printk (KERN_WARNING LOGNAME
2764                         "cannot set number of voices to 32.\n");
2765                 goto gone_bad;
2766         }
2767
2768
2769         return 0;
2770
2771  gone_bad:
2772         /* reset that sucker so that it doesn't bother us. */
2773
2774         outb (0x0, dev.control_port);
2775         dev.interrupts_on = 0;
2776         if (dev.irq >= 0) {
2777                 free_irq (dev.irq, &dev);
2778         }
2779         return 1;
2780 }
2781
2782 static int __init wavefront_init (int atboot)
2783 {
2784         int samples_are_from_rom;
2785
2786         if (dev.israw) {
2787                 samples_are_from_rom = 1;
2788         } else {
2789                 /* XXX is this always true ? */
2790                 samples_are_from_rom = 0;
2791         }
2792
2793         if (dev.israw || fx_raw) {
2794                 if (wavefront_do_reset (atboot)) {
2795                         return -1;
2796                 }
2797         }
2798
2799         wavefront_get_sample_status (samples_are_from_rom);
2800         wavefront_get_program_status ();
2801         wavefront_get_patch_status ();
2802
2803         /* Start normal operation: unreset, master interrupt enabled, no mute
2804         */
2805
2806         outb (0x80|0x40|0x20, dev.control_port); 
2807
2808         return (0);
2809 }
2810
2811 static int __init install_wavefront (void)
2812 {
2813         if (!request_region (dev.base+2, 6, "wavefront synth"))
2814                 return -1;
2815
2816         if (dev.has_fx) {
2817                 if (!request_region (dev.base+8, 8, "wavefront fx")) {
2818                         release_region (dev.base+2, 6);
2819                         return -1;
2820                 }
2821         }
2822
2823         if ((dev.synth_dev = register_sound_synth (&wavefront_fops, -1)) < 0) {
2824                 printk (KERN_ERR LOGNAME "cannot register raw synth\n");
2825                 goto err_out;
2826         }
2827
2828 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2829         if ((dev.oss_dev = sound_alloc_synthdev()) == -1) {
2830                 printk (KERN_ERR LOGNAME "Too many sequencers\n");
2831                 /* FIXME: leak: should unregister sound synth */
2832                 goto err_out;
2833         } else {
2834                 synth_devs[dev.oss_dev] = &wavefront_operations;
2835         }
2836 #endif /* OSS_SUPPORT_SEQ */
2837
2838         if (wavefront_init (1) < 0) {
2839                 printk (KERN_WARNING LOGNAME "initialization failed.\n");
2840
2841 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2842                 sound_unload_synthdev (dev.oss_dev);
2843 #endif /* OSS_SUPPORT_SEQ */ 
2844
2845                 goto err_out;
2846         }
2847     
2848         if (wavefront_config_midi ()) {
2849                 printk (KERN_WARNING LOGNAME "could not initialize MIDI.\n");
2850         }
2851
2852         return dev.oss_dev;
2853
2854 err_out:
2855         release_region (dev.base+2, 6);
2856         if (dev.has_fx)
2857                 release_region (dev.base+8, 8);
2858         return -1;
2859 }
2860
2861 static void __exit uninstall_wavefront (void)
2862 {
2863         /* the first two i/o addresses are freed by the wf_mpu code */
2864         release_region (dev.base+2, 6);
2865
2866         if (dev.has_fx) {
2867                 release_region (dev.base+8, 8);
2868         }
2869
2870         unregister_sound_synth (dev.synth_dev);
2871
2872 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2873         sound_unload_synthdev (dev.oss_dev);
2874 #endif /* OSS_SUPPORT_SEQ */ 
2875         uninstall_wf_mpu ();
2876 }
2877
2878 /***********************************************************************/
2879 /*   WaveFront FX control                                              */
2880 /***********************************************************************/
2881
2882 #include "yss225.h"
2883
2884 /* Control bits for the Load Control Register
2885  */
2886
2887 #define FX_LSB_TRANSFER 0x01    /* transfer after DSP LSB byte written */
2888 #define FX_MSB_TRANSFER 0x02    /* transfer after DSP MSB byte written */
2889 #define FX_AUTO_INCR    0x04    /* auto-increment DSP address after transfer */
2890
2891 static int
2892 wffx_idle (void) 
2893     
2894 {
2895         int i;
2896         unsigned int x = 0x80;
2897     
2898         for (i = 0; i < 1000; i++) {
2899                 x = inb (dev.fx_status);
2900                 if ((x & 0x80) == 0) {
2901                         break;
2902                 }
2903         }
2904     
2905         if (x & 0x80) {
2906                 printk (KERN_ERR LOGNAME "FX device never idle.\n");
2907                 return 0;
2908         }
2909     
2910         return (1);
2911 }
2912
2913 int __init detect_wffx (void)
2914 {
2915         /* This is a crude check, but its the best one I have for now.
2916            Certainly on the Maui and the Tropez, wffx_idle() will
2917            report "never idle", which suggests that this test should
2918            work OK.
2919         */
2920
2921         if (inb (dev.fx_status) & 0x80) {
2922                 printk (KERN_INFO LOGNAME "Hmm, probably a Maui or Tropez.\n");
2923                 return -1;
2924         }
2925
2926         return 0;
2927 }       
2928
2929 static void
2930 wffx_mute (int onoff)
2931     
2932 {
2933         if (!wffx_idle()) {
2934                 return;
2935         }
2936     
2937         outb (onoff ? 0x02 : 0x00, dev.fx_op);
2938 }
2939
2940 static int
2941 wffx_memset (int page,
2942              int addr, int cnt, unsigned short *data)
2943 {
2944         if (page < 0 || page > 7) {
2945                 printk (KERN_ERR LOGNAME "FX memset: "
2946                         "page must be >= 0 and <= 7\n");
2947                 return -(EINVAL);
2948         }
2949
2950         if (addr < 0 || addr > 0x7f) {
2951                 printk (KERN_ERR LOGNAME "FX memset: "
2952                         "addr must be >= 0 and <= 7f\n");
2953                 return -(EINVAL);
2954         }
2955
2956         if (cnt == 1) {
2957
2958                 outb (FX_LSB_TRANSFER, dev.fx_lcr);
2959                 outb (page, dev.fx_dsp_page);
2960                 outb (addr, dev.fx_dsp_addr);
2961                 outb ((data[0] >> 8), dev.fx_dsp_msb);
2962                 outb ((data[0] & 0xff), dev.fx_dsp_lsb);
2963
2964                 printk (KERN_INFO LOGNAME "FX: addr %d:%x set to 0x%x\n",
2965                         page, addr, data[0]);
2966         
2967         } else {
2968                 int i;
2969
2970                 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
2971                 outb (page, dev.fx_dsp_page);
2972                 outb (addr, dev.fx_dsp_addr);
2973
2974                 for (i = 0; i < cnt; i++) {
2975                         outb ((data[i] >> 8), dev.fx_dsp_msb);
2976                         outb ((data[i] & 0xff), dev.fx_dsp_lsb);
2977                         if (!wffx_idle ()) {
2978                                 break;
2979                         }
2980                 }
2981
2982                 if (i != cnt) {
2983                         printk (KERN_WARNING LOGNAME
2984                                 "FX memset "
2985                                 "(0x%x, 0x%x, %p, %d) incomplete\n",
2986                                 page, addr, data, cnt);
2987                         return -(EIO);
2988                 }
2989         }
2990
2991         return 0;
2992 }
2993
2994 static int
2995 wffx_ioctl (wavefront_fx_info *r)
2996
2997 {
2998         unsigned short page_data[256];
2999         unsigned short *pd;
3000
3001         switch (r->request) {
3002         case WFFX_MUTE:
3003                 wffx_mute (r->data[0]);
3004                 return 0;
3005
3006         case WFFX_MEMSET:
3007
3008                 if (r->data[2] <= 0) {
3009                         printk (KERN_ERR LOGNAME "cannot write "
3010                                 "<= 0 bytes to FX\n");
3011                         return -(EINVAL);
3012                 } else if (r->data[2] == 1) {
3013                         pd = (unsigned short *) &r->data[3];
3014                 } else {
3015                         if (r->data[2] > sizeof (page_data)) {
3016                                 printk (KERN_ERR LOGNAME "cannot write "
3017                                         "> 255 bytes to FX\n");
3018                                 return -(EINVAL);
3019                         }
3020                         if (copy_from_user(page_data,
3021                                            (unsigned char __user *)r->data[3],
3022                                            r->data[2]))
3023                                 return -EFAULT;
3024                         pd = page_data;
3025                 }
3026
3027                 return wffx_memset (r->data[0], /* page */
3028                                     r->data[1], /* addr */
3029                                     r->data[2], /* cnt */
3030                                     pd);
3031
3032         default:
3033                 printk (KERN_WARNING LOGNAME
3034                         "FX: ioctl %d not yet supported\n",
3035                         r->request);
3036                 return -(EINVAL);
3037         }
3038 }
3039
3040 /* YSS225 initialization.
3041
3042    This code was developed using DOSEMU. The Turtle Beach SETUPSND
3043    utility was run with I/O tracing in DOSEMU enabled, and a reconstruction
3044    of the port I/O done, using the Yamaha faxback document as a guide
3045    to add more logic to the code. Its really pretty weird.
3046
3047    There was an alternative approach of just dumping the whole I/O
3048    sequence as a series of port/value pairs and a simple loop
3049    that output it. However, I hope that eventually I'll get more
3050    control over what this code does, and so I tried to stick with
3051    a somewhat "algorithmic" approach.
3052 */
3053
3054 static int __init wffx_init (void)
3055 {
3056         int i;
3057         int j;
3058
3059         /* Set all bits for all channels on the MOD unit to zero */
3060         /* XXX But why do this twice ? */
3061
3062         for (j = 0; j < 2; j++) {
3063                 for (i = 0x10; i <= 0xff; i++) {
3064             
3065                         if (!wffx_idle ()) {
3066                                 return (-1);
3067                         }
3068             
3069                         outb (i, dev.fx_mod_addr);
3070                         outb (0x0, dev.fx_mod_data);
3071                 }
3072         }
3073
3074         if (!wffx_idle()) return (-1);
3075         outb (0x02, dev.fx_op);                        /* mute on */
3076
3077         if (!wffx_idle()) return (-1);
3078         outb (0x07, dev.fx_dsp_page);
3079         outb (0x44, dev.fx_dsp_addr);
3080         outb (0x00, dev.fx_dsp_msb);
3081         outb (0x00, dev.fx_dsp_lsb);
3082         if (!wffx_idle()) return (-1);
3083         outb (0x07, dev.fx_dsp_page);
3084         outb (0x42, dev.fx_dsp_addr);
3085         outb (0x00, dev.fx_dsp_msb);
3086         outb (0x00, dev.fx_dsp_lsb);
3087         if (!wffx_idle()) return (-1);
3088         outb (0x07, dev.fx_dsp_page);
3089         outb (0x43, dev.fx_dsp_addr);
3090         outb (0x00, dev.fx_dsp_msb);
3091         outb (0x00, dev.fx_dsp_lsb);
3092         if (!wffx_idle()) return (-1);
3093         outb (0x07, dev.fx_dsp_page);
3094         outb (0x7c, dev.fx_dsp_addr);
3095         outb (0x00, dev.fx_dsp_msb);
3096         outb (0x00, dev.fx_dsp_lsb);
3097         if (!wffx_idle()) return (-1);
3098         outb (0x07, dev.fx_dsp_page);
3099         outb (0x7e, dev.fx_dsp_addr);
3100         outb (0x00, dev.fx_dsp_msb);
3101         outb (0x00, dev.fx_dsp_lsb);
3102         if (!wffx_idle()) return (-1);
3103         outb (0x07, dev.fx_dsp_page);
3104         outb (0x46, dev.fx_dsp_addr);
3105         outb (0x00, dev.fx_dsp_msb);
3106         outb (0x00, dev.fx_dsp_lsb);
3107         if (!wffx_idle()) return (-1);
3108         outb (0x07, dev.fx_dsp_page);
3109         outb (0x49, dev.fx_dsp_addr);
3110         outb (0x00, dev.fx_dsp_msb);
3111         outb (0x00, dev.fx_dsp_lsb);
3112         if (!wffx_idle()) return (-1);
3113         outb (0x07, dev.fx_dsp_page);
3114         outb (0x47, dev.fx_dsp_addr);
3115         outb (0x00, dev.fx_dsp_msb);
3116         outb (0x00, dev.fx_dsp_lsb);
3117         if (!wffx_idle()) return (-1);
3118         outb (0x07, dev.fx_dsp_page);
3119         outb (0x4a, dev.fx_dsp_addr);
3120         outb (0x00, dev.fx_dsp_msb);
3121         outb (0x00, dev.fx_dsp_lsb);
3122
3123         /* either because of stupidity by TB's programmers, or because it
3124            actually does something, rezero the MOD page.
3125         */
3126         for (i = 0x10; i <= 0xff; i++) {
3127         
3128                 if (!wffx_idle ()) {
3129                         return (-1);
3130                 }
3131         
3132                 outb (i, dev.fx_mod_addr);
3133                 outb (0x0, dev.fx_mod_data);
3134         }
3135         /* load page zero */
3136
3137         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3138         outb (0x00, dev.fx_dsp_page);
3139         outb (0x00, dev.fx_dsp_addr);
3140
3141         for (i = 0; i < sizeof (page_zero); i += 2) {
3142                 outb (page_zero[i], dev.fx_dsp_msb);
3143                 outb (page_zero[i+1], dev.fx_dsp_lsb);
3144                 if (!wffx_idle()) return (-1);
3145         }
3146
3147         /* Now load page one */
3148
3149         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3150         outb (0x01, dev.fx_dsp_page);
3151         outb (0x00, dev.fx_dsp_addr);
3152
3153         for (i = 0; i < sizeof (page_one); i += 2) {
3154                 outb (page_one[i], dev.fx_dsp_msb);
3155                 outb (page_one[i+1], dev.fx_dsp_lsb);
3156                 if (!wffx_idle()) return (-1);
3157         }
3158     
3159         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3160         outb (0x02, dev.fx_dsp_page);
3161         outb (0x00, dev.fx_dsp_addr);
3162
3163         for (i = 0; i < sizeof (page_two); i++) {
3164                 outb (page_two[i], dev.fx_dsp_lsb);
3165                 if (!wffx_idle()) return (-1);
3166         }
3167     
3168         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3169         outb (0x03, dev.fx_dsp_page);
3170         outb (0x00, dev.fx_dsp_addr);
3171
3172         for (i = 0; i < sizeof (page_three); i++) {
3173                 outb (page_three[i], dev.fx_dsp_lsb);
3174                 if (!wffx_idle()) return (-1);
3175         }
3176     
3177         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3178         outb (0x04, dev.fx_dsp_page);
3179         outb (0x00, dev.fx_dsp_addr);
3180
3181         for (i = 0; i < sizeof (page_four); i++) {
3182                 outb (page_four[i], dev.fx_dsp_lsb);
3183                 if (!wffx_idle()) return (-1);
3184         }
3185
3186         /* Load memory area (page six) */
3187     
3188         outb (FX_LSB_TRANSFER, dev.fx_lcr); 
3189         outb (0x06, dev.fx_dsp_page); 
3190
3191         for (i = 0; i < sizeof (page_six); i += 3) {
3192                 outb (page_six[i], dev.fx_dsp_addr);
3193                 outb (page_six[i+1], dev.fx_dsp_msb);
3194                 outb (page_six[i+2], dev.fx_dsp_lsb);
3195                 if (!wffx_idle()) return (-1);
3196         }
3197     
3198         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3199         outb (0x07, dev.fx_dsp_page);
3200         outb (0x00, dev.fx_dsp_addr);
3201
3202         for (i = 0; i < sizeof (page_seven); i += 2) {
3203                 outb (page_seven[i], dev.fx_dsp_msb);
3204                 outb (page_seven[i+1], dev.fx_dsp_lsb);
3205                 if (!wffx_idle()) return (-1);
3206         }
3207
3208         /* Now setup the MOD area. We do this algorithmically in order to
3209            save a little data space. It could be done in the same fashion
3210            as the "pages".
3211         */
3212
3213         for (i = 0x00; i <= 0x0f; i++) {
3214                 outb (0x01, dev.fx_mod_addr);
3215                 outb (i, dev.fx_mod_data);
3216                 if (!wffx_idle()) return (-1);
3217                 outb (0x02, dev.fx_mod_addr);
3218                 outb (0x00, dev.fx_mod_data);
3219                 if (!wffx_idle()) return (-1);
3220         }
3221
3222         for (i = 0xb0; i <= 0xbf; i++) {
3223                 outb (i, dev.fx_mod_addr);
3224                 outb (0x20, dev.fx_mod_data);
3225                 if (!wffx_idle()) return (-1);
3226         }
3227
3228         for (i = 0xf0; i <= 0xff; i++) {
3229                 outb (i, dev.fx_mod_addr);
3230                 outb (0x20, dev.fx_mod_data);
3231                 if (!wffx_idle()) return (-1);
3232         }
3233
3234         for (i = 0x10; i <= 0x1d; i++) {
3235                 outb (i, dev.fx_mod_addr);
3236                 outb (0xff, dev.fx_mod_data);
3237                 if (!wffx_idle()) return (-1);
3238         }
3239
3240         outb (0x1e, dev.fx_mod_addr);
3241         outb (0x40, dev.fx_mod_data);
3242         if (!wffx_idle()) return (-1);
3243
3244         for (i = 0x1f; i <= 0x2d; i++) {
3245                 outb (i, dev.fx_mod_addr);
3246                 outb (0xff, dev.fx_mod_data);
3247                 if (!wffx_idle()) return (-1);
3248         }
3249
3250         outb (0x2e, dev.fx_mod_addr);
3251         outb (0x00, dev.fx_mod_data);
3252         if (!wffx_idle()) return (-1);
3253
3254         for (i = 0x2f; i <= 0x3e; i++) {
3255                 outb (i, dev.fx_mod_addr);
3256                 outb (0x00, dev.fx_mod_data);
3257                 if (!wffx_idle()) return (-1);
3258         }
3259
3260         outb (0x3f, dev.fx_mod_addr);
3261         outb (0x20, dev.fx_mod_data);
3262         if (!wffx_idle()) return (-1);
3263
3264         for (i = 0x40; i <= 0x4d; i++) {
3265                 outb (i, dev.fx_mod_addr);
3266                 outb (0x00, dev.fx_mod_data);
3267                 if (!wffx_idle()) return (-1);
3268         }
3269
3270         outb (0x4e, dev.fx_mod_addr);
3271         outb (0x0e, dev.fx_mod_data);
3272         if (!wffx_idle()) return (-1);
3273         outb (0x4f, dev.fx_mod_addr);
3274         outb (0x0e, dev.fx_mod_data);
3275         if (!wffx_idle()) return (-1);
3276
3277
3278         for (i = 0x50; i <= 0x6b; i++) {
3279                 outb (i, dev.fx_mod_addr);
3280                 outb (0x00, dev.fx_mod_data);
3281                 if (!wffx_idle()) return (-1);
3282         }
3283
3284         outb (0x6c, dev.fx_mod_addr);
3285         outb (0x40, dev.fx_mod_data);
3286         if (!wffx_idle()) return (-1);
3287
3288         outb (0x6d, dev.fx_mod_addr);
3289         outb (0x00, dev.fx_mod_data);
3290         if (!wffx_idle()) return (-1);
3291
3292         outb (0x6e, dev.fx_mod_addr);
3293         outb (0x40, dev.fx_mod_data);
3294         if (!wffx_idle()) return (-1);
3295
3296         outb (0x6f, dev.fx_mod_addr);
3297         outb (0x40, dev.fx_mod_data);
3298         if (!wffx_idle()) return (-1);
3299
3300         for (i = 0x70; i <= 0x7f; i++) {
3301                 outb (i, dev.fx_mod_addr);
3302                 outb (0xc0, dev.fx_mod_data);
3303                 if (!wffx_idle()) return (-1);
3304         }
3305     
3306         for (i = 0x80; i <= 0xaf; i++) {
3307                 outb (i, dev.fx_mod_addr);
3308                 outb (0x00, dev.fx_mod_data);
3309                 if (!wffx_idle()) return (-1);
3310         }
3311
3312         for (i = 0xc0; i <= 0xdd; i++) {
3313                 outb (i, dev.fx_mod_addr);
3314                 outb (0x00, dev.fx_mod_data);
3315                 if (!wffx_idle()) return (-1);
3316         }
3317
3318         outb (0xde, dev.fx_mod_addr);
3319         outb (0x10, dev.fx_mod_data);
3320         if (!wffx_idle()) return (-1);
3321         outb (0xdf, dev.fx_mod_addr);
3322         outb (0x10, dev.fx_mod_data);
3323         if (!wffx_idle()) return (-1);
3324
3325         for (i = 0xe0; i <= 0xef; i++) {
3326                 outb (i, dev.fx_mod_addr);
3327                 outb (0x00, dev.fx_mod_data);
3328                 if (!wffx_idle()) return (-1);
3329         }
3330
3331         for (i = 0x00; i <= 0x0f; i++) {
3332                 outb (0x01, dev.fx_mod_addr);
3333                 outb (i, dev.fx_mod_data);
3334                 outb (0x02, dev.fx_mod_addr);
3335                 outb (0x01, dev.fx_mod_data);
3336                 if (!wffx_idle()) return (-1);
3337         }
3338
3339         outb (0x02, dev.fx_op); /* mute on */
3340
3341         /* Now set the coefficients and so forth for the programs above */
3342
3343         for (i = 0; i < sizeof (coefficients); i += 4) {
3344                 outb (coefficients[i], dev.fx_dsp_page);
3345                 outb (coefficients[i+1], dev.fx_dsp_addr);
3346                 outb (coefficients[i+2], dev.fx_dsp_msb);
3347                 outb (coefficients[i+3], dev.fx_dsp_lsb);
3348                 if (!wffx_idle()) return (-1);
3349         }
3350
3351         /* Some settings (?) that are too small to bundle into loops */
3352
3353         if (!wffx_idle()) return (-1);
3354         outb (0x1e, dev.fx_mod_addr);
3355         outb (0x14, dev.fx_mod_data);
3356         if (!wffx_idle()) return (-1);
3357         outb (0xde, dev.fx_mod_addr);
3358         outb (0x20, dev.fx_mod_data);
3359         if (!wffx_idle()) return (-1);
3360         outb (0xdf, dev.fx_mod_addr);
3361         outb (0x20, dev.fx_mod_data);
3362     
3363         /* some more coefficients */
3364
3365         if (!wffx_idle()) return (-1);
3366         outb (0x06, dev.fx_dsp_page);
3367         outb (0x78, dev.fx_dsp_addr);
3368         outb (0x00, dev.fx_dsp_msb);
3369         outb (0x40, dev.fx_dsp_lsb);
3370         if (!wffx_idle()) return (-1);
3371         outb (0x07, dev.fx_dsp_page);
3372         outb (0x03, dev.fx_dsp_addr);
3373         outb (0x0f, dev.fx_dsp_msb);
3374         outb (0xff, dev.fx_dsp_lsb);
3375         if (!wffx_idle()) return (-1);
3376         outb (0x07, dev.fx_dsp_page);
3377         outb (0x0b, dev.fx_dsp_addr);
3378         outb (0x0f, dev.fx_dsp_msb);
3379         outb (0xff, dev.fx_dsp_lsb);
3380         if (!wffx_idle()) return (-1);
3381         outb (0x07, dev.fx_dsp_page);
3382         outb (0x02, dev.fx_dsp_addr);
3383         outb (0x00, dev.fx_dsp_msb);
3384         outb (0x00, dev.fx_dsp_lsb);
3385         if (!wffx_idle()) return (-1);
3386         outb (0x07, dev.fx_dsp_page);
3387         outb (0x0a, dev.fx_dsp_addr);
3388         outb (0x00, dev.fx_dsp_msb);
3389         outb (0x00, dev.fx_dsp_lsb);
3390         if (!wffx_idle()) return (-1);
3391         outb (0x07, dev.fx_dsp_page);
3392         outb (0x46, dev.fx_dsp_addr);
3393         outb (0x00, dev.fx_dsp_msb);
3394         outb (0x00, dev.fx_dsp_lsb);
3395         if (!wffx_idle()) return (-1);
3396         outb (0x07, dev.fx_dsp_page);
3397         outb (0x49, dev.fx_dsp_addr);
3398         outb (0x00, dev.fx_dsp_msb);
3399         outb (0x00, dev.fx_dsp_lsb);
3400     
3401         /* Now, for some strange reason, lets reload every page
3402            and all the coefficients over again. I have *NO* idea
3403            why this is done. I do know that no sound is produced
3404            is this phase is omitted.
3405         */
3406
3407         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3408         outb (0x00, dev.fx_dsp_page);  
3409         outb (0x10, dev.fx_dsp_addr);
3410
3411         for (i = 0; i < sizeof (page_zero_v2); i += 2) {
3412                 outb (page_zero_v2[i], dev.fx_dsp_msb);
3413                 outb (page_zero_v2[i+1], dev.fx_dsp_lsb);
3414                 if (!wffx_idle()) return (-1);
3415         }
3416     
3417         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3418         outb (0x01, dev.fx_dsp_page);
3419         outb (0x10, dev.fx_dsp_addr);
3420
3421         for (i = 0; i < sizeof (page_one_v2); i += 2) {
3422                 outb (page_one_v2[i], dev.fx_dsp_msb);
3423                 outb (page_one_v2[i+1], dev.fx_dsp_lsb);
3424                 if (!wffx_idle()) return (-1);
3425         }
3426     
3427         if (!wffx_idle()) return (-1);
3428         if (!wffx_idle()) return (-1);
3429     
3430         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3431         outb (0x02, dev.fx_dsp_page);
3432         outb (0x10, dev.fx_dsp_addr);
3433
3434         for (i = 0; i < sizeof (page_two_v2); i++) {
3435                 outb (page_two_v2[i], dev.fx_dsp_lsb);
3436                 if (!wffx_idle()) return (-1);
3437         }
3438         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3439         outb (0x03, dev.fx_dsp_page);
3440         outb (0x10, dev.fx_dsp_addr);
3441
3442         for (i = 0; i < sizeof (page_three_v2); i++) {
3443                 outb (page_three_v2[i], dev.fx_dsp_lsb);
3444                 if (!wffx_idle()) return (-1);
3445         }
3446     
3447         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3448         outb (0x04, dev.fx_dsp_page);
3449         outb (0x10, dev.fx_dsp_addr);
3450
3451         for (i = 0; i < sizeof (page_four_v2); i++) {
3452                 outb (page_four_v2[i], dev.fx_dsp_lsb);
3453                 if (!wffx_idle()) return (-1);
3454         }
3455     
3456         outb (FX_LSB_TRANSFER, dev.fx_lcr);
3457         outb (0x06, dev.fx_dsp_page);
3458
3459         /* Page six v.2 is algorithmic */
3460     
3461         for (i = 0x10; i <= 0x3e; i += 2) {
3462                 outb (i, dev.fx_dsp_addr);
3463                 outb (0x00, dev.fx_dsp_msb);
3464                 outb (0x00, dev.fx_dsp_lsb);
3465                 if (!wffx_idle()) return (-1);
3466         }
3467
3468         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3469         outb (0x07, dev.fx_dsp_page);
3470         outb (0x10, dev.fx_dsp_addr);
3471
3472         for (i = 0; i < sizeof (page_seven_v2); i += 2) {
3473                 outb (page_seven_v2[i], dev.fx_dsp_msb);
3474                 outb (page_seven_v2[i+1], dev.fx_dsp_lsb);
3475                 if (!wffx_idle()) return (-1);
3476         }
3477
3478         for (i = 0x00; i < sizeof(mod_v2); i += 2) {
3479                 outb (mod_v2[i], dev.fx_mod_addr);
3480                 outb (mod_v2[i+1], dev.fx_mod_data);
3481                 if (!wffx_idle()) return (-1);
3482         }
3483
3484         for (i = 0; i < sizeof (coefficients2); i += 4) {
3485                 outb (coefficients2[i], dev.fx_dsp_page);
3486                 outb (coefficients2[i+1], dev.fx_dsp_addr);
3487                 outb (coefficients2[i+2], dev.fx_dsp_msb);
3488                 outb (coefficients2[i+3], dev.fx_dsp_lsb);
3489                 if (!wffx_idle()) return (-1);
3490         }
3491
3492         for (i = 0; i < sizeof (coefficients3); i += 2) {
3493                 int x;
3494
3495                 outb (0x07, dev.fx_dsp_page);
3496                 x = (i % 4) ? 0x4e : 0x4c;
3497                 outb (x, dev.fx_dsp_addr);
3498                 outb (coefficients3[i], dev.fx_dsp_msb);
3499                 outb (coefficients3[i+1], dev.fx_dsp_lsb);
3500         }
3501
3502         outb (0x00, dev.fx_op); /* mute off */
3503         if (!wffx_idle()) return (-1);
3504
3505         return (0);
3506 }
3507
3508 static int io = -1;
3509 static int irq = -1;
3510
3511 MODULE_AUTHOR      ("Paul Barton-Davis <pbd@op.net>");
3512 MODULE_DESCRIPTION ("Turtle Beach WaveFront Linux Driver");
3513 MODULE_LICENSE("GPL");
3514 module_param       (io, int, 0);
3515 module_param       (irq, int, 0);
3516
3517 static int __init init_wavfront (void)
3518 {
3519         printk ("Turtle Beach WaveFront Driver\n"
3520                 "Copyright (C) by Hannu Solvainen, "
3521                 "Paul Barton-Davis 1993-1998.\n");
3522
3523         /* XXX t'would be lovely to ask the CS4232 for these values, eh ? */
3524
3525         if (io == -1 || irq == -1) {
3526                 printk (KERN_INFO LOGNAME "irq and io options must be set.\n");
3527                 return -EINVAL;
3528         }
3529
3530         if (wavefront_interrupt_bits (irq) < 0) {
3531                 printk (KERN_INFO LOGNAME
3532                         "IRQ must be 9, 5, 12 or 15 (not %d)\n", irq);
3533                 return -ENODEV;
3534         }
3535
3536         if (detect_wavefront (irq, io) < 0) {
3537                 return -ENODEV;
3538         } 
3539
3540         if (install_wavefront () < 0) {
3541                 return -EIO;
3542         }
3543
3544         return 0;
3545 }
3546
3547 static void __exit cleanup_wavfront (void)
3548 {
3549         uninstall_wavefront ();
3550 }
3551
3552 module_init(init_wavfront);
3553 module_exit(cleanup_wavfront);