ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / isa / wavefront / wavefront_synth.c
1 /* Copyright (C) by Paul Barton-Davis 1998-1999
2  *
3  * Some portions of this file are taken from work that is
4  * copyright (C) by Hannu Savolainen 1993-1996
5  *
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
8  * for more info.  
9  */
10
11 /*  
12  * An ALSA lowlevel driver for Turtle Beach ICS2115 wavetable synth
13  *                                             (Maui, Tropez, Tropez Plus)
14  *
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.
20  *
21  */
22
23 #include <sound/driver.h>
24 #include <asm/io.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>
33
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.
38                 */
39                    
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.
45                 */
46
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
50                               wavefront.h
51                            */
52
53 /* XXX this needs to be made firmware and hardware version dependent */
54
55 char *ospath = "/etc/sound/wavefront.os"; /* where to find a processed
56                                              version of the WaveFront OS
57                                           */
58
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
66                          whole CPU.
67
68                          Specifically, with this number, out of about 134,000
69                          status waits, only about 250 result in a sleep.
70                       */
71
72 int sleep_interval = 100;   /* HZ/sleep_interval seconds per sleep */
73 int sleep_tries = 50;       /* number of times we'll try to sleep */
74
75 int reset_time = 2;        /* hundreths of a second we wait after a HW
76                               reset for the expected interrupt.
77                            */
78
79 int ramcheck_time = 20;    /* time in seconds to wait while ROM code
80                               checks on-board RAM.
81                            */
82
83 int osrun_time = 10;       /* time in seconds we wait for the OS to
84                               start running.
85                            */
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");
106
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 
110    version.
111 */
112
113 #define WF_DEBUG 1
114
115 #ifdef WF_DEBUG
116
117 #if defined(NEW_MACRO_VARARGS) || __GNUC__ >= 3
118 #define DPRINT(cond, ...) \
119        if ((dev->debug & (cond)) == (cond)) { \
120              snd_printk (__VA_ARGS__); \
121        }
122 #else
123 #define DPRINT(cond, args...) \
124        if ((dev->debug & (cond)) == (cond)) { \
125              snd_printk (##args); \
126        }
127 #endif
128 #else
129 #define DPRINT(cond, args...)
130 #endif /* WF_DEBUG */
131
132 #define LOGNAME "WaveFront: "
133
134 /* bitmasks for WaveFront status port value */
135
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
142
143 static int wavefront_delete_sample (snd_wavefront_t *, int sampnum);
144 static int wavefront_find_free_sample (snd_wavefront_t *);
145
146 typedef struct {
147         int cmd;
148         char *action;
149         unsigned int read_cnt;
150         unsigned int write_cnt;
151         int need_ack;
152 } wavefront_command;
153
154 static struct {
155         int errno;
156         const char *errstr;
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" },
166         { 0x80, "Success" },
167         { 0x0, 0x0 }
168 };
169
170 #define NEEDS_ACK 1
171
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 },
198
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.
202            Ugh.
203         */
204
205         { WFC_DOWNLOAD_MULTISAMPLE, "download multisample", 0, 0, NEEDS_ACK },
206
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.
210         */
211
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,
225           NEEDS_ACK},
226         { WFC_UPLOAD_EDRUM_PROGRAM, "upload enhanced drum program", 8, 1, 0},
227         { WFC_SET_EDRUM_CHANNEL, "set enhanced drum program channel",
228           0, 1, NEEDS_ACK },
229         { WFC_DISABLE_DRUM_PROGRAM, "disable drum program", 0, 1, NEEDS_ACK },
230         { WFC_REPORT_CHANNEL_PROGRAMS, "report channel program numbers",
231           32, 0, 0 },
232         { WFC_NOOP, "the no-op command", 0, 0, NEEDS_ACK },
233         { 0x00 }
234 };
235
236 static const char *
237 wavefront_errorstr (int errnum)
238
239 {
240         int i;
241
242         for (i = 0; wavefront_errors[i].errstr; i++) {
243                 if (wavefront_errors[i].errno == errnum) {
244                         return wavefront_errors[i].errstr;
245                 }
246         }
247
248         return "Unknown WaveFront error";
249 }
250
251 static wavefront_command *
252 wavefront_get_command (int cmd) 
253
254 {
255         int i;
256
257         for (i = 0; wavefront_commands[i].cmd != 0; i++) {
258                 if (cmd == wavefront_commands[i].cmd) {
259                         return &wavefront_commands[i];
260                 }
261         }
262
263         return (wavefront_command *) 0;
264 }
265
266 static inline int
267 wavefront_status (snd_wavefront_t *dev) 
268
269 {
270         return inb (dev->status_port);
271 }
272
273 static int
274 wavefront_sleep (int limit)
275
276 {
277         set_current_state(TASK_INTERRUPTIBLE);
278         schedule_timeout(limit);
279
280         return signal_pending(current);
281 }
282
283 static int
284 wavefront_wait (snd_wavefront_t *dev, int mask)
285
286 {
287         int             i;
288
289         /* Spin for a short period of time, because >99% of all
290            requests to the WaveFront can be serviced inline like this.
291         */
292
293         for (i = 0; i < wait_usecs; i += 5) {
294                 if (wavefront_status (dev) & mask) {
295                         return 1;
296                 }
297                 udelay(5);
298         }
299
300         for (i = 0; i < sleep_tries; i++) {
301
302                 if (wavefront_status (dev) & mask) {
303                         return 1;
304                 }
305
306                 if (wavefront_sleep (HZ/sleep_interval)) {
307                         return (0);
308                 }
309         }
310
311         return (0);
312 }
313
314 static int
315 wavefront_read (snd_wavefront_t *dev)
316
317 {
318         if (wavefront_wait (dev, STAT_CAN_READ))
319                 return inb (dev->data_port);
320
321         DPRINT (WF_DEBUG_DATA, "read timeout.\n");
322
323         return -1;
324 }
325
326 static int
327 wavefront_write (snd_wavefront_t *dev, unsigned char data)
328
329 {
330         if (wavefront_wait (dev, STAT_CAN_WRITE)) {
331                 outb (data, dev->data_port);
332                 return 0;
333         }
334
335         DPRINT (WF_DEBUG_DATA, "write timeout.\n");
336
337         return -1;
338 }
339
340 int
341 snd_wavefront_cmd (snd_wavefront_t *dev, 
342                    int cmd, unsigned char *rbuf, unsigned char *wbuf)
343
344 {
345         int ack;
346         unsigned int i;
347         int c;
348         wavefront_command *wfcmd;
349
350         if ((wfcmd = wavefront_get_command (cmd)) == (wavefront_command *) 0) {
351                 snd_printk ("command 0x%x not supported.\n",
352                         cmd);
353                 return 1;
354         }
355
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.
359         */
360
361         if (cmd == WFC_DOWNLOAD_MULTISAMPLE) {
362                 wfcmd->write_cnt = (unsigned long) rbuf;
363                 rbuf = 0;
364         }
365
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);
369     
370         if (wavefront_write (dev, cmd)) { 
371                 DPRINT ((WF_DEBUG_IO|WF_DEBUG_CMD), "cannot request "
372                                                      "0x%x [%s].\n",
373                                                      cmd, wfcmd->action);
374                 return 1;
375         } 
376
377         if (wfcmd->write_cnt > 0) {
378                 DPRINT (WF_DEBUG_DATA, "writing %d bytes "
379                                         "for 0x%x\n",
380                                         wfcmd->write_cnt, cmd);
381
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);
387                                 return 1;
388                         }
389
390                         DPRINT (WF_DEBUG_DATA, "write[%d] = 0x%x\n",
391                                                 i, wbuf[i]);
392                 }
393         }
394
395         if (wfcmd->read_cnt > 0) {
396                 DPRINT (WF_DEBUG_DATA, "reading %d ints "
397                                         "for 0x%x\n",
398                                         wfcmd->read_cnt, cmd);
399
400                 for (i = 0; i < wfcmd->read_cnt; i++) {
401
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);
406                                 return 1;
407                         }
408
409                         /* Now handle errors. Lots of special cases here */
410             
411                         if (c == 0xff) { 
412                                 if ((c = wavefront_read (dev)) == -1) {
413                                         DPRINT (WF_DEBUG_IO, "bad read for "
414                                                               "error byte at "
415                                                               "read byte %d "
416                                                               "of 0x%x [%s].\n",
417                                                               i, cmd,
418                                                               wfcmd->action);
419                                         return 1;
420                                 }
421
422                                 /* Can you believe this madness ? */
423
424                                 if (c == 1 &&
425                                     wfcmd->cmd == WFC_IDENTIFY_SAMPLE_TYPE) {
426                                         rbuf[0] = WF_ST_EMPTY;
427                                         return (0);
428
429                                 } else if (c == 3 &&
430                                            wfcmd->cmd == WFC_UPLOAD_PATCH) {
431
432                                         return 3;
433
434                                 } else if (c == 1 &&
435                                            wfcmd->cmd == WFC_UPLOAD_PROGRAM) {
436
437                                         return 1;
438
439                                 } else {
440
441                                         DPRINT (WF_DEBUG_IO, "error %d (%s) "
442                                                               "during "
443                                                               "read for byte "
444                                                               "%d of 0x%x "
445                                                               "[%s].\n",
446                                                               c,
447                                                               wavefront_errorstr (c),
448                                                               i, cmd,
449                                                               wfcmd->action);
450                                         return 1;
451
452                                 }
453                 
454                 } else {
455                                 rbuf[i] = c;
456                         }
457                         
458                         DPRINT (WF_DEBUG_DATA, "read[%d] = 0x%x\n",i, rbuf[i]);
459                 }
460         }
461         
462         if ((wfcmd->read_cnt == 0 && wfcmd->write_cnt == 0) || wfcmd->need_ack) {
463
464                 DPRINT (WF_DEBUG_CMD, "reading ACK for 0x%x\n", cmd);
465
466                 /* Some commands need an ACK, but return zero instead
467                    of the standard value.
468                 */
469             
470                 if ((ack = wavefront_read (dev)) == 0) {
471                         ack = WF_ACK;
472                 }
473         
474                 if (ack != WF_ACK) {
475                         if (ack == -1) {
476                                 DPRINT (WF_DEBUG_IO, "cannot read ack for "
477                                                       "0x%x [%s].\n",
478                                                       cmd, wfcmd->action);
479                                 return 1;
480                 
481                         } else {
482                                 int err = -1; /* something unknown */
483
484                                 if (ack == 0xff) { /* explicit error */
485                     
486                                         if ((err = wavefront_read (dev)) == -1) {
487                                                 DPRINT (WF_DEBUG_DATA,
488                                                         "cannot read err "
489                                                         "for 0x%x [%s].\n",
490                                                         cmd, wfcmd->action);
491                                         }
492                                 }
493                                 
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));
498                                 
499                                 return -err;
500                         }
501                 }
502                 
503                 DPRINT (WF_DEBUG_DATA, "ack received "
504                                         "for 0x%x [%s]\n",
505                                         cmd, wfcmd->action);
506         } else {
507
508                 DPRINT (WF_DEBUG_CMD, "0x%x [%s] does not need "
509                                        "ACK (%d,%d,%d)\n",
510                                        cmd, wfcmd->action, wfcmd->read_cnt,
511                                        wfcmd->write_cnt, wfcmd->need_ack);
512         }
513
514         return 0;
515         
516 }
517 \f
518 /***********************************************************************
519 WaveFront data munging   
520
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.
524
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.
531
532 **********************************************************************/
533
534 static unsigned char *
535 munge_int32 (unsigned int src,
536              unsigned char *dst,
537              unsigned int dst_size)
538 {
539         unsigned int i;
540
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 */ 
545
546                 dst++;
547         };
548         return dst;
549 };
550
551 static int 
552 demunge_int32 (unsigned char* src, int src_size)
553
554 {
555         int i;
556         int outval = 0;
557         
558         for (i = src_size - 1; i >= 0; i--) {
559                 outval=(outval<<7)+src[i];
560         }
561
562         return outval;
563 };
564
565 static 
566 unsigned char *
567 munge_buf (unsigned char *src, unsigned char *dst, unsigned int dst_size)
568
569 {
570         unsigned int i;
571         unsigned int last = dst_size / 2;
572
573         for (i = 0; i < last; i++) {
574                 *dst++ = src[i] & 0x7f;
575                 *dst++ = src[i] >> 7;
576         }
577         return dst;
578 }
579
580 static 
581 unsigned char *
582 demunge_buf (unsigned char *src, unsigned char *dst, unsigned int src_bytes)
583
584 {
585         int i;
586         unsigned char *end = src + src_bytes;
587     
588         end = src + src_bytes;
589
590         /* NOTE: src and dst *CAN* point to the same address */
591
592         for (i = 0; src != end; i++) {
593                 dst[i] = *src++;
594                 dst[i] |= (*src++)<<7;
595         }
596
597         return dst;
598 }
599 \f
600 /***********************************************************************
601 WaveFront: sample, patch and program management.
602 ***********************************************************************/
603
604 static int
605 wavefront_delete_sample (snd_wavefront_t *dev, int sample_num)
606
607 {
608         unsigned char wbuf[2];
609         int x;
610
611         wbuf[0] = sample_num & 0x7f;
612         wbuf[1] = sample_num >> 7;
613
614         if ((x = snd_wavefront_cmd (dev, WFC_DELETE_SAMPLE, 0, wbuf)) == 0) {
615                 dev->sample_status[sample_num] = WF_ST_EMPTY;
616         }
617
618         return x;
619 }
620
621 static int
622 wavefront_get_sample_status (snd_wavefront_t *dev, int assume_rom)
623
624 {
625         int i;
626         unsigned char rbuf[32], wbuf[32];
627         unsigned int    sc_real, sc_alias, sc_multi;
628
629         /* check sample status */
630     
631         if (snd_wavefront_cmd (dev, WFC_GET_NSAMPLES, rbuf, wbuf)) {
632                 snd_printk ("cannot request sample count.\n");
633                 return -1;
634         } 
635     
636         sc_real = sc_alias = sc_multi = dev->samples_used = 0;
637     
638         for (i = 0; i < WF_MAX_SAMPLE; i++) {
639         
640                 wbuf[0] = i & 0x7f;
641                 wbuf[1] = i >> 7;
642
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;
647                         continue;
648                 }
649
650                 dev->sample_status[i] = (WF_SLOT_FILLED|rbuf[0]);
651
652                 if (assume_rom) {
653                         dev->sample_status[i] |= WF_SLOT_ROM;
654                 }
655
656                 switch (rbuf[0] & WF_ST_MASK) {
657                 case WF_ST_SAMPLE:
658                         sc_real++;
659                         break;
660                 case WF_ST_MULTISAMPLE:
661                         sc_multi++;
662                         break;
663                 case WF_ST_ALIAS:
664                         sc_alias++;
665                         break;
666                 case WF_ST_EMPTY:
667                         break;
668
669                 default:
670                         snd_printk ("unknown sample type for "
671                                     "slot %d (0x%x)\n", 
672                                     i, rbuf[0]);
673                 }
674
675                 if (rbuf[0] != WF_ST_EMPTY) {
676                         dev->samples_used++;
677                 } 
678         }
679
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);
683
684
685         return (0);
686
687 }
688
689 static int
690 wavefront_get_patch_status (snd_wavefront_t *dev)
691
692 {
693         unsigned char patchbuf[WF_PATCH_BYTES];
694         unsigned char patchnum[2];
695         wavefront_patch *p;
696         int i, x, cnt, cnt2;
697
698         for (i = 0; i < WF_MAX_PATCH; i++) {
699                 patchnum[0] = i & 0x7f;
700                 patchnum[1] = i >> 7;
701
702                 if ((x = snd_wavefront_cmd (dev, WFC_UPLOAD_PATCH, patchbuf,
703                                         patchnum)) == 0) {
704
705                         dev->patch_status[i] |= WF_SLOT_FILLED;
706                         p = (wavefront_patch *) patchbuf;
707                         dev->sample_status
708                                 [p->sample_number|(p->sample_msb<<7)] |=
709                                 WF_SLOT_USED;
710             
711                 } else if (x == 3) { /* Bad patch number */
712                         dev->patch_status[i] = 0;
713                 } else {
714                         snd_printk ("upload patch "
715                                     "error 0x%x\n", x);
716                         dev->patch_status[i] = 0;
717                         return 1;
718                 }
719         }
720
721         /* program status has already filled in slot_used bits */
722
723         for (i = 0, cnt = 0, cnt2 = 0; i < WF_MAX_PATCH; i++) {
724                 if (dev->patch_status[i] & WF_SLOT_FILLED) {
725                         cnt++;
726                 }
727                 if (dev->patch_status[i] & WF_SLOT_USED) {
728                         cnt2++;
729                 }
730         
731         }
732         snd_printk ("%d patch slots filled, %d in use\n", cnt, cnt2);
733
734         return (0);
735 }
736
737 static int
738 wavefront_get_program_status (snd_wavefront_t *dev)
739
740 {
741         unsigned char progbuf[WF_PROGRAM_BYTES];
742         wavefront_program prog;
743         unsigned char prognum;
744         int i, x, l, cnt;
745
746         for (i = 0; i < WF_MAX_PROGRAM; i++) {
747                 prognum = i;
748
749                 if ((x = snd_wavefront_cmd (dev, WFC_UPLOAD_PROGRAM, progbuf,
750                                         &prognum)) == 0) {
751
752                         dev->prog_status[i] |= WF_SLOT_USED;
753
754                         demunge_buf (progbuf, (unsigned char *) &prog,
755                                      WF_PROGRAM_BYTES);
756
757                         for (l = 0; l < WF_NUM_LAYERS; l++) {
758                                 if (prog.layer[l].mute) {
759                                         dev->patch_status
760                                                 [prog.layer[l].patch_number] |=
761                                                 WF_SLOT_USED;
762                                 }
763                         }
764                 } else if (x == 1) { /* Bad program number */
765                         dev->prog_status[i] = 0;
766                 } else {
767                         snd_printk ("upload program "
768                                     "error 0x%x\n", x);
769                         dev->prog_status[i] = 0;
770                 }
771         }
772
773         for (i = 0, cnt = 0; i < WF_MAX_PROGRAM; i++) {
774                 if (dev->prog_status[i]) {
775                         cnt++;
776                 }
777         }
778
779         snd_printk ("%d programs slots in use\n", cnt);
780
781         return (0);
782 }
783
784 static int
785 wavefront_send_patch (snd_wavefront_t *dev, wavefront_patch_info *header)
786
787 {
788         unsigned char buf[WF_PATCH_BYTES+2];
789         unsigned char *bptr;
790
791         DPRINT (WF_DEBUG_LOAD_PATCH, "downloading patch %d\n",
792                                       header->number);
793
794         dev->patch_status[header->number] |= WF_SLOT_FILLED;
795
796         bptr = buf;
797         bptr = munge_int32 (header->number, buf, 2);
798         munge_buf ((unsigned char *)&header->hdr.p, bptr, WF_PATCH_BYTES);
799     
800         if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_PATCH, 0, buf)) {
801                 snd_printk ("download patch failed\n");
802                 return -(EIO);
803         }
804
805         return (0);
806 }
807
808 static int
809 wavefront_send_program (snd_wavefront_t *dev, wavefront_patch_info *header)
810
811 {
812         unsigned char buf[WF_PROGRAM_BYTES+1];
813         int i;
814
815         DPRINT (WF_DEBUG_LOAD_PATCH, "downloading program %d\n",
816                 header->number);
817
818         dev->prog_status[header->number] = WF_SLOT_USED;
819
820         /* XXX need to zero existing SLOT_USED bit for program_status[i]
821            where `i' is the program that's being (potentially) overwritten.
822         */
823     
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] |=
827                                 WF_SLOT_USED;
828
829                         /* XXX need to mark SLOT_USED for sample used by
830                            patch_number, but this means we have to load it. Ick.
831                         */
832                 }
833         }
834
835         buf[0] = header->number;
836         munge_buf ((unsigned char *)&header->hdr.pr, &buf[1], WF_PROGRAM_BYTES);
837     
838         if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_PROGRAM, 0, buf)) {
839                 snd_printk ("download patch failed\n"); 
840                 return -(EIO);
841         }
842
843         return (0);
844 }
845
846 static int
847 wavefront_freemem (snd_wavefront_t *dev)
848
849 {
850         char rbuf[8];
851
852         if (snd_wavefront_cmd (dev, WFC_REPORT_FREE_MEMORY, rbuf, 0)) {
853                 snd_printk ("can't get memory stats.\n");
854                 return -1;
855         } else {
856                 return demunge_int32 (rbuf, 4);
857         }
858 }
859
860 static int
861 wavefront_send_sample (snd_wavefront_t *dev, 
862                        wavefront_patch_info *header,
863                        u16 *dataptr,
864                        int data_is_unsigned)
865
866 {
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
873            divided by 2.
874         */
875
876         u16 sample_short;
877         u32 length;
878         u16 *data_end = 0;
879         unsigned int i;
880         const unsigned int max_blksize = 4096/2;
881         unsigned int written;
882         unsigned int blocksize;
883         int dma_ack;
884         int blocknum;
885         unsigned char sample_hdr[WF_SAMPLE_HDR_BYTES];
886         unsigned char *shptr;
887         int skip = 0;
888         int initial_skip = 0;
889
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,
894                                       header->size,
895                                       (unsigned long) header->dataptr);
896
897         if (header->number == WAVEFRONT_FIND_FREE_SAMPLE_SLOT) {
898                 int x;
899
900                 if ((x = wavefront_find_free_sample (dev)) < 0) {
901                         return -ENOMEM;
902                 }
903                 snd_printk ("unspecified sample => %d\n", x);
904                 header->number = x;
905         }
906
907         if (header->size) {
908
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.
913
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.
919
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. 
926
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.
930                 */
931
932                 if (dev->rom_samples_rdonly) {
933                         if (dev->sample_status[header->number] & WF_SLOT_ROM) {
934                                 snd_printk ("sample slot %d "
935                                             "write protected\n",
936                                             header->number);
937                                 return -EACCES;
938                         }
939                 }
940
941                 wavefront_delete_sample (dev, header->number);
942         }
943
944         if (header->size) {
945                 dev->freemem = wavefront_freemem (dev);
946
947                 if (dev->freemem < (int)header->size) {
948                         snd_printk ("insufficient memory to "
949                                     "load %d byte sample.\n",
950                                     header->size);
951                         return -ENOMEM;
952                 }
953         
954         }
955
956         skip = WF_GET_CHANNEL(&header->hdr.s);
957
958         if (skip > 0 && header->hdr.s.SampleResolution != LINEAR_16BIT) {
959                 snd_printk ("channel selection only "
960                             "possible on 16-bit samples");
961                 return -(EINVAL);
962         }
963
964         switch (skip) {
965         case 0:
966                 initial_skip = 0;
967                 skip = 1;
968                 break;
969         case 1:
970                 initial_skip = 0;
971                 skip = 2;
972                 break;
973         case 2:
974                 initial_skip = 1;
975                 skip = 2;
976                 break;
977         case 3:
978                 initial_skip = 2;
979                 skip = 3;
980                 break;
981         case 4:
982                 initial_skip = 3;
983                 skip = 4;
984                 break;
985         case 5:
986                 initial_skip = 4;
987                 skip = 5;
988                 break;
989         case 6:
990                 initial_skip = 5;
991                 skip = 6;
992                 break;
993         }
994
995         DPRINT (WF_DEBUG_LOAD_PATCH, "channel selection: %d => "
996                                       "initial skip = %d, skip = %d\n",
997                                       WF_GET_CHANNEL (&header->hdr.s),
998                                       initial_skip, skip);
999     
1000         /* Be safe, and zero the "Unused" bits ... */
1001
1002         WF_SET_CHANNEL(&header->hdr.s, 0);
1003
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.
1007         */
1008
1009         length = header->size / 2;
1010
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.
1014         */
1015
1016         shptr = &sample_hdr[0];
1017
1018         shptr = munge_int32 (header->number, shptr, 2);
1019
1020         if (header->size) {
1021                 shptr = munge_int32 (length, shptr, 4);
1022         }
1023
1024         /* Yes, a 4 byte result doesn't contain all of the offset bits,
1025            but the offset only uses 24 bits.
1026         */
1027
1028         shptr = munge_int32 (*((u32 *) &header->hdr.s.sampleStartOffset),
1029                              shptr, 4);
1030         shptr = munge_int32 (*((u32 *) &header->hdr.s.loopStartOffset),
1031                              shptr, 4);
1032         shptr = munge_int32 (*((u32 *) &header->hdr.s.loopEndOffset),
1033                              shptr, 4);
1034         shptr = munge_int32 (*((u32 *) &header->hdr.s.sampleEndOffset),
1035                              shptr, 4);
1036         
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
1039            a just 12 bits ?
1040         */
1041         
1042         shptr = munge_int32 (header->hdr.s.FrequencyBias, shptr, 3);
1043         
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.
1047         */
1048         
1049         shptr = munge_int32 (*(&header->hdr.s.FrequencyBias+1),
1050                              shptr, 2);
1051
1052         if (snd_wavefront_cmd (dev, 
1053                            header->size ?
1054                            WFC_DOWNLOAD_SAMPLE : WFC_DOWNLOAD_SAMPLE_HEADER,
1055                            0, sample_hdr)) {
1056                 snd_printk ("sample %sdownload refused.\n",
1057                             header->size ? "" : "header ");
1058                 return -(EIO);
1059         }
1060
1061         if (header->size == 0) {
1062                 goto sent; /* Sorry. Just had to have one somewhere */
1063         }
1064     
1065         data_end = dataptr + length;
1066
1067         /* Do any initial skip over an unused channel's data */
1068
1069         dataptr += initial_skip;
1070     
1071         for (written = 0, blocknum = 0;
1072              written < length; written += max_blksize, blocknum++) {
1073         
1074                 if ((length - written) > max_blksize) {
1075                         blocksize = max_blksize;
1076                 } else {
1077                         /* round to nearest 16-byte value */
1078                         blocksize = ((length-written+7)&~0x7);
1079                 }
1080
1081                 if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_BLOCK, 0, 0)) {
1082                         snd_printk ("download block "
1083                                     "request refused.\n");
1084                         return -(EIO);
1085                 }
1086
1087                 for (i = 0; i < blocksize; i++) {
1088
1089                         if (dataptr < data_end) {
1090                 
1091                                 __get_user (sample_short, dataptr);
1092                                 dataptr += skip;
1093                 
1094                                 if (data_is_unsigned) { /* GUS ? */
1095
1096                                         if (WF_SAMPLE_IS_8BIT(&header->hdr.s)) {
1097                         
1098                                                 /* 8 bit sample
1099                                                  resolution, sign
1100                                                  extend both bytes.
1101                                                 */
1102                         
1103                                                 ((unsigned char*)
1104                                                  &sample_short)[0] += 0x7f;
1105                                                 ((unsigned char*)
1106                                                  &sample_short)[1] += 0x7f;
1107                         
1108                                         } else {
1109                         
1110                                                 /* 16 bit sample
1111                                                  resolution, sign
1112                                                  extend the MSB.
1113                                                 */
1114                         
1115                                                 sample_short += 0x7fff;
1116                                         }
1117                                 }
1118
1119                         } else {
1120
1121                                 /* In padding section of final block:
1122
1123                                    Don't fetch unsupplied data from
1124                                    user space, just continue with
1125                                    whatever the final value was.
1126                                 */
1127                         }
1128             
1129                         if (i < blocksize - 1) {
1130                                 outw (sample_short, dev->block_port);
1131                         } else {
1132                                 outw (sample_short, dev->last_block_port);
1133                         }
1134                 }
1135
1136                 /* Get "DMA page acknowledge", even though its really
1137                    nothing to do with DMA at all.
1138                 */
1139         
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");
1144                                 return -(EIO);
1145                         } else {
1146                                 snd_printk ("upload sample "
1147                                             "DMA ack error 0x%x\n",
1148                                             dma_ack);
1149                                 return -(EIO);
1150                         }
1151                 }
1152         }
1153
1154         dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_SAMPLE);
1155
1156         /* Note, label is here because sending the sample header shouldn't
1157            alter the sample_status info at all.
1158         */
1159
1160  sent:
1161         return (0);
1162 }
1163
1164 static int
1165 wavefront_send_alias (snd_wavefront_t *dev, wavefront_patch_info *header)
1166
1167 {
1168         unsigned char alias_hdr[WF_ALIAS_BYTES];
1169
1170         DPRINT (WF_DEBUG_LOAD_PATCH, "download alias, %d is "
1171                                       "alias for %d\n",
1172                                       header->number,
1173                                       header->hdr.a.OriginalSample);
1174     
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),
1178                      &alias_hdr[4], 4);
1179         munge_int32 (*((unsigned int *)&header->hdr.a.loopStartOffset),
1180                      &alias_hdr[8], 4);
1181         munge_int32 (*((unsigned int *)&header->hdr.a.loopEndOffset),
1182                      &alias_hdr[12], 4);
1183         munge_int32 (*((unsigned int *)&header->hdr.a.sampleEndOffset),
1184                      &alias_hdr[16], 4);
1185         munge_int32 (header->hdr.a.FrequencyBias, &alias_hdr[20], 3);
1186         munge_int32 (*(&header->hdr.a.FrequencyBias+1), &alias_hdr[23], 2);
1187
1188         if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_SAMPLE_ALIAS, 0, alias_hdr)) {
1189                 snd_printk ("download alias failed.\n");
1190                 return -(EIO);
1191         }
1192
1193         dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_ALIAS);
1194
1195         return (0);
1196 }
1197
1198 static int
1199 wavefront_send_multisample (snd_wavefront_t *dev, wavefront_patch_info *header)
1200 {
1201         int i;
1202         int num_samples;
1203         unsigned char msample_hdr[WF_MSAMPLE_BYTES];
1204
1205         munge_int32 (header->number, &msample_hdr[0], 2);
1206
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.
1210         */
1211
1212         num_samples = (1<<(header->hdr.ms.NumberOfSamples&7));
1213         msample_hdr[2] = (unsigned char) header->hdr.ms.NumberOfSamples;
1214
1215         DPRINT (WF_DEBUG_LOAD_PATCH, "multi %d with %d=%d samples\n",
1216                                       header->number,
1217                                       header->hdr.ms.NumberOfSamples,
1218                                       num_samples);
1219
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);
1225         }
1226     
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.
1230         */
1231
1232         if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_MULTISAMPLE, 
1233                            (unsigned char *) (long) ((num_samples*2)+3),
1234                            msample_hdr)) {
1235                 snd_printk ("download of multisample failed.\n");
1236                 return -(EIO);
1237         }
1238
1239         dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_MULTISAMPLE);
1240
1241         return (0);
1242 }
1243
1244 static int
1245 wavefront_fetch_multisample (snd_wavefront_t *dev, 
1246                              wavefront_patch_info *header)
1247 {
1248         int i;
1249         unsigned char log_ns[1];
1250         unsigned char number[2];
1251         int num_samples;
1252
1253         munge_int32 (header->number, number, 2);
1254     
1255         if (snd_wavefront_cmd (dev, WFC_UPLOAD_MULTISAMPLE, log_ns, number)) {
1256                 snd_printk ("upload multisample failed.\n");
1257                 return -(EIO);
1258         }
1259     
1260         DPRINT (WF_DEBUG_DATA, "msample %d has %d samples\n",
1261                                 header->number, log_ns[0]);
1262
1263         header->hdr.ms.NumberOfSamples = log_ns[0];
1264
1265         /* get the number of samples ... */
1266
1267         num_samples = (1 << log_ns[0]);
1268     
1269         for (i = 0; i < num_samples; i++) {
1270                 char d[2];
1271                 int val;
1272         
1273                 if ((val = wavefront_read (dev)) == -1) {
1274                         snd_printk ("upload multisample failed "
1275                                     "during sample loop.\n");
1276                         return -(EIO);
1277                 }
1278                 d[0] = val;
1279
1280                 if ((val = wavefront_read (dev)) == -1) {
1281                         snd_printk ("upload multisample failed "
1282                                     "during sample loop.\n");
1283                         return -(EIO);
1284                 }
1285                 d[1] = val;
1286         
1287                 header->hdr.ms.SampleNumber[i] =
1288                         demunge_int32 ((unsigned char *) d, 2);
1289         
1290                 DPRINT (WF_DEBUG_DATA, "msample sample[%d] = %d\n",
1291                                         i, header->hdr.ms.SampleNumber[i]);
1292         }
1293
1294         return (0);
1295 }
1296
1297
1298 static int
1299 wavefront_send_drum (snd_wavefront_t *dev, wavefront_patch_info *header)
1300
1301 {
1302         unsigned char drumbuf[WF_DRUM_BYTES];
1303         wavefront_drum *drum = &header->hdr.d;
1304         int i;
1305
1306         DPRINT (WF_DEBUG_LOAD_PATCH, "downloading edrum for MIDI "
1307                 "note %d, patch = %d\n", 
1308                 header->number, drum->PatchNumber);
1309
1310         drumbuf[0] = header->number & 0x7f;
1311
1312         for (i = 0; i < 4; i++) {
1313                 munge_int32 (((unsigned char *)drum)[i], &drumbuf[1+(i*2)], 2);
1314         }
1315
1316         if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_EDRUM_PROGRAM, 0, drumbuf)) {
1317                 snd_printk ("download drum failed.\n");
1318                 return -(EIO);
1319         }
1320
1321         return (0);
1322 }
1323
1324 static int 
1325 wavefront_find_free_sample (snd_wavefront_t *dev)
1326
1327 {
1328         int i;
1329
1330         for (i = 0; i < WF_MAX_SAMPLE; i++) {
1331                 if (!(dev->sample_status[i] & WF_SLOT_FILLED)) {
1332                         return i;
1333                 }
1334         }
1335         snd_printk ("no free sample slots!\n");
1336         return -1;
1337 }
1338
1339 #if 0
1340 static int 
1341 wavefront_find_free_patch (snd_wavefront_t *dev)
1342
1343 {
1344         int i;
1345
1346         for (i = 0; i < WF_MAX_PATCH; i++) {
1347                 if (!(dev->patch_status[i] & WF_SLOT_FILLED)) {
1348                         return i;
1349                 }
1350         }
1351         snd_printk ("no free patch slots!\n");
1352         return -1;
1353 }
1354 #endif
1355
1356 static int
1357 wavefront_load_patch (snd_wavefront_t *dev, const char *addr)
1358
1359 {
1360         wavefront_patch_info header;
1361         
1362         if (copy_from_user (&header, addr, sizeof(wavefront_patch_info) -
1363                             sizeof(wavefront_any))) {
1364                 snd_printk ("bad address for load patch.\n");
1365                 return -(EFAULT);
1366         }
1367
1368         DPRINT (WF_DEBUG_LOAD_PATCH, "download "
1369                                       "Sample type: %d "
1370                                       "Sample number: %d "
1371                                       "Sample size: %d\n",
1372                                       header.subkey,
1373                                       header.number,
1374                                       header.size);
1375
1376         switch (header.subkey) {
1377         case WF_ST_SAMPLE:  /* sample or sample_header, based on patch->size */
1378
1379                 if (copy_from_user ((unsigned char *) &header.hdr.s,
1380                                     (unsigned char *) header.hdrptr,
1381                                     sizeof (wavefront_sample)))
1382                         return -EFAULT;
1383
1384                 return wavefront_send_sample (dev, &header, header.dataptr, 0);
1385
1386         case WF_ST_MULTISAMPLE:
1387
1388                 if (copy_from_user ((unsigned char *) &header.hdr.s,
1389                                     (unsigned char *) header.hdrptr,
1390                                     sizeof (wavefront_multisample)))
1391                         return -EFAULT;
1392
1393                 return wavefront_send_multisample (dev, &header);
1394
1395
1396         case WF_ST_ALIAS:
1397
1398                 if (copy_from_user ((unsigned char *) &header.hdr.a,
1399                                     (unsigned char *) header.hdrptr,
1400                                     sizeof (wavefront_alias)))
1401                         return -EFAULT;
1402
1403                 return wavefront_send_alias (dev, &header);
1404
1405         case WF_ST_DRUM:
1406                 if (copy_from_user ((unsigned char *) &header.hdr.d, 
1407                                     (unsigned char *) header.hdrptr,
1408                                     sizeof (wavefront_drum)))
1409                         return -EFAULT;
1410
1411                 return wavefront_send_drum (dev, &header);
1412
1413         case WF_ST_PATCH:
1414                 if (copy_from_user ((unsigned char *) &header.hdr.p, 
1415                                     (unsigned char *) header.hdrptr,
1416                                     sizeof (wavefront_patch)))
1417                         return -EFAULT;
1418
1419                 return wavefront_send_patch (dev, &header);
1420
1421         case WF_ST_PROGRAM:
1422                 if (copy_from_user ((unsigned char *) &header.hdr.pr, 
1423                                     (unsigned char *) header.hdrptr,
1424                                     sizeof (wavefront_program)))
1425                         return -EFAULT;
1426
1427                 return wavefront_send_program (dev, &header);
1428
1429         default:
1430                 snd_printk ("unknown patch type %d.\n",
1431                             header.subkey);
1432                 return -(EINVAL);
1433         }
1434
1435         return 0;
1436 }
1437 \f
1438 /***********************************************************************
1439 WaveFront: hardware-dependent interface
1440 ***********************************************************************/
1441
1442 static void
1443 process_sample_hdr (u8 *buf)
1444
1445 {
1446         wavefront_sample s;
1447         u8 *ptr;
1448
1449         ptr = buf;
1450
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.
1456         */
1457
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;
1463
1464         s.SampleResolution = *ptr & 0x3;
1465         s.Loop = *ptr & 0x8;
1466         s.Bidirectional = *ptr & 0x10;
1467         s.Reverse = *ptr & 0x40;
1468
1469         /* Now copy it back to where it came from */
1470
1471         memcpy (buf, (unsigned char *) &s, sizeof (wavefront_sample));
1472 }
1473
1474 static int
1475 wavefront_synth_control (snd_wavefront_card_t *acard, 
1476                          wavefront_control *wc)
1477
1478 {
1479         snd_wavefront_t *dev = &acard->wavefront;
1480         unsigned char patchnumbuf[2];
1481         int i;
1482
1483         DPRINT (WF_DEBUG_CMD, "synth control with "
1484                 "cmd 0x%x\n", wc->cmd);
1485
1486         /* Pre-handling of or for various commands */
1487
1488         switch (wc->cmd) {
1489                 
1490         case WFC_DISABLE_INTERRUPTS:
1491                 snd_printk ("interrupts disabled.\n");
1492                 outb (0x80|0x20, dev->control_port);
1493                 dev->interrupts_are_midi = 1;
1494                 return 0;
1495
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;
1500                 return 0;
1501
1502         case WFC_INTERRUPT_STATUS:
1503                 wc->rbuf[0] = dev->interrupts_are_midi;
1504                 return 0;
1505
1506         case WFC_ROMSAMPLES_RDONLY:
1507                 dev->rom_samples_rdonly = wc->wbuf[0];
1508                 wc->status = 0;
1509                 return 0;
1510
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",
1515                                 i);
1516                         wc->status = EINVAL;
1517                         return -EINVAL;
1518                 }
1519                 wc->rbuf[0] = dev->sample_status[i];
1520                 wc->status = 0;
1521                 return 0;
1522
1523         case WFC_DEBUG_DRIVER:
1524                 dev->debug = wc->wbuf[0];
1525                 snd_printk ("debug = 0x%x\n", dev->debug);
1526                 return 0;
1527
1528         case WFC_UPLOAD_PATCH:
1529                 munge_int32 (*((u32 *) wc->wbuf), patchnumbuf, 2);
1530                 memcpy (wc->wbuf, patchnumbuf, 2);
1531                 break;
1532
1533         case WFC_UPLOAD_MULTISAMPLE:
1534                 /* multisamples have to be handled differently, and
1535                    cannot be dealt with properly by snd_wavefront_cmd() alone.
1536                 */
1537                 wc->status = wavefront_fetch_multisample
1538                         (dev, (wavefront_patch_info *) wc->rbuf);
1539                 return 0;
1540
1541         case WFC_UPLOAD_SAMPLE_ALIAS:
1542                 snd_printk ("support for sample alias upload "
1543                         "being considered.\n");
1544                 wc->status = EINVAL;
1545                 return -EINVAL;
1546         }
1547
1548         wc->status = snd_wavefront_cmd (dev, wc->cmd, wc->rbuf, wc->wbuf);
1549
1550         /* Post-handling of certain commands.
1551
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.
1554         */
1555
1556         if (wc->status == 0) {
1557                 switch (wc->cmd) {
1558                         /* intercept any freemem requests so that we know
1559                            we are always current with the user-level view
1560                            of things.
1561                         */
1562
1563                 case WFC_REPORT_FREE_MEMORY:
1564                         dev->freemem = demunge_int32 (wc->rbuf, 4);
1565                         break;
1566
1567                 case WFC_UPLOAD_PATCH:
1568                         demunge_buf (wc->rbuf, wc->rbuf, WF_PATCH_BYTES);
1569                         break;
1570
1571                 case WFC_UPLOAD_PROGRAM:
1572                         demunge_buf (wc->rbuf, wc->rbuf, WF_PROGRAM_BYTES);
1573                         break;
1574
1575                 case WFC_UPLOAD_EDRUM_PROGRAM:
1576                         demunge_buf (wc->rbuf, wc->rbuf, WF_DRUM_BYTES - 1);
1577                         break;
1578
1579                 case WFC_UPLOAD_SAMPLE_HEADER:
1580                         process_sample_hdr (wc->rbuf);
1581                         break;
1582
1583                 case WFC_UPLOAD_SAMPLE_ALIAS:
1584                         snd_printk ("support for "
1585                                     "sample aliases still "
1586                                     "being considered.\n");
1587                         break;
1588
1589                 case WFC_VMIDI_OFF:
1590                         snd_wavefront_midi_disable_virtual (acard);
1591                         break;
1592
1593                 case WFC_VMIDI_ON:
1594                         snd_wavefront_midi_enable_virtual (acard);
1595                         break;
1596                 }
1597         }
1598
1599         return 0;
1600 }
1601
1602 int 
1603 snd_wavefront_synth_open (snd_hwdep_t *hw, struct file *file)
1604
1605 {
1606         if (!try_module_get(hw->card->module))
1607                 return -EFAULT;
1608         file->private_data = hw;
1609         return 0;
1610 }
1611
1612 int 
1613 snd_wavefront_synth_release (snd_hwdep_t *hw, struct file *file)
1614
1615 {
1616         module_put(hw->card->module);
1617         return 0;
1618 }
1619
1620 int
1621 snd_wavefront_synth_ioctl (snd_hwdep_t *hw, struct file *file,
1622                            unsigned int cmd, unsigned long arg)
1623
1624 {
1625         snd_card_t *card;
1626         snd_wavefront_t *dev;
1627         snd_wavefront_card_t *acard;
1628         wavefront_control wc;
1629
1630         card = (snd_card_t *) hw->card;
1631
1632         snd_assert(card != NULL, return -ENODEV);
1633
1634         snd_assert(card->private_data != NULL, return -ENODEV);
1635
1636         acard = card->private_data;
1637         dev = &acard->wavefront;
1638         
1639         switch (cmd) {
1640         case WFCTL_LOAD_SPP:
1641                 if (wavefront_load_patch (dev, (char *) arg) != 0) {
1642                         return -EIO;
1643                 }
1644                 break;
1645
1646         case WFCTL_WFCMD:
1647                 if (copy_from_user (&wc, (void *) arg, sizeof (wc)))
1648                         return -EFAULT;
1649                 if (wavefront_synth_control (acard, &wc) < 0) {
1650                         return -EIO;
1651                 }
1652                 if (copy_to_user ((void *) arg, &wc, sizeof (wc)))
1653                         return -EFAULT;
1654                 break;
1655
1656         default:
1657                 return -EINVAL;
1658         }
1659
1660         return 0;
1661 }
1662
1663 \f
1664 /***********************************************************************/
1665 /*  WaveFront: interface for card-level wavefront module               */
1666 /***********************************************************************/
1667
1668 void
1669 snd_wavefront_internal_interrupt (snd_wavefront_card_t *card)
1670 {
1671         snd_wavefront_t *dev = &card->wavefront;
1672
1673         /*
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.
1685
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.
1691         */
1692
1693         if ((wavefront_status(dev) & (STAT_INTR_READ|STAT_INTR_WRITE)) == 0) {
1694                 return;
1695         }
1696
1697         spin_lock(&dev->irq_lock);
1698         dev->irq_ok = 1;
1699         dev->irq_cnt++;
1700         spin_unlock(&dev->irq_lock);
1701         wake_up(&dev->interrupt_sleeper);
1702 }
1703
1704 /* STATUS REGISTER 
1705
1706 0 Host Rx Interrupt Enable (1=Enabled)
1707 1 Host Rx Register Full (1=Full)
1708 2 Host Rx Interrupt Pending (1=Interrupt)
1709 3 Unused
1710 4 Host Tx Interrupt (1=Enabled)
1711 5 Host Tx Register empty (1=Empty)
1712 6 Host Tx Interrupt Pending (1=Interrupt)
1713 7 Unused
1714 */
1715
1716 int __init
1717 snd_wavefront_interrupt_bits (int irq)
1718
1719 {
1720         int bits;
1721
1722         switch (irq) {
1723         case 9:
1724                 bits = 0x00;
1725                 break;
1726         case 5:
1727                 bits = 0x08;
1728                 break;
1729         case 12:
1730                 bits = 0x10;
1731                 break;
1732         case 15:
1733                 bits = 0x18;
1734                 break;
1735         
1736         default:
1737                 snd_printk ("invalid IRQ %d\n", irq);
1738                 bits = -1;
1739         }
1740
1741         return bits;
1742 }
1743
1744 static void __init
1745 wavefront_should_cause_interrupt (snd_wavefront_t *dev, 
1746                                   int val, int port, int timeout)
1747
1748 {
1749         wait_queue_t wait;
1750
1751         init_waitqueue_entry(&wait, current);
1752         spin_lock_irq(&dev->irq_lock);
1753         add_wait_queue(&dev->interrupt_sleeper, &wait);
1754         dev->irq_ok = 0;
1755         outb (val,port);
1756         spin_unlock_irq(&dev->irq_lock);
1757         while (1) {
1758                 if ((timeout = schedule_timeout(timeout)) == 0)
1759                         return;
1760                 if (dev->irq_ok)
1761                         return;
1762         }
1763 }
1764
1765 static int __init
1766 wavefront_reset_to_cleanliness (snd_wavefront_t *dev)
1767
1768 {
1769         int bits;
1770         int hwv[2];
1771
1772         /* IRQ already checked */
1773
1774         bits = snd_wavefront_interrupt_bits (dev->irq);
1775
1776         /* try reset of port */
1777
1778         outb (0x0, dev->control_port); 
1779   
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.
1782      
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.
1786      
1787            Bit 6 - MIDI Interface Select
1788
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
1792            serial MIDI source.
1793      
1794            Bits 5:3 - IRQ Selection
1795            0 0 0 - IRQ 2/9
1796            0 0 1 - IRQ 5
1797            0 1 0 - IRQ 12
1798            0 1 1 - IRQ 15
1799            1 0 0 - Reserved
1800            1 0 1 - Reserved
1801            1 1 0 - Reserved
1802            1 1 1 - Reserved
1803      
1804            Bits 2:1 - Reserved
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 
1808            storage.
1809      
1810         */
1811
1812         /* configure hardware: IRQ, enable interrupts, 
1813            plus external 9-pin MIDI interface selected
1814         */
1815
1816         outb (0x80 | 0x40 | bits, dev->data_port);      
1817   
1818         /* CONTROL REGISTER
1819
1820            0 Host Rx Interrupt Enable (1=Enabled)      0x1
1821            1 Unused                                    0x2
1822            2 Unused                                    0x4
1823            3 Unused                                    0x8
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
1828
1829            Take us out of reset, mute output, master + TX + RX interrupts on.
1830            
1831            We'll get an interrupt presumably to tell us that the TX
1832            register is clear.
1833         */
1834
1835         wavefront_should_cause_interrupt(dev, 0x80|0x40|0x10|0x1,
1836                                          dev->control_port,
1837                                          (reset_time*HZ)/100);
1838
1839         /* Note: data port is now the data port, not the h/w initialization
1840            port.
1841          */
1842
1843         if (!dev->irq_ok) {
1844                 snd_printk ("intr not received after h/w un-reset.\n");
1845                 goto gone_bad;
1846         } 
1847
1848         /* Note: data port is now the data port, not the h/w initialization
1849            port.
1850
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.
1860
1861            XXX Interesting question: why is no RX interrupt received first ?
1862         */
1863
1864         wavefront_should_cause_interrupt(dev, WFC_HARDWARE_VERSION, 
1865                                          dev->data_port, ramcheck_time*HZ);
1866
1867         if (!dev->irq_ok) {
1868                 snd_printk ("post-RAM-check interrupt not received.\n");
1869                 goto gone_bad;
1870         } 
1871
1872         if (!wavefront_wait (dev, STAT_CAN_READ)) {
1873                 snd_printk ("no response to HW version cmd.\n");
1874                 goto gone_bad;
1875         }
1876         
1877         if ((hwv[0] = wavefront_read (dev)) == -1) {
1878                 snd_printk ("board not responding correctly.\n");
1879                 goto gone_bad;
1880         }
1881
1882         if (hwv[0] == 0xFF) { /* NAK */
1883
1884                 /* Board's RAM test failed. Try to read error code,
1885                    and tell us about it either way.
1886                 */
1887                 
1888                 if ((hwv[0] = wavefront_read (dev)) == -1) {
1889                         snd_printk ("on-board RAM test failed "
1890                                     "(bad error code).\n");
1891                 } else {
1892                         snd_printk ("on-board RAM test failed "
1893                                     "(error code: 0x%x).\n",
1894                                 hwv[0]);
1895                 }
1896                 goto gone_bad;
1897         }
1898
1899         /* We're OK, just get the next byte of the HW version response */
1900
1901         if ((hwv[1] = wavefront_read (dev)) == -1) {
1902                 snd_printk ("incorrect h/w response.\n");
1903                 goto gone_bad;
1904         }
1905
1906         snd_printk ("hardware version %d.%d\n",
1907                     hwv[0], hwv[1]);
1908
1909         return 0;
1910
1911
1912      gone_bad:
1913         return (1);
1914 }
1915
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>
1922
1923
1924 static int __init
1925 wavefront_download_firmware (snd_wavefront_t *dev, char *path)
1926
1927 {
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;
1931         int fd;
1932         int c;
1933         int i;
1934         mm_segment_t fs;
1935
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.
1944         */
1945
1946         fs = get_fs();
1947         set_fs (get_ds());
1948
1949         if ((fd = sys_open (path, 0, 0)) < 0) {
1950                 snd_printk ("Unable to load \"%s\".\n",
1951                         path);
1952                 return 1;
1953         }
1954
1955         while (1) {
1956                 int x;
1957
1958                 if ((x = sys_read (fd, &section_length, sizeof (section_length))) !=
1959                     sizeof (section_length)) {
1960                         snd_printk ("firmware read error.\n");
1961                         goto failure;
1962                 }
1963
1964                 if (section_length == 0) {
1965                         break;
1966                 }
1967
1968                 if (sys_read (fd, section, section_length) != section_length) {
1969                         snd_printk ("firmware section "
1970                                 "read error.\n");
1971                         goto failure;
1972                 }
1973
1974                 /* Send command */
1975         
1976                 if (wavefront_write (dev, WFC_DOWNLOAD_OS)) {
1977                         goto failure;
1978                 }
1979         
1980                 for (i = 0; i < section_length; i++) {
1981                         if (wavefront_write (dev, section[i])) {
1982                                 goto failure;
1983                         }
1984                 }
1985         
1986                 /* get ACK */
1987         
1988                 if (wavefront_wait (dev, STAT_CAN_READ)) {
1989
1990                         if ((c = inb (dev->data_port)) != WF_ACK) {
1991
1992                                 snd_printk ("download "
1993                                             "of section #%d not "
1994                                             "acknowledged, ack = 0x%x\n",
1995                                             section_cnt_downloaded + 1, c);
1996                                 goto failure;
1997                 
1998                         }
1999
2000                 } else {
2001                         snd_printk ("time out for firmware ACK.\n");
2002                         goto failure;
2003                 }
2004
2005         }
2006
2007         sys_close (fd);
2008         set_fs (fs);
2009         return 0;
2010
2011  failure:
2012         sys_close (fd);
2013         set_fs (fs);
2014         snd_printk ("firmware download failed!!!\n");
2015         return 1;
2016 }
2017
2018
2019 static int __init
2020 wavefront_do_reset (snd_wavefront_t *dev)
2021
2022 {
2023         char voices[1];
2024
2025         if (wavefront_reset_to_cleanliness (dev)) {
2026                 snd_printk ("hw reset failed.\n");
2027                 goto gone_bad;
2028         }
2029
2030         if (dev->israw) {
2031                 if (wavefront_download_firmware (dev, ospath)) {
2032                         goto gone_bad;
2033                 }
2034
2035                 dev->israw = 0;
2036
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.
2041                    
2042                    Rather than using timed waits, use interrupts creatively.
2043                 */
2044
2045                 wavefront_should_cause_interrupt (dev, WFC_NOOP,
2046                                                   dev->data_port,
2047                                                   (osrun_time*HZ));
2048
2049                 if (!dev->irq_ok) {
2050                         snd_printk ("no post-OS interrupt.\n");
2051                         goto gone_bad;
2052                 }
2053                 
2054                 /* Now, do it again ! */
2055                 
2056                 wavefront_should_cause_interrupt (dev, WFC_NOOP,
2057                                                   dev->data_port, (10*HZ));
2058                 
2059                 if (!dev->irq_ok) {
2060                         snd_printk ("no post-OS interrupt(2).\n");
2061                         goto gone_bad;
2062                 }
2063
2064                 /* OK, no (RX/TX) interrupts any more, but leave mute
2065                    in effect. 
2066                 */
2067                 
2068                 outb (0x80|0x40, dev->control_port); 
2069         }
2070
2071         /* SETUPSND.EXE asks for sample memory config here, but since i
2072            have no idea how to interpret the result, we'll forget
2073            about it.
2074         */
2075         
2076         if ((dev->freemem = wavefront_freemem (dev)) < 0) {
2077                 goto gone_bad;
2078         }
2079                 
2080         snd_printk ("available DRAM %dk\n", dev->freemem / 1024);
2081
2082         if (wavefront_write (dev, 0xf0) ||
2083             wavefront_write (dev, 1) ||
2084             (wavefront_read (dev) < 0)) {
2085                 dev->debug = 0;
2086                 snd_printk ("MPU emulation mode not set.\n");
2087                 goto gone_bad;
2088         }
2089
2090         voices[0] = 32;
2091
2092         if (snd_wavefront_cmd (dev, WFC_SET_NVOICES, 0, voices)) {
2093                 snd_printk ("cannot set number of voices to 32.\n");
2094                 goto gone_bad;
2095         }
2096
2097
2098         return 0;
2099
2100  gone_bad:
2101         /* reset that sucker so that it doesn't bother us. */
2102
2103         outb (0x0, dev->control_port);
2104         dev->interrupts_are_midi = 0;
2105         return 1;
2106 }
2107
2108 int __init
2109 snd_wavefront_start (snd_wavefront_t *dev)
2110
2111 {
2112         int samples_are_from_rom;
2113
2114         /* IMPORTANT: assumes that snd_wavefront_detect() and/or
2115            wavefront_reset_to_cleanliness() has already been called 
2116         */
2117
2118         if (dev->israw) {
2119                 samples_are_from_rom = 1;
2120         } else {
2121                 /* XXX is this always true ? */
2122                 samples_are_from_rom = 0;
2123         }
2124
2125         if (dev->israw || fx_raw) {
2126                 if (wavefront_do_reset (dev)) {
2127                         return -1;
2128                 }
2129         }
2130         /* Check for FX device, present only on Tropez+ */
2131
2132         dev->has_fx = (snd_wavefront_fx_detect (dev) == 0);
2133
2134         if (dev->has_fx && fx_raw) {
2135                 snd_wavefront_fx_start (dev);
2136         }
2137
2138         wavefront_get_sample_status (dev, samples_are_from_rom);
2139         wavefront_get_program_status (dev);
2140         wavefront_get_patch_status (dev);
2141
2142         /* Start normal operation: unreset, master interrupt enabled, no mute
2143         */
2144
2145         outb (0x80|0x40|0x20, dev->control_port); 
2146
2147         return (0);
2148 }
2149
2150 int __init
2151 snd_wavefront_detect (snd_wavefront_card_t *card)
2152
2153 {
2154         unsigned char   rbuf[4], wbuf[4];
2155         snd_wavefront_t *dev = &card->wavefront;
2156         
2157         /* returns zero if a WaveFront card is successfully detected.
2158            negative otherwise.
2159         */
2160
2161         dev->israw = 0;
2162         dev->has_fx = 0;
2163         dev->debug = debug_default;
2164         dev->interrupts_are_midi = 0;
2165         dev->irq_cnt = 0;
2166         dev->rom_samples_rdonly = 1;
2167
2168         if (snd_wavefront_cmd (dev, WFC_FIRMWARE_VERSION, rbuf, wbuf) == 0) {
2169
2170                 dev->fw_version[0] = rbuf[0];
2171                 dev->fw_version[1] = rbuf[1];
2172
2173                 snd_printk ("firmware %d.%d already loaded.\n",
2174                             rbuf[0], rbuf[1]);
2175
2176                 /* check that a command actually works */
2177       
2178                 if (snd_wavefront_cmd (dev, WFC_HARDWARE_VERSION,
2179                                        rbuf, wbuf) == 0) {
2180                         dev->hw_version[0] = rbuf[0];
2181                         dev->hw_version[1] = rbuf[1];
2182                 } else {
2183                         snd_printk ("not raw, but no "
2184                                     "hardware version!\n");
2185                         return -1;
2186                 }
2187
2188                 if (!wf_raw) {
2189                         return 0;
2190                 } else {
2191                         snd_printk ("reloading firmware as you requested.\n");
2192                         dev->israw = 1;
2193                 }
2194
2195         } else {
2196
2197                 dev->israw = 1;
2198                 snd_printk ("no response to firmware probe, assume raw.\n");
2199
2200         }
2201
2202         return 0;
2203 }