Import 2.3.18pre1
[davej-history.git] / drivers / sound / wavfront.c
blobf365c24341de227bb0bf33d1d20cd7203586ea2a
1 /* -*- linux-c -*-
3 * sound/wavfront.c
5 * A Linux driver for Turtle Beach WaveFront Series (Maui, Tropez, Tropez Plus)
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.
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.
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.
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.
30 * Supported devices:
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
36 **********************************************************************
38 * Copyright (C) by Paul Barton-Davis 1998
40 * Some portions of this file are taken from work that is
41 * copyright (C) by Hannu Savolainen 1993-1996
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.
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.
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.
59 * $Id: wavfront.c,v 0.7 1998/09/09 15:47:36 pbd Exp $
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. */
65 #include <linux/module.h>
67 #include <linux/kernel.h>
68 #include <linux/sched.h>
69 #include <linux/ptrace.h>
70 #include <linux/fcntl.h>
71 #include <linux/ioport.h>
73 #include <linux/interrupt.h>
74 #include <linux/config.h>
75 #include <linux/init.h>
77 #include <linux/delay.h>
79 #include "sound_config.h"
80 #include "soundmodule.h"
82 #include <linux/wavefront.h>
85 * This sucks, hopefully it'll get standardised
88 #if defined(__alpha__)
89 #ifdef __SMP__
90 #define LOOPS_PER_SEC cpu_data[smp_processor_id()].loops_per_sec
91 #else
92 #define LOOPS_PER_SEC loops_per_sec
93 #endif
94 #endif
96 #if defined(__i386__)
97 #define LOOPS_PER_SEC current_cpu_data.loops_per_sec
98 #endif
100 #define _MIDI_SYNTH_C_
101 #define MIDI_SYNTH_NAME "WaveFront MIDI"
102 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
103 #include "midi_synth.h"
105 /* Compile-time control of the extent to which OSS is supported.
107 I consider /dev/sequencer to be an anachronism, but given its
108 widespread usage by various Linux MIDI software, it seems worth
109 offering support to it if its not too painful. Instead of using
110 /dev/sequencer, I recommend:
112 for synth programming and patch loading: /dev/synthNN
113 for kernel-synchronized MIDI sequencing: the ALSA sequencer
114 for direct MIDI control: /dev/midiNN
116 I have never tried static compilation into the kernel. The #if's
117 for this are really just notes to myself about what the code is
118 for.
121 #define OSS_SUPPORT_SEQ 0x1 /* use of /dev/sequencer */
122 #define OSS_SUPPORT_STATIC_INSTALL 0x2 /* static compilation into kernel */
124 #define OSS_SUPPORT_LEVEL 0x1 /* just /dev/sequencer for now */
126 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
127 static int (*midi_load_patch) (int devno, int format, const char *addr,
128 int offs, int count, int pmgr_flag) = NULL;
129 #endif OSS_SUPPORT_SEQ
131 /* This is meant to work as a module */
133 #if defined(CONFIG_SOUND_WAVEFRONT_MODULE) && defined(MODULE)
135 /* if WF_DEBUG not defined, no run-time debugging messages will
136 be available via the debug flag setting. Given the current
137 beta state of the driver, this will remain set until a future
138 version.
141 #define WF_DEBUG 1
143 #ifdef WF_DEBUG
145 /* Thank goodness for gcc's preprocessor ... */
147 #define DPRINT(cond, format, args...) \
148 if ((dev.debug & (cond)) == (cond)) { \
149 printk (KERN_DEBUG LOGNAME format, ## args); \
151 #else
152 #define DPRINT(cond, format, args...)
153 #endif
155 #define LOGNAME "WaveFront: "
157 /* bitmasks for WaveFront status port value */
159 #define STAT_RINTR_ENABLED 0x01
160 #define STAT_CAN_READ 0x02
161 #define STAT_INTR_READ 0x04
162 #define STAT_WINTR_ENABLED 0x10
163 #define STAT_CAN_WRITE 0x20
164 #define STAT_INTR_WRITE 0x40
166 /*** Module-accessible parameters ***************************************/
168 int wf_raw = 0; /* we normally check for "raw state" to firmware
169 loading. if set, then during driver loading, the
170 state of the board is ignored, and we reset the
171 board and load the firmware anyway.
174 int fx_raw = 1; /* if this is zero, we'll leave the FX processor in
175 whatever state it is when the driver is loaded.
176 The default is to download the microprogram and
177 associated coefficients to set it up for "default"
178 operation, whatever that means.
181 int debug_default = 0; /* you can set this to control debugging
182 during driver loading. it takes any combination
183 of the WF_DEBUG_* flags defined in
184 wavefront.h
187 /* XXX this needs to be made firmware and hardware version dependent */
189 char *ospath = "/etc/sound/wavefront.os"; /* where to find a processed
190 version of the WaveFront OS
193 int wait_usecs = 150; /* This magic number seems to give pretty optimal
194 throughput based on my limited experimentation.
195 If you want to play around with it and find a better
196 value, be my guest. Remember, the idea is to
197 get a number that causes us to just busy wait
198 for as many WaveFront commands as possible, without
199 coming up with a number so large that we hog the
200 whole CPU.
202 Specifically, with this number, out of about 134,000
203 status waits, only about 250 result in a sleep.
206 int sleep_interval = 100; /* HZ/sleep_interval seconds per sleep */
207 int sleep_tries = 50; /* number of times we'll try to sleep */
209 int reset_time = 2; /* hundreths of a second we wait after a HW reset for
210 the expected interrupt.
213 int ramcheck_time = 20; /* time in seconds to wait while ROM code
214 checks on-board RAM.
217 int osrun_time = 10; /* time in seconds we wait for the OS to
218 start running.
221 MODULE_PARM(wf_raw,"i");
222 MODULE_PARM(fx_raw,"i");
223 MODULE_PARM(debug_default,"i");
224 MODULE_PARM(wait_usecs,"i");
225 MODULE_PARM(sleep_interval,"i");
226 MODULE_PARM(sleep_tries,"i");
227 MODULE_PARM(ospath,"s");
228 MODULE_PARM(reset_time,"i");
229 MODULE_PARM(ramcheck_time,"i");
230 MODULE_PARM(osrun_time,"i");
232 /***************************************************************************/
234 /* Note: because this module doesn't export any symbols, this really isn't
235 a global variable, even if it looks like one. I was quite confused by
236 this when I started writing this as a (newer) module -- pbd.
239 struct wf_config {
240 int devno; /* device number from kernel */
241 int irq; /* "you were one, one of the few ..." */
242 int base; /* low i/o port address */
244 #define mpu_data_port base
245 #define mpu_command_port base + 1 /* write semantics */
246 #define mpu_status_port base + 1 /* read semantics */
247 #define data_port base + 2
248 #define status_port base + 3 /* read semantics */
249 #define control_port base + 3 /* write semantics */
250 #define block_port base + 4 /* 16 bit, writeonly */
251 #define last_block_port base + 6 /* 16 bit, writeonly */
253 /* FX ports. These are mapped through the ICS2115 to the YS225.
254 The ICS2115 takes care of flipping the relevant pins on the
255 YS225 so that access to each of these ports does the right
256 thing. Note: these are NOT documented by Turtle Beach.
259 #define fx_status base + 8
260 #define fx_op base + 8
261 #define fx_lcr base + 9
262 #define fx_dsp_addr base + 0xa
263 #define fx_dsp_page base + 0xb
264 #define fx_dsp_lsb base + 0xc
265 #define fx_dsp_msb base + 0xd
266 #define fx_mod_addr base + 0xe
267 #define fx_mod_data base + 0xf
269 volatile int irq_ok; /* set by interrupt handler */
270 volatile int irq_cnt; /* ditto */
271 int opened; /* flag, holds open(2) mode */
272 char debug; /* debugging flags */
273 int freemem; /* installed RAM, in bytes */
275 int synth_dev; /* devno for "raw" synth */
276 int mididev; /* devno for internal MIDI */
277 int ext_mididev; /* devno for external MIDI */
278 int fx_mididev; /* devno for FX MIDI interface */
279 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
280 int oss_dev; /* devno for OSS sequencer synth */
281 #endif OSS_SUPPORT_SEQ
283 char fw_version[2]; /* major = [0], minor = [1] */
284 char hw_version[2]; /* major = [0], minor = [1] */
285 char israw; /* needs Motorola microcode */
286 char has_fx; /* has FX processor (Tropez+) */
287 char prog_status[WF_MAX_PROGRAM]; /* WF_SLOT_* */
288 char patch_status[WF_MAX_PATCH]; /* WF_SLOT_* */
289 char sample_status[WF_MAX_SAMPLE]; /* WF_ST_* | WF_SLOT_* */
290 int samples_used; /* how many */
291 char interrupts_on; /* h/w MPU interrupts enabled ? */
292 char rom_samples_rdonly; /* can we write on ROM samples */
293 wait_queue_head_t interrupt_sleeper;
294 } dev;
296 static int detect_wffx(void);
297 static int wffx_ioctl (wavefront_fx_info *);
298 static int wffx_init (void);
300 static int wavefront_delete_sample (int sampnum);
301 static int wavefront_find_free_sample (void);
303 /* From wf_midi.c */
305 extern int virtual_midi_enable (void);
306 extern int virtual_midi_disable (void);
307 extern int detect_wf_mpu (int, int);
308 extern int install_wf_mpu (void);
309 extern int uninstall_wf_mpu (void);
311 typedef struct {
312 int cmd;
313 char *action;
314 unsigned int read_cnt;
315 unsigned int write_cnt;
316 int need_ack;
317 } wavefront_command;
319 static struct {
320 int errno;
321 const char *errstr;
322 } wavefront_errors[] = {
323 { 0x01, "Bad sample number" },
324 { 0x02, "Out of sample memory" },
325 { 0x03, "Bad patch number" },
326 { 0x04, "Error in number of voices" },
327 { 0x06, "Sample load already in progress" },
328 { 0x0B, "No sample load request pending" },
329 { 0x0E, "Bad MIDI channel number" },
330 { 0x10, "Download Record Error" },
331 { 0x80, "Success" },
332 { 0x0, 0x0 }
335 #define NEEDS_ACK 1
337 static wavefront_command wavefront_commands[] = {
338 { WFC_SET_SYNTHVOL, "set synthesizer volume", 0, 1, NEEDS_ACK },
339 { WFC_GET_SYNTHVOL, "get synthesizer volume", 1, 0, 0},
340 { WFC_SET_NVOICES, "set number of voices", 0, 1, NEEDS_ACK },
341 { WFC_GET_NVOICES, "get number of voices", 1, 0, 0 },
342 { WFC_SET_TUNING, "set synthesizer tuning", 0, 2, NEEDS_ACK },
343 { WFC_GET_TUNING, "get synthesizer tuning", 2, 0, 0 },
344 { WFC_DISABLE_CHANNEL, "disable synth channel", 0, 1, NEEDS_ACK },
345 { WFC_ENABLE_CHANNEL, "enable synth channel", 0, 1, NEEDS_ACK },
346 { WFC_GET_CHANNEL_STATUS, "get synth channel status", 3, 0, 0 },
347 { WFC_MISYNTH_OFF, "disable midi-in to synth", 0, 0, NEEDS_ACK },
348 { WFC_MISYNTH_ON, "enable midi-in to synth", 0, 0, NEEDS_ACK },
349 { WFC_VMIDI_ON, "enable virtual midi mode", 0, 0, NEEDS_ACK },
350 { WFC_VMIDI_OFF, "disable virtual midi mode", 0, 0, NEEDS_ACK },
351 { WFC_MIDI_STATUS, "report midi status", 1, 0, 0 },
352 { WFC_FIRMWARE_VERSION, "report firmware version", 2, 0, 0 },
353 { WFC_HARDWARE_VERSION, "report hardware version", 2, 0, 0 },
354 { WFC_GET_NSAMPLES, "report number of samples", 2, 0, 0 },
355 { WFC_INSTOUT_LEVELS, "report instantaneous output levels", 7, 0, 0 },
356 { WFC_PEAKOUT_LEVELS, "report peak output levels", 7, 0, 0 },
357 { WFC_DOWNLOAD_SAMPLE, "download sample",
358 0, WF_SAMPLE_BYTES, NEEDS_ACK },
359 { WFC_DOWNLOAD_BLOCK, "download block", 0, 0, NEEDS_ACK},
360 { WFC_DOWNLOAD_SAMPLE_HEADER, "download sample header",
361 0, WF_SAMPLE_HDR_BYTES, NEEDS_ACK },
362 { WFC_UPLOAD_SAMPLE_HEADER, "upload sample header", 13, 2, 0 },
364 /* This command requires a variable number of bytes to be written.
365 There is a hack in wavefront_cmd() to support this. The actual
366 count is passed in as the read buffer ptr, cast appropriately.
367 Ugh.
370 { WFC_DOWNLOAD_MULTISAMPLE, "download multisample", 0, 0, NEEDS_ACK },
372 /* This one is a hack as well. We just read the first byte of the
373 response, don't fetch an ACK, and leave the rest to the
374 calling function. Ugly, ugly, ugly.
377 { WFC_UPLOAD_MULTISAMPLE, "upload multisample", 2, 1, 0 },
378 { WFC_DOWNLOAD_SAMPLE_ALIAS, "download sample alias",
379 0, WF_ALIAS_BYTES, NEEDS_ACK },
380 { WFC_UPLOAD_SAMPLE_ALIAS, "upload sample alias", WF_ALIAS_BYTES, 2, 0},
381 { WFC_DELETE_SAMPLE, "delete sample", 0, 2, NEEDS_ACK },
382 { WFC_IDENTIFY_SAMPLE_TYPE, "identify sample type", 5, 2, 0 },
383 { WFC_UPLOAD_SAMPLE_PARAMS, "upload sample parameters" },
384 { WFC_REPORT_FREE_MEMORY, "report free memory", 4, 0, 0 },
385 { WFC_DOWNLOAD_PATCH, "download patch", 0, 134, NEEDS_ACK },
386 { WFC_UPLOAD_PATCH, "upload patch", 132, 2, 0 },
387 { WFC_DOWNLOAD_PROGRAM, "download program", 0, 33, NEEDS_ACK },
388 { WFC_UPLOAD_PROGRAM, "upload program", 32, 1, 0 },
389 { WFC_DOWNLOAD_EDRUM_PROGRAM, "download enhanced drum program", 0, 9,
390 NEEDS_ACK},
391 { WFC_UPLOAD_EDRUM_PROGRAM, "upload enhanced drum program", 8, 1, 0},
392 { WFC_SET_EDRUM_CHANNEL, "set enhanced drum program channel",
393 0, 1, NEEDS_ACK },
394 { WFC_DISABLE_DRUM_PROGRAM, "disable drum program", 0, 1, NEEDS_ACK },
395 { WFC_REPORT_CHANNEL_PROGRAMS, "report channel program numbers",
396 32, 0, 0 },
397 { WFC_NOOP, "the no-op command", 0, 0, NEEDS_ACK },
398 { 0x00 }
401 static const char *
402 wavefront_errorstr (int errnum)
405 int i;
407 for (i = 0; wavefront_errors[i].errstr; i++) {
408 if (wavefront_errors[i].errno == errnum) {
409 return wavefront_errors[i].errstr;
413 return "Unknown WaveFront error";
416 static wavefront_command *
417 wavefront_get_command (int cmd)
420 int i;
422 for (i = 0; wavefront_commands[i].cmd != 0; i++) {
423 if (cmd == wavefront_commands[i].cmd) {
424 return &wavefront_commands[i];
428 return (wavefront_command *) 0;
431 static inline int
432 wavefront_status (void)
435 return inb (dev.status_port);
438 static int
439 wavefront_sleep (int limit)
442 current->state = TASK_INTERRUPTIBLE;
443 schedule_timeout(limit);
445 return signal_pending(current);
448 static int
449 wavefront_wait (int mask)
452 int i;
453 static int short_loop_cnt = 0;
455 /* Compute the loop count that lets us sleep for about the
456 right amount of time, cache issues, bus speeds and all
457 other issues being unequal but largely irrelevant.
460 if (short_loop_cnt == 0) {
461 short_loop_cnt = wait_usecs *
462 (LOOPS_PER_SEC / 1000000);
465 /* Spin for a short period of time, because >99% of all
466 requests to the WaveFront can be serviced inline like this.
469 for (i = 0; i < short_loop_cnt; i++) {
470 if (wavefront_status() & mask) {
471 return 1;
475 for (i = 0; i < sleep_tries; i++) {
477 if (wavefront_status() & mask) {
478 return 1;
481 if (wavefront_sleep (HZ/sleep_interval)) {
482 return (0);
486 return (0);
489 static int
490 wavefront_read (void)
493 if (wavefront_wait (STAT_CAN_READ))
494 return inb (dev.data_port);
496 DPRINT (WF_DEBUG_DATA, "read timeout.\n");
498 return -1;
501 static int
502 wavefront_write (unsigned char data)
505 if (wavefront_wait (STAT_CAN_WRITE)) {
506 outb (data, dev.data_port);
507 return 0;
510 DPRINT (WF_DEBUG_DATA, "write timeout.\n");
512 return -1;
515 static int
516 wavefront_cmd (int cmd, unsigned char *rbuf, unsigned char *wbuf)
519 int ack;
520 int i;
521 int c;
522 wavefront_command *wfcmd;
524 if ((wfcmd = wavefront_get_command (cmd)) == (wavefront_command *) 0) {
525 printk (KERN_WARNING LOGNAME "command 0x%x not supported.\n",
526 cmd);
527 return 1;
530 /* Hack to handle the one variable-size write command. See
531 wavefront_send_multisample() for the other half of this
532 gross and ugly strategy.
535 if (cmd == WFC_DOWNLOAD_MULTISAMPLE) {
536 wfcmd->write_cnt = (unsigned int) rbuf;
537 rbuf = 0;
540 DPRINT (WF_DEBUG_CMD, "0x%x [%s] (%d,%d,%d)\n",
541 cmd, wfcmd->action, wfcmd->read_cnt,
542 wfcmd->write_cnt, wfcmd->need_ack);
544 if (wavefront_write (cmd)) {
545 DPRINT ((WF_DEBUG_IO|WF_DEBUG_CMD), "cannot request "
546 "0x%x [%s].\n",
547 cmd, wfcmd->action);
548 return 1;
551 if (wfcmd->write_cnt > 0) {
552 DPRINT (WF_DEBUG_DATA, "writing %d bytes "
553 "for 0x%x\n",
554 wfcmd->write_cnt, cmd);
556 for (i = 0; i < wfcmd->write_cnt; i++) {
557 if (wavefront_write (wbuf[i])) {
558 DPRINT (WF_DEBUG_IO, "bad write for byte "
559 "%d of 0x%x [%s].\n",
560 i, cmd, wfcmd->action);
561 return 1;
564 DPRINT (WF_DEBUG_DATA, "write[%d] = 0x%x\n",
565 i, wbuf[i]);
569 if (wfcmd->read_cnt > 0) {
570 DPRINT (WF_DEBUG_DATA, "reading %d ints "
571 "for 0x%x\n",
572 wfcmd->read_cnt, cmd);
574 for (i = 0; i < wfcmd->read_cnt; i++) {
576 if ((c = wavefront_read()) == -1) {
577 DPRINT (WF_DEBUG_IO, "bad read for byte "
578 "%d of 0x%x [%s].\n",
579 i, cmd, wfcmd->action);
580 return 1;
583 /* Now handle errors. Lots of special cases here */
585 if (c == 0xff) {
586 if ((c = wavefront_read ()) == -1) {
587 DPRINT (WF_DEBUG_IO, "bad read for "
588 "error byte at "
589 "read byte %d "
590 "of 0x%x [%s].\n",
591 i, cmd,
592 wfcmd->action);
593 return 1;
596 /* Can you believe this madness ? */
598 if (c == 1 &&
599 wfcmd->cmd == WFC_IDENTIFY_SAMPLE_TYPE) {
600 rbuf[0] = WF_ST_EMPTY;
601 return (0);
603 } else if (c == 3 &&
604 wfcmd->cmd == WFC_UPLOAD_PATCH) {
606 return 3;
608 } else if (c == 1 &&
609 wfcmd->cmd == WFC_UPLOAD_PROGRAM) {
611 return 1;
613 } else {
615 DPRINT (WF_DEBUG_IO, "error %d (%s) "
616 "during "
617 "read for byte "
618 "%d of 0x%x "
619 "[%s].\n",
621 wavefront_errorstr (c),
622 i, cmd,
623 wfcmd->action);
624 return 1;
628 } else {
629 rbuf[i] = c;
632 DPRINT (WF_DEBUG_DATA, "read[%d] = 0x%x\n",i, rbuf[i]);
636 if ((wfcmd->read_cnt == 0 && wfcmd->write_cnt == 0) || wfcmd->need_ack) {
638 DPRINT (WF_DEBUG_CMD, "reading ACK for 0x%x\n", cmd);
640 /* Some commands need an ACK, but return zero instead
641 of the standard value.
644 if ((ack = wavefront_read()) == 0) {
645 ack = WF_ACK;
648 if (ack != WF_ACK) {
649 if (ack == -1) {
650 DPRINT (WF_DEBUG_IO, "cannot read ack for "
651 "0x%x [%s].\n",
652 cmd, wfcmd->action);
653 return 1;
655 } else {
656 int err = -1; /* something unknown */
658 if (ack == 0xff) { /* explicit error */
660 if ((err = wavefront_read ()) == -1) {
661 DPRINT (WF_DEBUG_DATA,
662 "cannot read err "
663 "for 0x%x [%s].\n",
664 cmd, wfcmd->action);
668 DPRINT (WF_DEBUG_IO, "0x%x [%s] "
669 "failed (0x%x, 0x%x, %s)\n",
670 cmd, wfcmd->action, ack, err,
671 wavefront_errorstr (err));
673 return -err;
677 DPRINT (WF_DEBUG_DATA, "ack received "
678 "for 0x%x [%s]\n",
679 cmd, wfcmd->action);
680 } else {
682 DPRINT (WF_DEBUG_CMD, "0x%x [%s] does not need "
683 "ACK (%d,%d,%d)\n",
684 cmd, wfcmd->action, wfcmd->read_cnt,
685 wfcmd->write_cnt, wfcmd->need_ack);
688 return 0;
692 /***********************************************************************
693 WaveFront: data munging
695 Things here are wierd. All data written to the board cannot
696 have its most significant bit set. Any data item with values
697 potentially > 0x7F (127) must be split across multiple bytes.
699 Sometimes, we need to munge numeric values that are represented on
700 the x86 side as 8-32 bit values. Sometimes, we need to munge data
701 that is represented on the x86 side as an array of bytes. The most
702 efficient approach to handling both cases seems to be to use 2
703 different functions for munging and 2 for de-munging. This avoids
704 wierd casting and worrying about bit-level offsets.
706 **********************************************************************/
708 static
709 unsigned char *
710 munge_int32 (unsigned int src,
711 unsigned char *dst,
712 unsigned int dst_size)
714 int i;
716 for (i = 0;i < dst_size; i++) {
717 *dst = src & 0x7F; /* Mask high bit of LSB */
718 src = src >> 7; /* Rotate Right 7 bits */
719 /* Note: we leave the upper bits in place */
721 dst++;
723 return dst;
726 static int
727 demunge_int32 (unsigned char* src, int src_size)
730 int i;
731 int outval = 0;
733 for (i = src_size - 1; i >= 0; i--) {
734 outval=(outval<<7)+src[i];
737 return outval;
740 static
741 unsigned char *
742 munge_buf (unsigned char *src, unsigned char *dst, unsigned int dst_size)
745 int i;
746 unsigned int last = dst_size / 2;
748 for (i = 0; i < last; i++) {
749 *dst++ = src[i] & 0x7f;
750 *dst++ = src[i] >> 7;
752 return dst;
755 static
756 unsigned char *
757 demunge_buf (unsigned char *src, unsigned char *dst, unsigned int src_bytes)
760 int i;
761 unsigned char *end = src + src_bytes;
763 end = src + src_bytes;
765 /* NOTE: src and dst *CAN* point to the same address */
767 for (i = 0; src != end; i++) {
768 dst[i] = *src++;
769 dst[i] |= (*src++)<<7;
772 return dst;
775 /***********************************************************************
776 WaveFront: sample, patch and program management.
777 ***********************************************************************/
779 static int
780 wavefront_delete_sample (int sample_num)
783 unsigned char wbuf[2];
784 int x;
786 wbuf[0] = sample_num & 0x7f;
787 wbuf[1] = sample_num >> 7;
789 if ((x = wavefront_cmd (WFC_DELETE_SAMPLE, 0, wbuf)) == 0) {
790 dev.sample_status[sample_num] = WF_ST_EMPTY;
793 return x;
796 static int
797 wavefront_get_sample_status (int assume_rom)
800 int i;
801 unsigned char rbuf[32], wbuf[32];
802 unsigned int sc_real, sc_alias, sc_multi;
804 /* check sample status */
806 if (wavefront_cmd (WFC_GET_NSAMPLES, rbuf, wbuf)) {
807 printk (KERN_WARNING LOGNAME "cannot request sample count.\n");
808 return -1;
811 sc_real = sc_alias = sc_multi = dev.samples_used = 0;
813 for (i = 0; i < WF_MAX_SAMPLE; i++) {
815 wbuf[0] = i & 0x7f;
816 wbuf[1] = i >> 7;
818 if (wavefront_cmd (WFC_IDENTIFY_SAMPLE_TYPE, rbuf, wbuf)) {
819 printk (KERN_WARNING LOGNAME
820 "cannot identify sample "
821 "type of slot %d\n", i);
822 dev.sample_status[i] = WF_ST_EMPTY;
823 continue;
826 dev.sample_status[i] = (WF_SLOT_FILLED|rbuf[0]);
828 if (assume_rom) {
829 dev.sample_status[i] |= WF_SLOT_ROM;
832 switch (rbuf[0] & WF_ST_MASK) {
833 case WF_ST_SAMPLE:
834 sc_real++;
835 break;
836 case WF_ST_MULTISAMPLE:
837 sc_multi++;
838 break;
839 case WF_ST_ALIAS:
840 sc_alias++;
841 break;
842 case WF_ST_EMPTY:
843 break;
845 default:
846 printk (KERN_WARNING LOGNAME "unknown sample type for "
847 "slot %d (0x%x)\n",
848 i, rbuf[0]);
851 if (rbuf[0] != WF_ST_EMPTY) {
852 dev.samples_used++;
856 printk (KERN_INFO LOGNAME
857 "%d samples used (%d real, %d aliases, %d multi), "
858 "%d empty\n", dev.samples_used, sc_real, sc_alias, sc_multi,
859 WF_MAX_SAMPLE - dev.samples_used);
862 return (0);
866 static int
867 wavefront_get_patch_status (void)
870 unsigned char patchbuf[WF_PATCH_BYTES];
871 unsigned char patchnum[2];
872 wavefront_patch *p;
873 int i, x, cnt, cnt2;
875 for (i = 0; i < WF_MAX_PATCH; i++) {
876 patchnum[0] = i & 0x7f;
877 patchnum[1] = i >> 7;
879 if ((x = wavefront_cmd (WFC_UPLOAD_PATCH, patchbuf,
880 patchnum)) == 0) {
882 dev.patch_status[i] |= WF_SLOT_FILLED;
883 p = (wavefront_patch *) patchbuf;
884 dev.sample_status
885 [p->sample_number|(p->sample_msb<<7)] |=
886 WF_SLOT_USED;
888 } else if (x == 3) { /* Bad patch number */
889 dev.patch_status[i] = 0;
890 } else {
891 printk (KERN_ERR LOGNAME "upload patch "
892 "error 0x%x\n", x);
893 dev.patch_status[i] = 0;
894 return 1;
898 /* program status has already filled in slot_used bits */
900 for (i = 0, cnt = 0, cnt2 = 0; i < WF_MAX_PATCH; i++) {
901 if (dev.patch_status[i] & WF_SLOT_FILLED) {
902 cnt++;
904 if (dev.patch_status[i] & WF_SLOT_USED) {
905 cnt2++;
909 printk (KERN_INFO LOGNAME
910 "%d patch slots filled, %d in use\n", cnt, cnt2);
912 return (0);
915 static int
916 wavefront_get_program_status (void)
919 unsigned char progbuf[WF_PROGRAM_BYTES];
920 wavefront_program prog;
921 unsigned char prognum;
922 int i, x, l, cnt;
924 for (i = 0; i < WF_MAX_PROGRAM; i++) {
925 prognum = i;
927 if ((x = wavefront_cmd (WFC_UPLOAD_PROGRAM, progbuf,
928 &prognum)) == 0) {
930 dev.prog_status[i] |= WF_SLOT_USED;
932 demunge_buf (progbuf, (unsigned char *) &prog,
933 WF_PROGRAM_BYTES);
935 for (l = 0; l < WF_NUM_LAYERS; l++) {
936 if (prog.layer[l].mute) {
937 dev.patch_status
938 [prog.layer[l].patch_number] |=
939 WF_SLOT_USED;
942 } else if (x == 1) { /* Bad program number */
943 dev.prog_status[i] = 0;
944 } else {
945 printk (KERN_ERR LOGNAME "upload program "
946 "error 0x%x\n", x);
947 dev.prog_status[i] = 0;
951 for (i = 0, cnt = 0; i < WF_MAX_PROGRAM; i++) {
952 if (dev.prog_status[i]) {
953 cnt++;
957 printk (KERN_INFO LOGNAME "%d programs slots in use\n", cnt);
959 return (0);
962 static int
963 wavefront_send_patch (wavefront_patch_info *header)
966 unsigned char buf[WF_PATCH_BYTES+2];
967 unsigned char *bptr;
969 DPRINT (WF_DEBUG_LOAD_PATCH, "downloading patch %d\n",
970 header->number);
972 dev.patch_status[header->number] |= WF_SLOT_FILLED;
974 bptr = buf;
975 bptr = munge_int32 (header->number, buf, 2);
976 munge_buf ((unsigned char *)&header->hdr.p, bptr, WF_PATCH_BYTES);
978 if (wavefront_cmd (WFC_DOWNLOAD_PATCH, 0, buf)) {
979 printk (KERN_ERR LOGNAME "download patch failed\n");
980 return -(EIO);
983 return (0);
986 static int
987 wavefront_send_program (wavefront_patch_info *header)
990 unsigned char buf[WF_PROGRAM_BYTES+1];
991 int i;
993 DPRINT (WF_DEBUG_LOAD_PATCH, "downloading program %d\n",
994 header->number);
996 dev.prog_status[header->number] = WF_SLOT_USED;
998 /* XXX need to zero existing SLOT_USED bit for program_status[i]
999 where `i' is the program that's being (potentially) overwritten.
1002 for (i = 0; i < WF_NUM_LAYERS; i++) {
1003 if (header->hdr.pr.layer[i].mute) {
1004 dev.patch_status[header->hdr.pr.layer[i].patch_number] |=
1005 WF_SLOT_USED;
1007 /* XXX need to mark SLOT_USED for sample used by
1008 patch_number, but this means we have to load it. Ick.
1013 buf[0] = header->number;
1014 munge_buf ((unsigned char *)&header->hdr.pr, &buf[1], WF_PROGRAM_BYTES);
1016 if (wavefront_cmd (WFC_DOWNLOAD_PROGRAM, 0, buf)) {
1017 printk (KERN_WARNING LOGNAME "download patch failed\n");
1018 return -(EIO);
1021 return (0);
1024 static int
1025 wavefront_freemem (void)
1028 char rbuf[8];
1030 if (wavefront_cmd (WFC_REPORT_FREE_MEMORY, rbuf, 0)) {
1031 printk (KERN_WARNING LOGNAME "can't get memory stats.\n");
1032 return -1;
1033 } else {
1034 return demunge_int32 (rbuf, 4);
1038 static int
1039 wavefront_send_sample (wavefront_patch_info *header,
1040 UINT16 *dataptr,
1041 int data_is_unsigned)
1044 /* samples are downloaded via a 16-bit wide i/o port
1045 (you could think of it as 2 adjacent 8-bit wide ports
1046 but its less efficient that way). therefore, all
1047 the blocksizes and so forth listed in the documentation,
1048 and used conventionally to refer to sample sizes,
1049 which are given in 8-bit units (bytes), need to be
1050 divided by 2.
1053 UINT16 sample_short;
1054 UINT32 length;
1055 UINT16 *data_end = 0;
1056 unsigned int i;
1057 const int max_blksize = 4096/2;
1058 unsigned int written;
1059 unsigned int blocksize;
1060 int dma_ack;
1061 int blocknum;
1062 unsigned char sample_hdr[WF_SAMPLE_HDR_BYTES];
1063 unsigned char *shptr;
1064 int skip = 0;
1065 int initial_skip = 0;
1067 DPRINT (WF_DEBUG_LOAD_PATCH, "sample %sdownload for slot %d, "
1068 "type %d, %d bytes from 0x%x\n",
1069 header->size ? "" : "header ",
1070 header->number, header->subkey,
1071 header->size,
1072 (int) header->dataptr);
1074 if (header->number == WAVEFRONT_FIND_FREE_SAMPLE_SLOT) {
1075 int x;
1077 if ((x = wavefront_find_free_sample ()) < 0) {
1078 return -ENOMEM;
1080 printk (KERN_DEBUG LOGNAME "unspecified sample => %d\n", x);
1081 header->number = x;
1084 if (header->size) {
1086 /* XXX its a debatable point whether or not RDONLY semantics
1087 on the ROM samples should cover just the sample data or
1088 the sample header. For now, it only covers the sample data,
1089 so anyone is free at all times to rewrite sample headers.
1091 My reason for this is that we have the sample headers
1092 available in the WFB file for General MIDI, and so these
1093 can always be reset if needed. The sample data, however,
1094 cannot be recovered without a complete reset and firmware
1095 reload of the ICS2115, which is a very expensive operation.
1097 So, doing things this way allows us to honor the notion of
1098 "RESETSAMPLES" reasonably cheaply. Note however, that this
1099 is done purely at user level: there is no WFB parser in
1100 this driver, and so a complete reset (back to General MIDI,
1101 or theoretically some other configuration) is the
1102 responsibility of the user level library.
1104 To try to do this in the kernel would be a little
1105 crazy: we'd need 158K of kernel space just to hold
1106 a copy of the patch/program/sample header data.
1109 if (dev.rom_samples_rdonly) {
1110 if (dev.sample_status[header->number] & WF_SLOT_ROM) {
1111 printk (KERN_ERR LOGNAME "sample slot %d "
1112 "write protected\n",
1113 header->number);
1114 return -EACCES;
1118 wavefront_delete_sample (header->number);
1121 if (header->size) {
1122 dev.freemem = wavefront_freemem ();
1124 if (dev.freemem < header->size) {
1125 printk (KERN_ERR LOGNAME
1126 "insufficient memory to "
1127 "load %d byte sample.\n",
1128 header->size);
1129 return -ENOMEM;
1134 skip = WF_GET_CHANNEL(&header->hdr.s);
1136 if (skip > 0 && header->hdr.s.SampleResolution != LINEAR_16BIT) {
1137 printk (KERN_ERR LOGNAME "channel selection only "
1138 "possible on 16-bit samples");
1139 return -(EINVAL);
1142 switch (skip) {
1143 case 0:
1144 initial_skip = 0;
1145 skip = 1;
1146 break;
1147 case 1:
1148 initial_skip = 0;
1149 skip = 2;
1150 break;
1151 case 2:
1152 initial_skip = 1;
1153 skip = 2;
1154 break;
1155 case 3:
1156 initial_skip = 2;
1157 skip = 3;
1158 break;
1159 case 4:
1160 initial_skip = 3;
1161 skip = 4;
1162 break;
1163 case 5:
1164 initial_skip = 4;
1165 skip = 5;
1166 break;
1167 case 6:
1168 initial_skip = 5;
1169 skip = 6;
1170 break;
1173 DPRINT (WF_DEBUG_LOAD_PATCH, "channel selection: %d => "
1174 "initial skip = %d, skip = %d\n",
1175 WF_GET_CHANNEL (&header->hdr.s),
1176 initial_skip, skip);
1178 /* Be safe, and zero the "Unused" bits ... */
1180 WF_SET_CHANNEL(&header->hdr.s, 0);
1182 /* adjust size for 16 bit samples by dividing by two. We always
1183 send 16 bits per write, even for 8 bit samples, so the length
1184 is always half the size of the sample data in bytes.
1187 length = header->size / 2;
1189 /* the data we're sent has not been munged, and in fact, the
1190 header we have to send isn't just a munged copy either.
1191 so, build the sample header right here.
1194 shptr = &sample_hdr[0];
1196 shptr = munge_int32 (header->number, shptr, 2);
1198 if (header->size) {
1199 shptr = munge_int32 (length, shptr, 4);
1202 /* Yes, a 4 byte result doesn't contain all of the offset bits,
1203 but the offset only uses 24 bits.
1206 shptr = munge_int32 (*((UINT32 *) &header->hdr.s.sampleStartOffset),
1207 shptr, 4);
1208 shptr = munge_int32 (*((UINT32 *) &header->hdr.s.loopStartOffset),
1209 shptr, 4);
1210 shptr = munge_int32 (*((UINT32 *) &header->hdr.s.loopEndOffset),
1211 shptr, 4);
1212 shptr = munge_int32 (*((UINT32 *) &header->hdr.s.sampleEndOffset),
1213 shptr, 4);
1215 /* This one is truly wierd. What kind of wierdo decided that in
1216 a system dominated by 16 and 32 bit integers, they would use
1217 a just 12 bits ?
1220 shptr = munge_int32 (header->hdr.s.FrequencyBias, shptr, 3);
1222 /* Why is this nybblified, when the MSB is *always* zero ?
1223 Anyway, we can't take address of bitfield, so make a
1224 good-faith guess at where it starts.
1227 shptr = munge_int32 (*(&header->hdr.s.FrequencyBias+1),
1228 shptr, 2);
1230 if (wavefront_cmd (header->size ?
1231 WFC_DOWNLOAD_SAMPLE : WFC_DOWNLOAD_SAMPLE_HEADER,
1232 0, sample_hdr)) {
1233 printk (KERN_WARNING LOGNAME "sample %sdownload refused.\n",
1234 header->size ? "" : "header ");
1235 return -(EIO);
1238 if (header->size == 0) {
1239 goto sent; /* Sorry. Just had to have one somewhere */
1242 data_end = dataptr + length;
1244 /* Do any initial skip over an unused channel's data */
1246 dataptr += initial_skip;
1248 for (written = 0, blocknum = 0;
1249 written < length; written += max_blksize, blocknum++) {
1251 if ((length - written) > max_blksize) {
1252 blocksize = max_blksize;
1253 } else {
1254 /* round to nearest 16-byte value */
1255 blocksize = ((length-written+7)&~0x7);
1258 if (wavefront_cmd (WFC_DOWNLOAD_BLOCK, 0, 0)) {
1259 printk (KERN_WARNING LOGNAME "download block "
1260 "request refused.\n");
1261 return -(EIO);
1264 for (i = 0; i < blocksize; i++) {
1266 if (dataptr < data_end) {
1268 __get_user (sample_short, dataptr);
1269 dataptr += skip;
1271 if (data_is_unsigned) { /* GUS ? */
1273 if (WF_SAMPLE_IS_8BIT(&header->hdr.s)) {
1275 /* 8 bit sample
1276 resolution, sign
1277 extend both bytes.
1280 ((unsigned char*)
1281 &sample_short)[0] += 0x7f;
1282 ((unsigned char*)
1283 &sample_short)[1] += 0x7f;
1285 } else {
1287 /* 16 bit sample
1288 resolution, sign
1289 extend the MSB.
1292 sample_short += 0x7fff;
1296 } else {
1298 /* In padding section of final block:
1300 Don't fetch unsupplied data from
1301 user space, just continue with
1302 whatever the final value was.
1306 if (i < blocksize - 1) {
1307 outw (sample_short, dev.block_port);
1308 } else {
1309 outw (sample_short, dev.last_block_port);
1313 /* Get "DMA page acknowledge", even though its really
1314 nothing to do with DMA at all.
1317 if ((dma_ack = wavefront_read ()) != WF_DMA_ACK) {
1318 if (dma_ack == -1) {
1319 printk (KERN_ERR LOGNAME "upload sample "
1320 "DMA ack timeout\n");
1321 return -(EIO);
1322 } else {
1323 printk (KERN_ERR LOGNAME "upload sample "
1324 "DMA ack error 0x%x\n",
1325 dma_ack);
1326 return -(EIO);
1331 dev.sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_SAMPLE);
1333 /* Note, label is here because sending the sample header shouldn't
1334 alter the sample_status info at all.
1337 sent:
1338 return (0);
1341 static int
1342 wavefront_send_alias (wavefront_patch_info *header)
1345 unsigned char alias_hdr[WF_ALIAS_BYTES];
1347 DPRINT (WF_DEBUG_LOAD_PATCH, "download alias, %d is "
1348 "alias for %d\n",
1349 header->number,
1350 header->hdr.a.OriginalSample);
1352 munge_int32 (header->number, &alias_hdr[0], 2);
1353 munge_int32 (header->hdr.a.OriginalSample, &alias_hdr[2], 2);
1354 munge_int32 (*((unsigned int *)&header->hdr.a.sampleStartOffset),
1355 &alias_hdr[4], 4);
1356 munge_int32 (*((unsigned int *)&header->hdr.a.loopStartOffset),
1357 &alias_hdr[8], 4);
1358 munge_int32 (*((unsigned int *)&header->hdr.a.loopEndOffset),
1359 &alias_hdr[12], 4);
1360 munge_int32 (*((unsigned int *)&header->hdr.a.sampleEndOffset),
1361 &alias_hdr[16], 4);
1362 munge_int32 (header->hdr.a.FrequencyBias, &alias_hdr[20], 3);
1363 munge_int32 (*(&header->hdr.a.FrequencyBias+1), &alias_hdr[23], 2);
1365 if (wavefront_cmd (WFC_DOWNLOAD_SAMPLE_ALIAS, 0, alias_hdr)) {
1366 printk (KERN_ERR LOGNAME "download alias failed.\n");
1367 return -(EIO);
1370 dev.sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_ALIAS);
1372 return (0);
1375 static int
1376 wavefront_send_multisample (wavefront_patch_info *header)
1378 int i;
1379 int num_samples;
1380 unsigned char msample_hdr[WF_MSAMPLE_BYTES];
1382 munge_int32 (header->number, &msample_hdr[0], 2);
1384 /* You'll recall at this point that the "number of samples" value
1385 in a wavefront_multisample struct is actually the log2 of the
1386 real number of samples.
1389 num_samples = (1<<(header->hdr.ms.NumberOfSamples&7));
1390 msample_hdr[2] = (unsigned char) header->hdr.ms.NumberOfSamples;
1392 DPRINT (WF_DEBUG_LOAD_PATCH, "multi %d with %d=%d samples\n",
1393 header->number,
1394 header->hdr.ms.NumberOfSamples,
1395 num_samples);
1397 for (i = 0; i < num_samples; i++) {
1398 DPRINT(WF_DEBUG_LOAD_PATCH|WF_DEBUG_DATA, "sample[%d] = %d\n",
1399 i, header->hdr.ms.SampleNumber[i]);
1400 munge_int32 (header->hdr.ms.SampleNumber[i],
1401 &msample_hdr[3+(i*2)], 2);
1404 /* Need a hack here to pass in the number of bytes
1405 to be written to the synth. This is ugly, and perhaps
1406 one day, I'll fix it.
1409 if (wavefront_cmd (WFC_DOWNLOAD_MULTISAMPLE,
1410 (unsigned char *) ((num_samples*2)+3),
1411 msample_hdr)) {
1412 printk (KERN_ERR LOGNAME "download of multisample failed.\n");
1413 return -(EIO);
1416 dev.sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_MULTISAMPLE);
1418 return (0);
1421 static int
1422 wavefront_fetch_multisample (wavefront_patch_info *header)
1424 int i;
1425 unsigned char log_ns[1];
1426 unsigned char number[2];
1427 int num_samples;
1429 munge_int32 (header->number, number, 2);
1431 if (wavefront_cmd (WFC_UPLOAD_MULTISAMPLE, log_ns, number)) {
1432 printk (KERN_ERR LOGNAME "upload multisample failed.\n");
1433 return -(EIO);
1436 DPRINT (WF_DEBUG_DATA, "msample %d has %d samples\n",
1437 header->number, log_ns[0]);
1439 header->hdr.ms.NumberOfSamples = log_ns[0];
1441 /* get the number of samples ... */
1443 num_samples = (1 << log_ns[0]);
1445 for (i = 0; i < num_samples; i++) {
1446 char d[2];
1448 if ((d[0] = wavefront_read ()) == -1) {
1449 printk (KERN_ERR LOGNAME "upload multisample failed "
1450 "during sample loop.\n");
1451 return -(EIO);
1454 if ((d[1] = wavefront_read ()) == -1) {
1455 printk (KERN_ERR LOGNAME "upload multisample failed "
1456 "during sample loop.\n");
1457 return -(EIO);
1460 header->hdr.ms.SampleNumber[i] =
1461 demunge_int32 ((unsigned char *) d, 2);
1463 DPRINT (WF_DEBUG_DATA, "msample sample[%d] = %d\n",
1464 i, header->hdr.ms.SampleNumber[i]);
1467 return (0);
1471 static int
1472 wavefront_send_drum (wavefront_patch_info *header)
1475 unsigned char drumbuf[WF_DRUM_BYTES];
1476 wavefront_drum *drum = &header->hdr.d;
1477 int i;
1479 DPRINT (WF_DEBUG_LOAD_PATCH, "downloading edrum for MIDI "
1480 "note %d, patch = %d\n",
1481 header->number, drum->PatchNumber);
1483 drumbuf[0] = header->number & 0x7f;
1485 for (i = 0; i < 4; i++) {
1486 munge_int32 (((unsigned char *)drum)[i], &drumbuf[1+(i*2)], 2);
1489 if (wavefront_cmd (WFC_DOWNLOAD_EDRUM_PROGRAM, 0, drumbuf)) {
1490 printk (KERN_ERR LOGNAME "download drum failed.\n");
1491 return -(EIO);
1494 return (0);
1497 static int
1498 wavefront_find_free_sample (void)
1501 int i;
1503 for (i = 0; i < WF_MAX_SAMPLE; i++) {
1504 if (!(dev.sample_status[i] & WF_SLOT_FILLED)) {
1505 return i;
1508 printk (KERN_WARNING LOGNAME "no free sample slots!\n");
1509 return -1;
1512 static int
1513 wavefront_find_free_patch (void)
1516 int i;
1518 for (i = 0; i < WF_MAX_PATCH; i++) {
1519 if (!(dev.patch_status[i] & WF_SLOT_FILLED)) {
1520 return i;
1523 printk (KERN_WARNING LOGNAME "no free patch slots!\n");
1524 return -1;
1527 static int
1528 log2_2048(int n)
1531 int tbl[]={0, 0, 2048, 3246, 4096, 4755, 5294, 5749, 6143,
1532 6492, 6803, 7084, 7342, 7578, 7797, 8001, 8192,
1533 8371, 8540, 8699, 8851, 8995, 9132, 9264, 9390,
1534 9510, 9626, 9738, 9845, 9949, 10049, 10146};
1535 int i;
1537 /* Returns 2048*log2(n) */
1539 /* FIXME: this is like doing integer math
1540 on quantum particles (RuN) */
1542 i=0;
1543 while(n>=32*256) {
1544 n>>=8;
1545 i+=2048*8;
1547 while(n>=32) {
1548 n>>=1;
1549 i+=2048;
1551 i+=tbl[n];
1552 return(i);
1555 static int
1556 wavefront_load_gus_patch (int devno, int format, const char *addr,
1557 int offs, int count, int pmgr_flag)
1559 struct patch_info guspatch;
1560 wavefront_patch_info samp, pat, prog;
1561 wavefront_patch *patp;
1562 wavefront_sample *sampp;
1563 wavefront_program *progp;
1565 int i,base_note;
1566 long sizeof_patch;
1568 /* Copy in the header of the GUS patch */
1570 sizeof_patch = (long) &guspatch.data[0] - (long) &guspatch;
1571 copy_from_user (&((char *) &guspatch)[offs],
1572 &(addr)[offs], sizeof_patch - offs);
1574 if ((i = wavefront_find_free_patch ()) == -1) {
1575 return -EBUSY;
1577 pat.number = i;
1578 pat.subkey = WF_ST_PATCH;
1579 patp = &pat.hdr.p;
1581 if ((i = wavefront_find_free_sample ()) == -1) {
1582 return -EBUSY;
1584 samp.number = i;
1585 samp.subkey = WF_ST_SAMPLE;
1586 samp.size = guspatch.len;
1587 sampp = &samp.hdr.s;
1589 prog.number = guspatch.instr_no;
1590 progp = &prog.hdr.pr;
1592 /* Setup the patch structure */
1594 patp->amplitude_bias=guspatch.volume;
1595 patp->portamento=0;
1596 patp->sample_number= samp.number & 0xff;
1597 patp->sample_msb= samp.number>>8;
1598 patp->pitch_bend= /*12*/ 0;
1599 patp->mono=1;
1600 patp->retrigger=1;
1601 patp->nohold=(guspatch.mode & WAVE_SUSTAIN_ON) ? 0:1;
1602 patp->frequency_bias=0;
1603 patp->restart=0;
1604 patp->reuse=0;
1605 patp->reset_lfo=1;
1606 patp->fm_src2=0;
1607 patp->fm_src1=WF_MOD_MOD_WHEEL;
1608 patp->am_src=WF_MOD_PRESSURE;
1609 patp->am_amount=127;
1610 patp->fc1_mod_amount=0;
1611 patp->fc2_mod_amount=0;
1612 patp->fm_amount1=0;
1613 patp->fm_amount2=0;
1614 patp->envelope1.attack_level=127;
1615 patp->envelope1.decay1_level=127;
1616 patp->envelope1.decay2_level=127;
1617 patp->envelope1.sustain_level=127;
1618 patp->envelope1.release_level=0;
1619 patp->envelope2.attack_velocity=127;
1620 patp->envelope2.attack_level=127;
1621 patp->envelope2.decay1_level=127;
1622 patp->envelope2.decay2_level=127;
1623 patp->envelope2.sustain_level=127;
1624 patp->envelope2.release_level=0;
1625 patp->envelope2.attack_velocity=127;
1626 patp->randomizer=0;
1628 /* Program for this patch */
1630 progp->layer[0].patch_number= pat.number; /* XXX is this right ? */
1631 progp->layer[0].mute=1;
1632 progp->layer[0].pan_or_mod=1;
1633 progp->layer[0].pan=7;
1634 progp->layer[0].mix_level=127 /* guspatch.volume */;
1635 progp->layer[0].split_type=0;
1636 progp->layer[0].split_point=0;
1637 progp->layer[0].play_below=0;
1639 for (i = 1; i < 4; i++) {
1640 progp->layer[i].mute=0;
1643 /* Sample data */
1645 sampp->SampleResolution=((~guspatch.mode & WAVE_16_BITS)<<1);
1647 for (base_note=0;
1648 note_to_freq (base_note) < guspatch.base_note;
1649 base_note++);
1651 if ((guspatch.base_note-note_to_freq(base_note))
1652 >(note_to_freq(base_note)-guspatch.base_note))
1653 base_note++;
1655 printk(KERN_DEBUG "ref freq=%d,base note=%d\n",
1656 guspatch.base_freq,
1657 base_note);
1659 sampp->FrequencyBias = (29550 - log2_2048(guspatch.base_freq)
1660 + base_note*171);
1661 printk(KERN_DEBUG "Freq Bias is %d\n", sampp->FrequencyBias);
1662 sampp->Loop=(guspatch.mode & WAVE_LOOPING) ? 1:0;
1663 sampp->sampleStartOffset.Fraction=0;
1664 sampp->sampleStartOffset.Integer=0;
1665 sampp->loopStartOffset.Fraction=0;
1666 sampp->loopStartOffset.Integer=guspatch.loop_start
1667 >>((guspatch.mode&WAVE_16_BITS) ? 1:0);
1668 sampp->loopEndOffset.Fraction=0;
1669 sampp->loopEndOffset.Integer=guspatch.loop_end
1670 >>((guspatch.mode&WAVE_16_BITS) ? 1:0);
1671 sampp->sampleEndOffset.Fraction=0;
1672 sampp->sampleEndOffset.Integer=guspatch.len >> (guspatch.mode&1);
1673 sampp->Bidirectional=(guspatch.mode&WAVE_BIDIR_LOOP) ? 1:0;
1674 sampp->Reverse=(guspatch.mode&WAVE_LOOP_BACK) ? 1:0;
1676 /* Now ship it down */
1678 wavefront_send_sample (&samp,
1679 (unsigned short *) &(addr)[sizeof_patch],
1680 (guspatch.mode & WAVE_UNSIGNED) ? 1:0);
1681 wavefront_send_patch (&pat);
1682 wavefront_send_program (&prog);
1684 /* Now pan as best we can ... use the slave/internal MIDI device
1685 number if it exists (since it talks to the WaveFront), or the
1686 master otherwise.
1689 #ifdef CONFIG_MIDI
1690 if (dev.mididev > 0) {
1691 midi_synth_controller (dev.mididev, guspatch.instr_no, 10,
1692 ((guspatch.panning << 4) > 127) ?
1693 127 : (guspatch.panning << 4));
1695 #endif CONFIG_MIDI
1697 return(0);
1700 static int
1701 wavefront_load_patch (const char *addr)
1705 wavefront_patch_info header;
1707 if (copy_from_user (&header, addr, sizeof(wavefront_patch_info) -
1708 sizeof(wavefront_any))) {
1709 printk (KERN_WARNING LOGNAME "bad address for load patch.\n");
1710 return -(EINVAL);
1713 DPRINT (WF_DEBUG_LOAD_PATCH, "download "
1714 "Sample type: %d "
1715 "Sample number: %d "
1716 "Sample size: %d\n",
1717 header.subkey,
1718 header.number,
1719 header.size);
1721 switch (header.subkey) {
1722 case WF_ST_SAMPLE: /* sample or sample_header, based on patch->size */
1724 copy_from_user ((unsigned char *) &header.hdr.s,
1725 (unsigned char *) header.hdrptr,
1726 sizeof (wavefront_sample));
1728 return wavefront_send_sample (&header, header.dataptr, 0);
1730 case WF_ST_MULTISAMPLE:
1732 copy_from_user ((unsigned char *) &header.hdr.s,
1733 (unsigned char *) header.hdrptr,
1734 sizeof (wavefront_multisample));
1736 return wavefront_send_multisample (&header);
1739 case WF_ST_ALIAS:
1741 copy_from_user ((unsigned char *) &header.hdr.a,
1742 (unsigned char *) header.hdrptr,
1743 sizeof (wavefront_alias));
1745 return wavefront_send_alias (&header);
1747 case WF_ST_DRUM:
1748 copy_from_user ((unsigned char *) &header.hdr.d,
1749 (unsigned char *) header.hdrptr,
1750 sizeof (wavefront_drum));
1752 return wavefront_send_drum (&header);
1754 case WF_ST_PATCH:
1755 copy_from_user ((unsigned char *) &header.hdr.p,
1756 (unsigned char *) header.hdrptr,
1757 sizeof (wavefront_patch));
1759 return wavefront_send_patch (&header);
1761 case WF_ST_PROGRAM:
1762 copy_from_user ((unsigned char *) &header.hdr.pr,
1763 (unsigned char *) header.hdrptr,
1764 sizeof (wavefront_program));
1766 return wavefront_send_program (&header);
1768 default:
1769 printk (KERN_ERR LOGNAME "unknown patch type %d.\n",
1770 header.subkey);
1771 return -(EINVAL);
1774 return 0;
1777 /***********************************************************************
1778 WaveFront: /dev/sequencer{,2} and other hardware-dependent interfaces
1779 ***********************************************************************/
1781 static void
1782 process_sample_hdr (UCHAR8 *buf)
1785 wavefront_sample s;
1786 UCHAR8 *ptr;
1788 ptr = buf;
1790 /* The board doesn't send us an exact copy of a "wavefront_sample"
1791 in response to an Upload Sample Header command. Instead, we
1792 have to convert the data format back into our data structure,
1793 just as in the Download Sample command, where we have to do
1794 something very similar in the reverse direction.
1797 *((UINT32 *) &s.sampleStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
1798 *((UINT32 *) &s.loopStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
1799 *((UINT32 *) &s.loopEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
1800 *((UINT32 *) &s.sampleEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
1801 *((UINT32 *) &s.FrequencyBias) = demunge_int32 (ptr, 3); ptr += 3;
1803 s.SampleResolution = *ptr & 0x3;
1804 s.Loop = *ptr & 0x8;
1805 s.Bidirectional = *ptr & 0x10;
1806 s.Reverse = *ptr & 0x40;
1808 /* Now copy it back to where it came from */
1810 memcpy (buf, (unsigned char *) &s, sizeof (wavefront_sample));
1813 static int
1814 wavefront_synth_control (int cmd, wavefront_control *wc)
1817 unsigned char patchnumbuf[2];
1818 int i;
1820 DPRINT (WF_DEBUG_CMD, "synth control with "
1821 "cmd 0x%x\n", wc->cmd);
1823 /* Pre-handling of or for various commands */
1825 switch (wc->cmd) {
1826 case WFC_DISABLE_INTERRUPTS:
1827 printk (KERN_INFO LOGNAME "interrupts disabled.\n");
1828 outb (0x80|0x20, dev.control_port);
1829 dev.interrupts_on = 0;
1830 return 0;
1832 case WFC_ENABLE_INTERRUPTS:
1833 printk (KERN_INFO LOGNAME "interrupts enabled.\n");
1834 outb (0x80|0x40|0x20, dev.control_port);
1835 dev.interrupts_on = 1;
1836 return 0;
1838 case WFC_INTERRUPT_STATUS:
1839 wc->rbuf[0] = dev.interrupts_on;
1840 return 0;
1842 case WFC_ROMSAMPLES_RDONLY:
1843 dev.rom_samples_rdonly = wc->wbuf[0];
1844 wc->status = 0;
1845 return 0;
1847 case WFC_IDENTIFY_SLOT_TYPE:
1848 i = wc->wbuf[0] | (wc->wbuf[1] << 7);
1849 if (i <0 || i >= WF_MAX_SAMPLE) {
1850 printk (KERN_WARNING LOGNAME "invalid slot ID %d\n",
1852 wc->status = EINVAL;
1853 return 0;
1855 wc->rbuf[0] = dev.sample_status[i];
1856 wc->status = 0;
1857 return 0;
1859 case WFC_DEBUG_DRIVER:
1860 dev.debug = wc->wbuf[0];
1861 printk (KERN_INFO LOGNAME "debug = 0x%x\n", dev.debug);
1862 return 0;
1864 case WFC_FX_IOCTL:
1865 wffx_ioctl ((wavefront_fx_info *) &wc->wbuf[0]);
1866 return 0;
1868 case WFC_UPLOAD_PATCH:
1869 munge_int32 (*((UINT32 *) wc->wbuf), patchnumbuf, 2);
1870 memcpy (wc->wbuf, patchnumbuf, 2);
1871 break;
1873 case WFC_UPLOAD_MULTISAMPLE:
1874 /* multisamples have to be handled differently, and
1875 cannot be dealt with properly by wavefront_cmd() alone.
1877 wc->status = wavefront_fetch_multisample
1878 ((wavefront_patch_info *) wc->rbuf);
1879 return 0;
1881 case WFC_UPLOAD_SAMPLE_ALIAS:
1882 printk (KERN_INFO LOGNAME "support for sample alias upload "
1883 "being considered.\n");
1884 wc->status = EINVAL;
1885 return -EINVAL;
1888 wc->status = wavefront_cmd (wc->cmd, wc->rbuf, wc->wbuf);
1890 /* Post-handling of certain commands.
1892 In particular, if the command was an upload, demunge the data
1893 so that the user-level doesn't have to think about it.
1896 if (wc->status == 0) {
1897 switch (wc->cmd) {
1898 /* intercept any freemem requests so that we know
1899 we are always current with the user-level view
1900 of things.
1903 case WFC_REPORT_FREE_MEMORY:
1904 dev.freemem = demunge_int32 (wc->rbuf, 4);
1905 break;
1907 case WFC_UPLOAD_PATCH:
1908 demunge_buf (wc->rbuf, wc->rbuf, WF_PATCH_BYTES);
1909 break;
1911 case WFC_UPLOAD_PROGRAM:
1912 demunge_buf (wc->rbuf, wc->rbuf, WF_PROGRAM_BYTES);
1913 break;
1915 case WFC_UPLOAD_EDRUM_PROGRAM:
1916 demunge_buf (wc->rbuf, wc->rbuf, WF_DRUM_BYTES - 1);
1917 break;
1919 case WFC_UPLOAD_SAMPLE_HEADER:
1920 process_sample_hdr (wc->rbuf);
1921 break;
1923 case WFC_UPLOAD_SAMPLE_ALIAS:
1924 printk (KERN_INFO LOGNAME "support for "
1925 "sample aliases still "
1926 "being considered.\n");
1927 break;
1929 case WFC_VMIDI_OFF:
1930 if (virtual_midi_disable () < 0) {
1931 return -(EIO);
1933 break;
1935 case WFC_VMIDI_ON:
1936 if (virtual_midi_enable () < 0) {
1937 return -(EIO);
1939 break;
1943 return 0;
1947 /***********************************************************************/
1948 /* WaveFront: Linux file system interface (for access via raw synth) */
1949 /***********************************************************************/
1951 static loff_t
1952 wavefront_llseek(struct file *file, loff_t offset, int origin)
1954 return -ESPIPE;
1957 static int
1958 wavefront_open (struct inode *inode, struct file *file)
1960 /* XXX fix me */
1961 dev.opened = file->f_flags;
1962 MOD_INC_USE_COUNT;
1963 return 0;
1966 static int
1967 wavefront_release(struct inode *inode, struct file *file)
1969 dev.opened = 0;
1970 dev.debug = 0;
1971 MOD_DEC_USE_COUNT;
1972 return 0;
1975 static int
1976 wavefront_ioctl(struct inode *inode, struct file *file,
1977 unsigned int cmd, unsigned long arg)
1979 wavefront_control wc;
1980 int err;
1982 switch (cmd) {
1984 case WFCTL_WFCMD:
1985 copy_from_user (&wc, (void *) arg, sizeof (wc));
1987 if ((err = wavefront_synth_control (cmd, &wc)) == 0) {
1988 copy_to_user ((void *) arg, &wc, sizeof (wc));
1991 return err;
1993 case WFCTL_LOAD_SPP:
1994 return wavefront_load_patch ((const char *) arg);
1996 default:
1997 printk (KERN_WARNING LOGNAME "invalid ioctl %#x\n", cmd);
1998 return -(EINVAL);
2001 return 0;
2004 static /*const*/ struct file_operations wavefront_fops = {
2005 &wavefront_llseek,
2006 NULL, /* read */
2007 NULL, /* write */
2008 NULL, /* readdir */
2009 NULL, /* poll */
2010 &wavefront_ioctl,
2011 NULL, /* mmap */
2012 &wavefront_open,
2013 NULL, /* flush */
2014 &wavefront_release,
2015 NULL, /* fsync */
2016 NULL, /* fasync */
2017 NULL, /* check_media_change */
2018 NULL, /* revalidate */
2019 NULL, /* lock */
2023 /***********************************************************************/
2024 /* WaveFront: OSS installation and support interface */
2025 /***********************************************************************/
2027 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2029 static struct synth_info wavefront_info =
2030 {"Turtle Beach WaveFront", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_WAVEFRONT,
2031 0, 32, 0, 0, SYNTH_CAP_INPUT};
2033 static int
2034 wavefront_oss_open (int devno, int mode)
2037 dev.opened = mode;
2038 return 0;
2041 static void
2042 wavefront_oss_close (int devno)
2045 dev.opened = 0;
2046 dev.debug = 0;
2047 return;
2050 static int
2051 wavefront_oss_ioctl (int devno, unsigned int cmd, caddr_t arg)
2054 wavefront_control wc;
2055 int err;
2057 switch (cmd) {
2058 case SNDCTL_SYNTH_INFO:
2059 memcpy (&((char *) arg)[0], &wavefront_info,
2060 sizeof (wavefront_info));
2061 return 0;
2062 break;
2064 case SNDCTL_SEQ_RESETSAMPLES:
2065 printk (KERN_WARNING LOGNAME "driver cannot reset samples.\n");
2066 return 0; /* don't force an error */
2067 break;
2069 case SNDCTL_SEQ_PERCMODE:
2070 return 0; /* don't force an error */
2071 break;
2073 case SNDCTL_SYNTH_MEMAVL:
2074 if ((dev.freemem = wavefront_freemem ()) < 0) {
2075 printk (KERN_ERR LOGNAME "cannot get memory size\n");
2076 return -EIO;
2077 } else {
2078 return dev.freemem;
2080 break;
2082 case SNDCTL_SYNTH_CONTROL:
2083 copy_from_user (&wc, arg, sizeof (wc));
2085 if ((err = wavefront_synth_control (cmd, &wc)) == 0) {
2086 copy_to_user (arg, &wc, sizeof (wc));
2089 return err;
2091 default:
2092 return -(EINVAL);
2097 wavefront_oss_load_patch (int devno, int format, const char *addr,
2098 int offs, int count, int pmgr_flag)
2101 if (format == SYSEX_PATCH) { /* Handled by midi_synth.c */
2102 if (midi_load_patch == NULL) {
2103 printk (KERN_ERR LOGNAME
2104 "SYSEX not loadable: "
2105 "no midi patch loader!\n");
2106 return -(EINVAL);
2109 return midi_load_patch (devno, format, addr,
2110 offs, count, pmgr_flag);
2112 } else if (format == GUS_PATCH) {
2113 return wavefront_load_gus_patch (devno, format,
2114 addr, offs, count, pmgr_flag);
2116 } else if (format != WAVEFRONT_PATCH) {
2117 printk (KERN_ERR LOGNAME "unknown patch format %d\n", format);
2118 return -(EINVAL);
2121 if (count < sizeof (wavefront_patch_info)) {
2122 printk (KERN_ERR LOGNAME "sample header too short\n");
2123 return -(EINVAL);
2126 /* "addr" points to a user-space wavefront_patch_info */
2128 return wavefront_load_patch (addr);
2131 static struct synth_operations wavefront_operations =
2133 "WaveFront",
2134 &wavefront_info,
2136 SYNTH_TYPE_SAMPLE,
2137 SAMPLE_TYPE_WAVEFRONT,
2138 wavefront_oss_open,
2139 wavefront_oss_close,
2140 wavefront_oss_ioctl,
2142 midi_synth_kill_note,
2143 midi_synth_start_note,
2144 midi_synth_set_instr,
2145 midi_synth_reset,
2146 NULL, /* hw_control */
2147 midi_synth_load_patch,
2148 midi_synth_aftertouch,
2149 midi_synth_controller,
2150 midi_synth_panning,
2151 NULL, /* volume method */
2152 midi_synth_bender,
2153 NULL, /* alloc voice */
2154 midi_synth_setup_voice
2156 #endif OSS_SUPPORT_SEQ
2158 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_STATIC_INSTALL
2160 void attach_wavefront (struct address_info *hw_config)
2163 (void) install_wavefront ();
2166 int probe_wavefront (struct address_info *hw_config)
2169 return !detect_wavefront (hw_config->irq, hw_config->io_base);
2172 void unload_wavefront (struct address_info *hw_config)
2174 (void) uninstall_wavefront ();
2177 #endif OSS_SUPPORT_STATIC_INSTALL
2179 /***********************************************************************/
2180 /* WaveFront: Linux modular sound kernel installation interface */
2181 /***********************************************************************/
2183 void
2184 wavefrontintr (int irq, void *dev_id, struct pt_regs *dummy)
2186 struct wf_config *hw = dev_id;
2189 Some comments on interrupts. I attempted a version of this
2190 driver that used interrupts throughout the code instead of
2191 doing busy and/or sleep-waiting. Alas, it appears that once
2192 the Motorola firmware is downloaded, the card *never*
2193 generates an RX interrupt. These are successfully generated
2194 during firmware loading, and after that wavefront_status()
2195 reports that an interrupt is pending on the card from time
2196 to time, but it never seems to be delivered to this
2197 driver. Note also that wavefront_status() continues to
2198 report that RX interrupts are enabled, suggesting that I
2199 didn't goof up and disable them by mistake.
2201 Thus, I stepped back to a prior version of
2202 wavefront_wait(), the only place where this really
2203 matters. Its sad, but I've looked through the code to check
2204 on things, and I really feel certain that the Motorola
2205 firmware prevents RX-ready interrupts.
2208 if ((wavefront_status() & (STAT_INTR_READ|STAT_INTR_WRITE)) == 0) {
2209 return;
2212 hw->irq_ok = 1;
2213 hw->irq_cnt++;
2214 wake_up_interruptible (&hw->interrupt_sleeper);
2217 /* STATUS REGISTER
2219 0 Host Rx Interrupt Enable (1=Enabled)
2220 1 Host Rx Register Full (1=Full)
2221 2 Host Rx Interrupt Pending (1=Interrupt)
2222 3 Unused
2223 4 Host Tx Interrupt (1=Enabled)
2224 5 Host Tx Register empty (1=Empty)
2225 6 Host Tx Interrupt Pending (1=Interrupt)
2226 7 Unused
2230 wavefront_interrupt_bits (int irq)
2233 int bits;
2235 switch (irq) {
2236 case 9:
2237 bits = 0x00;
2238 break;
2239 case 5:
2240 bits = 0x08;
2241 break;
2242 case 12:
2243 bits = 0x10;
2244 break;
2245 case 15:
2246 bits = 0x18;
2247 break;
2249 default:
2250 printk (KERN_WARNING LOGNAME "invalid IRQ %d\n", irq);
2251 bits = -1;
2254 return bits;
2257 void
2258 wavefront_should_cause_interrupt (int val, int port, int timeout)
2261 unsigned long flags;
2263 save_flags (flags);
2264 cli();
2265 dev.irq_ok = 0;
2266 outb (val,port);
2267 interruptible_sleep_on_timeout (&dev.interrupt_sleeper, timeout);
2268 restore_flags (flags);
2271 static int
2272 wavefront_hw_reset (void)
2275 int bits;
2276 int hwv[2];
2277 unsigned long irq_mask;
2278 short reported_irq;
2280 /* IRQ already checked in init_module() */
2282 bits = wavefront_interrupt_bits (dev.irq);
2284 printk (KERN_DEBUG LOGNAME "autodetecting WaveFront IRQ\n");
2286 sti ();
2288 irq_mask = probe_irq_on ();
2290 outb (0x0, dev.control_port);
2291 outb (0x80 | 0x40 | bits, dev.data_port);
2292 wavefront_should_cause_interrupt(0x80|0x40|0x10|0x1,
2293 dev.control_port,
2294 (reset_time*HZ)/100);
2296 reported_irq = probe_irq_off (irq_mask);
2298 if (reported_irq != dev.irq) {
2299 if (reported_irq == 0) {
2300 printk (KERN_ERR LOGNAME
2301 "No unassigned interrupts detected "
2302 "after h/w reset\n");
2303 } else if (reported_irq < 0) {
2304 printk (KERN_ERR LOGNAME
2305 "Multiple unassigned interrupts detected "
2306 "after h/w reset\n");
2307 } else {
2308 printk (KERN_ERR LOGNAME "autodetected IRQ %d not the "
2309 "value provided (%d)\n", reported_irq,
2310 dev.irq);
2312 dev.irq = -1;
2313 return 1;
2314 } else {
2315 printk (KERN_INFO LOGNAME "autodetected IRQ at %d\n",
2316 reported_irq);
2319 if (request_irq (dev.irq, wavefrontintr,
2320 SA_INTERRUPT|SA_SHIRQ,
2321 "wavefront synth", &dev) < 0) {
2322 printk (KERN_WARNING LOGNAME "IRQ %d not available!\n",
2323 dev.irq);
2324 return 1;
2327 /* try reset of port */
2329 outb (0x0, dev.control_port);
2331 /* At this point, the board is in reset, and the H/W initialization
2332 register is accessed at the same address as the data port.
2334 Bit 7 - Enable IRQ Driver
2335 0 - Tri-state the Wave-Board drivers for the PC Bus IRQs
2336 1 - Enable IRQ selected by bits 5:3 to be driven onto the PC Bus.
2338 Bit 6 - MIDI Interface Select
2340 0 - Use the MIDI Input from the 26-pin WaveBlaster
2341 compatible header as the serial MIDI source
2342 1 - Use the MIDI Input from the 9-pin D connector as the
2343 serial MIDI source.
2345 Bits 5:3 - IRQ Selection
2346 0 0 0 - IRQ 2/9
2347 0 0 1 - IRQ 5
2348 0 1 0 - IRQ 12
2349 0 1 1 - IRQ 15
2350 1 0 0 - Reserved
2351 1 0 1 - Reserved
2352 1 1 0 - Reserved
2353 1 1 1 - Reserved
2355 Bits 2:1 - Reserved
2356 Bit 0 - Disable Boot ROM
2357 0 - memory accesses to 03FC30-03FFFFH utilize the internal Boot ROM
2358 1 - memory accesses to 03FC30-03FFFFH are directed to external
2359 storage.
2363 /* configure hardware: IRQ, enable interrupts,
2364 plus external 9-pin MIDI interface selected
2367 outb (0x80 | 0x40 | bits, dev.data_port);
2369 /* CONTROL REGISTER
2371 0 Host Rx Interrupt Enable (1=Enabled) 0x1
2372 1 Unused 0x2
2373 2 Unused 0x4
2374 3 Unused 0x8
2375 4 Host Tx Interrupt Enable 0x10
2376 5 Mute (0=Mute; 1=Play) 0x20
2377 6 Master Interrupt Enable (1=Enabled) 0x40
2378 7 Master Reset (0=Reset; 1=Run) 0x80
2380 Take us out of reset, mute output, master + TX + RX interrupts on.
2382 We'll get an interrupt presumably to tell us that the TX
2383 register is clear.
2386 wavefront_should_cause_interrupt(0x80|0x40|0x10|0x1,
2387 dev.control_port,
2388 (reset_time*HZ)/100);
2390 /* Note: data port is now the data port, not the h/w initialization
2391 port.
2394 if (!dev.irq_ok) {
2395 printk (KERN_WARNING LOGNAME
2396 "intr not received after h/w un-reset.\n");
2397 goto gone_bad;
2400 dev.interrupts_on = 1;
2402 /* Note: data port is now the data port, not the h/w initialization
2403 port.
2405 At this point, only "HW VERSION" or "DOWNLOAD OS" commands
2406 will work. So, issue one of them, and wait for TX
2407 interrupt. This can take a *long* time after a cold boot,
2408 while the ISC ROM does its RAM test. The SDK says up to 4
2409 seconds - with 12MB of RAM on a Tropez+, it takes a lot
2410 longer than that (~16secs). Note that the card understands
2411 the difference between a warm and a cold boot, so
2412 subsequent ISC2115 reboots (say, caused by module
2413 reloading) will get through this much faster.
2415 XXX Interesting question: why is no RX interrupt received first ?
2418 wavefront_should_cause_interrupt(WFC_HARDWARE_VERSION,
2419 dev.data_port, ramcheck_time*HZ);
2421 if (!dev.irq_ok) {
2422 printk (KERN_WARNING LOGNAME
2423 "post-RAM-check interrupt not received.\n");
2424 goto gone_bad;
2427 if (!wavefront_wait (STAT_CAN_READ)) {
2428 printk (KERN_WARNING LOGNAME
2429 "no response to HW version cmd.\n");
2430 goto gone_bad;
2433 if ((hwv[0] = wavefront_read ()) == -1) {
2434 printk (KERN_WARNING LOGNAME
2435 "board not responding correctly.\n");
2436 goto gone_bad;
2439 if (hwv[0] == 0xFF) { /* NAK */
2441 /* Board's RAM test failed. Try to read error code,
2442 and tell us about it either way.
2445 if ((hwv[0] = wavefront_read ()) == -1) {
2446 printk (KERN_WARNING LOGNAME "on-board RAM test failed "
2447 "(bad error code).\n");
2448 } else {
2449 printk (KERN_WARNING LOGNAME "on-board RAM test failed "
2450 "(error code: 0x%x).\n",
2451 hwv[0]);
2453 goto gone_bad;
2456 /* We're OK, just get the next byte of the HW version response */
2458 if ((hwv[1] = wavefront_read ()) == -1) {
2459 printk (KERN_WARNING LOGNAME "incorrect h/w response.\n");
2460 goto gone_bad;
2463 printk (KERN_INFO LOGNAME "hardware version %d.%d\n",
2464 hwv[0], hwv[1]);
2466 return 0;
2469 gone_bad:
2470 if (dev.irq >= 0) {
2471 free_irq (dev.irq, &dev);
2472 dev.irq = -1;
2474 return (1);
2477 static int __init detect_wavefront (int irq, int io_base)
2479 unsigned char rbuf[4], wbuf[4];
2481 /* TB docs say the device takes up 8 ports, but we know that
2482 if there is an FX device present (i.e. a Tropez+) it really
2483 consumes 16.
2486 if (check_region (io_base, 16)) {
2487 printk (KERN_ERR LOGNAME "IO address range 0x%x - 0x%x "
2488 "already in use - ignored\n", dev.base,
2489 dev.base+15);
2490 return -1;
2493 dev.irq = irq;
2494 dev.base = io_base;
2495 dev.israw = 0;
2496 dev.debug = debug_default;
2497 dev.interrupts_on = 0;
2498 dev.irq_cnt = 0;
2499 dev.rom_samples_rdonly = 1; /* XXX default lock on ROM sample slots */
2501 if (wavefront_cmd (WFC_FIRMWARE_VERSION, rbuf, wbuf) == 0) {
2503 dev.fw_version[0] = rbuf[0];
2504 dev.fw_version[1] = rbuf[1];
2505 printk (KERN_INFO LOGNAME
2506 "firmware %d.%d already loaded.\n",
2507 rbuf[0], rbuf[1]);
2509 /* check that a command actually works */
2511 if (wavefront_cmd (WFC_HARDWARE_VERSION,
2512 rbuf, wbuf) == 0) {
2513 dev.hw_version[0] = rbuf[0];
2514 dev.hw_version[1] = rbuf[1];
2515 } else {
2516 printk (KERN_WARNING LOGNAME "not raw, but no "
2517 "hardware version!\n");
2518 return 0;
2521 if (!wf_raw) {
2522 return 1;
2523 } else {
2524 printk (KERN_INFO LOGNAME
2525 "reloading firmware anyway.\n");
2526 dev.israw = 1;
2529 } else {
2531 dev.israw = 1;
2532 printk (KERN_INFO LOGNAME
2533 "no response to firmware probe, assume raw.\n");
2537 init_waitqueue_head (&dev.interrupt_sleeper);
2539 if (wavefront_hw_reset ()) {
2540 printk (KERN_WARNING LOGNAME "hardware reset failed\n");
2541 return 0;
2544 /* Check for FX device, present only on Tropez+ */
2546 dev.has_fx = (detect_wffx () == 0);
2548 return 1;
2551 #include "os.h"
2552 #define __KERNEL_SYSCALLS__
2553 #include <linux/fs.h>
2554 #include <linux/mm.h>
2555 #include <linux/malloc.h>
2556 #include <linux/unistd.h>
2557 #include <asm/uaccess.h>
2559 static int errno;
2561 static int
2562 wavefront_download_firmware (char *path)
2565 unsigned char section[WF_SECTION_MAX];
2566 char section_length; /* yes, just a char; max value is WF_SECTION_MAX */
2567 int section_cnt_downloaded = 0;
2568 int fd;
2569 int c;
2570 int i;
2571 mm_segment_t fs;
2573 /* This tries to be a bit cleverer than the stuff Alan Cox did for
2574 the generic sound firmware, in that it actually knows
2575 something about the structure of the Motorola firmware. In
2576 particular, it uses a version that has been stripped of the
2577 20K of useless header information, and had section lengths
2578 added, making it possible to load the entire OS without any
2579 [kv]malloc() activity, since the longest entity we ever read is
2580 42 bytes (well, WF_SECTION_MAX) long.
2583 fs = get_fs();
2584 set_fs (get_ds());
2586 if ((fd = open (path, 0, 0)) < 0) {
2587 printk (KERN_WARNING LOGNAME "Unable to load \"%s\".\n",
2588 path);
2589 return 1;
2592 while (1) {
2593 int x;
2595 if ((x = read (fd, &section_length, sizeof (section_length))) !=
2596 sizeof (section_length)) {
2597 printk (KERN_ERR LOGNAME "firmware read error.\n");
2598 goto failure;
2601 if (section_length == 0) {
2602 break;
2605 if (read (fd, section, section_length) != section_length) {
2606 printk (KERN_ERR LOGNAME "firmware section "
2607 "read error.\n");
2608 goto failure;
2611 /* Send command */
2613 if (wavefront_write (WFC_DOWNLOAD_OS)) {
2614 goto failure;
2617 for (i = 0; i < section_length; i++) {
2618 if (wavefront_write (section[i])) {
2619 goto failure;
2623 /* get ACK */
2625 if (wavefront_wait (STAT_CAN_READ)) {
2627 if ((c = inb (dev.data_port)) != WF_ACK) {
2629 printk (KERN_ERR LOGNAME "download "
2630 "of section #%d not "
2631 "acknowledged, ack = 0x%x\n",
2632 section_cnt_downloaded + 1, c);
2633 goto failure;
2637 } else {
2638 printk (KERN_ERR LOGNAME "time out for firmware ACK.\n");
2639 goto failure;
2644 close (fd);
2645 set_fs (fs);
2646 return 0;
2648 failure:
2649 close (fd);
2650 set_fs (fs);
2651 printk (KERN_ERR "\nWaveFront: firmware download failed!!!\n");
2652 return 1;
2655 static int __init wavefront_config_midi (void)
2657 unsigned char rbuf[4], wbuf[4];
2659 if (detect_wf_mpu (dev.irq, dev.base) < 0) {
2660 printk (KERN_WARNING LOGNAME
2661 "could not find working MIDI device\n");
2662 return -1;
2665 if ((dev.mididev = install_wf_mpu ()) < 0) {
2666 printk (KERN_WARNING LOGNAME
2667 "MIDI interfaces not configured\n");
2668 return -1;
2671 /* Route external MIDI to WaveFront synth (by default) */
2673 if (wavefront_cmd (WFC_MISYNTH_ON, rbuf, wbuf)) {
2674 printk (KERN_WARNING LOGNAME
2675 "cannot enable MIDI-IN to synth routing.\n");
2676 /* XXX error ? */
2680 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2681 /* Get the regular MIDI patch loading function, so we can
2682 use it if we ever get handed a SYSEX patch. This is
2683 unlikely, because its so damn slow, but we may as well
2684 leave this functionality from maui.c behind, since it
2685 could be useful for sequencer applications that can
2686 only use MIDI to do patch loading.
2689 if (midi_devs[dev.mididev]->converter != NULL) {
2690 midi_load_patch = midi_devs[dev.mididev]->converter->load_patch;
2691 midi_devs[dev.mididev]->converter->load_patch =
2692 &wavefront_oss_load_patch;
2695 #endif OSS_SUPPORT_SEQ
2697 /* Turn on Virtual MIDI, but first *always* turn it off,
2698 since otherwise consectutive reloads of the driver will
2699 never cause the hardware to generate the initial "internal" or
2700 "external" source bytes in the MIDI data stream. This
2701 is pretty important, since the internal hardware generally will
2702 be used to generate none or very little MIDI output, and
2703 thus the only source of MIDI data is actually external. Without
2704 the switch bytes, the driver will think it all comes from
2705 the internal interface. Duh.
2708 if (wavefront_cmd (WFC_VMIDI_OFF, rbuf, wbuf)) {
2709 printk (KERN_WARNING LOGNAME
2710 "virtual MIDI mode not disabled\n");
2711 return 0; /* We're OK, but missing the external MIDI dev */
2714 if ((dev.ext_mididev = virtual_midi_enable ()) < 0) {
2715 printk (KERN_WARNING LOGNAME "no virtual MIDI access.\n");
2716 } else {
2717 if (wavefront_cmd (WFC_VMIDI_ON, rbuf, wbuf)) {
2718 printk (KERN_WARNING LOGNAME
2719 "cannot enable virtual MIDI mode.\n");
2720 virtual_midi_disable ();
2724 return 0;
2727 static int
2728 wavefront_do_reset (int atboot)
2730 char voices[1];
2732 if (!atboot && wavefront_hw_reset ()) {
2733 printk (KERN_WARNING LOGNAME "hw reset failed.\n");
2734 goto gone_bad;
2737 if (dev.israw) {
2738 if (wavefront_download_firmware (ospath)) {
2739 goto gone_bad;
2742 dev.israw = 0;
2744 /* Wait for the OS to get running. The protocol for
2745 this is non-obvious, and was determined by
2746 using port-IO tracing in DOSemu and some
2747 experimentation here.
2749 Rather than using timed waits, use interrupts creatively.
2752 wavefront_should_cause_interrupt (WFC_NOOP,
2753 dev.data_port,
2754 (osrun_time*HZ));
2756 if (!dev.irq_ok) {
2757 printk (KERN_WARNING LOGNAME
2758 "no post-OS interrupt.\n");
2759 goto gone_bad;
2762 /* Now, do it again ! */
2764 wavefront_should_cause_interrupt (WFC_NOOP,
2765 dev.data_port, (10*HZ));
2767 if (!dev.irq_ok) {
2768 printk (KERN_WARNING LOGNAME
2769 "no post-OS interrupt(2).\n");
2770 goto gone_bad;
2773 /* OK, no (RX/TX) interrupts any more, but leave mute
2774 in effect.
2777 outb (0x80|0x40, dev.control_port);
2779 /* No need for the IRQ anymore */
2781 free_irq (dev.irq, &dev);
2785 if (dev.has_fx && fx_raw) {
2786 wffx_init ();
2789 /* SETUPSND.EXE asks for sample memory config here, but since i
2790 have no idea how to interpret the result, we'll forget
2791 about it.
2794 if ((dev.freemem = wavefront_freemem ()) < 0) {
2795 goto gone_bad;
2798 printk (KERN_INFO LOGNAME "available DRAM %dk\n", dev.freemem / 1024);
2800 if (wavefront_write (0xf0) ||
2801 wavefront_write (1) ||
2802 (wavefront_read () < 0)) {
2803 dev.debug = 0;
2804 printk (KERN_WARNING LOGNAME "MPU emulation mode not set.\n");
2805 goto gone_bad;
2808 voices[0] = 32;
2810 if (wavefront_cmd (WFC_SET_NVOICES, 0, voices)) {
2811 printk (KERN_WARNING LOGNAME
2812 "cannot set number of voices to 32.\n");
2813 goto gone_bad;
2817 return 0;
2819 gone_bad:
2820 /* reset that sucker so that it doesn't bother us. */
2822 outb (0x0, dev.control_port);
2823 dev.interrupts_on = 0;
2824 if (dev.irq >= 0) {
2825 free_irq (dev.irq, &dev);
2827 return 1;
2830 static int
2831 wavefront_init (int atboot)
2833 int samples_are_from_rom;
2835 if (dev.israw) {
2836 samples_are_from_rom = 1;
2837 } else {
2838 /* XXX is this always true ? */
2839 samples_are_from_rom = 0;
2842 if (dev.israw || fx_raw) {
2843 if (wavefront_do_reset (atboot)) {
2844 return -1;
2848 wavefront_get_sample_status (samples_are_from_rom);
2849 wavefront_get_program_status ();
2850 wavefront_get_patch_status ();
2852 /* Start normal operation: unreset, master interrupt enabled, no mute
2855 outb (0x80|0x40|0x20, dev.control_port);
2857 return (0);
2860 static int __init install_wavefront (void)
2863 if ((dev.synth_dev = register_sound_synth (&wavefront_fops, -1)) < 0) {
2864 printk (KERN_ERR LOGNAME "cannot register raw synth\n");
2865 return -1;
2868 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2869 if ((dev.oss_dev = sound_alloc_synthdev()) == -1) {
2870 printk (KERN_ERR LOGNAME "Too many sequencers\n");
2871 return -1;
2872 } else {
2873 synth_devs[dev.oss_dev] = &wavefront_operations;
2875 #endif OSS_SUPPORT_SEQ
2877 if (wavefront_init (1) < 0) {
2878 printk (KERN_WARNING LOGNAME "initialization failed.\n");
2880 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2881 sound_unload_synthdev (dev.oss_dev);
2882 #endif OSS_SUPPORT_SEQ
2884 return -1;
2887 request_region (dev.base+2, 6, "wavefront synth");
2889 if (dev.has_fx) {
2890 request_region (dev.base+8, 8, "wavefront fx");
2893 if (wavefront_config_midi ()) {
2894 printk (KERN_WARNING LOGNAME "could not initialize MIDI.\n");
2897 return dev.oss_dev;
2900 void
2901 uninstall_wavefront (void)
2904 /* the first two i/o addresses are freed by the wf_mpu code */
2905 release_region (dev.base+2, 6);
2907 if (dev.has_fx) {
2908 release_region (dev.base+8, 8);
2911 unregister_sound_synth (dev.synth_dev);
2913 #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
2914 sound_unload_synthdev (dev.oss_dev);
2915 #endif OSS_SUPPORT_SEQ
2916 uninstall_wf_mpu ();
2919 /***********************************************************************/
2920 /* WaveFront FX control */
2921 /***********************************************************************/
2923 #include "yss225.h"
2925 /* Control bits for the Load Control Register
2928 #define FX_LSB_TRANSFER 0x01 /* transfer after DSP LSB byte written */
2929 #define FX_MSB_TRANSFER 0x02 /* transfer after DSP MSB byte written */
2930 #define FX_AUTO_INCR 0x04 /* auto-increment DSP address after transfer */
2932 static int
2933 wffx_idle (void)
2936 int i;
2937 unsigned int x = 0x80;
2939 for (i = 0; i < 1000; i++) {
2940 x = inb (dev.fx_status);
2941 if ((x & 0x80) == 0) {
2942 break;
2946 if (x & 0x80) {
2947 printk (KERN_ERR LOGNAME "FX device never idle.\n");
2948 return 0;
2951 return (1);
2954 static int __init detect_wffx (void)
2956 /* This is a crude check, but its the best one I have for now.
2957 Certainly on the Maui and the Tropez, wffx_idle() will
2958 report "never idle", which suggests that this test should
2959 work OK.
2962 if (inb (dev.fx_status) & 0x80) {
2963 printk (KERN_INFO LOGNAME "Hmm, probably a Maui or Tropez.\n");
2964 return -1;
2967 return 0;
2970 static int __init attach_wffx (void)
2972 if ((dev.fx_mididev = sound_alloc_mididev ()) < 0) {
2973 printk (KERN_WARNING LOGNAME "cannot install FX Midi driver\n");
2974 return -1;
2977 return 0;
2980 static void
2981 wffx_mute (int onoff)
2984 if (!wffx_idle()) {
2985 return;
2988 outb (onoff ? 0x02 : 0x00, dev.fx_op);
2991 static int
2992 wffx_memset (int page,
2993 int addr, int cnt, unsigned short *data)
2995 if (page < 0 || page > 7) {
2996 printk (KERN_ERR LOGNAME "FX memset: "
2997 "page must be >= 0 and <= 7\n");
2998 return -(EINVAL);
3001 if (addr < 0 || addr > 0x7f) {
3002 printk (KERN_ERR LOGNAME "FX memset: "
3003 "addr must be >= 0 and <= 7f\n");
3004 return -(EINVAL);
3007 if (cnt == 1) {
3009 outb (FX_LSB_TRANSFER, dev.fx_lcr);
3010 outb (page, dev.fx_dsp_page);
3011 outb (addr, dev.fx_dsp_addr);
3012 outb ((data[0] >> 8), dev.fx_dsp_msb);
3013 outb ((data[0] & 0xff), dev.fx_dsp_lsb);
3015 printk (KERN_INFO LOGNAME "FX: addr %d:%x set to 0x%x\n",
3016 page, addr, data[0]);
3018 } else {
3019 int i;
3021 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3022 outb (page, dev.fx_dsp_page);
3023 outb (addr, dev.fx_dsp_addr);
3025 for (i = 0; i < cnt; i++) {
3026 outb ((data[i] >> 8), dev.fx_dsp_msb);
3027 outb ((data[i] & 0xff), dev.fx_dsp_lsb);
3028 if (!wffx_idle ()) {
3029 break;
3033 if (i != cnt) {
3034 printk (KERN_WARNING LOGNAME
3035 "FX memset "
3036 "(0x%x, 0x%x, 0x%x, %d) incomplete\n",
3037 page, addr, (int) data, cnt);
3038 return -(EIO);
3042 return 0;
3045 static int
3046 wffx_ioctl (wavefront_fx_info *r)
3049 unsigned short page_data[256];
3050 unsigned short *pd;
3052 switch (r->request) {
3053 case WFFX_MUTE:
3054 wffx_mute (r->data[0]);
3055 return 0;
3057 case WFFX_MEMSET:
3059 if (r->data[2] <= 0) {
3060 printk (KERN_ERR LOGNAME "cannot write "
3061 "<= 0 bytes to FX\n");
3062 return -(EINVAL);
3063 } else if (r->data[2] == 1) {
3064 pd = (unsigned short *) &r->data[3];
3065 } else {
3066 if (r->data[2] > sizeof (page_data)) {
3067 printk (KERN_ERR LOGNAME "cannot write "
3068 "> 255 bytes to FX\n");
3069 return -(EINVAL);
3071 copy_from_user (page_data, (unsigned char *) r->data[3],
3072 r->data[2]);
3073 pd = page_data;
3076 return wffx_memset (r->data[0], /* page */
3077 r->data[1], /* addr */
3078 r->data[2], /* cnt */
3079 pd);
3081 default:
3082 printk (KERN_WARNING LOGNAME
3083 "FX: ioctl %d not yet supported\n",
3084 r->request);
3085 return -(EINVAL);
3089 /* YSS225 initialization.
3091 This code was developed using DOSEMU. The Turtle Beach SETUPSND
3092 utility was run with I/O tracing in DOSEMU enabled, and a reconstruction
3093 of the port I/O done, using the Yamaha faxback document as a guide
3094 to add more logic to the code. Its really pretty wierd.
3096 There was an alternative approach of just dumping the whole I/O
3097 sequence as a series of port/value pairs and a simple loop
3098 that output it. However, I hope that eventually I'll get more
3099 control over what this code does, and so I tried to stick with
3100 a somewhat "algorithmic" approach.
3103 static int
3104 wffx_init (void)
3107 int i;
3108 int j;
3110 /* Set all bits for all channels on the MOD unit to zero */
3111 /* XXX But why do this twice ? */
3113 for (j = 0; j < 2; j++) {
3114 for (i = 0x10; i <= 0xff; i++) {
3116 if (!wffx_idle ()) {
3117 return (-1);
3120 outb (i, dev.fx_mod_addr);
3121 outb (0x0, dev.fx_mod_data);
3125 if (!wffx_idle()) return (-1);
3126 outb (0x02, dev.fx_op); /* mute on */
3128 if (!wffx_idle()) return (-1);
3129 outb (0x07, dev.fx_dsp_page);
3130 outb (0x44, dev.fx_dsp_addr);
3131 outb (0x00, dev.fx_dsp_msb);
3132 outb (0x00, dev.fx_dsp_lsb);
3133 if (!wffx_idle()) return (-1);
3134 outb (0x07, dev.fx_dsp_page);
3135 outb (0x42, dev.fx_dsp_addr);
3136 outb (0x00, dev.fx_dsp_msb);
3137 outb (0x00, dev.fx_dsp_lsb);
3138 if (!wffx_idle()) return (-1);
3139 outb (0x07, dev.fx_dsp_page);
3140 outb (0x43, dev.fx_dsp_addr);
3141 outb (0x00, dev.fx_dsp_msb);
3142 outb (0x00, dev.fx_dsp_lsb);
3143 if (!wffx_idle()) return (-1);
3144 outb (0x07, dev.fx_dsp_page);
3145 outb (0x7c, dev.fx_dsp_addr);
3146 outb (0x00, dev.fx_dsp_msb);
3147 outb (0x00, dev.fx_dsp_lsb);
3148 if (!wffx_idle()) return (-1);
3149 outb (0x07, dev.fx_dsp_page);
3150 outb (0x7e, dev.fx_dsp_addr);
3151 outb (0x00, dev.fx_dsp_msb);
3152 outb (0x00, dev.fx_dsp_lsb);
3153 if (!wffx_idle()) return (-1);
3154 outb (0x07, dev.fx_dsp_page);
3155 outb (0x46, dev.fx_dsp_addr);
3156 outb (0x00, dev.fx_dsp_msb);
3157 outb (0x00, dev.fx_dsp_lsb);
3158 if (!wffx_idle()) return (-1);
3159 outb (0x07, dev.fx_dsp_page);
3160 outb (0x49, dev.fx_dsp_addr);
3161 outb (0x00, dev.fx_dsp_msb);
3162 outb (0x00, dev.fx_dsp_lsb);
3163 if (!wffx_idle()) return (-1);
3164 outb (0x07, dev.fx_dsp_page);
3165 outb (0x47, dev.fx_dsp_addr);
3166 outb (0x00, dev.fx_dsp_msb);
3167 outb (0x00, dev.fx_dsp_lsb);
3168 if (!wffx_idle()) return (-1);
3169 outb (0x07, dev.fx_dsp_page);
3170 outb (0x4a, dev.fx_dsp_addr);
3171 outb (0x00, dev.fx_dsp_msb);
3172 outb (0x00, dev.fx_dsp_lsb);
3174 /* either because of stupidity by TB's programmers, or because it
3175 actually does something, rezero the MOD page.
3177 for (i = 0x10; i <= 0xff; i++) {
3179 if (!wffx_idle ()) {
3180 return (-1);
3183 outb (i, dev.fx_mod_addr);
3184 outb (0x0, dev.fx_mod_data);
3186 /* load page zero */
3188 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3189 outb (0x00, dev.fx_dsp_page);
3190 outb (0x00, dev.fx_dsp_addr);
3192 for (i = 0; i < sizeof (page_zero); i += 2) {
3193 outb (page_zero[i], dev.fx_dsp_msb);
3194 outb (page_zero[i+1], dev.fx_dsp_lsb);
3195 if (!wffx_idle()) return (-1);
3198 /* Now load page one */
3200 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3201 outb (0x01, dev.fx_dsp_page);
3202 outb (0x00, dev.fx_dsp_addr);
3204 for (i = 0; i < sizeof (page_one); i += 2) {
3205 outb (page_one[i], dev.fx_dsp_msb);
3206 outb (page_one[i+1], dev.fx_dsp_lsb);
3207 if (!wffx_idle()) return (-1);
3210 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3211 outb (0x02, dev.fx_dsp_page);
3212 outb (0x00, dev.fx_dsp_addr);
3214 for (i = 0; i < sizeof (page_two); i++) {
3215 outb (page_two[i], dev.fx_dsp_lsb);
3216 if (!wffx_idle()) return (-1);
3219 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3220 outb (0x03, dev.fx_dsp_page);
3221 outb (0x00, dev.fx_dsp_addr);
3223 for (i = 0; i < sizeof (page_three); i++) {
3224 outb (page_three[i], dev.fx_dsp_lsb);
3225 if (!wffx_idle()) return (-1);
3228 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3229 outb (0x04, dev.fx_dsp_page);
3230 outb (0x00, dev.fx_dsp_addr);
3232 for (i = 0; i < sizeof (page_four); i++) {
3233 outb (page_four[i], dev.fx_dsp_lsb);
3234 if (!wffx_idle()) return (-1);
3237 /* Load memory area (page six) */
3239 outb (FX_LSB_TRANSFER, dev.fx_lcr);
3240 outb (0x06, dev.fx_dsp_page);
3242 for (i = 0; i < sizeof (page_six); i += 3) {
3243 outb (page_six[i], dev.fx_dsp_addr);
3244 outb (page_six[i+1], dev.fx_dsp_msb);
3245 outb (page_six[i+2], dev.fx_dsp_lsb);
3246 if (!wffx_idle()) return (-1);
3249 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3250 outb (0x07, dev.fx_dsp_page);
3251 outb (0x00, dev.fx_dsp_addr);
3253 for (i = 0; i < sizeof (page_seven); i += 2) {
3254 outb (page_seven[i], dev.fx_dsp_msb);
3255 outb (page_seven[i+1], dev.fx_dsp_lsb);
3256 if (!wffx_idle()) return (-1);
3259 /* Now setup the MOD area. We do this algorithmically in order to
3260 save a little data space. It could be done in the same fashion
3261 as the "pages".
3264 for (i = 0x00; i <= 0x0f; i++) {
3265 outb (0x01, dev.fx_mod_addr);
3266 outb (i, dev.fx_mod_data);
3267 if (!wffx_idle()) return (-1);
3268 outb (0x02, dev.fx_mod_addr);
3269 outb (0x00, dev.fx_mod_data);
3270 if (!wffx_idle()) return (-1);
3273 for (i = 0xb0; i <= 0xbf; i++) {
3274 outb (i, dev.fx_mod_addr);
3275 outb (0x20, dev.fx_mod_data);
3276 if (!wffx_idle()) return (-1);
3279 for (i = 0xf0; i <= 0xff; i++) {
3280 outb (i, dev.fx_mod_addr);
3281 outb (0x20, dev.fx_mod_data);
3282 if (!wffx_idle()) return (-1);
3285 for (i = 0x10; i <= 0x1d; i++) {
3286 outb (i, dev.fx_mod_addr);
3287 outb (0xff, dev.fx_mod_data);
3288 if (!wffx_idle()) return (-1);
3291 outb (0x1e, dev.fx_mod_addr);
3292 outb (0x40, dev.fx_mod_data);
3293 if (!wffx_idle()) return (-1);
3295 for (i = 0x1f; i <= 0x2d; i++) {
3296 outb (i, dev.fx_mod_addr);
3297 outb (0xff, dev.fx_mod_data);
3298 if (!wffx_idle()) return (-1);
3301 outb (0x2e, dev.fx_mod_addr);
3302 outb (0x00, dev.fx_mod_data);
3303 if (!wffx_idle()) return (-1);
3305 for (i = 0x2f; i <= 0x3e; i++) {
3306 outb (i, dev.fx_mod_addr);
3307 outb (0x00, dev.fx_mod_data);
3308 if (!wffx_idle()) return (-1);
3311 outb (0x3f, dev.fx_mod_addr);
3312 outb (0x20, dev.fx_mod_data);
3313 if (!wffx_idle()) return (-1);
3315 for (i = 0x40; i <= 0x4d; i++) {
3316 outb (i, dev.fx_mod_addr);
3317 outb (0x00, dev.fx_mod_data);
3318 if (!wffx_idle()) return (-1);
3321 outb (0x4e, dev.fx_mod_addr);
3322 outb (0x0e, dev.fx_mod_data);
3323 if (!wffx_idle()) return (-1);
3324 outb (0x4f, dev.fx_mod_addr);
3325 outb (0x0e, dev.fx_mod_data);
3326 if (!wffx_idle()) return (-1);
3329 for (i = 0x50; i <= 0x6b; i++) {
3330 outb (i, dev.fx_mod_addr);
3331 outb (0x00, dev.fx_mod_data);
3332 if (!wffx_idle()) return (-1);
3335 outb (0x6c, dev.fx_mod_addr);
3336 outb (0x40, dev.fx_mod_data);
3337 if (!wffx_idle()) return (-1);
3339 outb (0x6d, dev.fx_mod_addr);
3340 outb (0x00, dev.fx_mod_data);
3341 if (!wffx_idle()) return (-1);
3343 outb (0x6e, dev.fx_mod_addr);
3344 outb (0x40, dev.fx_mod_data);
3345 if (!wffx_idle()) return (-1);
3347 outb (0x6f, dev.fx_mod_addr);
3348 outb (0x40, dev.fx_mod_data);
3349 if (!wffx_idle()) return (-1);
3351 for (i = 0x70; i <= 0x7f; i++) {
3352 outb (i, dev.fx_mod_addr);
3353 outb (0xc0, dev.fx_mod_data);
3354 if (!wffx_idle()) return (-1);
3357 for (i = 0x80; i <= 0xaf; i++) {
3358 outb (i, dev.fx_mod_addr);
3359 outb (0x00, dev.fx_mod_data);
3360 if (!wffx_idle()) return (-1);
3363 for (i = 0xc0; i <= 0xdd; i++) {
3364 outb (i, dev.fx_mod_addr);
3365 outb (0x00, dev.fx_mod_data);
3366 if (!wffx_idle()) return (-1);
3369 outb (0xde, dev.fx_mod_addr);
3370 outb (0x10, dev.fx_mod_data);
3371 if (!wffx_idle()) return (-1);
3372 outb (0xdf, dev.fx_mod_addr);
3373 outb (0x10, dev.fx_mod_data);
3374 if (!wffx_idle()) return (-1);
3376 for (i = 0xe0; i <= 0xef; i++) {
3377 outb (i, dev.fx_mod_addr);
3378 outb (0x00, dev.fx_mod_data);
3379 if (!wffx_idle()) return (-1);
3382 for (i = 0x00; i <= 0x0f; i++) {
3383 outb (0x01, dev.fx_mod_addr);
3384 outb (i, dev.fx_mod_data);
3385 outb (0x02, dev.fx_mod_addr);
3386 outb (0x01, dev.fx_mod_data);
3387 if (!wffx_idle()) return (-1);
3390 outb (0x02, dev.fx_op); /* mute on */
3392 /* Now set the coefficients and so forth for the programs above */
3394 for (i = 0; i < sizeof (coefficients); i += 4) {
3395 outb (coefficients[i], dev.fx_dsp_page);
3396 outb (coefficients[i+1], dev.fx_dsp_addr);
3397 outb (coefficients[i+2], dev.fx_dsp_msb);
3398 outb (coefficients[i+3], dev.fx_dsp_lsb);
3399 if (!wffx_idle()) return (-1);
3402 /* Some settings (?) that are too small to bundle into loops */
3404 if (!wffx_idle()) return (-1);
3405 outb (0x1e, dev.fx_mod_addr);
3406 outb (0x14, dev.fx_mod_data);
3407 if (!wffx_idle()) return (-1);
3408 outb (0xde, dev.fx_mod_addr);
3409 outb (0x20, dev.fx_mod_data);
3410 if (!wffx_idle()) return (-1);
3411 outb (0xdf, dev.fx_mod_addr);
3412 outb (0x20, dev.fx_mod_data);
3414 /* some more coefficients */
3416 if (!wffx_idle()) return (-1);
3417 outb (0x06, dev.fx_dsp_page);
3418 outb (0x78, dev.fx_dsp_addr);
3419 outb (0x00, dev.fx_dsp_msb);
3420 outb (0x40, dev.fx_dsp_lsb);
3421 if (!wffx_idle()) return (-1);
3422 outb (0x07, dev.fx_dsp_page);
3423 outb (0x03, dev.fx_dsp_addr);
3424 outb (0x0f, dev.fx_dsp_msb);
3425 outb (0xff, dev.fx_dsp_lsb);
3426 if (!wffx_idle()) return (-1);
3427 outb (0x07, dev.fx_dsp_page);
3428 outb (0x0b, dev.fx_dsp_addr);
3429 outb (0x0f, dev.fx_dsp_msb);
3430 outb (0xff, dev.fx_dsp_lsb);
3431 if (!wffx_idle()) return (-1);
3432 outb (0x07, dev.fx_dsp_page);
3433 outb (0x02, dev.fx_dsp_addr);
3434 outb (0x00, dev.fx_dsp_msb);
3435 outb (0x00, dev.fx_dsp_lsb);
3436 if (!wffx_idle()) return (-1);
3437 outb (0x07, dev.fx_dsp_page);
3438 outb (0x0a, dev.fx_dsp_addr);
3439 outb (0x00, dev.fx_dsp_msb);
3440 outb (0x00, dev.fx_dsp_lsb);
3441 if (!wffx_idle()) return (-1);
3442 outb (0x07, dev.fx_dsp_page);
3443 outb (0x46, dev.fx_dsp_addr);
3444 outb (0x00, dev.fx_dsp_msb);
3445 outb (0x00, dev.fx_dsp_lsb);
3446 if (!wffx_idle()) return (-1);
3447 outb (0x07, dev.fx_dsp_page);
3448 outb (0x49, dev.fx_dsp_addr);
3449 outb (0x00, dev.fx_dsp_msb);
3450 outb (0x00, dev.fx_dsp_lsb);
3452 /* Now, for some strange reason, lets reload every page
3453 and all the coefficients over again. I have *NO* idea
3454 why this is done. I do know that no sound is produced
3455 is this phase is omitted.
3458 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3459 outb (0x00, dev.fx_dsp_page);
3460 outb (0x10, dev.fx_dsp_addr);
3462 for (i = 0; i < sizeof (page_zero_v2); i += 2) {
3463 outb (page_zero_v2[i], dev.fx_dsp_msb);
3464 outb (page_zero_v2[i+1], dev.fx_dsp_lsb);
3465 if (!wffx_idle()) return (-1);
3468 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3469 outb (0x01, dev.fx_dsp_page);
3470 outb (0x10, dev.fx_dsp_addr);
3472 for (i = 0; i < sizeof (page_one_v2); i += 2) {
3473 outb (page_one_v2[i], dev.fx_dsp_msb);
3474 outb (page_one_v2[i+1], dev.fx_dsp_lsb);
3475 if (!wffx_idle()) return (-1);
3478 if (!wffx_idle()) return (-1);
3479 if (!wffx_idle()) return (-1);
3481 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3482 outb (0x02, dev.fx_dsp_page);
3483 outb (0x10, dev.fx_dsp_addr);
3485 for (i = 0; i < sizeof (page_two_v2); i++) {
3486 outb (page_two_v2[i], dev.fx_dsp_lsb);
3487 if (!wffx_idle()) return (-1);
3489 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3490 outb (0x03, dev.fx_dsp_page);
3491 outb (0x10, dev.fx_dsp_addr);
3493 for (i = 0; i < sizeof (page_three_v2); i++) {
3494 outb (page_three_v2[i], dev.fx_dsp_lsb);
3495 if (!wffx_idle()) return (-1);
3498 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3499 outb (0x04, dev.fx_dsp_page);
3500 outb (0x10, dev.fx_dsp_addr);
3502 for (i = 0; i < sizeof (page_four_v2); i++) {
3503 outb (page_four_v2[i], dev.fx_dsp_lsb);
3504 if (!wffx_idle()) return (-1);
3507 outb (FX_LSB_TRANSFER, dev.fx_lcr);
3508 outb (0x06, dev.fx_dsp_page);
3510 /* Page six v.2 is algorithmic */
3512 for (i = 0x10; i <= 0x3e; i += 2) {
3513 outb (i, dev.fx_dsp_addr);
3514 outb (0x00, dev.fx_dsp_msb);
3515 outb (0x00, dev.fx_dsp_lsb);
3516 if (!wffx_idle()) return (-1);
3519 outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
3520 outb (0x07, dev.fx_dsp_page);
3521 outb (0x10, dev.fx_dsp_addr);
3523 for (i = 0; i < sizeof (page_seven_v2); i += 2) {
3524 outb (page_seven_v2[i], dev.fx_dsp_msb);
3525 outb (page_seven_v2[i+1], dev.fx_dsp_lsb);
3526 if (!wffx_idle()) return (-1);
3529 for (i = 0x00; i < sizeof(mod_v2); i += 2) {
3530 outb (mod_v2[i], dev.fx_mod_addr);
3531 outb (mod_v2[i+1], dev.fx_mod_data);
3532 if (!wffx_idle()) return (-1);
3535 for (i = 0; i < sizeof (coefficients2); i += 4) {
3536 outb (coefficients2[i], dev.fx_dsp_page);
3537 outb (coefficients2[i+1], dev.fx_dsp_addr);
3538 outb (coefficients2[i+2], dev.fx_dsp_msb);
3539 outb (coefficients2[i+3], dev.fx_dsp_lsb);
3540 if (!wffx_idle()) return (-1);
3543 for (i = 0; i < sizeof (coefficients3); i += 2) {
3544 int x;
3546 outb (0x07, dev.fx_dsp_page);
3547 x = (i % 4) ? 0x4e : 0x4c;
3548 outb (x, dev.fx_dsp_addr);
3549 outb (coefficients3[i], dev.fx_dsp_msb);
3550 outb (coefficients3[i+1], dev.fx_dsp_lsb);
3553 outb (0x00, dev.fx_op); /* mute off */
3554 if (!wffx_idle()) return (-1);
3556 return (0);
3559 EXPORT_NO_SYMBOLS;
3561 int io = -1;
3562 int irq = -1;
3564 MODULE_AUTHOR ("Paul Barton-Davis <pbd@op.net>");
3565 MODULE_DESCRIPTION ("Turtle Beach WaveFront Linux Driver");
3566 MODULE_PARM (io,"i");
3567 MODULE_PARM (irq,"i");
3569 int init_module (void)
3572 printk ("Turtle Beach WaveFront Driver\n"
3573 "Copyright (C) by Hannu Solvainen, "
3574 "Paul Barton-Davis 1993-1998.\n");
3576 /* XXX t'would be lovely to ask the CS4232 for these values, eh ? */
3578 if (io == -1 || irq == -1) {
3579 printk (KERN_INFO LOGNAME "irq and io options must be set.\n");
3580 return -EINVAL;
3583 if (wavefront_interrupt_bits (irq) < 0) {
3584 printk (KERN_INFO LOGNAME
3585 "IRQ must be 9, 5, 12 or 15 (not %d)\n", irq);
3586 return -ENODEV;
3589 if (detect_wavefront (irq, io) < 0) {
3590 return -ENODEV;
3593 if (install_wavefront () < 0) {
3594 return -EIO;
3597 SOUND_LOCK;
3598 return 0;
3601 void cleanup_module (void)
3604 uninstall_wavefront ();
3606 SOUND_LOCK_END;
3609 #endif CONFIG_SOUND_WAVEFRONT_MODULE && MODULE