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