patch-2_6_7-vs1_9_1_12
[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 __user *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 __user *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 __user *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 %p\n",
1028                                       header->size ? "" : "header ", 
1029                                       header->number, header->subkey,
1030                                       header->size,
1031                                       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 __user *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 __user *) &(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 __user *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 __user *) 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(&header.hdr.s, header.hdrptr,
1692                                    sizeof(wavefront_multisample)))
1693                         return -EFAULT;
1694
1695                 return wavefront_send_multisample (&header);
1696
1697
1698         case WF_ST_ALIAS:
1699
1700                 if (copy_from_user(&header.hdr.a, header.hdrptr,
1701                                    sizeof (wavefront_alias)))
1702                         return -EFAULT;
1703
1704                 return wavefront_send_alias (&header);
1705
1706         case WF_ST_DRUM:
1707                 if (copy_from_user(&header.hdr.d, header.hdrptr,
1708                                    sizeof (wavefront_drum)))
1709                         return -EFAULT;
1710
1711                 return wavefront_send_drum (&header);
1712
1713         case WF_ST_PATCH:
1714                 if (copy_from_user(&header.hdr.p, header.hdrptr,
1715                                    sizeof (wavefront_patch)))
1716                         return -EFAULT;
1717
1718                 return wavefront_send_patch (&header);
1719
1720         case WF_ST_PROGRAM:
1721                 if (copy_from_user(&header.hdr.pr, header.hdrptr,
1722                                    sizeof (wavefront_program)))
1723                         return -EFAULT;
1724
1725                 return wavefront_send_program (&header);
1726
1727         default:
1728                 printk (KERN_ERR LOGNAME "unknown patch type %d.\n",
1729                         header.subkey);
1730                 return -(EINVAL);
1731         }
1732
1733         return 0;
1734 }
1735 \f
1736 /***********************************************************************
1737 WaveFront: /dev/sequencer{,2} and other hardware-dependent interfaces
1738 ***********************************************************************/
1739
1740 static void
1741 process_sample_hdr (UCHAR8 *buf)
1742
1743 {
1744         wavefront_sample s;
1745         UCHAR8 *ptr;
1746
1747         ptr = buf;
1748
1749         /* The board doesn't send us an exact copy of a "wavefront_sample"
1750            in response to an Upload Sample Header command. Instead, we 
1751            have to convert the data format back into our data structure,
1752            just as in the Download Sample command, where we have to do
1753            something very similar in the reverse direction.
1754         */
1755
1756         *((UINT32 *) &s.sampleStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
1757         *((UINT32 *) &s.loopStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
1758         *((UINT32 *) &s.loopEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
1759         *((UINT32 *) &s.sampleEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
1760         *((UINT32 *) &s.FrequencyBias) = demunge_int32 (ptr, 3); ptr += 3;
1761
1762         s.SampleResolution = *ptr & 0x3;
1763         s.Loop = *ptr & 0x8;
1764         s.Bidirectional = *ptr & 0x10;
1765         s.Reverse = *ptr & 0x40;
1766
1767         /* Now copy it back to where it came from */
1768
1769         memcpy (buf, (unsigned char *) &s, sizeof (wavefront_sample));
1770 }
1771
1772 static int
1773 wavefront_synth_control (int cmd, wavefront_control *wc)
1774
1775 {
1776         unsigned char patchnumbuf[2];
1777         int i;
1778
1779         DPRINT (WF_DEBUG_CMD, "synth control with "
1780                 "cmd 0x%x\n", wc->cmd);
1781
1782         /* Pre-handling of or for various commands */
1783
1784         switch (wc->cmd) {
1785         case WFC_DISABLE_INTERRUPTS:
1786                 printk (KERN_INFO LOGNAME "interrupts disabled.\n");
1787                 outb (0x80|0x20, dev.control_port);
1788                 dev.interrupts_on = 0;
1789                 return 0;
1790
1791         case WFC_ENABLE_INTERRUPTS:
1792                 printk (KERN_INFO LOGNAME "interrupts enabled.\n");
1793                 outb (0x80|0x40|0x20, dev.control_port);
1794                 dev.interrupts_on = 1;
1795                 return 0;
1796
1797         case WFC_INTERRUPT_STATUS:
1798                 wc->rbuf[0] = dev.interrupts_on;
1799                 return 0;
1800
1801         case WFC_ROMSAMPLES_RDONLY:
1802                 dev.rom_samples_rdonly = wc->wbuf[0];
1803                 wc->status = 0;
1804                 return 0;
1805
1806         case WFC_IDENTIFY_SLOT_TYPE:
1807                 i = wc->wbuf[0] | (wc->wbuf[1] << 7);
1808                 if (i <0 || i >= WF_MAX_SAMPLE) {
1809                         printk (KERN_WARNING LOGNAME "invalid slot ID %d\n",
1810                                 i);
1811                         wc->status = EINVAL;
1812                         return 0;
1813                 }
1814                 wc->rbuf[0] = dev.sample_status[i];
1815                 wc->status = 0;
1816                 return 0;
1817
1818         case WFC_DEBUG_DRIVER:
1819                 dev.debug = wc->wbuf[0];
1820                 printk (KERN_INFO LOGNAME "debug = 0x%x\n", dev.debug);
1821                 return 0;
1822
1823         case WFC_FX_IOCTL:
1824                 wffx_ioctl ((wavefront_fx_info *) &wc->wbuf[0]);
1825                 return 0;
1826
1827         case WFC_UPLOAD_PATCH:
1828                 munge_int32 (*((UINT32 *) wc->wbuf), patchnumbuf, 2);
1829                 memcpy (wc->wbuf, patchnumbuf, 2);
1830                 break;
1831
1832         case WFC_UPLOAD_MULTISAMPLE:
1833                 /* multisamples have to be handled differently, and
1834                    cannot be dealt with properly by wavefront_cmd() alone.
1835                 */
1836                 wc->status = wavefront_fetch_multisample
1837                         ((wavefront_patch_info *) wc->rbuf);
1838                 return 0;
1839
1840         case WFC_UPLOAD_SAMPLE_ALIAS:
1841                 printk (KERN_INFO LOGNAME "support for sample alias upload "
1842                         "being considered.\n");
1843                 wc->status = EINVAL;
1844                 return -EINVAL;
1845         }
1846
1847         wc->status = wavefront_cmd (wc->cmd, wc->rbuf, wc->wbuf);
1848
1849         /* Post-handling of certain commands.
1850
1851            In particular, if the command was an upload, demunge the data
1852            so that the user-level doesn't have to think about it.
1853         */
1854
1855         if (wc->status == 0) {
1856                 switch (wc->cmd) {
1857                         /* intercept any freemem requests so that we know
1858                            we are always current with the user-level view
1859                            of things.
1860                         */
1861
1862                 case WFC_REPORT_FREE_MEMORY:
1863                         dev.freemem = demunge_int32 (wc->rbuf, 4);
1864                         break;
1865
1866                 case WFC_UPLOAD_PATCH:
1867                         demunge_buf (wc->rbuf, wc->rbuf, WF_PATCH_BYTES);
1868                         break;
1869
1870                 case WFC_UPLOAD_PROGRAM:
1871                         demunge_buf (wc->rbuf, wc->rbuf, WF_PROGRAM_BYTES);
1872                         break;
1873
1874                 case WFC_UPLOAD_EDRUM_PROGRAM:
1875                         demunge_buf (wc->rbuf, wc->rbuf, WF_DRUM_BYTES - 1);
1876                         break;
1877
1878                 case WFC_UPLOAD_SAMPLE_HEADER:
1879                         process_sample_hdr (wc->rbuf);
1880                         break;
1881
1882                 case WFC_UPLOAD_SAMPLE_ALIAS:
1883                         printk (KERN_INFO LOGNAME "support for "
1884                                 "sample aliases still "
1885                                 "being considered.\n");
1886                         break;
1887
1888                 case WFC_VMIDI_OFF:
1889                         if (virtual_midi_disable () < 0) {
1890                                 return -(EIO);
1891                         }
1892                         break;
1893
1894                 case WFC_VMIDI_ON:
1895                         if (virtual_midi_enable () < 0) {
1896                                 return -(EIO);
1897                         }
1898                         break;
1899                 }
1900         }
1901
1902         return 0;
1903 }
1904
1905 \f
1906 /***********************************************************************/
1907 /* WaveFront: Linux file system interface (for access via raw synth)    */
1908 /***********************************************************************/
1909
1910 static int 
1911 wavefront_open (struct inode *inode, struct file *file)
1912 {
1913         /* XXX fix me */
1914         dev.opened = file->f_flags;
1915         return 0;
1916 }
1917
1918 static int
1919 wavefront_release(struct inode *inode, struct file *file)
1920 {
1921         lock_kernel();
1922         dev.opened = 0;
1923         dev.debug = 0;
1924         unlock_kernel();
1925         return 0;
1926 }
1927
1928 static int
1929 wavefront_ioctl(struct inode *inode, struct file *file,
1930                 unsigned int cmd, unsigned long arg)
1931 {
1932         wavefront_control wc;
1933         int err;
1934
1935         switch (cmd) {
1936
1937         case WFCTL_WFCMD:
1938                 if (copy_from_user(&wc, (void __user *) arg, sizeof (wc)))
1939                         return -EFAULT;
1940                 
1941                 if ((err = wavefront_synth_control (cmd, &wc)) == 0) {
1942                         if (copy_to_user ((void __user *) arg, &wc, sizeof (wc)))
1943                                 return -EFAULT;
1944                 }
1945
1946                 return err;
1947                 
1948         case WFCTL_LOAD_SPP:
1949                 return wavefront_load_patch ((const char __user *) arg);
1950                 
1951         default:
1952                 printk (KERN_WARNING LOGNAME "invalid ioctl %#x\n", cmd);
1953                 return -(EINVAL);
1954
1955         }
1956         return 0;
1957 }
1958
1959 static /*const*/ struct file_operations wavefront_fops = {
1960         .owner          = THIS_MODULE,
1961         .llseek         = no_llseek,
1962         .ioctl          = wavefront_ioctl,
1963         .open           = wavefront_open,
1964         .release        = wavefront_release,
1965 };
1966
1967 \f
1968 /***********************************************************************/
1969 /* WaveFront: OSS installation and support interface                   */
1970 /***********************************************************************/
1971
1972 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
1973
1974 static struct synth_info wavefront_info =
1975 {"Turtle Beach WaveFront", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_WAVEFRONT,
1976  0, 32, 0, 0, SYNTH_CAP_INPUT};
1977
1978 static int
1979 wavefront_oss_open (int devno, int mode)
1980
1981 {
1982         dev.opened = mode;
1983         return 0;
1984 }
1985
1986 static void
1987 wavefront_oss_close (int devno)
1988
1989 {
1990         dev.opened = 0;
1991         dev.debug = 0;
1992         return;
1993 }
1994
1995 static int
1996 wavefront_oss_ioctl (int devno, unsigned int cmd, void __user * arg)
1997
1998 {
1999         wavefront_control wc;
2000         int err;
2001
2002         switch (cmd) {
2003         case SNDCTL_SYNTH_INFO:
2004                 if(copy_to_user(arg, &wavefront_info, sizeof (wavefront_info)))
2005                         return -EFAULT;
2006                 return 0;
2007
2008         case SNDCTL_SEQ_RESETSAMPLES:
2009 //              printk (KERN_WARNING LOGNAME "driver cannot reset samples.\n");
2010                 return 0; /* don't force an error */
2011
2012         case SNDCTL_SEQ_PERCMODE:
2013                 return 0; /* don't force an error */
2014
2015         case SNDCTL_SYNTH_MEMAVL:
2016                 if ((dev.freemem = wavefront_freemem ()) < 0) {
2017                         printk (KERN_ERR LOGNAME "cannot get memory size\n");
2018                         return -EIO;
2019                 } else {
2020                         return dev.freemem;
2021                 }
2022                 break;
2023
2024         case SNDCTL_SYNTH_CONTROL:
2025                 if(copy_from_user (&wc, arg, sizeof (wc)))
2026                         err = -EFAULT;
2027                 else if ((err = wavefront_synth_control (cmd, &wc)) == 0) {
2028                         if(copy_to_user (arg, &wc, sizeof (wc)))
2029                                 err = -EFAULT;
2030                 }
2031
2032                 return err;
2033
2034         default:
2035                 return -(EINVAL);
2036         }
2037 }
2038
2039 int
2040 wavefront_oss_load_patch (int devno, int format, const char __user *addr,
2041                           int offs, int count, int pmgr_flag)
2042 {
2043
2044         if (format == SYSEX_PATCH) {    /* Handled by midi_synth.c */
2045                 if (midi_load_patch == NULL) {
2046                         printk (KERN_ERR LOGNAME
2047                                 "SYSEX not loadable: "
2048                                 "no midi patch loader!\n");
2049                         return -(EINVAL);
2050                 }
2051
2052                 return midi_load_patch (devno, format, addr,
2053                                         offs, count, pmgr_flag);
2054
2055         } else if (format == GUS_PATCH) {
2056                 return wavefront_load_gus_patch (devno, format,
2057                                                  addr, offs, count, pmgr_flag);
2058
2059         } else if (format != WAVEFRONT_PATCH) {
2060                 printk (KERN_ERR LOGNAME "unknown patch format %d\n", format);
2061                 return -(EINVAL);
2062         }
2063
2064         if (count < sizeof (wavefront_patch_info)) {
2065                 printk (KERN_ERR LOGNAME "sample header too short\n");
2066                 return -(EINVAL);
2067         }
2068
2069         /* "addr" points to a user-space wavefront_patch_info */
2070
2071         return wavefront_load_patch (addr);
2072 }       
2073
2074 static struct synth_operations wavefront_operations =
2075 {
2076         .owner          = THIS_MODULE,
2077         .id             = "WaveFront",
2078         .info           = &wavefront_info,
2079         .midi_dev       = 0,
2080         .synth_type     = SYNTH_TYPE_SAMPLE,
2081         .synth_subtype  = SAMPLE_TYPE_WAVEFRONT,
2082         .open           = wavefront_oss_open,
2083         .close          = wavefront_oss_close,
2084         .ioctl          = wavefront_oss_ioctl,
2085         .kill_note      = midi_synth_kill_note,
2086         .start_note     = midi_synth_start_note,
2087         .set_instr      = midi_synth_set_instr,
2088         .reset          = midi_synth_reset,
2089         .load_patch     = midi_synth_load_patch,
2090         .aftertouch     = midi_synth_aftertouch,
2091         .controller     = midi_synth_controller,
2092         .panning        = midi_synth_panning,
2093         .bender         = midi_synth_bender,
2094         .setup_voice    = midi_synth_setup_voice
2095 };
2096 #endif /* OSS_SUPPORT_SEQ */
2097
2098 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_STATIC_INSTALL
2099
2100 static void __init attach_wavefront (struct address_info *hw_config)
2101 {
2102     (void) install_wavefront ();
2103 }
2104
2105 static int __init probe_wavefront (struct address_info *hw_config)
2106 {
2107     return !detect_wavefront (hw_config->irq, hw_config->io_base);
2108 }
2109
2110 static void __exit unload_wavefront (struct address_info *hw_config) 
2111 {
2112     (void) uninstall_wavefront ();
2113 }
2114
2115 #endif /* OSS_SUPPORT_STATIC_INSTALL */
2116
2117 /***********************************************************************/
2118 /* WaveFront: Linux modular sound kernel installation interface        */
2119 /***********************************************************************/
2120
2121 static irqreturn_t
2122 wavefrontintr(int irq, void *dev_id, struct pt_regs *dummy)
2123 {
2124         struct wf_config *hw = dev_id;
2125
2126         /*
2127            Some comments on interrupts. I attempted a version of this
2128            driver that used interrupts throughout the code instead of
2129            doing busy and/or sleep-waiting. Alas, it appears that once
2130            the Motorola firmware is downloaded, the card *never*
2131            generates an RX interrupt. These are successfully generated
2132            during firmware loading, and after that wavefront_status()
2133            reports that an interrupt is pending on the card from time
2134            to time, but it never seems to be delivered to this
2135            driver. Note also that wavefront_status() continues to
2136            report that RX interrupts are enabled, suggesting that I
2137            didn't goof up and disable them by mistake.
2138
2139            Thus, I stepped back to a prior version of
2140            wavefront_wait(), the only place where this really
2141            matters. Its sad, but I've looked through the code to check
2142            on things, and I really feel certain that the Motorola
2143            firmware prevents RX-ready interrupts.
2144         */
2145
2146         if ((wavefront_status() & (STAT_INTR_READ|STAT_INTR_WRITE)) == 0) {
2147                 return IRQ_NONE;
2148         }
2149
2150         hw->irq_ok = 1;
2151         hw->irq_cnt++;
2152         wake_up_interruptible (&hw->interrupt_sleeper);
2153         return IRQ_HANDLED;
2154 }
2155
2156 /* STATUS REGISTER 
2157
2158 0 Host Rx Interrupt Enable (1=Enabled)
2159 1 Host Rx Register Full (1=Full)
2160 2 Host Rx Interrupt Pending (1=Interrupt)
2161 3 Unused
2162 4 Host Tx Interrupt (1=Enabled)
2163 5 Host Tx Register empty (1=Empty)
2164 6 Host Tx Interrupt Pending (1=Interrupt)
2165 7 Unused
2166 */
2167
2168 int
2169 wavefront_interrupt_bits (int irq)
2170
2171 {
2172         int bits;
2173
2174         switch (irq) {
2175         case 9:
2176                 bits = 0x00;
2177                 break;
2178         case 5:
2179                 bits = 0x08;
2180                 break;
2181         case 12:
2182                 bits = 0x10;
2183                 break;
2184         case 15:
2185                 bits = 0x18;
2186                 break;
2187         
2188         default:
2189                 printk (KERN_WARNING LOGNAME "invalid IRQ %d\n", irq);
2190                 bits = -1;
2191         }
2192
2193         return bits;
2194 }
2195
2196 void
2197 wavefront_should_cause_interrupt (int val, int port, int timeout)
2198
2199 {
2200         unsigned long flags;
2201
2202         /* this will not help on SMP - but at least it compiles */
2203         spin_lock_irqsave(&lock, flags);
2204         dev.irq_ok = 0;
2205         outb (val,port);
2206         interruptible_sleep_on_timeout (&dev.interrupt_sleeper, timeout);
2207         spin_unlock_irqrestore(&lock,flags);
2208 }
2209
2210 static int __init wavefront_hw_reset (void)
2211 {
2212         int bits;
2213         int hwv[2];
2214         unsigned long irq_mask;
2215         short reported_irq;
2216
2217         /* IRQ already checked in init_module() */
2218
2219         bits = wavefront_interrupt_bits (dev.irq);
2220
2221         printk (KERN_DEBUG LOGNAME "autodetecting WaveFront IRQ\n");
2222
2223         irq_mask = probe_irq_on ();
2224
2225         outb (0x0, dev.control_port); 
2226         outb (0x80 | 0x40 | bits, dev.data_port);       
2227         wavefront_should_cause_interrupt(0x80|0x40|0x10|0x1,
2228                                          dev.control_port,
2229                                          (reset_time*HZ)/100);
2230
2231         reported_irq = probe_irq_off (irq_mask);
2232
2233         if (reported_irq != dev.irq) {
2234                 if (reported_irq == 0) {
2235                         printk (KERN_ERR LOGNAME
2236                                 "No unassigned interrupts detected "
2237                                 "after h/w reset\n");
2238                 } else if (reported_irq < 0) {
2239                         printk (KERN_ERR LOGNAME
2240                                 "Multiple unassigned interrupts detected "
2241                                 "after h/w reset\n");
2242                 } else {
2243                         printk (KERN_ERR LOGNAME "autodetected IRQ %d not the "
2244                                 "value provided (%d)\n", reported_irq,
2245                                 dev.irq);
2246                 }
2247                 dev.irq = -1;
2248                 return 1;
2249         } else {
2250                 printk (KERN_INFO LOGNAME "autodetected IRQ at %d\n",
2251                         reported_irq);
2252         }
2253
2254         if (request_irq (dev.irq, wavefrontintr,
2255                          SA_INTERRUPT|SA_SHIRQ,
2256                          "wavefront synth", &dev) < 0) {
2257                 printk (KERN_WARNING LOGNAME "IRQ %d not available!\n",
2258                         dev.irq);
2259                 return 1;
2260         }
2261
2262         /* try reset of port */
2263       
2264         outb (0x0, dev.control_port); 
2265   
2266         /* At this point, the board is in reset, and the H/W initialization
2267            register is accessed at the same address as the data port.
2268      
2269            Bit 7 - Enable IRQ Driver    
2270            0 - Tri-state the Wave-Board drivers for the PC Bus IRQs
2271            1 - Enable IRQ selected by bits 5:3 to be driven onto the PC Bus.
2272      
2273            Bit 6 - MIDI Interface Select
2274
2275            0 - Use the MIDI Input from the 26-pin WaveBlaster
2276            compatible header as the serial MIDI source
2277            1 - Use the MIDI Input from the 9-pin D connector as the
2278            serial MIDI source.
2279      
2280            Bits 5:3 - IRQ Selection
2281            0 0 0 - IRQ 2/9
2282            0 0 1 - IRQ 5
2283            0 1 0 - IRQ 12
2284            0 1 1 - IRQ 15
2285            1 0 0 - Reserved
2286            1 0 1 - Reserved
2287            1 1 0 - Reserved
2288            1 1 1 - Reserved
2289      
2290            Bits 2:1 - Reserved
2291            Bit 0 - Disable Boot ROM
2292            0 - memory accesses to 03FC30-03FFFFH utilize the internal Boot ROM
2293            1 - memory accesses to 03FC30-03FFFFH are directed to external 
2294            storage.
2295      
2296         */
2297
2298         /* configure hardware: IRQ, enable interrupts, 
2299            plus external 9-pin MIDI interface selected
2300         */
2301
2302         outb (0x80 | 0x40 | bits, dev.data_port);       
2303   
2304         /* CONTROL REGISTER
2305
2306            0 Host Rx Interrupt Enable (1=Enabled)      0x1
2307            1 Unused                                    0x2
2308            2 Unused                                    0x4
2309            3 Unused                                    0x8
2310            4 Host Tx Interrupt Enable                 0x10
2311            5 Mute (0=Mute; 1=Play)                    0x20
2312            6 Master Interrupt Enable (1=Enabled)      0x40
2313            7 Master Reset (0=Reset; 1=Run)            0x80
2314
2315            Take us out of reset, mute output, master + TX + RX interrupts on.
2316            
2317            We'll get an interrupt presumably to tell us that the TX
2318            register is clear.
2319         */
2320
2321         wavefront_should_cause_interrupt(0x80|0x40|0x10|0x1,
2322                                          dev.control_port,
2323                                          (reset_time*HZ)/100);
2324
2325         /* Note: data port is now the data port, not the h/w initialization
2326            port.
2327          */
2328
2329         if (!dev.irq_ok) {
2330                 printk (KERN_WARNING LOGNAME
2331                         "intr not received after h/w un-reset.\n");
2332                 goto gone_bad;
2333         } 
2334
2335         dev.interrupts_on = 1;
2336         
2337         /* Note: data port is now the data port, not the h/w initialization
2338            port.
2339
2340            At this point, only "HW VERSION" or "DOWNLOAD OS" commands
2341            will work. So, issue one of them, and wait for TX
2342            interrupt. This can take a *long* time after a cold boot,
2343            while the ISC ROM does its RAM test. The SDK says up to 4
2344            seconds - with 12MB of RAM on a Tropez+, it takes a lot
2345            longer than that (~16secs). Note that the card understands
2346            the difference between a warm and a cold boot, so
2347            subsequent ISC2115 reboots (say, caused by module
2348            reloading) will get through this much faster.
2349
2350            XXX Interesting question: why is no RX interrupt received first ?
2351         */
2352
2353         wavefront_should_cause_interrupt(WFC_HARDWARE_VERSION, 
2354                                          dev.data_port, ramcheck_time*HZ);
2355
2356         if (!dev.irq_ok) {
2357                 printk (KERN_WARNING LOGNAME
2358                         "post-RAM-check interrupt not received.\n");
2359                 goto gone_bad;
2360         } 
2361
2362         if (!wavefront_wait (STAT_CAN_READ)) {
2363                 printk (KERN_WARNING LOGNAME
2364                         "no response to HW version cmd.\n");
2365                 goto gone_bad;
2366         }
2367         
2368         if ((hwv[0] = wavefront_read ()) == -1) {
2369                 printk (KERN_WARNING LOGNAME
2370                         "board not responding correctly.\n");
2371                 goto gone_bad;
2372         }
2373
2374         if (hwv[0] == 0xFF) { /* NAK */
2375
2376                 /* Board's RAM test failed. Try to read error code,
2377                    and tell us about it either way.
2378                 */
2379                 
2380                 if ((hwv[0] = wavefront_read ()) == -1) {
2381                         printk (KERN_WARNING LOGNAME "on-board RAM test failed "
2382                                 "(bad error code).\n");
2383                 } else {
2384                         printk (KERN_WARNING LOGNAME "on-board RAM test failed "
2385                                 "(error code: 0x%x).\n",
2386                                 hwv[0]);
2387                 }
2388                 goto gone_bad;
2389         }
2390
2391         /* We're OK, just get the next byte of the HW version response */
2392
2393         if ((hwv[1] = wavefront_read ()) == -1) {
2394                 printk (KERN_WARNING LOGNAME "incorrect h/w response.\n");
2395                 goto gone_bad;
2396         }
2397
2398         printk (KERN_INFO LOGNAME "hardware version %d.%d\n",
2399                 hwv[0], hwv[1]);
2400
2401         return 0;
2402
2403
2404      gone_bad:
2405         if (dev.irq >= 0) {
2406                 free_irq (dev.irq, &dev);
2407                 dev.irq = -1;
2408         }
2409         return (1);
2410 }
2411
2412 static int __init detect_wavefront (int irq, int io_base)
2413 {
2414         unsigned char   rbuf[4], wbuf[4];
2415
2416         /* TB docs say the device takes up 8 ports, but we know that
2417            if there is an FX device present (i.e. a Tropez+) it really
2418            consumes 16.
2419         */
2420
2421         if (check_region (io_base, 16)) {
2422                 printk (KERN_ERR LOGNAME "IO address range 0x%x - 0x%x "
2423                         "already in use - ignored\n", dev.base,
2424                         dev.base+15);
2425                 return -1;
2426         }
2427   
2428         dev.irq = irq;
2429         dev.base = io_base;
2430         dev.israw = 0;
2431         dev.debug = debug_default;
2432         dev.interrupts_on = 0;
2433         dev.irq_cnt = 0;
2434         dev.rom_samples_rdonly = 1; /* XXX default lock on ROM sample slots */
2435
2436         if (wavefront_cmd (WFC_FIRMWARE_VERSION, rbuf, wbuf) == 0) {
2437
2438                 dev.fw_version[0] = rbuf[0];
2439                 dev.fw_version[1] = rbuf[1];
2440                 printk (KERN_INFO LOGNAME
2441                         "firmware %d.%d already loaded.\n",
2442                         rbuf[0], rbuf[1]);
2443
2444                 /* check that a command actually works */
2445       
2446                 if (wavefront_cmd (WFC_HARDWARE_VERSION,
2447                                    rbuf, wbuf) == 0) {
2448                         dev.hw_version[0] = rbuf[0];
2449                         dev.hw_version[1] = rbuf[1];
2450                 } else {
2451                         printk (KERN_WARNING LOGNAME "not raw, but no "
2452                                 "hardware version!\n");
2453                         return 0;
2454                 }
2455
2456                 if (!wf_raw) {
2457                         return 1;
2458                 } else {
2459                         printk (KERN_INFO LOGNAME
2460                                 "reloading firmware anyway.\n");
2461                         dev.israw = 1;
2462                 }
2463
2464         } else {
2465
2466                 dev.israw = 1;
2467                 printk (KERN_INFO LOGNAME
2468                         "no response to firmware probe, assume raw.\n");
2469
2470         }
2471
2472         init_waitqueue_head (&dev.interrupt_sleeper);
2473
2474         if (wavefront_hw_reset ()) {
2475                 printk (KERN_WARNING LOGNAME "hardware reset failed\n");
2476                 return 0;
2477         }
2478
2479         /* Check for FX device, present only on Tropez+ */
2480
2481         dev.has_fx = (detect_wffx () == 0);
2482
2483         return 1;
2484 }
2485
2486 #include "os.h"
2487 #include <linux/fs.h>
2488 #include <linux/mm.h>
2489 #include <linux/slab.h>
2490 #include <asm/uaccess.h>
2491
2492
2493 static int
2494 wavefront_download_firmware (char *path)
2495
2496 {
2497         unsigned char section[WF_SECTION_MAX];
2498         char section_length; /* yes, just a char; max value is WF_SECTION_MAX */
2499         int section_cnt_downloaded = 0;
2500         int fd;
2501         int c;
2502         int i;
2503         mm_segment_t fs;
2504
2505         /* This tries to be a bit cleverer than the stuff Alan Cox did for
2506            the generic sound firmware, in that it actually knows
2507            something about the structure of the Motorola firmware. In
2508            particular, it uses a version that has been stripped of the
2509            20K of useless header information, and had section lengths
2510            added, making it possible to load the entire OS without any
2511            [kv]malloc() activity, since the longest entity we ever read is
2512            42 bytes (well, WF_SECTION_MAX) long.
2513         */
2514
2515         fs = get_fs();
2516         set_fs (get_ds());
2517
2518         if ((fd = sys_open (path, 0, 0)) < 0) {
2519                 printk (KERN_WARNING LOGNAME "Unable to load \"%s\".\n",
2520                         path);
2521                 return 1;
2522         }
2523
2524         while (1) {
2525                 int x;
2526
2527                 if ((x = sys_read (fd, &section_length, sizeof (section_length))) !=
2528                     sizeof (section_length)) {
2529                         printk (KERN_ERR LOGNAME "firmware read error.\n");
2530                         goto failure;
2531                 }
2532
2533                 if (section_length == 0) {
2534                         break;
2535                 }
2536
2537                 if (sys_read (fd, section, section_length) != section_length) {
2538                         printk (KERN_ERR LOGNAME "firmware section "
2539                                 "read error.\n");
2540                         goto failure;
2541                 }
2542
2543                 /* Send command */
2544         
2545                 if (wavefront_write (WFC_DOWNLOAD_OS)) {
2546                         goto failure;
2547                 }
2548         
2549                 for (i = 0; i < section_length; i++) {
2550                         if (wavefront_write (section[i])) {
2551                                 goto failure;
2552                         }
2553                 }
2554         
2555                 /* get ACK */
2556         
2557                 if (wavefront_wait (STAT_CAN_READ)) {
2558
2559                         if ((c = inb (dev.data_port)) != WF_ACK) {
2560
2561                                 printk (KERN_ERR LOGNAME "download "
2562                                         "of section #%d not "
2563                                         "acknowledged, ack = 0x%x\n",
2564                                         section_cnt_downloaded + 1, c);
2565                                 goto failure;
2566                 
2567                         }
2568
2569                 } else {
2570                         printk (KERN_ERR LOGNAME "time out for firmware ACK.\n");
2571                         goto failure;
2572                 }
2573
2574         }
2575
2576         sys_close (fd);
2577         set_fs (fs);
2578         return 0;
2579
2580  failure:
2581         sys_close (fd);
2582         set_fs (fs);
2583         printk (KERN_ERR "\nWaveFront: firmware download failed!!!\n");
2584         return 1;
2585 }
2586
2587 static int __init wavefront_config_midi (void)
2588 {
2589         unsigned char rbuf[4], wbuf[4];
2590     
2591         if (detect_wf_mpu (dev.irq, dev.base) < 0) {
2592                 printk (KERN_WARNING LOGNAME
2593                         "could not find working MIDI device\n");
2594                 return -1;
2595         } 
2596
2597         if ((dev.mididev = install_wf_mpu ()) < 0) {
2598                 printk (KERN_WARNING LOGNAME
2599                         "MIDI interfaces not configured\n");
2600                 return -1;
2601         }
2602
2603         /* Route external MIDI to WaveFront synth (by default) */
2604     
2605         if (wavefront_cmd (WFC_MISYNTH_ON, rbuf, wbuf)) {
2606                 printk (KERN_WARNING LOGNAME
2607                         "cannot enable MIDI-IN to synth routing.\n");
2608                 /* XXX error ? */
2609         }
2610
2611
2612 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2613         /* Get the regular MIDI patch loading function, so we can
2614            use it if we ever get handed a SYSEX patch. This is
2615            unlikely, because its so damn slow, but we may as well
2616            leave this functionality from maui.c behind, since it
2617            could be useful for sequencer applications that can
2618            only use MIDI to do patch loading.
2619         */
2620
2621         if (midi_devs[dev.mididev]->converter != NULL) {
2622                 midi_load_patch = midi_devs[dev.mididev]->converter->load_patch;
2623                 midi_devs[dev.mididev]->converter->load_patch =
2624                     &wavefront_oss_load_patch;
2625         }
2626
2627 #endif /* OSS_SUPPORT_SEQ */
2628         
2629         /* Turn on Virtual MIDI, but first *always* turn it off,
2630            since otherwise consectutive reloads of the driver will
2631            never cause the hardware to generate the initial "internal" or 
2632            "external" source bytes in the MIDI data stream. This
2633            is pretty important, since the internal hardware generally will
2634            be used to generate none or very little MIDI output, and
2635            thus the only source of MIDI data is actually external. Without
2636            the switch bytes, the driver will think it all comes from
2637            the internal interface. Duh.
2638         */
2639
2640         if (wavefront_cmd (WFC_VMIDI_OFF, rbuf, wbuf)) { 
2641                 printk (KERN_WARNING LOGNAME
2642                         "virtual MIDI mode not disabled\n");
2643                 return 0; /* We're OK, but missing the external MIDI dev */
2644         }
2645
2646         if ((dev.ext_mididev = virtual_midi_enable ()) < 0) {
2647                 printk (KERN_WARNING LOGNAME "no virtual MIDI access.\n");
2648         } else {
2649                 if (wavefront_cmd (WFC_VMIDI_ON, rbuf, wbuf)) {
2650                         printk (KERN_WARNING LOGNAME
2651                                 "cannot enable virtual MIDI mode.\n");
2652                         virtual_midi_disable ();
2653                 } 
2654         }
2655     
2656         return 0;
2657 }
2658
2659 static int __init wavefront_do_reset (int atboot)
2660 {
2661         char voices[1];
2662
2663         if (!atboot && wavefront_hw_reset ()) {
2664                 printk (KERN_WARNING LOGNAME "hw reset failed.\n");
2665                 goto gone_bad;
2666         }
2667
2668         if (dev.israw) {
2669                 if (wavefront_download_firmware (ospath)) {
2670                         goto gone_bad;
2671                 }
2672
2673                 dev.israw = 0;
2674
2675                 /* Wait for the OS to get running. The protocol for
2676                    this is non-obvious, and was determined by
2677                    using port-IO tracing in DOSemu and some
2678                    experimentation here.
2679                    
2680                    Rather than using timed waits, use interrupts creatively.
2681                 */
2682
2683                 wavefront_should_cause_interrupt (WFC_NOOP,
2684                                                   dev.data_port,
2685                                                   (osrun_time*HZ));
2686
2687                 if (!dev.irq_ok) {
2688                         printk (KERN_WARNING LOGNAME
2689                                 "no post-OS interrupt.\n");
2690                         goto gone_bad;
2691                 }
2692                 
2693                 /* Now, do it again ! */
2694                 
2695                 wavefront_should_cause_interrupt (WFC_NOOP,
2696                                                   dev.data_port, (10*HZ));
2697                 
2698                 if (!dev.irq_ok) {
2699                         printk (KERN_WARNING LOGNAME
2700                                 "no post-OS interrupt(2).\n");
2701                         goto gone_bad;
2702                 }
2703
2704                 /* OK, no (RX/TX) interrupts any more, but leave mute
2705                    in effect. 
2706                 */
2707                 
2708                 outb (0x80|0x40, dev.control_port); 
2709
2710                 /* No need for the IRQ anymore */
2711                 
2712                 free_irq (dev.irq, &dev);
2713
2714         }
2715
2716         if (dev.has_fx && fx_raw) {
2717                 wffx_init ();
2718         }
2719
2720         /* SETUPSND.EXE asks for sample memory config here, but since i
2721            have no idea how to interpret the result, we'll forget
2722            about it.
2723         */
2724         
2725         if ((dev.freemem = wavefront_freemem ()) < 0) {
2726                 goto gone_bad;
2727         }
2728                 
2729         printk (KERN_INFO LOGNAME "available DRAM %dk\n", dev.freemem / 1024);
2730
2731         if (wavefront_write (0xf0) ||
2732             wavefront_write (1) ||
2733             (wavefront_read () < 0)) {
2734                 dev.debug = 0;
2735                 printk (KERN_WARNING LOGNAME "MPU emulation mode not set.\n");
2736                 goto gone_bad;
2737         }
2738
2739         voices[0] = 32;
2740
2741         if (wavefront_cmd (WFC_SET_NVOICES, 0, voices)) {
2742                 printk (KERN_WARNING LOGNAME
2743                         "cannot set number of voices to 32.\n");
2744                 goto gone_bad;
2745         }
2746
2747
2748         return 0;
2749
2750  gone_bad:
2751         /* reset that sucker so that it doesn't bother us. */
2752
2753         outb (0x0, dev.control_port);
2754         dev.interrupts_on = 0;
2755         if (dev.irq >= 0) {
2756                 free_irq (dev.irq, &dev);
2757         }
2758         return 1;
2759 }
2760
2761 static int __init wavefront_init (int atboot)
2762 {
2763         int samples_are_from_rom;
2764
2765         if (dev.israw) {
2766                 samples_are_from_rom = 1;
2767         } else {
2768                 /* XXX is this always true ? */
2769                 samples_are_from_rom = 0;
2770         }
2771
2772         if (dev.israw || fx_raw) {
2773                 if (wavefront_do_reset (atboot)) {
2774                         return -1;
2775                 }
2776         }
2777
2778         wavefront_get_sample_status (samples_are_from_rom);
2779         wavefront_get_program_status ();
2780         wavefront_get_patch_status ();
2781
2782         /* Start normal operation: unreset, master interrupt enabled, no mute
2783         */
2784
2785         outb (0x80|0x40|0x20, dev.control_port); 
2786
2787         return (0);
2788 }
2789
2790 static int __init install_wavefront (void)
2791
2792 {
2793         if ((dev.synth_dev = register_sound_synth (&wavefront_fops, -1)) < 0) {
2794                 printk (KERN_ERR LOGNAME "cannot register raw synth\n");
2795                 return -1;
2796         }
2797
2798 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2799         if ((dev.oss_dev = sound_alloc_synthdev()) == -1) {
2800                 printk (KERN_ERR LOGNAME "Too many sequencers\n");
2801                 return -1;
2802         } else {
2803                 synth_devs[dev.oss_dev] = &wavefront_operations;
2804         }
2805 #endif /* OSS_SUPPORT_SEQ */
2806
2807         if (wavefront_init (1) < 0) {
2808                 printk (KERN_WARNING LOGNAME "initialization failed.\n");
2809
2810 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2811                 sound_unload_synthdev (dev.oss_dev);
2812 #endif /* OSS_SUPPORT_SEQ */ 
2813
2814                 return -1;
2815         }
2816     
2817         request_region (dev.base+2, 6, "wavefront synth");
2818
2819         if (dev.has_fx) {
2820                 request_region (dev.base+8, 8, "wavefront fx");
2821         }
2822
2823         if (wavefront_config_midi ()) {
2824                 printk (KERN_WARNING LOGNAME "could not initialize MIDI.\n");
2825         }
2826
2827         return dev.oss_dev;
2828 }
2829
2830 static void __exit uninstall_wavefront (void)
2831 {
2832         /* the first two i/o addresses are freed by the wf_mpu code */
2833         release_region (dev.base+2, 6);
2834
2835         if (dev.has_fx) {
2836                 release_region (dev.base+8, 8);
2837         }
2838
2839         unregister_sound_synth (dev.synth_dev);
2840
2841 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2842         sound_unload_synthdev (dev.oss_dev);
2843 #endif /* OSS_SUPPORT_SEQ */ 
2844         uninstall_wf_mpu ();
2845 }
2846
2847 /***********************************************************************/
2848 /*   WaveFront FX control                                              */
2849 /***********************************************************************/
2850
2851 #include "yss225.h"
2852
2853 /* Control bits for the Load Control Register
2854  */
2855
2856 #define FX_LSB_TRANSFER 0x01    /* transfer after DSP LSB byte written */
2857 #define FX_MSB_TRANSFER 0x02    /* transfer after DSP MSB byte written */
2858 #define FX_AUTO_INCR    0x04    /* auto-increment DSP address after transfer */
2859
2860 static int
2861 wffx_idle (void) 
2862     
2863 {
2864         int i;
2865         unsigned int x = 0x80;
2866     
2867         for (i = 0; i < 1000; i++) {
2868                 x = inb (dev.fx_status);
2869                 if ((x & 0x80) == 0) {
2870                         break;
2871                 }
2872         }
2873     
2874         if (x & 0x80) {
2875                 printk (KERN_ERR LOGNAME "FX device never idle.\n");
2876                 return 0;
2877         }
2878     
2879         return (1);
2880 }
2881
2882 int __init detect_wffx (void)
2883 {
2884         /* This is a crude check, but its the best one I have for now.
2885            Certainly on the Maui and the Tropez, wffx_idle() will
2886            report "never idle", which suggests that this test should
2887            work OK.
2888         */
2889
2890         if (inb (dev.fx_status) & 0x80) {
2891                 printk (KERN_INFO LOGNAME "Hmm, probably a Maui or Tropez.\n");
2892                 return -1;
2893         }
2894
2895         return 0;
2896 }       
2897
2898 int __init attach_wffx (void)
2899 {
2900         if ((dev.fx_mididev = sound_alloc_mididev ()) < 0) {
2901                 printk (KERN_WARNING LOGNAME "cannot install FX Midi driver\n");
2902                 return -1;
2903         }
2904
2905         return 0;
2906 }
2907
2908 void
2909 wffx_mute (int onoff)
2910     
2911 {
2912         if (!wffx_idle()) {
2913                 return;
2914         }
2915     
2916         outb (onoff ? 0x02 : 0x00, dev.fx_op);
2917 }
2918
2919 static int
2920 wffx_memset (int page,
2921              int addr, int cnt, unsigned short *data)
2922 {
2923         if (page < 0 || page > 7) {
2924                 printk (KERN_ERR LOGNAME "FX memset: "
2925                         "page must be >= 0 and <= 7\n");
2926                 return -(EINVAL);
2927         }
2928
2929         if (addr < 0 || addr > 0x7f) {
2930                 printk (KERN_ERR LOGNAME "FX memset: "
2931                         "addr must be >= 0 and <= 7f\n");
2932                 return -(EINVAL);
2933         }
2934
2935         if (cnt == 1) {
2936
2937                 outb (FX_LSB_TRANSFER, dev.fx_lcr);
2938                 outb (page, dev.fx_dsp_page);
2939                 outb (addr, dev.fx_dsp_addr);
2940                 outb ((data[0] >> 8), dev.fx_dsp_msb);
2941                 outb ((data[0] & 0xff), dev.fx_dsp_lsb);
2942
2943                 printk (KERN_INFO LOGNAME "FX: addr %d:%x set to 0x%x\n",
2944                         page, addr, data[0]);
2945         
2946         } else {
2947                 int i;
2948
2949                 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
2950                 outb (page, dev.fx_dsp_page);
2951                 outb (addr, dev.fx_dsp_addr);
2952
2953                 for (i = 0; i < cnt; i++) {
2954                         outb ((data[i] >> 8), dev.fx_dsp_msb);
2955                         outb ((data[i] & 0xff), dev.fx_dsp_lsb);
2956                         if (!wffx_idle ()) {
2957                                 break;
2958                         }
2959                 }
2960
2961                 if (i != cnt) {
2962                         printk (KERN_WARNING LOGNAME
2963                                 "FX memset "
2964                                 "(0x%x, 0x%x, 0x%x, %d) incomplete\n",
2965                                 page, addr, (int) data, cnt);
2966                         return -(EIO);
2967                 }
2968         }
2969
2970         return 0;
2971 }
2972
2973 static int
2974 wffx_ioctl (wavefront_fx_info *r)
2975
2976 {
2977         unsigned short page_data[256];
2978         unsigned short *pd;
2979
2980         switch (r->request) {
2981         case WFFX_MUTE:
2982                 wffx_mute (r->data[0]);
2983                 return 0;
2984
2985         case WFFX_MEMSET:
2986
2987                 if (r->data[2] <= 0) {
2988                         printk (KERN_ERR LOGNAME "cannot write "
2989                                 "<= 0 bytes to FX\n");
2990                         return -(EINVAL);
2991                 } else if (r->data[2] == 1) {
2992                         pd = (unsigned short *) &r->data[3];
2993                 } else {
2994                         if (r->data[2] > sizeof (page_data)) {
2995                                 printk (KERN_ERR LOGNAME "cannot write "
2996                                         "> 255 bytes to FX\n");
2997                                 return -(EINVAL);
2998                         }
2999                         if (copy_from_user(page_data,
3000                                            (unsigned char __user *)r->data[3],
3001                                            r->data[2]))
3002                                 return -EFAULT;
3003                         pd = page_data;
3004                 }
3005
3006                 return wffx_memset (r->data[0], /* page */
3007                                     r->data[1], /* addr */
3008                                     r->data[2], /* cnt */
3009                                     pd);
3010
3011         default:
3012                 printk (KERN_WARNING LOGNAME
3013                         "FX: ioctl %d not yet supported\n",
3014                         r->request);
3015                 return -(EINVAL);
3016         }
3017 }
3018
3019 /* YSS225 initialization.
3020
3021    This code was developed using DOSEMU. The Turtle Beach SETUPSND
3022    utility was run with I/O tracing in DOSEMU enabled, and a reconstruction
3023    of the port I/O done, using the Yamaha faxback document as a guide
3024    to add more logic to the code. Its really pretty weird.
3025
3026    There was an alternative approach of just dumping the whole I/O
3027    sequence as a series of port/value pairs and a simple loop
3028    that output it. However, I hope that eventually I'll get more
3029    control over what this code does, and so I tried to stick with
3030    a somewhat "algorithmic" approach.
3031 */
3032
3033 static int __init wffx_init (void)
3034 {
3035         int i;
3036         int j;
3037
3038         /* Set all bits for all channels on the MOD unit to zero */
3039         /* XXX But why do this twice ? */
3040
3041         for (j = 0; j < 2; j++) {
3042                 for (i = 0x10; i <= 0xff; i++) {
3043             
3044                         if (!wffx_idle ()) {
3045                                 return (-1);
3046                         }
3047             
3048                         outb (i, dev.fx_mod_addr);
3049                         outb (0x0, dev.fx_mod_data);
3050                 }
3051         }
3052
3053         if (!wffx_idle()) return (-1);
3054         outb (0x02, dev.fx_op);                        /* mute on */
3055
3056         if (!wffx_idle()) return (-1);
3057         outb (0x07, dev.fx_dsp_page);
3058         outb (0x44, dev.fx_dsp_addr);
3059         outb (0x00, dev.fx_dsp_msb);
3060         outb (0x00, dev.fx_dsp_lsb);
3061         if (!wffx_idle()) return (-1);
3062         outb (0x07, dev.fx_dsp_page);
3063         outb (0x42, dev.fx_dsp_addr);
3064         outb (0x00, dev.fx_dsp_msb);
3065         outb (0x00, dev.fx_dsp_lsb);
3066         if (!wffx_idle()) return (-1);
3067         outb (0x07, dev.fx_dsp_page);
3068         outb (0x43, dev.fx_dsp_addr);
3069         outb (0x00, dev.fx_dsp_msb);
3070         outb (0x00, dev.fx_dsp_lsb);
3071         if (!wffx_idle()) return (-1);
3072         outb (0x07, dev.fx_dsp_page);
3073         outb (0x7c, dev.fx_dsp_addr);
3074         outb (0x00, dev.fx_dsp_msb);
3075         outb (0x00, dev.fx_dsp_lsb);
3076         if (!wffx_idle()) return (-1);
3077         outb (0x07, dev.fx_dsp_page);
3078         outb (0x7e, dev.fx_dsp_addr);
3079         outb (0x00, dev.fx_dsp_msb);
3080         outb (0x00, dev.fx_dsp_lsb);
3081         if (!wffx_idle()) return (-1);
3082         outb (0x07, dev.fx_dsp_page);
3083         outb (0x46, dev.fx_dsp_addr);
3084         outb (0x00, dev.fx_dsp_msb);
3085         outb (0x00, dev.fx_dsp_lsb);
3086         if (!wffx_idle()) return (-1);
3087         outb (0x07, dev.fx_dsp_page);
3088         outb (0x49, dev.fx_dsp_addr);
3089         outb (0x00, dev.fx_dsp_msb);
3090         outb (0x00, dev.fx_dsp_lsb);
3091         if (!wffx_idle()) return (-1);
3092         outb (0x07, dev.fx_dsp_page);
3093         outb (0x47, dev.fx_dsp_addr);
3094         outb (0x00, dev.fx_dsp_msb);
3095         outb (0x00, dev.fx_dsp_lsb);
3096         if (!wffx_idle()) return (-1);
3097         outb (0x07, dev.fx_dsp_page);
3098         outb (0x4a, dev.fx_dsp_addr);
3099         outb (0x00, dev.fx_dsp_msb);
3100         outb (0x00, dev.fx_dsp_lsb);
3101
3102         /* either because of stupidity by TB's programmers, or because it
3103            actually does something, rezero the MOD page.
3104         */
3105         for (i = 0x10; i <= 0xff; i++) {
3106         
3107                 if (!wffx_idle ()) {
3108                         return (-1);
3109                 }
3110         
3111                 outb (i, dev.fx_mod_addr);
3112                 outb (0x0, dev.fx_mod_data);
3113         }
3114         /* load page zero */
3115
3116         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3117         outb (0x00, dev.fx_dsp_page);
3118         outb (0x00, dev.fx_dsp_addr);
3119
3120         for (i = 0; i < sizeof (page_zero); i += 2) {
3121                 outb (page_zero[i], dev.fx_dsp_msb);
3122                 outb (page_zero[i+1], dev.fx_dsp_lsb);
3123                 if (!wffx_idle()) return (-1);
3124         }
3125
3126         /* Now load page one */
3127
3128         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3129         outb (0x01, dev.fx_dsp_page);
3130         outb (0x00, dev.fx_dsp_addr);
3131
3132         for (i = 0; i < sizeof (page_one); i += 2) {
3133                 outb (page_one[i], dev.fx_dsp_msb);
3134                 outb (page_one[i+1], dev.fx_dsp_lsb);
3135                 if (!wffx_idle()) return (-1);
3136         }
3137     
3138         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3139         outb (0x02, dev.fx_dsp_page);
3140         outb (0x00, dev.fx_dsp_addr);
3141
3142         for (i = 0; i < sizeof (page_two); i++) {
3143                 outb (page_two[i], dev.fx_dsp_lsb);
3144                 if (!wffx_idle()) return (-1);
3145         }
3146     
3147         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3148         outb (0x03, dev.fx_dsp_page);
3149         outb (0x00, dev.fx_dsp_addr);
3150
3151         for (i = 0; i < sizeof (page_three); i++) {
3152                 outb (page_three[i], dev.fx_dsp_lsb);
3153                 if (!wffx_idle()) return (-1);
3154         }
3155     
3156         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3157         outb (0x04, dev.fx_dsp_page);
3158         outb (0x00, dev.fx_dsp_addr);
3159
3160         for (i = 0; i < sizeof (page_four); i++) {
3161                 outb (page_four[i], dev.fx_dsp_lsb);
3162                 if (!wffx_idle()) return (-1);
3163         }
3164
3165         /* Load memory area (page six) */
3166     
3167         outb (FX_LSB_TRANSFER, dev.fx_lcr); 
3168         outb (0x06, dev.fx_dsp_page); 
3169
3170         for (i = 0; i < sizeof (page_six); i += 3) {
3171                 outb (page_six[i], dev.fx_dsp_addr);
3172                 outb (page_six[i+1], dev.fx_dsp_msb);
3173                 outb (page_six[i+2], dev.fx_dsp_lsb);
3174                 if (!wffx_idle()) return (-1);
3175         }
3176     
3177         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3178         outb (0x07, dev.fx_dsp_page);
3179         outb (0x00, dev.fx_dsp_addr);
3180
3181         for (i = 0; i < sizeof (page_seven); i += 2) {
3182                 outb (page_seven[i], dev.fx_dsp_msb);
3183                 outb (page_seven[i+1], dev.fx_dsp_lsb);
3184                 if (!wffx_idle()) return (-1);
3185         }
3186
3187         /* Now setup the MOD area. We do this algorithmically in order to
3188            save a little data space. It could be done in the same fashion
3189            as the "pages".
3190         */
3191
3192         for (i = 0x00; i <= 0x0f; i++) {
3193                 outb (0x01, dev.fx_mod_addr);
3194                 outb (i, dev.fx_mod_data);
3195                 if (!wffx_idle()) return (-1);
3196                 outb (0x02, dev.fx_mod_addr);
3197                 outb (0x00, dev.fx_mod_data);
3198                 if (!wffx_idle()) return (-1);
3199         }
3200
3201         for (i = 0xb0; i <= 0xbf; i++) {
3202                 outb (i, dev.fx_mod_addr);
3203                 outb (0x20, dev.fx_mod_data);
3204                 if (!wffx_idle()) return (-1);
3205         }
3206
3207         for (i = 0xf0; i <= 0xff; 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 = 0x10; i <= 0x1d; i++) {
3214                 outb (i, dev.fx_mod_addr);
3215                 outb (0xff, dev.fx_mod_data);
3216                 if (!wffx_idle()) return (-1);
3217         }
3218
3219         outb (0x1e, dev.fx_mod_addr);
3220         outb (0x40, dev.fx_mod_data);
3221         if (!wffx_idle()) return (-1);
3222
3223         for (i = 0x1f; i <= 0x2d; i++) {
3224                 outb (i, dev.fx_mod_addr);
3225                 outb (0xff, dev.fx_mod_data);
3226                 if (!wffx_idle()) return (-1);
3227         }
3228
3229         outb (0x2e, dev.fx_mod_addr);
3230         outb (0x00, dev.fx_mod_data);
3231         if (!wffx_idle()) return (-1);
3232
3233         for (i = 0x2f; i <= 0x3e; i++) {
3234                 outb (i, dev.fx_mod_addr);
3235                 outb (0x00, dev.fx_mod_data);
3236                 if (!wffx_idle()) return (-1);
3237         }
3238
3239         outb (0x3f, dev.fx_mod_addr);
3240         outb (0x20, dev.fx_mod_data);
3241         if (!wffx_idle()) return (-1);
3242
3243         for (i = 0x40; i <= 0x4d; i++) {
3244                 outb (i, dev.fx_mod_addr);
3245                 outb (0x00, dev.fx_mod_data);
3246                 if (!wffx_idle()) return (-1);
3247         }
3248
3249         outb (0x4e, dev.fx_mod_addr);
3250         outb (0x0e, dev.fx_mod_data);
3251         if (!wffx_idle()) return (-1);
3252         outb (0x4f, dev.fx_mod_addr);
3253         outb (0x0e, dev.fx_mod_data);
3254         if (!wffx_idle()) return (-1);
3255
3256
3257         for (i = 0x50; i <= 0x6b; i++) {
3258                 outb (i, dev.fx_mod_addr);
3259                 outb (0x00, dev.fx_mod_data);
3260                 if (!wffx_idle()) return (-1);
3261         }
3262
3263         outb (0x6c, dev.fx_mod_addr);
3264         outb (0x40, dev.fx_mod_data);
3265         if (!wffx_idle()) return (-1);
3266
3267         outb (0x6d, dev.fx_mod_addr);
3268         outb (0x00, dev.fx_mod_data);
3269         if (!wffx_idle()) return (-1);
3270
3271         outb (0x6e, dev.fx_mod_addr);
3272         outb (0x40, dev.fx_mod_data);
3273         if (!wffx_idle()) return (-1);
3274
3275         outb (0x6f, dev.fx_mod_addr);
3276         outb (0x40, dev.fx_mod_data);
3277         if (!wffx_idle()) return (-1);
3278
3279         for (i = 0x70; i <= 0x7f; i++) {
3280                 outb (i, dev.fx_mod_addr);
3281                 outb (0xc0, dev.fx_mod_data);
3282                 if (!wffx_idle()) return (-1);
3283         }
3284     
3285         for (i = 0x80; i <= 0xaf; i++) {
3286                 outb (i, dev.fx_mod_addr);
3287                 outb (0x00, dev.fx_mod_data);
3288                 if (!wffx_idle()) return (-1);
3289         }
3290
3291         for (i = 0xc0; i <= 0xdd; i++) {
3292                 outb (i, dev.fx_mod_addr);
3293                 outb (0x00, dev.fx_mod_data);
3294                 if (!wffx_idle()) return (-1);
3295         }
3296
3297         outb (0xde, dev.fx_mod_addr);
3298         outb (0x10, dev.fx_mod_data);
3299         if (!wffx_idle()) return (-1);
3300         outb (0xdf, dev.fx_mod_addr);
3301         outb (0x10, dev.fx_mod_data);
3302         if (!wffx_idle()) return (-1);
3303
3304         for (i = 0xe0; i <= 0xef; i++) {
3305                 outb (i, dev.fx_mod_addr);
3306                 outb (0x00, dev.fx_mod_data);
3307                 if (!wffx_idle()) return (-1);
3308         }
3309
3310         for (i = 0x00; i <= 0x0f; i++) {
3311                 outb (0x01, dev.fx_mod_addr);
3312                 outb (i, dev.fx_mod_data);
3313                 outb (0x02, dev.fx_mod_addr);
3314                 outb (0x01, dev.fx_mod_data);
3315                 if (!wffx_idle()) return (-1);
3316         }
3317
3318         outb (0x02, dev.fx_op); /* mute on */
3319
3320         /* Now set the coefficients and so forth for the programs above */
3321
3322         for (i = 0; i < sizeof (coefficients); i += 4) {
3323                 outb (coefficients[i], dev.fx_dsp_page);
3324                 outb (coefficients[i+1], dev.fx_dsp_addr);
3325                 outb (coefficients[i+2], dev.fx_dsp_msb);
3326                 outb (coefficients[i+3], dev.fx_dsp_lsb);
3327                 if (!wffx_idle()) return (-1);
3328         }
3329
3330         /* Some settings (?) that are too small to bundle into loops */
3331
3332         if (!wffx_idle()) return (-1);
3333         outb (0x1e, dev.fx_mod_addr);
3334         outb (0x14, dev.fx_mod_data);
3335         if (!wffx_idle()) return (-1);
3336         outb (0xde, dev.fx_mod_addr);
3337         outb (0x20, dev.fx_mod_data);
3338         if (!wffx_idle()) return (-1);
3339         outb (0xdf, dev.fx_mod_addr);
3340         outb (0x20, dev.fx_mod_data);
3341     
3342         /* some more coefficients */
3343
3344         if (!wffx_idle()) return (-1);
3345         outb (0x06, dev.fx_dsp_page);
3346         outb (0x78, dev.fx_dsp_addr);
3347         outb (0x00, dev.fx_dsp_msb);
3348         outb (0x40, dev.fx_dsp_lsb);
3349         if (!wffx_idle()) return (-1);
3350         outb (0x07, dev.fx_dsp_page);
3351         outb (0x03, dev.fx_dsp_addr);
3352         outb (0x0f, dev.fx_dsp_msb);
3353         outb (0xff, dev.fx_dsp_lsb);
3354         if (!wffx_idle()) return (-1);
3355         outb (0x07, dev.fx_dsp_page);
3356         outb (0x0b, dev.fx_dsp_addr);
3357         outb (0x0f, dev.fx_dsp_msb);
3358         outb (0xff, dev.fx_dsp_lsb);
3359         if (!wffx_idle()) return (-1);
3360         outb (0x07, dev.fx_dsp_page);
3361         outb (0x02, dev.fx_dsp_addr);
3362         outb (0x00, dev.fx_dsp_msb);
3363         outb (0x00, dev.fx_dsp_lsb);
3364         if (!wffx_idle()) return (-1);
3365         outb (0x07, dev.fx_dsp_page);
3366         outb (0x0a, dev.fx_dsp_addr);
3367         outb (0x00, dev.fx_dsp_msb);
3368         outb (0x00, dev.fx_dsp_lsb);
3369         if (!wffx_idle()) return (-1);
3370         outb (0x07, dev.fx_dsp_page);
3371         outb (0x46, dev.fx_dsp_addr);
3372         outb (0x00, dev.fx_dsp_msb);
3373         outb (0x00, dev.fx_dsp_lsb);
3374         if (!wffx_idle()) return (-1);
3375         outb (0x07, dev.fx_dsp_page);
3376         outb (0x49, dev.fx_dsp_addr);
3377         outb (0x00, dev.fx_dsp_msb);
3378         outb (0x00, dev.fx_dsp_lsb);
3379     
3380         /* Now, for some strange reason, lets reload every page
3381            and all the coefficients over again. I have *NO* idea
3382            why this is done. I do know that no sound is produced
3383            is this phase is omitted.
3384         */
3385
3386         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3387         outb (0x00, dev.fx_dsp_page);  
3388         outb (0x10, dev.fx_dsp_addr);
3389
3390         for (i = 0; i < sizeof (page_zero_v2); i += 2) {
3391                 outb (page_zero_v2[i], dev.fx_dsp_msb);
3392                 outb (page_zero_v2[i+1], dev.fx_dsp_lsb);
3393                 if (!wffx_idle()) return (-1);
3394         }
3395     
3396         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3397         outb (0x01, dev.fx_dsp_page);
3398         outb (0x10, dev.fx_dsp_addr);
3399
3400         for (i = 0; i < sizeof (page_one_v2); i += 2) {
3401                 outb (page_one_v2[i], dev.fx_dsp_msb);
3402                 outb (page_one_v2[i+1], dev.fx_dsp_lsb);
3403                 if (!wffx_idle()) return (-1);
3404         }
3405     
3406         if (!wffx_idle()) return (-1);
3407         if (!wffx_idle()) return (-1);
3408     
3409         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3410         outb (0x02, dev.fx_dsp_page);
3411         outb (0x10, dev.fx_dsp_addr);
3412
3413         for (i = 0; i < sizeof (page_two_v2); i++) {
3414                 outb (page_two_v2[i], dev.fx_dsp_lsb);
3415                 if (!wffx_idle()) return (-1);
3416         }
3417         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3418         outb (0x03, dev.fx_dsp_page);
3419         outb (0x10, dev.fx_dsp_addr);
3420
3421         for (i = 0; i < sizeof (page_three_v2); i++) {
3422                 outb (page_three_v2[i], dev.fx_dsp_lsb);
3423                 if (!wffx_idle()) return (-1);
3424         }
3425     
3426         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3427         outb (0x04, dev.fx_dsp_page);
3428         outb (0x10, dev.fx_dsp_addr);
3429
3430         for (i = 0; i < sizeof (page_four_v2); i++) {
3431                 outb (page_four_v2[i], dev.fx_dsp_lsb);
3432                 if (!wffx_idle()) return (-1);
3433         }
3434     
3435         outb (FX_LSB_TRANSFER, dev.fx_lcr);
3436         outb (0x06, dev.fx_dsp_page);
3437
3438         /* Page six v.2 is algorithmic */
3439     
3440         for (i = 0x10; i <= 0x3e; i += 2) {
3441                 outb (i, dev.fx_dsp_addr);
3442                 outb (0x00, dev.fx_dsp_msb);
3443                 outb (0x00, dev.fx_dsp_lsb);
3444                 if (!wffx_idle()) return (-1);
3445         }
3446
3447         outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3448         outb (0x07, dev.fx_dsp_page);
3449         outb (0x10, dev.fx_dsp_addr);
3450
3451         for (i = 0; i < sizeof (page_seven_v2); i += 2) {
3452                 outb (page_seven_v2[i], dev.fx_dsp_msb);
3453                 outb (page_seven_v2[i+1], dev.fx_dsp_lsb);
3454                 if (!wffx_idle()) return (-1);
3455         }
3456
3457         for (i = 0x00; i < sizeof(mod_v2); i += 2) {
3458                 outb (mod_v2[i], dev.fx_mod_addr);
3459                 outb (mod_v2[i+1], dev.fx_mod_data);
3460                 if (!wffx_idle()) return (-1);
3461         }
3462
3463         for (i = 0; i < sizeof (coefficients2); i += 4) {
3464                 outb (coefficients2[i], dev.fx_dsp_page);
3465                 outb (coefficients2[i+1], dev.fx_dsp_addr);
3466                 outb (coefficients2[i+2], dev.fx_dsp_msb);
3467                 outb (coefficients2[i+3], dev.fx_dsp_lsb);
3468                 if (!wffx_idle()) return (-1);
3469         }
3470
3471         for (i = 0; i < sizeof (coefficients3); i += 2) {
3472                 int x;
3473
3474                 outb (0x07, dev.fx_dsp_page);
3475                 x = (i % 4) ? 0x4e : 0x4c;
3476                 outb (x, dev.fx_dsp_addr);
3477                 outb (coefficients3[i], dev.fx_dsp_msb);
3478                 outb (coefficients3[i+1], dev.fx_dsp_lsb);
3479         }
3480
3481         outb (0x00, dev.fx_op); /* mute off */
3482         if (!wffx_idle()) return (-1);
3483
3484         return (0);
3485 }
3486
3487 static int io = -1;
3488 static int irq = -1;
3489
3490 MODULE_AUTHOR      ("Paul Barton-Davis <pbd@op.net>");
3491 MODULE_DESCRIPTION ("Turtle Beach WaveFront Linux Driver");
3492 MODULE_LICENSE("GPL");
3493 MODULE_PARM        (io,"i");
3494 MODULE_PARM        (irq,"i");
3495
3496 static int __init init_wavfront (void)
3497 {
3498         printk ("Turtle Beach WaveFront Driver\n"
3499                 "Copyright (C) by Hannu Solvainen, "
3500                 "Paul Barton-Davis 1993-1998.\n");
3501
3502         /* XXX t'would be lovely to ask the CS4232 for these values, eh ? */
3503
3504         if (io == -1 || irq == -1) {
3505                 printk (KERN_INFO LOGNAME "irq and io options must be set.\n");
3506                 return -EINVAL;
3507         }
3508
3509         if (wavefront_interrupt_bits (irq) < 0) {
3510                 printk (KERN_INFO LOGNAME
3511                         "IRQ must be 9, 5, 12 or 15 (not %d)\n", irq);
3512                 return -ENODEV;
3513         }
3514
3515         if (detect_wavefront (irq, io) < 0) {
3516                 return -ENODEV;
3517         } 
3518
3519         if (install_wavefront () < 0) {
3520                 return -EIO;
3521         }
3522
3523         return 0;
3524 }
3525
3526 static void __exit cleanup_wavfront (void)
3527 {
3528         uninstall_wavefront ();
3529 }
3530
3531 module_init(init_wavfront);
3532 module_exit(cleanup_wavfront);