1 /* Copyright (C) by Paul Barton-Davis 1998-1999
3 * Some portions of this file are taken from work that is
4 * copyright (C) by Hannu Savolainen 1993-1996
6 * This program is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
7 * Version 2 (June 1991). See the "COPYING" file distributed with this software
12 * An ALSA lowlevel driver for Turtle Beach ICS2115 wavetable synth
13 * (Maui, Tropez, Tropez Plus)
15 * This driver supports the onboard wavetable synthesizer (an ICS2115),
16 * including patch, sample and program loading and unloading, conversion
17 * of GUS patches during loading, and full user-level access to all
18 * WaveFront commands. It tries to provide semi-intelligent patch and
19 * sample management as well.
23 #include <sound/driver.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/time.h>
29 #include <linux/wait.h>
30 #include <sound/core.h>
31 #include <sound/snd_wavefront.h>
32 #include <sound/initval.h>
34 int wf_raw = 0; /* we normally check for "raw state" to firmware
35 loading. if non-zero, then during driver loading, the
36 state of the board is ignored, and we reset the
37 board and load the firmware anyway.
40 int fx_raw = 1; /* if this is zero, we'll leave the FX processor in
41 whatever state it is when the driver is loaded.
42 The default is to download the microprogram and
43 associated coefficients to set it up for "default"
44 operation, whatever that means.
47 int debug_default = 0; /* you can set this to control debugging
48 during driver loading. it takes any combination
49 of the WF_DEBUG_* flags defined in
53 /* XXX this needs to be made firmware and hardware version dependent */
55 char *ospath = "/etc/sound/wavefront.os"; /* where to find a processed
56 version of the WaveFront OS
59 int wait_usecs = 150; /* This magic number seems to give pretty optimal
60 throughput based on my limited experimentation.
61 If you want to play around with it and find a better
62 value, be my guest. Remember, the idea is to
63 get a number that causes us to just busy wait
64 for as many WaveFront commands as possible, without
65 coming up with a number so large that we hog the
68 Specifically, with this number, out of about 134,000
69 status waits, only about 250 result in a sleep.
72 int sleep_interval = 100; /* HZ/sleep_interval seconds per sleep */
73 int sleep_tries = 50; /* number of times we'll try to sleep */
75 int reset_time = 2; /* hundreths of a second we wait after a HW
76 reset for the expected interrupt.
79 int ramcheck_time = 20; /* time in seconds to wait while ROM code
83 int osrun_time = 10; /* time in seconds we wait for the OS to
86 MODULE_PARM(wf_raw,"i");
87 MODULE_PARM_DESC(wf_raw, "if non-zero, assume that we need to boot the OS");
88 MODULE_PARM(fx_raw,"i");
89 MODULE_PARM_DESC(fx_raw, "if non-zero, assume that the FX process needs help");
90 MODULE_PARM(debug_default,"i");
91 MODULE_PARM_DESC(debug_default, "debug parameters for card initialization");
92 MODULE_PARM(wait_usecs,"i");
93 MODULE_PARM_DESC(wait_usecs, "how long to wait without sleeping, usecs");
94 MODULE_PARM(sleep_interval,"i");
95 MODULE_PARM_DESC(sleep_interval, "how long to sleep when waiting for reply");
96 MODULE_PARM(sleep_tries,"i");
97 MODULE_PARM_DESC(sleep_tries, "how many times to try sleeping during a wait");
98 MODULE_PARM(ospath,"s");
99 MODULE_PARM_DESC(ospath, "full pathname to processed ICS2115 OS firmware");
100 MODULE_PARM(reset_time,"i");
101 MODULE_PARM_DESC(reset_time, "how long to wait for a reset to take effect");
102 MODULE_PARM(ramcheck_time,"i");
103 MODULE_PARM_DESC(ramcheck_time, "how many seconds to wait for the RAM test");
104 MODULE_PARM(osrun_time,"i");
105 MODULE_PARM_DESC(osrun_time, "how many seconds to wait for the ICS2115 OS");
107 /* if WF_DEBUG not defined, no run-time debugging messages will
108 be available via the debug flag setting. Given the current
109 beta state of the driver, this will remain set until a future
117 #if defined(NEW_MACRO_VARARGS) || __GNUC__ >= 3
118 #define DPRINT(cond, ...) \
119 if ((dev->debug & (cond)) == (cond)) { \
120 snd_printk (__VA_ARGS__); \
123 #define DPRINT(cond, args...) \
124 if ((dev->debug & (cond)) == (cond)) { \
125 snd_printk (##args); \
129 #define DPRINT(cond, args...)
130 #endif /* WF_DEBUG */
132 #define LOGNAME "WaveFront: "
134 /* bitmasks for WaveFront status port value */
136 #define STAT_RINTR_ENABLED 0x01
137 #define STAT_CAN_READ 0x02
138 #define STAT_INTR_READ 0x04
139 #define STAT_WINTR_ENABLED 0x10
140 #define STAT_CAN_WRITE 0x20
141 #define STAT_INTR_WRITE 0x40
143 static int wavefront_delete_sample (snd_wavefront_t *, int sampnum);
144 static int wavefront_find_free_sample (snd_wavefront_t *);
149 unsigned int read_cnt;
150 unsigned int write_cnt;
157 } wavefront_errors[] = {
158 { 0x01, "Bad sample number" },
159 { 0x02, "Out of sample memory" },
160 { 0x03, "Bad patch number" },
161 { 0x04, "Error in number of voices" },
162 { 0x06, "Sample load already in progress" },
163 { 0x0B, "No sample load request pending" },
164 { 0x0E, "Bad MIDI channel number" },
165 { 0x10, "Download Record Error" },
172 static wavefront_command wavefront_commands[] = {
173 { WFC_SET_SYNTHVOL, "set synthesizer volume", 0, 1, NEEDS_ACK },
174 { WFC_GET_SYNTHVOL, "get synthesizer volume", 1, 0, 0},
175 { WFC_SET_NVOICES, "set number of voices", 0, 1, NEEDS_ACK },
176 { WFC_GET_NVOICES, "get number of voices", 1, 0, 0 },
177 { WFC_SET_TUNING, "set synthesizer tuning", 0, 2, NEEDS_ACK },
178 { WFC_GET_TUNING, "get synthesizer tuning", 2, 0, 0 },
179 { WFC_DISABLE_CHANNEL, "disable synth channel", 0, 1, NEEDS_ACK },
180 { WFC_ENABLE_CHANNEL, "enable synth channel", 0, 1, NEEDS_ACK },
181 { WFC_GET_CHANNEL_STATUS, "get synth channel status", 3, 0, 0 },
182 { WFC_MISYNTH_OFF, "disable midi-in to synth", 0, 0, NEEDS_ACK },
183 { WFC_MISYNTH_ON, "enable midi-in to synth", 0, 0, NEEDS_ACK },
184 { WFC_VMIDI_ON, "enable virtual midi mode", 0, 0, NEEDS_ACK },
185 { WFC_VMIDI_OFF, "disable virtual midi mode", 0, 0, NEEDS_ACK },
186 { WFC_MIDI_STATUS, "report midi status", 1, 0, 0 },
187 { WFC_FIRMWARE_VERSION, "report firmware version", 2, 0, 0 },
188 { WFC_HARDWARE_VERSION, "report hardware version", 2, 0, 0 },
189 { WFC_GET_NSAMPLES, "report number of samples", 2, 0, 0 },
190 { WFC_INSTOUT_LEVELS, "report instantaneous output levels", 7, 0, 0 },
191 { WFC_PEAKOUT_LEVELS, "report peak output levels", 7, 0, 0 },
192 { WFC_DOWNLOAD_SAMPLE, "download sample",
193 0, WF_SAMPLE_BYTES, NEEDS_ACK },
194 { WFC_DOWNLOAD_BLOCK, "download block", 0, 0, NEEDS_ACK},
195 { WFC_DOWNLOAD_SAMPLE_HEADER, "download sample header",
196 0, WF_SAMPLE_HDR_BYTES, NEEDS_ACK },
197 { WFC_UPLOAD_SAMPLE_HEADER, "upload sample header", 13, 2, 0 },
199 /* This command requires a variable number of bytes to be written.
200 There is a hack in snd_wavefront_cmd() to support this. The actual
201 count is passed in as the read buffer ptr, cast appropriately.
205 { WFC_DOWNLOAD_MULTISAMPLE, "download multisample", 0, 0, NEEDS_ACK },
207 /* This one is a hack as well. We just read the first byte of the
208 response, don't fetch an ACK, and leave the rest to the
209 calling function. Ugly, ugly, ugly.
212 { WFC_UPLOAD_MULTISAMPLE, "upload multisample", 2, 1, 0 },
213 { WFC_DOWNLOAD_SAMPLE_ALIAS, "download sample alias",
214 0, WF_ALIAS_BYTES, NEEDS_ACK },
215 { WFC_UPLOAD_SAMPLE_ALIAS, "upload sample alias", WF_ALIAS_BYTES, 2, 0},
216 { WFC_DELETE_SAMPLE, "delete sample", 0, 2, NEEDS_ACK },
217 { WFC_IDENTIFY_SAMPLE_TYPE, "identify sample type", 5, 2, 0 },
218 { WFC_UPLOAD_SAMPLE_PARAMS, "upload sample parameters" },
219 { WFC_REPORT_FREE_MEMORY, "report free memory", 4, 0, 0 },
220 { WFC_DOWNLOAD_PATCH, "download patch", 0, 134, NEEDS_ACK },
221 { WFC_UPLOAD_PATCH, "upload patch", 132, 2, 0 },
222 { WFC_DOWNLOAD_PROGRAM, "download program", 0, 33, NEEDS_ACK },
223 { WFC_UPLOAD_PROGRAM, "upload program", 32, 1, 0 },
224 { WFC_DOWNLOAD_EDRUM_PROGRAM, "download enhanced drum program", 0, 9,
226 { WFC_UPLOAD_EDRUM_PROGRAM, "upload enhanced drum program", 8, 1, 0},
227 { WFC_SET_EDRUM_CHANNEL, "set enhanced drum program channel",
229 { WFC_DISABLE_DRUM_PROGRAM, "disable drum program", 0, 1, NEEDS_ACK },
230 { WFC_REPORT_CHANNEL_PROGRAMS, "report channel program numbers",
232 { WFC_NOOP, "the no-op command", 0, 0, NEEDS_ACK },
237 wavefront_errorstr (int errnum)
242 for (i = 0; wavefront_errors[i].errstr; i++) {
243 if (wavefront_errors[i].errno == errnum) {
244 return wavefront_errors[i].errstr;
248 return "Unknown WaveFront error";
251 static wavefront_command *
252 wavefront_get_command (int cmd)
257 for (i = 0; wavefront_commands[i].cmd != 0; i++) {
258 if (cmd == wavefront_commands[i].cmd) {
259 return &wavefront_commands[i];
263 return (wavefront_command *) 0;
267 wavefront_status (snd_wavefront_t *dev)
270 return inb (dev->status_port);
274 wavefront_sleep (int limit)
277 set_current_state(TASK_INTERRUPTIBLE);
278 schedule_timeout(limit);
280 return signal_pending(current);
284 wavefront_wait (snd_wavefront_t *dev, int mask)
289 /* Spin for a short period of time, because >99% of all
290 requests to the WaveFront can be serviced inline like this.
293 for (i = 0; i < wait_usecs; i += 5) {
294 if (wavefront_status (dev) & mask) {
300 for (i = 0; i < sleep_tries; i++) {
302 if (wavefront_status (dev) & mask) {
306 if (wavefront_sleep (HZ/sleep_interval)) {
315 wavefront_read (snd_wavefront_t *dev)
318 if (wavefront_wait (dev, STAT_CAN_READ))
319 return inb (dev->data_port);
321 DPRINT (WF_DEBUG_DATA, "read timeout.\n");
327 wavefront_write (snd_wavefront_t *dev, unsigned char data)
330 if (wavefront_wait (dev, STAT_CAN_WRITE)) {
331 outb (data, dev->data_port);
335 DPRINT (WF_DEBUG_DATA, "write timeout.\n");
341 snd_wavefront_cmd (snd_wavefront_t *dev,
342 int cmd, unsigned char *rbuf, unsigned char *wbuf)
348 wavefront_command *wfcmd;
350 if ((wfcmd = wavefront_get_command (cmd)) == (wavefront_command *) 0) {
351 snd_printk ("command 0x%x not supported.\n",
356 /* Hack to handle the one variable-size write command. See
357 wavefront_send_multisample() for the other half of this
358 gross and ugly strategy.
361 if (cmd == WFC_DOWNLOAD_MULTISAMPLE) {
362 wfcmd->write_cnt = (unsigned long) rbuf;
366 DPRINT (WF_DEBUG_CMD, "0x%x [%s] (%d,%d,%d)\n",
367 cmd, wfcmd->action, wfcmd->read_cnt,
368 wfcmd->write_cnt, wfcmd->need_ack);
370 if (wavefront_write (dev, cmd)) {
371 DPRINT ((WF_DEBUG_IO|WF_DEBUG_CMD), "cannot request "
377 if (wfcmd->write_cnt > 0) {
378 DPRINT (WF_DEBUG_DATA, "writing %d bytes "
380 wfcmd->write_cnt, cmd);
382 for (i = 0; i < wfcmd->write_cnt; i++) {
383 if (wavefront_write (dev, wbuf[i])) {
384 DPRINT (WF_DEBUG_IO, "bad write for byte "
385 "%d of 0x%x [%s].\n",
386 i, cmd, wfcmd->action);
390 DPRINT (WF_DEBUG_DATA, "write[%d] = 0x%x\n",
395 if (wfcmd->read_cnt > 0) {
396 DPRINT (WF_DEBUG_DATA, "reading %d ints "
398 wfcmd->read_cnt, cmd);
400 for (i = 0; i < wfcmd->read_cnt; i++) {
402 if ((c = wavefront_read (dev)) == -1) {
403 DPRINT (WF_DEBUG_IO, "bad read for byte "
404 "%d of 0x%x [%s].\n",
405 i, cmd, wfcmd->action);
409 /* Now handle errors. Lots of special cases here */
412 if ((c = wavefront_read (dev)) == -1) {
413 DPRINT (WF_DEBUG_IO, "bad read for "
422 /* Can you believe this madness ? */
425 wfcmd->cmd == WFC_IDENTIFY_SAMPLE_TYPE) {
426 rbuf[0] = WF_ST_EMPTY;
430 wfcmd->cmd == WFC_UPLOAD_PATCH) {
435 wfcmd->cmd == WFC_UPLOAD_PROGRAM) {
441 DPRINT (WF_DEBUG_IO, "error %d (%s) "
447 wavefront_errorstr (c),
458 DPRINT (WF_DEBUG_DATA, "read[%d] = 0x%x\n",i, rbuf[i]);
462 if ((wfcmd->read_cnt == 0 && wfcmd->write_cnt == 0) || wfcmd->need_ack) {
464 DPRINT (WF_DEBUG_CMD, "reading ACK for 0x%x\n", cmd);
466 /* Some commands need an ACK, but return zero instead
467 of the standard value.
470 if ((ack = wavefront_read (dev)) == 0) {
476 DPRINT (WF_DEBUG_IO, "cannot read ack for "
482 int err = -1; /* something unknown */
484 if (ack == 0xff) { /* explicit error */
486 if ((err = wavefront_read (dev)) == -1) {
487 DPRINT (WF_DEBUG_DATA,
494 DPRINT (WF_DEBUG_IO, "0x%x [%s] "
495 "failed (0x%x, 0x%x, %s)\n",
496 cmd, wfcmd->action, ack, err,
497 wavefront_errorstr (err));
503 DPRINT (WF_DEBUG_DATA, "ack received "
508 DPRINT (WF_DEBUG_CMD, "0x%x [%s] does not need "
510 cmd, wfcmd->action, wfcmd->read_cnt,
511 wfcmd->write_cnt, wfcmd->need_ack);
518 /***********************************************************************
519 WaveFront data munging
521 Things here are weird. All data written to the board cannot
522 have its most significant bit set. Any data item with values
523 potentially > 0x7F (127) must be split across multiple bytes.
525 Sometimes, we need to munge numeric values that are represented on
526 the x86 side as 8-32 bit values. Sometimes, we need to munge data
527 that is represented on the x86 side as an array of bytes. The most
528 efficient approach to handling both cases seems to be to use 2
529 different functions for munging and 2 for de-munging. This avoids
530 weird casting and worrying about bit-level offsets.
532 **********************************************************************/
534 static unsigned char *
535 munge_int32 (unsigned int src,
537 unsigned int dst_size)
541 for (i = 0; i < dst_size; i++) {
542 *dst = src & 0x7F; /* Mask high bit of LSB */
543 src = src >> 7; /* Rotate Right 7 bits */
544 /* Note: we leave the upper bits in place */
552 demunge_int32 (unsigned char* src, int src_size)
558 for (i = src_size - 1; i >= 0; i--) {
559 outval=(outval<<7)+src[i];
567 munge_buf (unsigned char *src, unsigned char *dst, unsigned int dst_size)
571 unsigned int last = dst_size / 2;
573 for (i = 0; i < last; i++) {
574 *dst++ = src[i] & 0x7f;
575 *dst++ = src[i] >> 7;
582 demunge_buf (unsigned char *src, unsigned char *dst, unsigned int src_bytes)
586 unsigned char *end = src + src_bytes;
588 end = src + src_bytes;
590 /* NOTE: src and dst *CAN* point to the same address */
592 for (i = 0; src != end; i++) {
594 dst[i] |= (*src++)<<7;
600 /***********************************************************************
601 WaveFront: sample, patch and program management.
602 ***********************************************************************/
605 wavefront_delete_sample (snd_wavefront_t *dev, int sample_num)
608 unsigned char wbuf[2];
611 wbuf[0] = sample_num & 0x7f;
612 wbuf[1] = sample_num >> 7;
614 if ((x = snd_wavefront_cmd (dev, WFC_DELETE_SAMPLE, 0, wbuf)) == 0) {
615 dev->sample_status[sample_num] = WF_ST_EMPTY;
622 wavefront_get_sample_status (snd_wavefront_t *dev, int assume_rom)
626 unsigned char rbuf[32], wbuf[32];
627 unsigned int sc_real, sc_alias, sc_multi;
629 /* check sample status */
631 if (snd_wavefront_cmd (dev, WFC_GET_NSAMPLES, rbuf, wbuf)) {
632 snd_printk ("cannot request sample count.\n");
636 sc_real = sc_alias = sc_multi = dev->samples_used = 0;
638 for (i = 0; i < WF_MAX_SAMPLE; i++) {
643 if (snd_wavefront_cmd (dev, WFC_IDENTIFY_SAMPLE_TYPE, rbuf, wbuf)) {
644 snd_printk("cannot identify sample "
645 "type of slot %d\n", i);
646 dev->sample_status[i] = WF_ST_EMPTY;
650 dev->sample_status[i] = (WF_SLOT_FILLED|rbuf[0]);
653 dev->sample_status[i] |= WF_SLOT_ROM;
656 switch (rbuf[0] & WF_ST_MASK) {
660 case WF_ST_MULTISAMPLE:
670 snd_printk ("unknown sample type for "
675 if (rbuf[0] != WF_ST_EMPTY) {
680 snd_printk ("%d samples used (%d real, %d aliases, %d multi), "
681 "%d empty\n", dev->samples_used, sc_real, sc_alias, sc_multi,
682 WF_MAX_SAMPLE - dev->samples_used);
690 wavefront_get_patch_status (snd_wavefront_t *dev)
693 unsigned char patchbuf[WF_PATCH_BYTES];
694 unsigned char patchnum[2];
698 for (i = 0; i < WF_MAX_PATCH; i++) {
699 patchnum[0] = i & 0x7f;
700 patchnum[1] = i >> 7;
702 if ((x = snd_wavefront_cmd (dev, WFC_UPLOAD_PATCH, patchbuf,
705 dev->patch_status[i] |= WF_SLOT_FILLED;
706 p = (wavefront_patch *) patchbuf;
708 [p->sample_number|(p->sample_msb<<7)] |=
711 } else if (x == 3) { /* Bad patch number */
712 dev->patch_status[i] = 0;
714 snd_printk ("upload patch "
716 dev->patch_status[i] = 0;
721 /* program status has already filled in slot_used bits */
723 for (i = 0, cnt = 0, cnt2 = 0; i < WF_MAX_PATCH; i++) {
724 if (dev->patch_status[i] & WF_SLOT_FILLED) {
727 if (dev->patch_status[i] & WF_SLOT_USED) {
732 snd_printk ("%d patch slots filled, %d in use\n", cnt, cnt2);
738 wavefront_get_program_status (snd_wavefront_t *dev)
741 unsigned char progbuf[WF_PROGRAM_BYTES];
742 wavefront_program prog;
743 unsigned char prognum;
746 for (i = 0; i < WF_MAX_PROGRAM; i++) {
749 if ((x = snd_wavefront_cmd (dev, WFC_UPLOAD_PROGRAM, progbuf,
752 dev->prog_status[i] |= WF_SLOT_USED;
754 demunge_buf (progbuf, (unsigned char *) &prog,
757 for (l = 0; l < WF_NUM_LAYERS; l++) {
758 if (prog.layer[l].mute) {
760 [prog.layer[l].patch_number] |=
764 } else if (x == 1) { /* Bad program number */
765 dev->prog_status[i] = 0;
767 snd_printk ("upload program "
769 dev->prog_status[i] = 0;
773 for (i = 0, cnt = 0; i < WF_MAX_PROGRAM; i++) {
774 if (dev->prog_status[i]) {
779 snd_printk ("%d programs slots in use\n", cnt);
785 wavefront_send_patch (snd_wavefront_t *dev, wavefront_patch_info *header)
788 unsigned char buf[WF_PATCH_BYTES+2];
791 DPRINT (WF_DEBUG_LOAD_PATCH, "downloading patch %d\n",
794 dev->patch_status[header->number] |= WF_SLOT_FILLED;
797 bptr = munge_int32 (header->number, buf, 2);
798 munge_buf ((unsigned char *)&header->hdr.p, bptr, WF_PATCH_BYTES);
800 if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_PATCH, 0, buf)) {
801 snd_printk ("download patch failed\n");
809 wavefront_send_program (snd_wavefront_t *dev, wavefront_patch_info *header)
812 unsigned char buf[WF_PROGRAM_BYTES+1];
815 DPRINT (WF_DEBUG_LOAD_PATCH, "downloading program %d\n",
818 dev->prog_status[header->number] = WF_SLOT_USED;
820 /* XXX need to zero existing SLOT_USED bit for program_status[i]
821 where `i' is the program that's being (potentially) overwritten.
824 for (i = 0; i < WF_NUM_LAYERS; i++) {
825 if (header->hdr.pr.layer[i].mute) {
826 dev->patch_status[header->hdr.pr.layer[i].patch_number] |=
829 /* XXX need to mark SLOT_USED for sample used by
830 patch_number, but this means we have to load it. Ick.
835 buf[0] = header->number;
836 munge_buf ((unsigned char *)&header->hdr.pr, &buf[1], WF_PROGRAM_BYTES);
838 if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_PROGRAM, 0, buf)) {
839 snd_printk ("download patch failed\n");
847 wavefront_freemem (snd_wavefront_t *dev)
852 if (snd_wavefront_cmd (dev, WFC_REPORT_FREE_MEMORY, rbuf, 0)) {
853 snd_printk ("can't get memory stats.\n");
856 return demunge_int32 (rbuf, 4);
861 wavefront_send_sample (snd_wavefront_t *dev,
862 wavefront_patch_info *header,
864 int data_is_unsigned)
867 /* samples are downloaded via a 16-bit wide i/o port
868 (you could think of it as 2 adjacent 8-bit wide ports
869 but its less efficient that way). therefore, all
870 the blocksizes and so forth listed in the documentation,
871 and used conventionally to refer to sample sizes,
872 which are given in 8-bit units (bytes), need to be
880 const unsigned int max_blksize = 4096/2;
881 unsigned int written;
882 unsigned int blocksize;
885 unsigned char sample_hdr[WF_SAMPLE_HDR_BYTES];
886 unsigned char *shptr;
888 int initial_skip = 0;
890 DPRINT (WF_DEBUG_LOAD_PATCH, "sample %sdownload for slot %d, "
891 "type %d, %d bytes from 0x%lx\n",
892 header->size ? "" : "header ",
893 header->number, header->subkey,
895 (unsigned long) header->dataptr);
897 if (header->number == WAVEFRONT_FIND_FREE_SAMPLE_SLOT) {
900 if ((x = wavefront_find_free_sample (dev)) < 0) {
903 snd_printk ("unspecified sample => %d\n", x);
909 /* XXX it's a debatable point whether or not RDONLY semantics
910 on the ROM samples should cover just the sample data or
911 the sample header. For now, it only covers the sample data,
912 so anyone is free at all times to rewrite sample headers.
914 My reason for this is that we have the sample headers
915 available in the WFB file for General MIDI, and so these
916 can always be reset if needed. The sample data, however,
917 cannot be recovered without a complete reset and firmware
918 reload of the ICS2115, which is a very expensive operation.
920 So, doing things this way allows us to honor the notion of
921 "RESETSAMPLES" reasonably cheaply. Note however, that this
922 is done purely at user level: there is no WFB parser in
923 this driver, and so a complete reset (back to General MIDI,
924 or theoretically some other configuration) is the
925 responsibility of the user level library.
927 To try to do this in the kernel would be a little
928 crazy: we'd need 158K of kernel space just to hold
929 a copy of the patch/program/sample header data.
932 if (dev->rom_samples_rdonly) {
933 if (dev->sample_status[header->number] & WF_SLOT_ROM) {
934 snd_printk ("sample slot %d "
941 wavefront_delete_sample (dev, header->number);
945 dev->freemem = wavefront_freemem (dev);
947 if (dev->freemem < (int)header->size) {
948 snd_printk ("insufficient memory to "
949 "load %d byte sample.\n",
956 skip = WF_GET_CHANNEL(&header->hdr.s);
958 if (skip > 0 && header->hdr.s.SampleResolution != LINEAR_16BIT) {
959 snd_printk ("channel selection only "
960 "possible on 16-bit samples");
995 DPRINT (WF_DEBUG_LOAD_PATCH, "channel selection: %d => "
996 "initial skip = %d, skip = %d\n",
997 WF_GET_CHANNEL (&header->hdr.s),
1000 /* Be safe, and zero the "Unused" bits ... */
1002 WF_SET_CHANNEL(&header->hdr.s, 0);
1004 /* adjust size for 16 bit samples by dividing by two. We always
1005 send 16 bits per write, even for 8 bit samples, so the length
1006 is always half the size of the sample data in bytes.
1009 length = header->size / 2;
1011 /* the data we're sent has not been munged, and in fact, the
1012 header we have to send isn't just a munged copy either.
1013 so, build the sample header right here.
1016 shptr = &sample_hdr[0];
1018 shptr = munge_int32 (header->number, shptr, 2);
1021 shptr = munge_int32 (length, shptr, 4);
1024 /* Yes, a 4 byte result doesn't contain all of the offset bits,
1025 but the offset only uses 24 bits.
1028 shptr = munge_int32 (*((u32 *) &header->hdr.s.sampleStartOffset),
1030 shptr = munge_int32 (*((u32 *) &header->hdr.s.loopStartOffset),
1032 shptr = munge_int32 (*((u32 *) &header->hdr.s.loopEndOffset),
1034 shptr = munge_int32 (*((u32 *) &header->hdr.s.sampleEndOffset),
1037 /* This one is truly weird. What kind of weirdo decided that in
1038 a system dominated by 16 and 32 bit integers, they would use
1042 shptr = munge_int32 (header->hdr.s.FrequencyBias, shptr, 3);
1044 /* Why is this nybblified, when the MSB is *always* zero ?
1045 Anyway, we can't take address of bitfield, so make a
1046 good-faith guess at where it starts.
1049 shptr = munge_int32 (*(&header->hdr.s.FrequencyBias+1),
1052 if (snd_wavefront_cmd (dev,
1054 WFC_DOWNLOAD_SAMPLE : WFC_DOWNLOAD_SAMPLE_HEADER,
1056 snd_printk ("sample %sdownload refused.\n",
1057 header->size ? "" : "header ");
1061 if (header->size == 0) {
1062 goto sent; /* Sorry. Just had to have one somewhere */
1065 data_end = dataptr + length;
1067 /* Do any initial skip over an unused channel's data */
1069 dataptr += initial_skip;
1071 for (written = 0, blocknum = 0;
1072 written < length; written += max_blksize, blocknum++) {
1074 if ((length - written) > max_blksize) {
1075 blocksize = max_blksize;
1077 /* round to nearest 16-byte value */
1078 blocksize = ((length-written+7)&~0x7);
1081 if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_BLOCK, 0, 0)) {
1082 snd_printk ("download block "
1083 "request refused.\n");
1087 for (i = 0; i < blocksize; i++) {
1089 if (dataptr < data_end) {
1091 __get_user (sample_short, dataptr);
1094 if (data_is_unsigned) { /* GUS ? */
1096 if (WF_SAMPLE_IS_8BIT(&header->hdr.s)) {
1104 &sample_short)[0] += 0x7f;
1106 &sample_short)[1] += 0x7f;
1115 sample_short += 0x7fff;
1121 /* In padding section of final block:
1123 Don't fetch unsupplied data from
1124 user space, just continue with
1125 whatever the final value was.
1129 if (i < blocksize - 1) {
1130 outw (sample_short, dev->block_port);
1132 outw (sample_short, dev->last_block_port);
1136 /* Get "DMA page acknowledge", even though its really
1137 nothing to do with DMA at all.
1140 if ((dma_ack = wavefront_read (dev)) != WF_DMA_ACK) {
1141 if (dma_ack == -1) {
1142 snd_printk ("upload sample "
1143 "DMA ack timeout\n");
1146 snd_printk ("upload sample "
1147 "DMA ack error 0x%x\n",
1154 dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_SAMPLE);
1156 /* Note, label is here because sending the sample header shouldn't
1157 alter the sample_status info at all.
1165 wavefront_send_alias (snd_wavefront_t *dev, wavefront_patch_info *header)
1168 unsigned char alias_hdr[WF_ALIAS_BYTES];
1170 DPRINT (WF_DEBUG_LOAD_PATCH, "download alias, %d is "
1173 header->hdr.a.OriginalSample);
1175 munge_int32 (header->number, &alias_hdr[0], 2);
1176 munge_int32 (header->hdr.a.OriginalSample, &alias_hdr[2], 2);
1177 munge_int32 (*((unsigned int *)&header->hdr.a.sampleStartOffset),
1179 munge_int32 (*((unsigned int *)&header->hdr.a.loopStartOffset),
1181 munge_int32 (*((unsigned int *)&header->hdr.a.loopEndOffset),
1183 munge_int32 (*((unsigned int *)&header->hdr.a.sampleEndOffset),
1185 munge_int32 (header->hdr.a.FrequencyBias, &alias_hdr[20], 3);
1186 munge_int32 (*(&header->hdr.a.FrequencyBias+1), &alias_hdr[23], 2);
1188 if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_SAMPLE_ALIAS, 0, alias_hdr)) {
1189 snd_printk ("download alias failed.\n");
1193 dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_ALIAS);
1199 wavefront_send_multisample (snd_wavefront_t *dev, wavefront_patch_info *header)
1203 unsigned char msample_hdr[WF_MSAMPLE_BYTES];
1205 munge_int32 (header->number, &msample_hdr[0], 2);
1207 /* You'll recall at this point that the "number of samples" value
1208 in a wavefront_multisample struct is actually the log2 of the
1209 real number of samples.
1212 num_samples = (1<<(header->hdr.ms.NumberOfSamples&7));
1213 msample_hdr[2] = (unsigned char) header->hdr.ms.NumberOfSamples;
1215 DPRINT (WF_DEBUG_LOAD_PATCH, "multi %d with %d=%d samples\n",
1217 header->hdr.ms.NumberOfSamples,
1220 for (i = 0; i < num_samples; i++) {
1221 DPRINT(WF_DEBUG_LOAD_PATCH|WF_DEBUG_DATA, "sample[%d] = %d\n",
1222 i, header->hdr.ms.SampleNumber[i]);
1223 munge_int32 (header->hdr.ms.SampleNumber[i],
1224 &msample_hdr[3+(i*2)], 2);
1227 /* Need a hack here to pass in the number of bytes
1228 to be written to the synth. This is ugly, and perhaps
1229 one day, I'll fix it.
1232 if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_MULTISAMPLE,
1233 (unsigned char *) (long) ((num_samples*2)+3),
1235 snd_printk ("download of multisample failed.\n");
1239 dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_MULTISAMPLE);
1245 wavefront_fetch_multisample (snd_wavefront_t *dev,
1246 wavefront_patch_info *header)
1249 unsigned char log_ns[1];
1250 unsigned char number[2];
1253 munge_int32 (header->number, number, 2);
1255 if (snd_wavefront_cmd (dev, WFC_UPLOAD_MULTISAMPLE, log_ns, number)) {
1256 snd_printk ("upload multisample failed.\n");
1260 DPRINT (WF_DEBUG_DATA, "msample %d has %d samples\n",
1261 header->number, log_ns[0]);
1263 header->hdr.ms.NumberOfSamples = log_ns[0];
1265 /* get the number of samples ... */
1267 num_samples = (1 << log_ns[0]);
1269 for (i = 0; i < num_samples; i++) {
1273 if ((val = wavefront_read (dev)) == -1) {
1274 snd_printk ("upload multisample failed "
1275 "during sample loop.\n");
1280 if ((val = wavefront_read (dev)) == -1) {
1281 snd_printk ("upload multisample failed "
1282 "during sample loop.\n");
1287 header->hdr.ms.SampleNumber[i] =
1288 demunge_int32 ((unsigned char *) d, 2);
1290 DPRINT (WF_DEBUG_DATA, "msample sample[%d] = %d\n",
1291 i, header->hdr.ms.SampleNumber[i]);
1299 wavefront_send_drum (snd_wavefront_t *dev, wavefront_patch_info *header)
1302 unsigned char drumbuf[WF_DRUM_BYTES];
1303 wavefront_drum *drum = &header->hdr.d;
1306 DPRINT (WF_DEBUG_LOAD_PATCH, "downloading edrum for MIDI "
1307 "note %d, patch = %d\n",
1308 header->number, drum->PatchNumber);
1310 drumbuf[0] = header->number & 0x7f;
1312 for (i = 0; i < 4; i++) {
1313 munge_int32 (((unsigned char *)drum)[i], &drumbuf[1+(i*2)], 2);
1316 if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_EDRUM_PROGRAM, 0, drumbuf)) {
1317 snd_printk ("download drum failed.\n");
1325 wavefront_find_free_sample (snd_wavefront_t *dev)
1330 for (i = 0; i < WF_MAX_SAMPLE; i++) {
1331 if (!(dev->sample_status[i] & WF_SLOT_FILLED)) {
1335 snd_printk ("no free sample slots!\n");
1341 wavefront_find_free_patch (snd_wavefront_t *dev)
1346 for (i = 0; i < WF_MAX_PATCH; i++) {
1347 if (!(dev->patch_status[i] & WF_SLOT_FILLED)) {
1351 snd_printk ("no free patch slots!\n");
1357 wavefront_load_patch (snd_wavefront_t *dev, const char *addr)
1360 wavefront_patch_info header;
1362 if (copy_from_user (&header, addr, sizeof(wavefront_patch_info) -
1363 sizeof(wavefront_any))) {
1364 snd_printk ("bad address for load patch.\n");
1368 DPRINT (WF_DEBUG_LOAD_PATCH, "download "
1370 "Sample number: %d "
1371 "Sample size: %d\n",
1376 switch (header.subkey) {
1377 case WF_ST_SAMPLE: /* sample or sample_header, based on patch->size */
1379 if (copy_from_user ((unsigned char *) &header.hdr.s,
1380 (unsigned char *) header.hdrptr,
1381 sizeof (wavefront_sample)))
1384 return wavefront_send_sample (dev, &header, header.dataptr, 0);
1386 case WF_ST_MULTISAMPLE:
1388 if (copy_from_user ((unsigned char *) &header.hdr.s,
1389 (unsigned char *) header.hdrptr,
1390 sizeof (wavefront_multisample)))
1393 return wavefront_send_multisample (dev, &header);
1398 if (copy_from_user ((unsigned char *) &header.hdr.a,
1399 (unsigned char *) header.hdrptr,
1400 sizeof (wavefront_alias)))
1403 return wavefront_send_alias (dev, &header);
1406 if (copy_from_user ((unsigned char *) &header.hdr.d,
1407 (unsigned char *) header.hdrptr,
1408 sizeof (wavefront_drum)))
1411 return wavefront_send_drum (dev, &header);
1414 if (copy_from_user ((unsigned char *) &header.hdr.p,
1415 (unsigned char *) header.hdrptr,
1416 sizeof (wavefront_patch)))
1419 return wavefront_send_patch (dev, &header);
1422 if (copy_from_user ((unsigned char *) &header.hdr.pr,
1423 (unsigned char *) header.hdrptr,
1424 sizeof (wavefront_program)))
1427 return wavefront_send_program (dev, &header);
1430 snd_printk ("unknown patch type %d.\n",
1438 /***********************************************************************
1439 WaveFront: hardware-dependent interface
1440 ***********************************************************************/
1443 process_sample_hdr (u8 *buf)
1451 /* The board doesn't send us an exact copy of a "wavefront_sample"
1452 in response to an Upload Sample Header command. Instead, we
1453 have to convert the data format back into our data structure,
1454 just as in the Download Sample command, where we have to do
1455 something very similar in the reverse direction.
1458 *((u32 *) &s.sampleStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
1459 *((u32 *) &s.loopStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
1460 *((u32 *) &s.loopEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
1461 *((u32 *) &s.sampleEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
1462 *((u32 *) &s.FrequencyBias) = demunge_int32 (ptr, 3); ptr += 3;
1464 s.SampleResolution = *ptr & 0x3;
1465 s.Loop = *ptr & 0x8;
1466 s.Bidirectional = *ptr & 0x10;
1467 s.Reverse = *ptr & 0x40;
1469 /* Now copy it back to where it came from */
1471 memcpy (buf, (unsigned char *) &s, sizeof (wavefront_sample));
1475 wavefront_synth_control (snd_wavefront_card_t *acard,
1476 wavefront_control *wc)
1479 snd_wavefront_t *dev = &acard->wavefront;
1480 unsigned char patchnumbuf[2];
1483 DPRINT (WF_DEBUG_CMD, "synth control with "
1484 "cmd 0x%x\n", wc->cmd);
1486 /* Pre-handling of or for various commands */
1490 case WFC_DISABLE_INTERRUPTS:
1491 snd_printk ("interrupts disabled.\n");
1492 outb (0x80|0x20, dev->control_port);
1493 dev->interrupts_are_midi = 1;
1496 case WFC_ENABLE_INTERRUPTS:
1497 snd_printk ("interrupts enabled.\n");
1498 outb (0x80|0x40|0x20, dev->control_port);
1499 dev->interrupts_are_midi = 1;
1502 case WFC_INTERRUPT_STATUS:
1503 wc->rbuf[0] = dev->interrupts_are_midi;
1506 case WFC_ROMSAMPLES_RDONLY:
1507 dev->rom_samples_rdonly = wc->wbuf[0];
1511 case WFC_IDENTIFY_SLOT_TYPE:
1512 i = wc->wbuf[0] | (wc->wbuf[1] << 7);
1513 if (i <0 || i >= WF_MAX_SAMPLE) {
1514 snd_printk ("invalid slot ID %d\n",
1516 wc->status = EINVAL;
1519 wc->rbuf[0] = dev->sample_status[i];
1523 case WFC_DEBUG_DRIVER:
1524 dev->debug = wc->wbuf[0];
1525 snd_printk ("debug = 0x%x\n", dev->debug);
1528 case WFC_UPLOAD_PATCH:
1529 munge_int32 (*((u32 *) wc->wbuf), patchnumbuf, 2);
1530 memcpy (wc->wbuf, patchnumbuf, 2);
1533 case WFC_UPLOAD_MULTISAMPLE:
1534 /* multisamples have to be handled differently, and
1535 cannot be dealt with properly by snd_wavefront_cmd() alone.
1537 wc->status = wavefront_fetch_multisample
1538 (dev, (wavefront_patch_info *) wc->rbuf);
1541 case WFC_UPLOAD_SAMPLE_ALIAS:
1542 snd_printk ("support for sample alias upload "
1543 "being considered.\n");
1544 wc->status = EINVAL;
1548 wc->status = snd_wavefront_cmd (dev, wc->cmd, wc->rbuf, wc->wbuf);
1550 /* Post-handling of certain commands.
1552 In particular, if the command was an upload, demunge the data
1553 so that the user-level doesn't have to think about it.
1556 if (wc->status == 0) {
1558 /* intercept any freemem requests so that we know
1559 we are always current with the user-level view
1563 case WFC_REPORT_FREE_MEMORY:
1564 dev->freemem = demunge_int32 (wc->rbuf, 4);
1567 case WFC_UPLOAD_PATCH:
1568 demunge_buf (wc->rbuf, wc->rbuf, WF_PATCH_BYTES);
1571 case WFC_UPLOAD_PROGRAM:
1572 demunge_buf (wc->rbuf, wc->rbuf, WF_PROGRAM_BYTES);
1575 case WFC_UPLOAD_EDRUM_PROGRAM:
1576 demunge_buf (wc->rbuf, wc->rbuf, WF_DRUM_BYTES - 1);
1579 case WFC_UPLOAD_SAMPLE_HEADER:
1580 process_sample_hdr (wc->rbuf);
1583 case WFC_UPLOAD_SAMPLE_ALIAS:
1584 snd_printk ("support for "
1585 "sample aliases still "
1586 "being considered.\n");
1590 snd_wavefront_midi_disable_virtual (acard);
1594 snd_wavefront_midi_enable_virtual (acard);
1603 snd_wavefront_synth_open (snd_hwdep_t *hw, struct file *file)
1606 if (!try_module_get(hw->card->module))
1608 file->private_data = hw;
1613 snd_wavefront_synth_release (snd_hwdep_t *hw, struct file *file)
1616 module_put(hw->card->module);
1621 snd_wavefront_synth_ioctl (snd_hwdep_t *hw, struct file *file,
1622 unsigned int cmd, unsigned long arg)
1626 snd_wavefront_t *dev;
1627 snd_wavefront_card_t *acard;
1628 wavefront_control wc;
1630 card = (snd_card_t *) hw->card;
1632 snd_assert(card != NULL, return -ENODEV);
1634 snd_assert(card->private_data != NULL, return -ENODEV);
1636 acard = card->private_data;
1637 dev = &acard->wavefront;
1640 case WFCTL_LOAD_SPP:
1641 if (wavefront_load_patch (dev, (char *) arg) != 0) {
1647 if (copy_from_user (&wc, (void *) arg, sizeof (wc)))
1649 if (wavefront_synth_control (acard, &wc) < 0) {
1652 if (copy_to_user ((void *) arg, &wc, sizeof (wc)))
1664 /***********************************************************************/
1665 /* WaveFront: interface for card-level wavefront module */
1666 /***********************************************************************/
1669 snd_wavefront_internal_interrupt (snd_wavefront_card_t *card)
1671 snd_wavefront_t *dev = &card->wavefront;
1674 Some comments on interrupts. I attempted a version of this
1675 driver that used interrupts throughout the code instead of
1676 doing busy and/or sleep-waiting. Alas, it appears that once
1677 the Motorola firmware is downloaded, the card *never*
1678 generates an RX interrupt. These are successfully generated
1679 during firmware loading, and after that wavefront_status()
1680 reports that an interrupt is pending on the card from time
1681 to time, but it never seems to be delivered to this
1682 driver. Note also that wavefront_status() continues to
1683 report that RX interrupts are enabled, suggesting that I
1684 didn't goof up and disable them by mistake.
1686 Thus, I stepped back to a prior version of
1687 wavefront_wait(), the only place where this really
1688 matters. Its sad, but I've looked through the code to check
1689 on things, and I really feel certain that the Motorola
1690 firmware prevents RX-ready interrupts.
1693 if ((wavefront_status(dev) & (STAT_INTR_READ|STAT_INTR_WRITE)) == 0) {
1697 spin_lock(&dev->irq_lock);
1700 spin_unlock(&dev->irq_lock);
1701 wake_up(&dev->interrupt_sleeper);
1706 0 Host Rx Interrupt Enable (1=Enabled)
1707 1 Host Rx Register Full (1=Full)
1708 2 Host Rx Interrupt Pending (1=Interrupt)
1710 4 Host Tx Interrupt (1=Enabled)
1711 5 Host Tx Register empty (1=Empty)
1712 6 Host Tx Interrupt Pending (1=Interrupt)
1717 snd_wavefront_interrupt_bits (int irq)
1737 snd_printk ("invalid IRQ %d\n", irq);
1745 wavefront_should_cause_interrupt (snd_wavefront_t *dev,
1746 int val, int port, int timeout)
1751 init_waitqueue_entry(&wait, current);
1752 spin_lock_irq(&dev->irq_lock);
1753 add_wait_queue(&dev->interrupt_sleeper, &wait);
1756 spin_unlock_irq(&dev->irq_lock);
1758 if ((timeout = schedule_timeout(timeout)) == 0)
1766 wavefront_reset_to_cleanliness (snd_wavefront_t *dev)
1772 /* IRQ already checked */
1774 bits = snd_wavefront_interrupt_bits (dev->irq);
1776 /* try reset of port */
1778 outb (0x0, dev->control_port);
1780 /* At this point, the board is in reset, and the H/W initialization
1781 register is accessed at the same address as the data port.
1783 Bit 7 - Enable IRQ Driver
1784 0 - Tri-state the Wave-Board drivers for the PC Bus IRQs
1785 1 - Enable IRQ selected by bits 5:3 to be driven onto the PC Bus.
1787 Bit 6 - MIDI Interface Select
1789 0 - Use the MIDI Input from the 26-pin WaveBlaster
1790 compatible header as the serial MIDI source
1791 1 - Use the MIDI Input from the 9-pin D connector as the
1794 Bits 5:3 - IRQ Selection
1805 Bit 0 - Disable Boot ROM
1806 0 - memory accesses to 03FC30-03FFFFH utilize the internal Boot ROM
1807 1 - memory accesses to 03FC30-03FFFFH are directed to external
1812 /* configure hardware: IRQ, enable interrupts,
1813 plus external 9-pin MIDI interface selected
1816 outb (0x80 | 0x40 | bits, dev->data_port);
1820 0 Host Rx Interrupt Enable (1=Enabled) 0x1
1824 4 Host Tx Interrupt Enable 0x10
1825 5 Mute (0=Mute; 1=Play) 0x20
1826 6 Master Interrupt Enable (1=Enabled) 0x40
1827 7 Master Reset (0=Reset; 1=Run) 0x80
1829 Take us out of reset, mute output, master + TX + RX interrupts on.
1831 We'll get an interrupt presumably to tell us that the TX
1835 wavefront_should_cause_interrupt(dev, 0x80|0x40|0x10|0x1,
1837 (reset_time*HZ)/100);
1839 /* Note: data port is now the data port, not the h/w initialization
1844 snd_printk ("intr not received after h/w un-reset.\n");
1848 /* Note: data port is now the data port, not the h/w initialization
1851 At this point, only "HW VERSION" or "DOWNLOAD OS" commands
1852 will work. So, issue one of them, and wait for TX
1853 interrupt. This can take a *long* time after a cold boot,
1854 while the ISC ROM does its RAM test. The SDK says up to 4
1855 seconds - with 12MB of RAM on a Tropez+, it takes a lot
1856 longer than that (~16secs). Note that the card understands
1857 the difference between a warm and a cold boot, so
1858 subsequent ISC2115 reboots (say, caused by module
1859 reloading) will get through this much faster.
1861 XXX Interesting question: why is no RX interrupt received first ?
1864 wavefront_should_cause_interrupt(dev, WFC_HARDWARE_VERSION,
1865 dev->data_port, ramcheck_time*HZ);
1868 snd_printk ("post-RAM-check interrupt not received.\n");
1872 if (!wavefront_wait (dev, STAT_CAN_READ)) {
1873 snd_printk ("no response to HW version cmd.\n");
1877 if ((hwv[0] = wavefront_read (dev)) == -1) {
1878 snd_printk ("board not responding correctly.\n");
1882 if (hwv[0] == 0xFF) { /* NAK */
1884 /* Board's RAM test failed. Try to read error code,
1885 and tell us about it either way.
1888 if ((hwv[0] = wavefront_read (dev)) == -1) {
1889 snd_printk ("on-board RAM test failed "
1890 "(bad error code).\n");
1892 snd_printk ("on-board RAM test failed "
1893 "(error code: 0x%x).\n",
1899 /* We're OK, just get the next byte of the HW version response */
1901 if ((hwv[1] = wavefront_read (dev)) == -1) {
1902 snd_printk ("incorrect h/w response.\n");
1906 snd_printk ("hardware version %d.%d\n",
1916 #include <linux/fs.h>
1917 #include <linux/mm.h>
1918 #include <linux/slab.h>
1919 #include <linux/unistd.h>
1920 #include <linux/syscalls.h>
1921 #include <asm/uaccess.h>
1925 wavefront_download_firmware (snd_wavefront_t *dev, char *path)
1928 unsigned char section[WF_SECTION_MAX];
1929 char section_length; /* yes, just a char; max value is WF_SECTION_MAX */
1930 int section_cnt_downloaded = 0;
1936 /* This tries to be a bit cleverer than the stuff Alan Cox did for
1937 the generic sound firmware, in that it actually knows
1938 something about the structure of the Motorola firmware. In
1939 particular, it uses a version that has been stripped of the
1940 20K of useless header information, and had section lengths
1941 added, making it possible to load the entire OS without any
1942 [kv]malloc() activity, since the longest entity we ever read is
1943 42 bytes (well, WF_SECTION_MAX) long.
1949 if ((fd = sys_open (path, 0, 0)) < 0) {
1950 snd_printk ("Unable to load \"%s\".\n",
1958 if ((x = sys_read (fd, §ion_length, sizeof (section_length))) !=
1959 sizeof (section_length)) {
1960 snd_printk ("firmware read error.\n");
1964 if (section_length == 0) {
1968 if (sys_read (fd, section, section_length) != section_length) {
1969 snd_printk ("firmware section "
1976 if (wavefront_write (dev, WFC_DOWNLOAD_OS)) {
1980 for (i = 0; i < section_length; i++) {
1981 if (wavefront_write (dev, section[i])) {
1988 if (wavefront_wait (dev, STAT_CAN_READ)) {
1990 if ((c = inb (dev->data_port)) != WF_ACK) {
1992 snd_printk ("download "
1993 "of section #%d not "
1994 "acknowledged, ack = 0x%x\n",
1995 section_cnt_downloaded + 1, c);
2001 snd_printk ("time out for firmware ACK.\n");
2014 snd_printk ("firmware download failed!!!\n");
2020 wavefront_do_reset (snd_wavefront_t *dev)
2025 if (wavefront_reset_to_cleanliness (dev)) {
2026 snd_printk ("hw reset failed.\n");
2031 if (wavefront_download_firmware (dev, ospath)) {
2037 /* Wait for the OS to get running. The protocol for
2038 this is non-obvious, and was determined by
2039 using port-IO tracing in DOSemu and some
2040 experimentation here.
2042 Rather than using timed waits, use interrupts creatively.
2045 wavefront_should_cause_interrupt (dev, WFC_NOOP,
2050 snd_printk ("no post-OS interrupt.\n");
2054 /* Now, do it again ! */
2056 wavefront_should_cause_interrupt (dev, WFC_NOOP,
2057 dev->data_port, (10*HZ));
2060 snd_printk ("no post-OS interrupt(2).\n");
2064 /* OK, no (RX/TX) interrupts any more, but leave mute
2068 outb (0x80|0x40, dev->control_port);
2071 /* SETUPSND.EXE asks for sample memory config here, but since i
2072 have no idea how to interpret the result, we'll forget
2076 if ((dev->freemem = wavefront_freemem (dev)) < 0) {
2080 snd_printk ("available DRAM %dk\n", dev->freemem / 1024);
2082 if (wavefront_write (dev, 0xf0) ||
2083 wavefront_write (dev, 1) ||
2084 (wavefront_read (dev) < 0)) {
2086 snd_printk ("MPU emulation mode not set.\n");
2092 if (snd_wavefront_cmd (dev, WFC_SET_NVOICES, 0, voices)) {
2093 snd_printk ("cannot set number of voices to 32.\n");
2101 /* reset that sucker so that it doesn't bother us. */
2103 outb (0x0, dev->control_port);
2104 dev->interrupts_are_midi = 0;
2109 snd_wavefront_start (snd_wavefront_t *dev)
2112 int samples_are_from_rom;
2114 /* IMPORTANT: assumes that snd_wavefront_detect() and/or
2115 wavefront_reset_to_cleanliness() has already been called
2119 samples_are_from_rom = 1;
2121 /* XXX is this always true ? */
2122 samples_are_from_rom = 0;
2125 if (dev->israw || fx_raw) {
2126 if (wavefront_do_reset (dev)) {
2130 /* Check for FX device, present only on Tropez+ */
2132 dev->has_fx = (snd_wavefront_fx_detect (dev) == 0);
2134 if (dev->has_fx && fx_raw) {
2135 snd_wavefront_fx_start (dev);
2138 wavefront_get_sample_status (dev, samples_are_from_rom);
2139 wavefront_get_program_status (dev);
2140 wavefront_get_patch_status (dev);
2142 /* Start normal operation: unreset, master interrupt enabled, no mute
2145 outb (0x80|0x40|0x20, dev->control_port);
2151 snd_wavefront_detect (snd_wavefront_card_t *card)
2154 unsigned char rbuf[4], wbuf[4];
2155 snd_wavefront_t *dev = &card->wavefront;
2157 /* returns zero if a WaveFront card is successfully detected.
2163 dev->debug = debug_default;
2164 dev->interrupts_are_midi = 0;
2166 dev->rom_samples_rdonly = 1;
2168 if (snd_wavefront_cmd (dev, WFC_FIRMWARE_VERSION, rbuf, wbuf) == 0) {
2170 dev->fw_version[0] = rbuf[0];
2171 dev->fw_version[1] = rbuf[1];
2173 snd_printk ("firmware %d.%d already loaded.\n",
2176 /* check that a command actually works */
2178 if (snd_wavefront_cmd (dev, WFC_HARDWARE_VERSION,
2180 dev->hw_version[0] = rbuf[0];
2181 dev->hw_version[1] = rbuf[1];
2183 snd_printk ("not raw, but no "
2184 "hardware version!\n");
2191 snd_printk ("reloading firmware as you requested.\n");
2198 snd_printk ("no response to firmware probe, assume raw.\n");