Path: blob/master/sound/isa/wavefront/wavefront_synth.c
10817 views
/* Copyright (C) by Paul Barton-Davis 1998-19991*2* Some portions of this file are taken from work that is3* copyright (C) by Hannu Savolainen 1993-19964*5* This program is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)6* Version 2 (June 1991). See the "COPYING" file distributed with this software7* for more info.8*/910/*11* An ALSA lowlevel driver for Turtle Beach ICS2115 wavetable synth12* (Maui, Tropez, Tropez Plus)13*14* This driver supports the onboard wavetable synthesizer (an ICS2115),15* including patch, sample and program loading and unloading, conversion16* of GUS patches during loading, and full user-level access to all17* WaveFront commands. It tries to provide semi-intelligent patch and18* sample management as well.19*20*/2122#include <asm/io.h>23#include <linux/interrupt.h>24#include <linux/init.h>25#include <linux/delay.h>26#include <linux/time.h>27#include <linux/wait.h>28#include <linux/firmware.h>29#include <linux/moduleparam.h>30#include <linux/slab.h>31#include <sound/core.h>32#include <sound/snd_wavefront.h>33#include <sound/initval.h>3435static int wf_raw = 0; /* we normally check for "raw state" to firmware36loading. if non-zero, then during driver loading, the37state of the board is ignored, and we reset the38board and load the firmware anyway.39*/4041static int fx_raw = 1; /* if this is zero, we'll leave the FX processor in42whatever state it is when the driver is loaded.43The default is to download the microprogram and44associated coefficients to set it up for "default"45operation, whatever that means.46*/4748static int debug_default = 0; /* you can set this to control debugging49during driver loading. it takes any combination50of the WF_DEBUG_* flags defined in51wavefront.h52*/5354/* XXX this needs to be made firmware and hardware version dependent */5556#define DEFAULT_OSPATH "wavefront.os"57static char *ospath = DEFAULT_OSPATH; /* the firmware file name */5859static int wait_usecs = 150; /* This magic number seems to give pretty optimal60throughput based on my limited experimentation.61If you want to play around with it and find a better62value, be my guest. Remember, the idea is to63get a number that causes us to just busy wait64for as many WaveFront commands as possible, without65coming up with a number so large that we hog the66whole CPU.6768Specifically, with this number, out of about 134,00069status waits, only about 250 result in a sleep.70*/7172static int sleep_interval = 100; /* HZ/sleep_interval seconds per sleep */73static int sleep_tries = 50; /* number of times we'll try to sleep */7475static int reset_time = 2; /* hundreths of a second we wait after a HW76reset for the expected interrupt.77*/7879static int ramcheck_time = 20; /* time in seconds to wait while ROM code80checks on-board RAM.81*/8283static int osrun_time = 10; /* time in seconds we wait for the OS to84start running.85*/86module_param(wf_raw, int, 0444);87MODULE_PARM_DESC(wf_raw, "if non-zero, assume that we need to boot the OS");88module_param(fx_raw, int, 0444);89MODULE_PARM_DESC(fx_raw, "if non-zero, assume that the FX process needs help");90module_param(debug_default, int, 0444);91MODULE_PARM_DESC(debug_default, "debug parameters for card initialization");92module_param(wait_usecs, int, 0444);93MODULE_PARM_DESC(wait_usecs, "how long to wait without sleeping, usecs");94module_param(sleep_interval, int, 0444);95MODULE_PARM_DESC(sleep_interval, "how long to sleep when waiting for reply");96module_param(sleep_tries, int, 0444);97MODULE_PARM_DESC(sleep_tries, "how many times to try sleeping during a wait");98module_param(ospath, charp, 0444);99MODULE_PARM_DESC(ospath, "pathname to processed ICS2115 OS firmware");100module_param(reset_time, int, 0444);101MODULE_PARM_DESC(reset_time, "how long to wait for a reset to take effect");102module_param(ramcheck_time, int, 0444);103MODULE_PARM_DESC(ramcheck_time, "how many seconds to wait for the RAM test");104module_param(osrun_time, int, 0444);105MODULE_PARM_DESC(osrun_time, "how many seconds to wait for the ICS2115 OS");106107/* if WF_DEBUG not defined, no run-time debugging messages will108be available via the debug flag setting. Given the current109beta state of the driver, this will remain set until a future110version.111*/112113#define WF_DEBUG 1114115#ifdef WF_DEBUG116117#define DPRINT(cond, ...) \118if ((dev->debug & (cond)) == (cond)) { \119snd_printk (__VA_ARGS__); \120}121#else122#define DPRINT(cond, args...)123#endif /* WF_DEBUG */124125#define LOGNAME "WaveFront: "126127/* bitmasks for WaveFront status port value */128129#define STAT_RINTR_ENABLED 0x01130#define STAT_CAN_READ 0x02131#define STAT_INTR_READ 0x04132#define STAT_WINTR_ENABLED 0x10133#define STAT_CAN_WRITE 0x20134#define STAT_INTR_WRITE 0x40135136static int wavefront_delete_sample (snd_wavefront_t *, int sampnum);137static int wavefront_find_free_sample (snd_wavefront_t *);138139struct wavefront_command {140int cmd;141char *action;142unsigned int read_cnt;143unsigned int write_cnt;144int need_ack;145};146147static struct {148int errno;149const char *errstr;150} wavefront_errors[] = {151{ 0x01, "Bad sample number" },152{ 0x02, "Out of sample memory" },153{ 0x03, "Bad patch number" },154{ 0x04, "Error in number of voices" },155{ 0x06, "Sample load already in progress" },156{ 0x0B, "No sample load request pending" },157{ 0x0E, "Bad MIDI channel number" },158{ 0x10, "Download Record Error" },159{ 0x80, "Success" },160{ 0x0 }161};162163#define NEEDS_ACK 1164165static struct wavefront_command wavefront_commands[] = {166{ WFC_SET_SYNTHVOL, "set synthesizer volume", 0, 1, NEEDS_ACK },167{ WFC_GET_SYNTHVOL, "get synthesizer volume", 1, 0, 0},168{ WFC_SET_NVOICES, "set number of voices", 0, 1, NEEDS_ACK },169{ WFC_GET_NVOICES, "get number of voices", 1, 0, 0 },170{ WFC_SET_TUNING, "set synthesizer tuning", 0, 2, NEEDS_ACK },171{ WFC_GET_TUNING, "get synthesizer tuning", 2, 0, 0 },172{ WFC_DISABLE_CHANNEL, "disable synth channel", 0, 1, NEEDS_ACK },173{ WFC_ENABLE_CHANNEL, "enable synth channel", 0, 1, NEEDS_ACK },174{ WFC_GET_CHANNEL_STATUS, "get synth channel status", 3, 0, 0 },175{ WFC_MISYNTH_OFF, "disable midi-in to synth", 0, 0, NEEDS_ACK },176{ WFC_MISYNTH_ON, "enable midi-in to synth", 0, 0, NEEDS_ACK },177{ WFC_VMIDI_ON, "enable virtual midi mode", 0, 0, NEEDS_ACK },178{ WFC_VMIDI_OFF, "disable virtual midi mode", 0, 0, NEEDS_ACK },179{ WFC_MIDI_STATUS, "report midi status", 1, 0, 0 },180{ WFC_FIRMWARE_VERSION, "report firmware version", 2, 0, 0 },181{ WFC_HARDWARE_VERSION, "report hardware version", 2, 0, 0 },182{ WFC_GET_NSAMPLES, "report number of samples", 2, 0, 0 },183{ WFC_INSTOUT_LEVELS, "report instantaneous output levels", 7, 0, 0 },184{ WFC_PEAKOUT_LEVELS, "report peak output levels", 7, 0, 0 },185{ WFC_DOWNLOAD_SAMPLE, "download sample",1860, WF_SAMPLE_BYTES, NEEDS_ACK },187{ WFC_DOWNLOAD_BLOCK, "download block", 0, 0, NEEDS_ACK},188{ WFC_DOWNLOAD_SAMPLE_HEADER, "download sample header",1890, WF_SAMPLE_HDR_BYTES, NEEDS_ACK },190{ WFC_UPLOAD_SAMPLE_HEADER, "upload sample header", 13, 2, 0 },191192/* This command requires a variable number of bytes to be written.193There is a hack in snd_wavefront_cmd() to support this. The actual194count is passed in as the read buffer ptr, cast appropriately.195Ugh.196*/197198{ WFC_DOWNLOAD_MULTISAMPLE, "download multisample", 0, 0, NEEDS_ACK },199200/* This one is a hack as well. We just read the first byte of the201response, don't fetch an ACK, and leave the rest to the202calling function. Ugly, ugly, ugly.203*/204205{ WFC_UPLOAD_MULTISAMPLE, "upload multisample", 2, 1, 0 },206{ WFC_DOWNLOAD_SAMPLE_ALIAS, "download sample alias",2070, WF_ALIAS_BYTES, NEEDS_ACK },208{ WFC_UPLOAD_SAMPLE_ALIAS, "upload sample alias", WF_ALIAS_BYTES, 2, 0},209{ WFC_DELETE_SAMPLE, "delete sample", 0, 2, NEEDS_ACK },210{ WFC_IDENTIFY_SAMPLE_TYPE, "identify sample type", 5, 2, 0 },211{ WFC_UPLOAD_SAMPLE_PARAMS, "upload sample parameters" },212{ WFC_REPORT_FREE_MEMORY, "report free memory", 4, 0, 0 },213{ WFC_DOWNLOAD_PATCH, "download patch", 0, 134, NEEDS_ACK },214{ WFC_UPLOAD_PATCH, "upload patch", 132, 2, 0 },215{ WFC_DOWNLOAD_PROGRAM, "download program", 0, 33, NEEDS_ACK },216{ WFC_UPLOAD_PROGRAM, "upload program", 32, 1, 0 },217{ WFC_DOWNLOAD_EDRUM_PROGRAM, "download enhanced drum program", 0, 9,218NEEDS_ACK},219{ WFC_UPLOAD_EDRUM_PROGRAM, "upload enhanced drum program", 8, 1, 0},220{ WFC_SET_EDRUM_CHANNEL, "set enhanced drum program channel",2210, 1, NEEDS_ACK },222{ WFC_DISABLE_DRUM_PROGRAM, "disable drum program", 0, 1, NEEDS_ACK },223{ WFC_REPORT_CHANNEL_PROGRAMS, "report channel program numbers",22432, 0, 0 },225{ WFC_NOOP, "the no-op command", 0, 0, NEEDS_ACK },226{ 0x00 }227};228229static const char *230wavefront_errorstr (int errnum)231232{233int i;234235for (i = 0; wavefront_errors[i].errstr; i++) {236if (wavefront_errors[i].errno == errnum) {237return wavefront_errors[i].errstr;238}239}240241return "Unknown WaveFront error";242}243244static struct wavefront_command *245wavefront_get_command (int cmd)246247{248int i;249250for (i = 0; wavefront_commands[i].cmd != 0; i++) {251if (cmd == wavefront_commands[i].cmd) {252return &wavefront_commands[i];253}254}255256return NULL;257}258259static inline int260wavefront_status (snd_wavefront_t *dev)261262{263return inb (dev->status_port);264}265266static int267wavefront_sleep (int limit)268269{270schedule_timeout_interruptible(limit);271272return signal_pending(current);273}274275static int276wavefront_wait (snd_wavefront_t *dev, int mask)277278{279int i;280281/* Spin for a short period of time, because >99% of all282requests to the WaveFront can be serviced inline like this.283*/284285for (i = 0; i < wait_usecs; i += 5) {286if (wavefront_status (dev) & mask) {287return 1;288}289udelay(5);290}291292for (i = 0; i < sleep_tries; i++) {293294if (wavefront_status (dev) & mask) {295return 1;296}297298if (wavefront_sleep (HZ/sleep_interval)) {299return (0);300}301}302303return (0);304}305306static int307wavefront_read (snd_wavefront_t *dev)308309{310if (wavefront_wait (dev, STAT_CAN_READ))311return inb (dev->data_port);312313DPRINT (WF_DEBUG_DATA, "read timeout.\n");314315return -1;316}317318static int319wavefront_write (snd_wavefront_t *dev, unsigned char data)320321{322if (wavefront_wait (dev, STAT_CAN_WRITE)) {323outb (data, dev->data_port);324return 0;325}326327DPRINT (WF_DEBUG_DATA, "write timeout.\n");328329return -1;330}331332int333snd_wavefront_cmd (snd_wavefront_t *dev,334int cmd, unsigned char *rbuf, unsigned char *wbuf)335336{337int ack;338unsigned int i;339int c;340struct wavefront_command *wfcmd;341342if ((wfcmd = wavefront_get_command (cmd)) == NULL) {343snd_printk ("command 0x%x not supported.\n",344cmd);345return 1;346}347348/* Hack to handle the one variable-size write command. See349wavefront_send_multisample() for the other half of this350gross and ugly strategy.351*/352353if (cmd == WFC_DOWNLOAD_MULTISAMPLE) {354wfcmd->write_cnt = (unsigned long) rbuf;355rbuf = NULL;356}357358DPRINT (WF_DEBUG_CMD, "0x%x [%s] (%d,%d,%d)\n",359cmd, wfcmd->action, wfcmd->read_cnt,360wfcmd->write_cnt, wfcmd->need_ack);361362if (wavefront_write (dev, cmd)) {363DPRINT ((WF_DEBUG_IO|WF_DEBUG_CMD), "cannot request "364"0x%x [%s].\n",365cmd, wfcmd->action);366return 1;367}368369if (wfcmd->write_cnt > 0) {370DPRINT (WF_DEBUG_DATA, "writing %d bytes "371"for 0x%x\n",372wfcmd->write_cnt, cmd);373374for (i = 0; i < wfcmd->write_cnt; i++) {375if (wavefront_write (dev, wbuf[i])) {376DPRINT (WF_DEBUG_IO, "bad write for byte "377"%d of 0x%x [%s].\n",378i, cmd, wfcmd->action);379return 1;380}381382DPRINT (WF_DEBUG_DATA, "write[%d] = 0x%x\n",383i, wbuf[i]);384}385}386387if (wfcmd->read_cnt > 0) {388DPRINT (WF_DEBUG_DATA, "reading %d ints "389"for 0x%x\n",390wfcmd->read_cnt, cmd);391392for (i = 0; i < wfcmd->read_cnt; i++) {393394if ((c = wavefront_read (dev)) == -1) {395DPRINT (WF_DEBUG_IO, "bad read for byte "396"%d of 0x%x [%s].\n",397i, cmd, wfcmd->action);398return 1;399}400401/* Now handle errors. Lots of special cases here */402403if (c == 0xff) {404if ((c = wavefront_read (dev)) == -1) {405DPRINT (WF_DEBUG_IO, "bad read for "406"error byte at "407"read byte %d "408"of 0x%x [%s].\n",409i, cmd,410wfcmd->action);411return 1;412}413414/* Can you believe this madness ? */415416if (c == 1 &&417wfcmd->cmd == WFC_IDENTIFY_SAMPLE_TYPE) {418rbuf[0] = WF_ST_EMPTY;419return (0);420421} else if (c == 3 &&422wfcmd->cmd == WFC_UPLOAD_PATCH) {423424return 3;425426} else if (c == 1 &&427wfcmd->cmd == WFC_UPLOAD_PROGRAM) {428429return 1;430431} else {432433DPRINT (WF_DEBUG_IO, "error %d (%s) "434"during "435"read for byte "436"%d of 0x%x "437"[%s].\n",438c,439wavefront_errorstr (c),440i, cmd,441wfcmd->action);442return 1;443444}445446} else {447rbuf[i] = c;448}449450DPRINT (WF_DEBUG_DATA, "read[%d] = 0x%x\n",i, rbuf[i]);451}452}453454if ((wfcmd->read_cnt == 0 && wfcmd->write_cnt == 0) || wfcmd->need_ack) {455456DPRINT (WF_DEBUG_CMD, "reading ACK for 0x%x\n", cmd);457458/* Some commands need an ACK, but return zero instead459of the standard value.460*/461462if ((ack = wavefront_read (dev)) == 0) {463ack = WF_ACK;464}465466if (ack != WF_ACK) {467if (ack == -1) {468DPRINT (WF_DEBUG_IO, "cannot read ack for "469"0x%x [%s].\n",470cmd, wfcmd->action);471return 1;472473} else {474int err = -1; /* something unknown */475476if (ack == 0xff) { /* explicit error */477478if ((err = wavefront_read (dev)) == -1) {479DPRINT (WF_DEBUG_DATA,480"cannot read err "481"for 0x%x [%s].\n",482cmd, wfcmd->action);483}484}485486DPRINT (WF_DEBUG_IO, "0x%x [%s] "487"failed (0x%x, 0x%x, %s)\n",488cmd, wfcmd->action, ack, err,489wavefront_errorstr (err));490491return -err;492}493}494495DPRINT (WF_DEBUG_DATA, "ack received "496"for 0x%x [%s]\n",497cmd, wfcmd->action);498} else {499500DPRINT (WF_DEBUG_CMD, "0x%x [%s] does not need "501"ACK (%d,%d,%d)\n",502cmd, wfcmd->action, wfcmd->read_cnt,503wfcmd->write_cnt, wfcmd->need_ack);504}505506return 0;507508}509510/***********************************************************************511WaveFront data munging512513Things here are weird. All data written to the board cannot514have its most significant bit set. Any data item with values515potentially > 0x7F (127) must be split across multiple bytes.516517Sometimes, we need to munge numeric values that are represented on518the x86 side as 8-32 bit values. Sometimes, we need to munge data519that is represented on the x86 side as an array of bytes. The most520efficient approach to handling both cases seems to be to use 2521different functions for munging and 2 for de-munging. This avoids522weird casting and worrying about bit-level offsets.523524**********************************************************************/525526static unsigned char *527munge_int32 (unsigned int src,528unsigned char *dst,529unsigned int dst_size)530{531unsigned int i;532533for (i = 0; i < dst_size; i++) {534*dst = src & 0x7F; /* Mask high bit of LSB */535src = src >> 7; /* Rotate Right 7 bits */536/* Note: we leave the upper bits in place */537538dst++;539};540return dst;541};542543static int544demunge_int32 (unsigned char* src, int src_size)545546{547int i;548int outval = 0;549550for (i = src_size - 1; i >= 0; i--) {551outval=(outval<<7)+src[i];552}553554return outval;555};556557static558unsigned char *559munge_buf (unsigned char *src, unsigned char *dst, unsigned int dst_size)560561{562unsigned int i;563unsigned int last = dst_size / 2;564565for (i = 0; i < last; i++) {566*dst++ = src[i] & 0x7f;567*dst++ = src[i] >> 7;568}569return dst;570}571572static573unsigned char *574demunge_buf (unsigned char *src, unsigned char *dst, unsigned int src_bytes)575576{577int i;578unsigned char *end = src + src_bytes;579580end = src + src_bytes;581582/* NOTE: src and dst *CAN* point to the same address */583584for (i = 0; src != end; i++) {585dst[i] = *src++;586dst[i] |= (*src++)<<7;587}588589return dst;590}591592/***********************************************************************593WaveFront: sample, patch and program management.594***********************************************************************/595596static int597wavefront_delete_sample (snd_wavefront_t *dev, int sample_num)598599{600unsigned char wbuf[2];601int x;602603wbuf[0] = sample_num & 0x7f;604wbuf[1] = sample_num >> 7;605606if ((x = snd_wavefront_cmd (dev, WFC_DELETE_SAMPLE, NULL, wbuf)) == 0) {607dev->sample_status[sample_num] = WF_ST_EMPTY;608}609610return x;611}612613static int614wavefront_get_sample_status (snd_wavefront_t *dev, int assume_rom)615616{617int i;618unsigned char rbuf[32], wbuf[32];619unsigned int sc_real, sc_alias, sc_multi;620621/* check sample status */622623if (snd_wavefront_cmd (dev, WFC_GET_NSAMPLES, rbuf, wbuf)) {624snd_printk ("cannot request sample count.\n");625return -1;626}627628sc_real = sc_alias = sc_multi = dev->samples_used = 0;629630for (i = 0; i < WF_MAX_SAMPLE; i++) {631632wbuf[0] = i & 0x7f;633wbuf[1] = i >> 7;634635if (snd_wavefront_cmd (dev, WFC_IDENTIFY_SAMPLE_TYPE, rbuf, wbuf)) {636snd_printk(KERN_WARNING "cannot identify sample "637"type of slot %d\n", i);638dev->sample_status[i] = WF_ST_EMPTY;639continue;640}641642dev->sample_status[i] = (WF_SLOT_FILLED|rbuf[0]);643644if (assume_rom) {645dev->sample_status[i] |= WF_SLOT_ROM;646}647648switch (rbuf[0] & WF_ST_MASK) {649case WF_ST_SAMPLE:650sc_real++;651break;652case WF_ST_MULTISAMPLE:653sc_multi++;654break;655case WF_ST_ALIAS:656sc_alias++;657break;658case WF_ST_EMPTY:659break;660661default:662snd_printk ("unknown sample type for "663"slot %d (0x%x)\n",664i, rbuf[0]);665}666667if (rbuf[0] != WF_ST_EMPTY) {668dev->samples_used++;669}670}671672snd_printk ("%d samples used (%d real, %d aliases, %d multi), "673"%d empty\n", dev->samples_used, sc_real, sc_alias, sc_multi,674WF_MAX_SAMPLE - dev->samples_used);675676677return (0);678679}680681static int682wavefront_get_patch_status (snd_wavefront_t *dev)683684{685unsigned char patchbuf[WF_PATCH_BYTES];686unsigned char patchnum[2];687wavefront_patch *p;688int i, x, cnt, cnt2;689690for (i = 0; i < WF_MAX_PATCH; i++) {691patchnum[0] = i & 0x7f;692patchnum[1] = i >> 7;693694if ((x = snd_wavefront_cmd (dev, WFC_UPLOAD_PATCH, patchbuf,695patchnum)) == 0) {696697dev->patch_status[i] |= WF_SLOT_FILLED;698p = (wavefront_patch *) patchbuf;699dev->sample_status700[p->sample_number|(p->sample_msb<<7)] |=701WF_SLOT_USED;702703} else if (x == 3) { /* Bad patch number */704dev->patch_status[i] = 0;705} else {706snd_printk ("upload patch "707"error 0x%x\n", x);708dev->patch_status[i] = 0;709return 1;710}711}712713/* program status has already filled in slot_used bits */714715for (i = 0, cnt = 0, cnt2 = 0; i < WF_MAX_PATCH; i++) {716if (dev->patch_status[i] & WF_SLOT_FILLED) {717cnt++;718}719if (dev->patch_status[i] & WF_SLOT_USED) {720cnt2++;721}722723}724snd_printk ("%d patch slots filled, %d in use\n", cnt, cnt2);725726return (0);727}728729static int730wavefront_get_program_status (snd_wavefront_t *dev)731732{733unsigned char progbuf[WF_PROGRAM_BYTES];734wavefront_program prog;735unsigned char prognum;736int i, x, l, cnt;737738for (i = 0; i < WF_MAX_PROGRAM; i++) {739prognum = i;740741if ((x = snd_wavefront_cmd (dev, WFC_UPLOAD_PROGRAM, progbuf,742&prognum)) == 0) {743744dev->prog_status[i] |= WF_SLOT_USED;745746demunge_buf (progbuf, (unsigned char *) &prog,747WF_PROGRAM_BYTES);748749for (l = 0; l < WF_NUM_LAYERS; l++) {750if (prog.layer[l].mute) {751dev->patch_status752[prog.layer[l].patch_number] |=753WF_SLOT_USED;754}755}756} else if (x == 1) { /* Bad program number */757dev->prog_status[i] = 0;758} else {759snd_printk ("upload program "760"error 0x%x\n", x);761dev->prog_status[i] = 0;762}763}764765for (i = 0, cnt = 0; i < WF_MAX_PROGRAM; i++) {766if (dev->prog_status[i]) {767cnt++;768}769}770771snd_printk ("%d programs slots in use\n", cnt);772773return (0);774}775776static int777wavefront_send_patch (snd_wavefront_t *dev, wavefront_patch_info *header)778779{780unsigned char buf[WF_PATCH_BYTES+2];781unsigned char *bptr;782783DPRINT (WF_DEBUG_LOAD_PATCH, "downloading patch %d\n",784header->number);785786dev->patch_status[header->number] |= WF_SLOT_FILLED;787788bptr = buf;789bptr = munge_int32 (header->number, buf, 2);790munge_buf ((unsigned char *)&header->hdr.p, bptr, WF_PATCH_BYTES);791792if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_PATCH, NULL, buf)) {793snd_printk ("download patch failed\n");794return -(EIO);795}796797return (0);798}799800static int801wavefront_send_program (snd_wavefront_t *dev, wavefront_patch_info *header)802803{804unsigned char buf[WF_PROGRAM_BYTES+1];805int i;806807DPRINT (WF_DEBUG_LOAD_PATCH, "downloading program %d\n",808header->number);809810dev->prog_status[header->number] = WF_SLOT_USED;811812/* XXX need to zero existing SLOT_USED bit for program_status[i]813where `i' is the program that's being (potentially) overwritten.814*/815816for (i = 0; i < WF_NUM_LAYERS; i++) {817if (header->hdr.pr.layer[i].mute) {818dev->patch_status[header->hdr.pr.layer[i].patch_number] |=819WF_SLOT_USED;820821/* XXX need to mark SLOT_USED for sample used by822patch_number, but this means we have to load it. Ick.823*/824}825}826827buf[0] = header->number;828munge_buf ((unsigned char *)&header->hdr.pr, &buf[1], WF_PROGRAM_BYTES);829830if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_PROGRAM, NULL, buf)) {831snd_printk ("download patch failed\n");832return -(EIO);833}834835return (0);836}837838static int839wavefront_freemem (snd_wavefront_t *dev)840841{842char rbuf[8];843844if (snd_wavefront_cmd (dev, WFC_REPORT_FREE_MEMORY, rbuf, NULL)) {845snd_printk ("can't get memory stats.\n");846return -1;847} else {848return demunge_int32 (rbuf, 4);849}850}851852static int853wavefront_send_sample (snd_wavefront_t *dev,854wavefront_patch_info *header,855u16 __user *dataptr,856int data_is_unsigned)857858{859/* samples are downloaded via a 16-bit wide i/o port860(you could think of it as 2 adjacent 8-bit wide ports861but its less efficient that way). therefore, all862the blocksizes and so forth listed in the documentation,863and used conventionally to refer to sample sizes,864which are given in 8-bit units (bytes), need to be865divided by 2.866*/867868u16 sample_short = 0;869u32 length;870u16 __user *data_end = NULL;871unsigned int i;872const unsigned int max_blksize = 4096/2;873unsigned int written;874unsigned int blocksize;875int dma_ack;876int blocknum;877unsigned char sample_hdr[WF_SAMPLE_HDR_BYTES];878unsigned char *shptr;879int skip = 0;880int initial_skip = 0;881882DPRINT (WF_DEBUG_LOAD_PATCH, "sample %sdownload for slot %d, "883"type %d, %d bytes from 0x%lx\n",884header->size ? "" : "header ",885header->number, header->subkey,886header->size,887(unsigned long) header->dataptr);888889if (header->number == WAVEFRONT_FIND_FREE_SAMPLE_SLOT) {890int x;891892if ((x = wavefront_find_free_sample (dev)) < 0) {893return -ENOMEM;894}895snd_printk ("unspecified sample => %d\n", x);896header->number = x;897}898899if (header->size) {900901/* XXX it's a debatable point whether or not RDONLY semantics902on the ROM samples should cover just the sample data or903the sample header. For now, it only covers the sample data,904so anyone is free at all times to rewrite sample headers.905906My reason for this is that we have the sample headers907available in the WFB file for General MIDI, and so these908can always be reset if needed. The sample data, however,909cannot be recovered without a complete reset and firmware910reload of the ICS2115, which is a very expensive operation.911912So, doing things this way allows us to honor the notion of913"RESETSAMPLES" reasonably cheaply. Note however, that this914is done purely at user level: there is no WFB parser in915this driver, and so a complete reset (back to General MIDI,916or theoretically some other configuration) is the917responsibility of the user level library.918919To try to do this in the kernel would be a little920crazy: we'd need 158K of kernel space just to hold921a copy of the patch/program/sample header data.922*/923924if (dev->rom_samples_rdonly) {925if (dev->sample_status[header->number] & WF_SLOT_ROM) {926snd_printk ("sample slot %d "927"write protected\n",928header->number);929return -EACCES;930}931}932933wavefront_delete_sample (dev, header->number);934}935936if (header->size) {937dev->freemem = wavefront_freemem (dev);938939if (dev->freemem < (int)header->size) {940snd_printk ("insufficient memory to "941"load %d byte sample.\n",942header->size);943return -ENOMEM;944}945946}947948skip = WF_GET_CHANNEL(&header->hdr.s);949950if (skip > 0 && header->hdr.s.SampleResolution != LINEAR_16BIT) {951snd_printk ("channel selection only "952"possible on 16-bit samples");953return -(EINVAL);954}955956switch (skip) {957case 0:958initial_skip = 0;959skip = 1;960break;961case 1:962initial_skip = 0;963skip = 2;964break;965case 2:966initial_skip = 1;967skip = 2;968break;969case 3:970initial_skip = 2;971skip = 3;972break;973case 4:974initial_skip = 3;975skip = 4;976break;977case 5:978initial_skip = 4;979skip = 5;980break;981case 6:982initial_skip = 5;983skip = 6;984break;985}986987DPRINT (WF_DEBUG_LOAD_PATCH, "channel selection: %d => "988"initial skip = %d, skip = %d\n",989WF_GET_CHANNEL (&header->hdr.s),990initial_skip, skip);991992/* Be safe, and zero the "Unused" bits ... */993994WF_SET_CHANNEL(&header->hdr.s, 0);995996/* adjust size for 16 bit samples by dividing by two. We always997send 16 bits per write, even for 8 bit samples, so the length998is always half the size of the sample data in bytes.999*/10001001length = header->size / 2;10021003/* the data we're sent has not been munged, and in fact, the1004header we have to send isn't just a munged copy either.1005so, build the sample header right here.1006*/10071008shptr = &sample_hdr[0];10091010shptr = munge_int32 (header->number, shptr, 2);10111012if (header->size) {1013shptr = munge_int32 (length, shptr, 4);1014}10151016/* Yes, a 4 byte result doesn't contain all of the offset bits,1017but the offset only uses 24 bits.1018*/10191020shptr = munge_int32 (*((u32 *) &header->hdr.s.sampleStartOffset),1021shptr, 4);1022shptr = munge_int32 (*((u32 *) &header->hdr.s.loopStartOffset),1023shptr, 4);1024shptr = munge_int32 (*((u32 *) &header->hdr.s.loopEndOffset),1025shptr, 4);1026shptr = munge_int32 (*((u32 *) &header->hdr.s.sampleEndOffset),1027shptr, 4);10281029/* This one is truly weird. What kind of weirdo decided that in1030a system dominated by 16 and 32 bit integers, they would use1031a just 12 bits ?1032*/10331034shptr = munge_int32 (header->hdr.s.FrequencyBias, shptr, 3);10351036/* Why is this nybblified, when the MSB is *always* zero ?1037Anyway, we can't take address of bitfield, so make a1038good-faith guess at where it starts.1039*/10401041shptr = munge_int32 (*(&header->hdr.s.FrequencyBias+1),1042shptr, 2);10431044if (snd_wavefront_cmd (dev,1045header->size ?1046WFC_DOWNLOAD_SAMPLE : WFC_DOWNLOAD_SAMPLE_HEADER,1047NULL, sample_hdr)) {1048snd_printk ("sample %sdownload refused.\n",1049header->size ? "" : "header ");1050return -(EIO);1051}10521053if (header->size == 0) {1054goto sent; /* Sorry. Just had to have one somewhere */1055}10561057data_end = dataptr + length;10581059/* Do any initial skip over an unused channel's data */10601061dataptr += initial_skip;10621063for (written = 0, blocknum = 0;1064written < length; written += max_blksize, blocknum++) {10651066if ((length - written) > max_blksize) {1067blocksize = max_blksize;1068} else {1069/* round to nearest 16-byte value */1070blocksize = ALIGN(length - written, 8);1071}10721073if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_BLOCK, NULL, NULL)) {1074snd_printk ("download block "1075"request refused.\n");1076return -(EIO);1077}10781079for (i = 0; i < blocksize; i++) {10801081if (dataptr < data_end) {10821083__get_user (sample_short, dataptr);1084dataptr += skip;10851086if (data_is_unsigned) { /* GUS ? */10871088if (WF_SAMPLE_IS_8BIT(&header->hdr.s)) {10891090/* 8 bit sample1091resolution, sign1092extend both bytes.1093*/10941095((unsigned char*)1096&sample_short)[0] += 0x7f;1097((unsigned char*)1098&sample_short)[1] += 0x7f;10991100} else {11011102/* 16 bit sample1103resolution, sign1104extend the MSB.1105*/11061107sample_short += 0x7fff;1108}1109}11101111} else {11121113/* In padding section of final block:11141115Don't fetch unsupplied data from1116user space, just continue with1117whatever the final value was.1118*/1119}11201121if (i < blocksize - 1) {1122outw (sample_short, dev->block_port);1123} else {1124outw (sample_short, dev->last_block_port);1125}1126}11271128/* Get "DMA page acknowledge", even though its really1129nothing to do with DMA at all.1130*/11311132if ((dma_ack = wavefront_read (dev)) != WF_DMA_ACK) {1133if (dma_ack == -1) {1134snd_printk ("upload sample "1135"DMA ack timeout\n");1136return -(EIO);1137} else {1138snd_printk ("upload sample "1139"DMA ack error 0x%x\n",1140dma_ack);1141return -(EIO);1142}1143}1144}11451146dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_SAMPLE);11471148/* Note, label is here because sending the sample header shouldn't1149alter the sample_status info at all.1150*/11511152sent:1153return (0);1154}11551156static int1157wavefront_send_alias (snd_wavefront_t *dev, wavefront_patch_info *header)11581159{1160unsigned char alias_hdr[WF_ALIAS_BYTES];11611162DPRINT (WF_DEBUG_LOAD_PATCH, "download alias, %d is "1163"alias for %d\n",1164header->number,1165header->hdr.a.OriginalSample);11661167munge_int32 (header->number, &alias_hdr[0], 2);1168munge_int32 (header->hdr.a.OriginalSample, &alias_hdr[2], 2);1169munge_int32 (*((unsigned int *)&header->hdr.a.sampleStartOffset),1170&alias_hdr[4], 4);1171munge_int32 (*((unsigned int *)&header->hdr.a.loopStartOffset),1172&alias_hdr[8], 4);1173munge_int32 (*((unsigned int *)&header->hdr.a.loopEndOffset),1174&alias_hdr[12], 4);1175munge_int32 (*((unsigned int *)&header->hdr.a.sampleEndOffset),1176&alias_hdr[16], 4);1177munge_int32 (header->hdr.a.FrequencyBias, &alias_hdr[20], 3);1178munge_int32 (*(&header->hdr.a.FrequencyBias+1), &alias_hdr[23], 2);11791180if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_SAMPLE_ALIAS, NULL, alias_hdr)) {1181snd_printk ("download alias failed.\n");1182return -(EIO);1183}11841185dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_ALIAS);11861187return (0);1188}11891190static int1191wavefront_send_multisample (snd_wavefront_t *dev, wavefront_patch_info *header)1192{1193int i;1194int num_samples;1195unsigned char *msample_hdr;11961197msample_hdr = kmalloc(sizeof(WF_MSAMPLE_BYTES), GFP_KERNEL);1198if (! msample_hdr)1199return -ENOMEM;12001201munge_int32 (header->number, &msample_hdr[0], 2);12021203/* You'll recall at this point that the "number of samples" value1204in a wavefront_multisample struct is actually the log2 of the1205real number of samples.1206*/12071208num_samples = (1<<(header->hdr.ms.NumberOfSamples&7));1209msample_hdr[2] = (unsigned char) header->hdr.ms.NumberOfSamples;12101211DPRINT (WF_DEBUG_LOAD_PATCH, "multi %d with %d=%d samples\n",1212header->number,1213header->hdr.ms.NumberOfSamples,1214num_samples);12151216for (i = 0; i < num_samples; i++) {1217DPRINT(WF_DEBUG_LOAD_PATCH|WF_DEBUG_DATA, "sample[%d] = %d\n",1218i, header->hdr.ms.SampleNumber[i]);1219munge_int32 (header->hdr.ms.SampleNumber[i],1220&msample_hdr[3+(i*2)], 2);1221}12221223/* Need a hack here to pass in the number of bytes1224to be written to the synth. This is ugly, and perhaps1225one day, I'll fix it.1226*/12271228if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_MULTISAMPLE,1229(unsigned char *) (long) ((num_samples*2)+3),1230msample_hdr)) {1231snd_printk ("download of multisample failed.\n");1232kfree(msample_hdr);1233return -(EIO);1234}12351236dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_MULTISAMPLE);12371238kfree(msample_hdr);1239return (0);1240}12411242static int1243wavefront_fetch_multisample (snd_wavefront_t *dev,1244wavefront_patch_info *header)1245{1246int i;1247unsigned char log_ns[1];1248unsigned char number[2];1249int num_samples;12501251munge_int32 (header->number, number, 2);12521253if (snd_wavefront_cmd (dev, WFC_UPLOAD_MULTISAMPLE, log_ns, number)) {1254snd_printk ("upload multisample failed.\n");1255return -(EIO);1256}12571258DPRINT (WF_DEBUG_DATA, "msample %d has %d samples\n",1259header->number, log_ns[0]);12601261header->hdr.ms.NumberOfSamples = log_ns[0];12621263/* get the number of samples ... */12641265num_samples = (1 << log_ns[0]);12661267for (i = 0; i < num_samples; i++) {1268char d[2];1269int val;12701271if ((val = wavefront_read (dev)) == -1) {1272snd_printk ("upload multisample failed "1273"during sample loop.\n");1274return -(EIO);1275}1276d[0] = val;12771278if ((val = wavefront_read (dev)) == -1) {1279snd_printk ("upload multisample failed "1280"during sample loop.\n");1281return -(EIO);1282}1283d[1] = val;12841285header->hdr.ms.SampleNumber[i] =1286demunge_int32 ((unsigned char *) d, 2);12871288DPRINT (WF_DEBUG_DATA, "msample sample[%d] = %d\n",1289i, header->hdr.ms.SampleNumber[i]);1290}12911292return (0);1293}129412951296static int1297wavefront_send_drum (snd_wavefront_t *dev, wavefront_patch_info *header)12981299{1300unsigned char drumbuf[WF_DRUM_BYTES];1301wavefront_drum *drum = &header->hdr.d;1302int i;13031304DPRINT (WF_DEBUG_LOAD_PATCH, "downloading edrum for MIDI "1305"note %d, patch = %d\n",1306header->number, drum->PatchNumber);13071308drumbuf[0] = header->number & 0x7f;13091310for (i = 0; i < 4; i++) {1311munge_int32 (((unsigned char *)drum)[i], &drumbuf[1+(i*2)], 2);1312}13131314if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_EDRUM_PROGRAM, NULL, drumbuf)) {1315snd_printk ("download drum failed.\n");1316return -(EIO);1317}13181319return (0);1320}13211322static int1323wavefront_find_free_sample (snd_wavefront_t *dev)13241325{1326int i;13271328for (i = 0; i < WF_MAX_SAMPLE; i++) {1329if (!(dev->sample_status[i] & WF_SLOT_FILLED)) {1330return i;1331}1332}1333snd_printk ("no free sample slots!\n");1334return -1;1335}13361337#if 01338static int1339wavefront_find_free_patch (snd_wavefront_t *dev)13401341{1342int i;13431344for (i = 0; i < WF_MAX_PATCH; i++) {1345if (!(dev->patch_status[i] & WF_SLOT_FILLED)) {1346return i;1347}1348}1349snd_printk ("no free patch slots!\n");1350return -1;1351}1352#endif13531354static int1355wavefront_load_patch (snd_wavefront_t *dev, const char __user *addr)1356{1357wavefront_patch_info *header;1358int err;13591360header = kmalloc(sizeof(*header), GFP_KERNEL);1361if (! header)1362return -ENOMEM;13631364if (copy_from_user (header, addr, sizeof(wavefront_patch_info) -1365sizeof(wavefront_any))) {1366snd_printk ("bad address for load patch.\n");1367err = -EFAULT;1368goto __error;1369}13701371DPRINT (WF_DEBUG_LOAD_PATCH, "download "1372"Sample type: %d "1373"Sample number: %d "1374"Sample size: %d\n",1375header->subkey,1376header->number,1377header->size);13781379switch (header->subkey) {1380case WF_ST_SAMPLE: /* sample or sample_header, based on patch->size */13811382if (copy_from_user (&header->hdr.s, header->hdrptr,1383sizeof (wavefront_sample))) {1384err = -EFAULT;1385break;1386}13871388err = wavefront_send_sample (dev, header, header->dataptr, 0);1389break;13901391case WF_ST_MULTISAMPLE:13921393if (copy_from_user (&header->hdr.s, header->hdrptr,1394sizeof (wavefront_multisample))) {1395err = -EFAULT;1396break;1397}13981399err = wavefront_send_multisample (dev, header);1400break;14011402case WF_ST_ALIAS:14031404if (copy_from_user (&header->hdr.a, header->hdrptr,1405sizeof (wavefront_alias))) {1406err = -EFAULT;1407break;1408}14091410err = wavefront_send_alias (dev, header);1411break;14121413case WF_ST_DRUM:1414if (copy_from_user (&header->hdr.d, header->hdrptr,1415sizeof (wavefront_drum))) {1416err = -EFAULT;1417break;1418}14191420err = wavefront_send_drum (dev, header);1421break;14221423case WF_ST_PATCH:1424if (copy_from_user (&header->hdr.p, header->hdrptr,1425sizeof (wavefront_patch))) {1426err = -EFAULT;1427break;1428}14291430err = wavefront_send_patch (dev, header);1431break;14321433case WF_ST_PROGRAM:1434if (copy_from_user (&header->hdr.pr, header->hdrptr,1435sizeof (wavefront_program))) {1436err = -EFAULT;1437break;1438}14391440err = wavefront_send_program (dev, header);1441break;14421443default:1444snd_printk ("unknown patch type %d.\n",1445header->subkey);1446err = -EINVAL;1447break;1448}14491450__error:1451kfree(header);1452return err;1453}14541455/***********************************************************************1456WaveFront: hardware-dependent interface1457***********************************************************************/14581459static void1460process_sample_hdr (u8 *buf)14611462{1463wavefront_sample s;1464u8 *ptr;14651466ptr = buf;14671468/* The board doesn't send us an exact copy of a "wavefront_sample"1469in response to an Upload Sample Header command. Instead, we1470have to convert the data format back into our data structure,1471just as in the Download Sample command, where we have to do1472something very similar in the reverse direction.1473*/14741475*((u32 *) &s.sampleStartOffset) = demunge_int32 (ptr, 4); ptr += 4;1476*((u32 *) &s.loopStartOffset) = demunge_int32 (ptr, 4); ptr += 4;1477*((u32 *) &s.loopEndOffset) = demunge_int32 (ptr, 4); ptr += 4;1478*((u32 *) &s.sampleEndOffset) = demunge_int32 (ptr, 4); ptr += 4;1479*((u32 *) &s.FrequencyBias) = demunge_int32 (ptr, 3); ptr += 3;14801481s.SampleResolution = *ptr & 0x3;1482s.Loop = *ptr & 0x8;1483s.Bidirectional = *ptr & 0x10;1484s.Reverse = *ptr & 0x40;14851486/* Now copy it back to where it came from */14871488memcpy (buf, (unsigned char *) &s, sizeof (wavefront_sample));1489}14901491static int1492wavefront_synth_control (snd_wavefront_card_t *acard,1493wavefront_control *wc)14941495{1496snd_wavefront_t *dev = &acard->wavefront;1497unsigned char patchnumbuf[2];1498int i;14991500DPRINT (WF_DEBUG_CMD, "synth control with "1501"cmd 0x%x\n", wc->cmd);15021503/* Pre-handling of or for various commands */15041505switch (wc->cmd) {15061507case WFC_DISABLE_INTERRUPTS:1508snd_printk ("interrupts disabled.\n");1509outb (0x80|0x20, dev->control_port);1510dev->interrupts_are_midi = 1;1511return 0;15121513case WFC_ENABLE_INTERRUPTS:1514snd_printk ("interrupts enabled.\n");1515outb (0x80|0x40|0x20, dev->control_port);1516dev->interrupts_are_midi = 1;1517return 0;15181519case WFC_INTERRUPT_STATUS:1520wc->rbuf[0] = dev->interrupts_are_midi;1521return 0;15221523case WFC_ROMSAMPLES_RDONLY:1524dev->rom_samples_rdonly = wc->wbuf[0];1525wc->status = 0;1526return 0;15271528case WFC_IDENTIFY_SLOT_TYPE:1529i = wc->wbuf[0] | (wc->wbuf[1] << 7);1530if (i <0 || i >= WF_MAX_SAMPLE) {1531snd_printk ("invalid slot ID %d\n",1532i);1533wc->status = EINVAL;1534return -EINVAL;1535}1536wc->rbuf[0] = dev->sample_status[i];1537wc->status = 0;1538return 0;15391540case WFC_DEBUG_DRIVER:1541dev->debug = wc->wbuf[0];1542snd_printk ("debug = 0x%x\n", dev->debug);1543return 0;15441545case WFC_UPLOAD_PATCH:1546munge_int32 (*((u32 *) wc->wbuf), patchnumbuf, 2);1547memcpy (wc->wbuf, patchnumbuf, 2);1548break;15491550case WFC_UPLOAD_MULTISAMPLE:1551/* multisamples have to be handled differently, and1552cannot be dealt with properly by snd_wavefront_cmd() alone.1553*/1554wc->status = wavefront_fetch_multisample1555(dev, (wavefront_patch_info *) wc->rbuf);1556return 0;15571558case WFC_UPLOAD_SAMPLE_ALIAS:1559snd_printk ("support for sample alias upload "1560"being considered.\n");1561wc->status = EINVAL;1562return -EINVAL;1563}15641565wc->status = snd_wavefront_cmd (dev, wc->cmd, wc->rbuf, wc->wbuf);15661567/* Post-handling of certain commands.15681569In particular, if the command was an upload, demunge the data1570so that the user-level doesn't have to think about it.1571*/15721573if (wc->status == 0) {1574switch (wc->cmd) {1575/* intercept any freemem requests so that we know1576we are always current with the user-level view1577of things.1578*/15791580case WFC_REPORT_FREE_MEMORY:1581dev->freemem = demunge_int32 (wc->rbuf, 4);1582break;15831584case WFC_UPLOAD_PATCH:1585demunge_buf (wc->rbuf, wc->rbuf, WF_PATCH_BYTES);1586break;15871588case WFC_UPLOAD_PROGRAM:1589demunge_buf (wc->rbuf, wc->rbuf, WF_PROGRAM_BYTES);1590break;15911592case WFC_UPLOAD_EDRUM_PROGRAM:1593demunge_buf (wc->rbuf, wc->rbuf, WF_DRUM_BYTES - 1);1594break;15951596case WFC_UPLOAD_SAMPLE_HEADER:1597process_sample_hdr (wc->rbuf);1598break;15991600case WFC_UPLOAD_SAMPLE_ALIAS:1601snd_printk ("support for "1602"sample aliases still "1603"being considered.\n");1604break;16051606case WFC_VMIDI_OFF:1607snd_wavefront_midi_disable_virtual (acard);1608break;16091610case WFC_VMIDI_ON:1611snd_wavefront_midi_enable_virtual (acard);1612break;1613}1614}16151616return 0;1617}16181619int1620snd_wavefront_synth_open (struct snd_hwdep *hw, struct file *file)16211622{1623if (!try_module_get(hw->card->module))1624return -EFAULT;1625file->private_data = hw;1626return 0;1627}16281629int1630snd_wavefront_synth_release (struct snd_hwdep *hw, struct file *file)16311632{1633module_put(hw->card->module);1634return 0;1635}16361637int1638snd_wavefront_synth_ioctl (struct snd_hwdep *hw, struct file *file,1639unsigned int cmd, unsigned long arg)16401641{1642struct snd_card *card;1643snd_wavefront_t *dev;1644snd_wavefront_card_t *acard;1645wavefront_control *wc;1646void __user *argp = (void __user *)arg;1647int err;16481649card = (struct snd_card *) hw->card;16501651if (snd_BUG_ON(!card))1652return -ENODEV;1653if (snd_BUG_ON(!card->private_data))1654return -ENODEV;16551656acard = card->private_data;1657dev = &acard->wavefront;16581659switch (cmd) {1660case WFCTL_LOAD_SPP:1661if (wavefront_load_patch (dev, argp) != 0) {1662return -EIO;1663}1664break;16651666case WFCTL_WFCMD:1667wc = memdup_user(argp, sizeof(*wc));1668if (IS_ERR(wc))1669return PTR_ERR(wc);16701671if (wavefront_synth_control (acard, wc) < 0)1672err = -EIO;1673else if (copy_to_user (argp, wc, sizeof (*wc)))1674err = -EFAULT;1675else1676err = 0;1677kfree(wc);1678return err;16791680default:1681return -EINVAL;1682}16831684return 0;1685}168616871688/***********************************************************************/1689/* WaveFront: interface for card-level wavefront module */1690/***********************************************************************/16911692void1693snd_wavefront_internal_interrupt (snd_wavefront_card_t *card)1694{1695snd_wavefront_t *dev = &card->wavefront;16961697/*1698Some comments on interrupts. I attempted a version of this1699driver that used interrupts throughout the code instead of1700doing busy and/or sleep-waiting. Alas, it appears that once1701the Motorola firmware is downloaded, the card *never*1702generates an RX interrupt. These are successfully generated1703during firmware loading, and after that wavefront_status()1704reports that an interrupt is pending on the card from time1705to time, but it never seems to be delivered to this1706driver. Note also that wavefront_status() continues to1707report that RX interrupts are enabled, suggesting that I1708didn't goof up and disable them by mistake.17091710Thus, I stepped back to a prior version of1711wavefront_wait(), the only place where this really1712matters. Its sad, but I've looked through the code to check1713on things, and I really feel certain that the Motorola1714firmware prevents RX-ready interrupts.1715*/17161717if ((wavefront_status(dev) & (STAT_INTR_READ|STAT_INTR_WRITE)) == 0) {1718return;1719}17201721spin_lock(&dev->irq_lock);1722dev->irq_ok = 1;1723dev->irq_cnt++;1724spin_unlock(&dev->irq_lock);1725wake_up(&dev->interrupt_sleeper);1726}17271728/* STATUS REGISTER172917300 Host Rx Interrupt Enable (1=Enabled)17311 Host Rx Register Full (1=Full)17322 Host Rx Interrupt Pending (1=Interrupt)17333 Unused17344 Host Tx Interrupt (1=Enabled)17355 Host Tx Register empty (1=Empty)17366 Host Tx Interrupt Pending (1=Interrupt)17377 Unused1738*/17391740static int __devinit1741snd_wavefront_interrupt_bits (int irq)17421743{1744int bits;17451746switch (irq) {1747case 9:1748bits = 0x00;1749break;1750case 5:1751bits = 0x08;1752break;1753case 12:1754bits = 0x10;1755break;1756case 15:1757bits = 0x18;1758break;17591760default:1761snd_printk ("invalid IRQ %d\n", irq);1762bits = -1;1763}17641765return bits;1766}17671768static void __devinit1769wavefront_should_cause_interrupt (snd_wavefront_t *dev,1770int val, int port, unsigned long timeout)17711772{1773wait_queue_t wait;17741775init_waitqueue_entry(&wait, current);1776spin_lock_irq(&dev->irq_lock);1777add_wait_queue(&dev->interrupt_sleeper, &wait);1778dev->irq_ok = 0;1779outb (val,port);1780spin_unlock_irq(&dev->irq_lock);1781while (!dev->irq_ok && time_before(jiffies, timeout)) {1782schedule_timeout_uninterruptible(1);1783barrier();1784}1785}17861787static int __devinit1788wavefront_reset_to_cleanliness (snd_wavefront_t *dev)17891790{1791int bits;1792int hwv[2];17931794/* IRQ already checked */17951796bits = snd_wavefront_interrupt_bits (dev->irq);17971798/* try reset of port */17991800outb (0x0, dev->control_port);18011802/* At this point, the board is in reset, and the H/W initialization1803register is accessed at the same address as the data port.18041805Bit 7 - Enable IRQ Driver18060 - Tri-state the Wave-Board drivers for the PC Bus IRQs18071 - Enable IRQ selected by bits 5:3 to be driven onto the PC Bus.18081809Bit 6 - MIDI Interface Select181018110 - Use the MIDI Input from the 26-pin WaveBlaster1812compatible header as the serial MIDI source18131 - Use the MIDI Input from the 9-pin D connector as the1814serial MIDI source.18151816Bits 5:3 - IRQ Selection18170 0 0 - IRQ 2/918180 0 1 - IRQ 518190 1 0 - IRQ 1218200 1 1 - IRQ 1518211 0 0 - Reserved18221 0 1 - Reserved18231 1 0 - Reserved18241 1 1 - Reserved18251826Bits 2:1 - Reserved1827Bit 0 - Disable Boot ROM18280 - memory accesses to 03FC30-03FFFFH utilize the internal Boot ROM18291 - memory accesses to 03FC30-03FFFFH are directed to external1830storage.18311832*/18331834/* configure hardware: IRQ, enable interrupts,1835plus external 9-pin MIDI interface selected1836*/18371838outb (0x80 | 0x40 | bits, dev->data_port);18391840/* CONTROL REGISTER184118420 Host Rx Interrupt Enable (1=Enabled) 0x118431 Unused 0x218442 Unused 0x418453 Unused 0x818464 Host Tx Interrupt Enable 0x1018475 Mute (0=Mute; 1=Play) 0x2018486 Master Interrupt Enable (1=Enabled) 0x4018497 Master Reset (0=Reset; 1=Run) 0x8018501851Take us out of reset, mute output, master + TX + RX interrupts on.18521853We'll get an interrupt presumably to tell us that the TX1854register is clear.1855*/18561857wavefront_should_cause_interrupt(dev, 0x80|0x40|0x10|0x1,1858dev->control_port,1859(reset_time*HZ)/100);18601861/* Note: data port is now the data port, not the h/w initialization1862port.1863*/18641865if (!dev->irq_ok) {1866snd_printk ("intr not received after h/w un-reset.\n");1867goto gone_bad;1868}18691870/* Note: data port is now the data port, not the h/w initialization1871port.18721873At this point, only "HW VERSION" or "DOWNLOAD OS" commands1874will work. So, issue one of them, and wait for TX1875interrupt. This can take a *long* time after a cold boot,1876while the ISC ROM does its RAM test. The SDK says up to 41877seconds - with 12MB of RAM on a Tropez+, it takes a lot1878longer than that (~16secs). Note that the card understands1879the difference between a warm and a cold boot, so1880subsequent ISC2115 reboots (say, caused by module1881reloading) will get through this much faster.18821883XXX Interesting question: why is no RX interrupt received first ?1884*/18851886wavefront_should_cause_interrupt(dev, WFC_HARDWARE_VERSION,1887dev->data_port, ramcheck_time*HZ);18881889if (!dev->irq_ok) {1890snd_printk ("post-RAM-check interrupt not received.\n");1891goto gone_bad;1892}18931894if (!wavefront_wait (dev, STAT_CAN_READ)) {1895snd_printk ("no response to HW version cmd.\n");1896goto gone_bad;1897}18981899if ((hwv[0] = wavefront_read (dev)) == -1) {1900snd_printk ("board not responding correctly.\n");1901goto gone_bad;1902}19031904if (hwv[0] == 0xFF) { /* NAK */19051906/* Board's RAM test failed. Try to read error code,1907and tell us about it either way.1908*/19091910if ((hwv[0] = wavefront_read (dev)) == -1) {1911snd_printk ("on-board RAM test failed "1912"(bad error code).\n");1913} else {1914snd_printk ("on-board RAM test failed "1915"(error code: 0x%x).\n",1916hwv[0]);1917}1918goto gone_bad;1919}19201921/* We're OK, just get the next byte of the HW version response */19221923if ((hwv[1] = wavefront_read (dev)) == -1) {1924snd_printk ("incorrect h/w response.\n");1925goto gone_bad;1926}19271928snd_printk ("hardware version %d.%d\n",1929hwv[0], hwv[1]);19301931return 0;193219331934gone_bad:1935return (1);1936}19371938static int __devinit1939wavefront_download_firmware (snd_wavefront_t *dev, char *path)19401941{1942const unsigned char *buf;1943int len, err;1944int section_cnt_downloaded = 0;1945const struct firmware *firmware;19461947err = request_firmware(&firmware, path, dev->card->dev);1948if (err < 0) {1949snd_printk(KERN_ERR "firmware (%s) download failed!!!\n", path);1950return 1;1951}19521953len = 0;1954buf = firmware->data;1955for (;;) {1956int section_length = *(signed char *)buf;1957if (section_length == 0)1958break;1959if (section_length < 0 || section_length > WF_SECTION_MAX) {1960snd_printk(KERN_ERR1961"invalid firmware section length %d\n",1962section_length);1963goto failure;1964}1965buf++;1966len++;19671968if (firmware->size < len + section_length) {1969snd_printk(KERN_ERR "firmware section read error.\n");1970goto failure;1971}19721973/* Send command */1974if (wavefront_write(dev, WFC_DOWNLOAD_OS))1975goto failure;19761977for (; section_length; section_length--) {1978if (wavefront_write(dev, *buf))1979goto failure;1980buf++;1981len++;1982}19831984/* get ACK */1985if (!wavefront_wait(dev, STAT_CAN_READ)) {1986snd_printk(KERN_ERR "time out for firmware ACK.\n");1987goto failure;1988}1989err = inb(dev->data_port);1990if (err != WF_ACK) {1991snd_printk(KERN_ERR1992"download of section #%d not "1993"acknowledged, ack = 0x%x\n",1994section_cnt_downloaded + 1, err);1995goto failure;1996}19971998section_cnt_downloaded++;1999}20002001release_firmware(firmware);2002return 0;20032004failure:2005release_firmware(firmware);2006snd_printk(KERN_ERR "firmware download failed!!!\n");2007return 1;2008}200920102011static int __devinit2012wavefront_do_reset (snd_wavefront_t *dev)20132014{2015char voices[1];20162017if (wavefront_reset_to_cleanliness (dev)) {2018snd_printk ("hw reset failed.\n");2019goto gone_bad;2020}20212022if (dev->israw) {2023if (wavefront_download_firmware (dev, ospath)) {2024goto gone_bad;2025}20262027dev->israw = 0;20282029/* Wait for the OS to get running. The protocol for2030this is non-obvious, and was determined by2031using port-IO tracing in DOSemu and some2032experimentation here.20332034Rather than using timed waits, use interrupts creatively.2035*/20362037wavefront_should_cause_interrupt (dev, WFC_NOOP,2038dev->data_port,2039(osrun_time*HZ));20402041if (!dev->irq_ok) {2042snd_printk ("no post-OS interrupt.\n");2043goto gone_bad;2044}20452046/* Now, do it again ! */20472048wavefront_should_cause_interrupt (dev, WFC_NOOP,2049dev->data_port, (10*HZ));20502051if (!dev->irq_ok) {2052snd_printk ("no post-OS interrupt(2).\n");2053goto gone_bad;2054}20552056/* OK, no (RX/TX) interrupts any more, but leave mute2057in effect.2058*/20592060outb (0x80|0x40, dev->control_port);2061}20622063/* SETUPSND.EXE asks for sample memory config here, but since i2064have no idea how to interpret the result, we'll forget2065about it.2066*/20672068if ((dev->freemem = wavefront_freemem (dev)) < 0) {2069goto gone_bad;2070}20712072snd_printk ("available DRAM %dk\n", dev->freemem / 1024);20732074if (wavefront_write (dev, 0xf0) ||2075wavefront_write (dev, 1) ||2076(wavefront_read (dev) < 0)) {2077dev->debug = 0;2078snd_printk ("MPU emulation mode not set.\n");2079goto gone_bad;2080}20812082voices[0] = 32;20832084if (snd_wavefront_cmd (dev, WFC_SET_NVOICES, NULL, voices)) {2085snd_printk ("cannot set number of voices to 32.\n");2086goto gone_bad;2087}208820892090return 0;20912092gone_bad:2093/* reset that sucker so that it doesn't bother us. */20942095outb (0x0, dev->control_port);2096dev->interrupts_are_midi = 0;2097return 1;2098}20992100int __devinit2101snd_wavefront_start (snd_wavefront_t *dev)21022103{2104int samples_are_from_rom;21052106/* IMPORTANT: assumes that snd_wavefront_detect() and/or2107wavefront_reset_to_cleanliness() has already been called2108*/21092110if (dev->israw) {2111samples_are_from_rom = 1;2112} else {2113/* XXX is this always true ? */2114samples_are_from_rom = 0;2115}21162117if (dev->israw || fx_raw) {2118if (wavefront_do_reset (dev)) {2119return -1;2120}2121}2122/* Check for FX device, present only on Tropez+ */21232124dev->has_fx = (snd_wavefront_fx_detect (dev) == 0);21252126if (dev->has_fx && fx_raw) {2127snd_wavefront_fx_start (dev);2128}21292130wavefront_get_sample_status (dev, samples_are_from_rom);2131wavefront_get_program_status (dev);2132wavefront_get_patch_status (dev);21332134/* Start normal operation: unreset, master interrupt enabled, no mute2135*/21362137outb (0x80|0x40|0x20, dev->control_port);21382139return (0);2140}21412142int __devinit2143snd_wavefront_detect (snd_wavefront_card_t *card)21442145{2146unsigned char rbuf[4], wbuf[4];2147snd_wavefront_t *dev = &card->wavefront;21482149/* returns zero if a WaveFront card is successfully detected.2150negative otherwise.2151*/21522153dev->israw = 0;2154dev->has_fx = 0;2155dev->debug = debug_default;2156dev->interrupts_are_midi = 0;2157dev->irq_cnt = 0;2158dev->rom_samples_rdonly = 1;21592160if (snd_wavefront_cmd (dev, WFC_FIRMWARE_VERSION, rbuf, wbuf) == 0) {21612162dev->fw_version[0] = rbuf[0];2163dev->fw_version[1] = rbuf[1];21642165snd_printk ("firmware %d.%d already loaded.\n",2166rbuf[0], rbuf[1]);21672168/* check that a command actually works */21692170if (snd_wavefront_cmd (dev, WFC_HARDWARE_VERSION,2171rbuf, wbuf) == 0) {2172dev->hw_version[0] = rbuf[0];2173dev->hw_version[1] = rbuf[1];2174} else {2175snd_printk ("not raw, but no "2176"hardware version!\n");2177return -1;2178}21792180if (!wf_raw) {2181return 0;2182} else {2183snd_printk ("reloading firmware as you requested.\n");2184dev->israw = 1;2185}21862187} else {21882189dev->israw = 1;2190snd_printk ("no response to firmware probe, assume raw.\n");21912192}21932194return 0;2195}21962197MODULE_FIRMWARE(DEFAULT_OSPATH);219821992200