⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wavefront_synth.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 4 页
字号:
/* Copyright (C) by Paul Barton-Davis 1998-1999 * * Some portions of this file are taken from work that is * copyright (C) by Hannu Savolainen 1993-1996 * * This program is distributed under the GNU GENERAL PUBLIC LICENSE (GPL) * Version 2 (June 1991). See the "COPYING" file distributed with this software * for more info.   *//*   * An ALSA lowlevel driver for Turtle Beach ICS2115 wavetable synth *                                             (Maui, Tropez, Tropez Plus) * * This driver supports the onboard wavetable synthesizer (an ICS2115), * including patch, sample and program loading and unloading, conversion * of GUS patches during loading, and full user-level access to all * WaveFront commands. It tries to provide semi-intelligent patch and * sample management as well. * */#include <sound/driver.h>#include <asm/io.h>#include <linux/interrupt.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/time.h>#include <linux/wait.h>#include <linux/moduleparam.h>#include <sound/core.h>#include <sound/snd_wavefront.h>#include <sound/initval.h>static int wf_raw = 0; /* we normally check for "raw state" to firmware			  loading. if non-zero, then during driver loading, the			  state of the board is ignored, and we reset the			  board and load the firmware anyway.		       */		   static int fx_raw = 1; /* if this is zero, we'll leave the FX processor in			  whatever state it is when the driver is loaded.			  The default is to download the microprogram and			  associated coefficients to set it up for "default"			  operation, whatever that means.		       */static int debug_default = 0;  /* you can set this to control debugging				  during driver loading. it takes any combination				  of the WF_DEBUG_* flags defined in				  wavefront.h			       *//* XXX this needs to be made firmware and hardware version dependent */static char *ospath = "/etc/sound/wavefront.os"; /* where to find a processed						    version of the WaveFront OS						 */static int wait_usecs = 150; /* This magic number seems to give pretty optimal				throughput based on my limited experimentation.				If you want to play around with it and find a better				value, be my guest. Remember, the idea is to				get a number that causes us to just busy wait				for as many WaveFront commands as possible, without				coming up with a number so large that we hog the				whole CPU.				Specifically, with this number, out of about 134,000				status waits, only about 250 result in a sleep.			    */static int sleep_interval = 100;   /* HZ/sleep_interval seconds per sleep */static int sleep_tries = 50;       /* number of times we'll try to sleep */static int reset_time = 2;        /* hundreths of a second we wait after a HW				     reset for the expected interrupt.				  */static int ramcheck_time = 20;    /* time in seconds to wait while ROM code				     checks on-board RAM.				  */static int osrun_time = 10;       /* time in seconds we wait for the OS to				     start running.				  */module_param(wf_raw, int, 0444);MODULE_PARM_DESC(wf_raw, "if non-zero, assume that we need to boot the OS");module_param(fx_raw, int, 0444);MODULE_PARM_DESC(fx_raw, "if non-zero, assume that the FX process needs help");module_param(debug_default, int, 0444);MODULE_PARM_DESC(debug_default, "debug parameters for card initialization");module_param(wait_usecs, int, 0444);MODULE_PARM_DESC(wait_usecs, "how long to wait without sleeping, usecs");module_param(sleep_interval, int, 0444);MODULE_PARM_DESC(sleep_interval, "how long to sleep when waiting for reply");module_param(sleep_tries, int, 0444);MODULE_PARM_DESC(sleep_tries, "how many times to try sleeping during a wait");module_param(ospath, charp, 0444);MODULE_PARM_DESC(ospath, "full pathname to processed ICS2115 OS firmware");module_param(reset_time, int, 0444);MODULE_PARM_DESC(reset_time, "how long to wait for a reset to take effect");module_param(ramcheck_time, int, 0444);MODULE_PARM_DESC(ramcheck_time, "how many seconds to wait for the RAM test");module_param(osrun_time, int, 0444);MODULE_PARM_DESC(osrun_time, "how many seconds to wait for the ICS2115 OS");/* if WF_DEBUG not defined, no run-time debugging messages will   be available via the debug flag setting. Given the current   beta state of the driver, this will remain set until a future    version.*/#define WF_DEBUG 1#ifdef WF_DEBUG#if defined(NEW_MACRO_VARARGS) || __GNUC__ >= 3#define DPRINT(cond, ...) \       if ((dev->debug & (cond)) == (cond)) { \	     snd_printk (__VA_ARGS__); \       }#else#define DPRINT(cond, args...) \       if ((dev->debug & (cond)) == (cond)) { \	     snd_printk (args); \       }#endif#else#define DPRINT(cond, args...)#endif /* WF_DEBUG */#define LOGNAME "WaveFront: "/* bitmasks for WaveFront status port value */#define STAT_RINTR_ENABLED	0x01#define STAT_CAN_READ		0x02#define STAT_INTR_READ		0x04#define STAT_WINTR_ENABLED	0x10#define STAT_CAN_WRITE		0x20#define STAT_INTR_WRITE		0x40static int wavefront_delete_sample (snd_wavefront_t *, int sampnum);static int wavefront_find_free_sample (snd_wavefront_t *);typedef struct {	int cmd;	char *action;	unsigned int read_cnt;	unsigned int write_cnt;	int need_ack;} wavefront_command;static struct {	int errno;	const char *errstr;} wavefront_errors[] = {	{ 0x01, "Bad sample number" },	{ 0x02, "Out of sample memory" },	{ 0x03, "Bad patch number" },	{ 0x04, "Error in number of voices" },	{ 0x06, "Sample load already in progress" },	{ 0x0B, "No sample load request pending" },	{ 0x0E, "Bad MIDI channel number" },	{ 0x10, "Download Record Error" },	{ 0x80, "Success" },	{ 0x0 }};#define NEEDS_ACK 1static wavefront_command wavefront_commands[] = {	{ WFC_SET_SYNTHVOL, "set synthesizer volume", 0, 1, NEEDS_ACK },	{ WFC_GET_SYNTHVOL, "get synthesizer volume", 1, 0, 0},	{ WFC_SET_NVOICES, "set number of voices", 0, 1, NEEDS_ACK },	{ WFC_GET_NVOICES, "get number of voices", 1, 0, 0 },	{ WFC_SET_TUNING, "set synthesizer tuning", 0, 2, NEEDS_ACK },	{ WFC_GET_TUNING, "get synthesizer tuning", 2, 0, 0 },	{ WFC_DISABLE_CHANNEL, "disable synth channel", 0, 1, NEEDS_ACK },	{ WFC_ENABLE_CHANNEL, "enable synth channel", 0, 1, NEEDS_ACK },	{ WFC_GET_CHANNEL_STATUS, "get synth channel status", 3, 0, 0 },	{ WFC_MISYNTH_OFF, "disable midi-in to synth", 0, 0, NEEDS_ACK },	{ WFC_MISYNTH_ON, "enable midi-in to synth", 0, 0, NEEDS_ACK },	{ WFC_VMIDI_ON, "enable virtual midi mode", 0, 0, NEEDS_ACK },	{ WFC_VMIDI_OFF, "disable virtual midi mode", 0, 0, NEEDS_ACK },	{ WFC_MIDI_STATUS, "report midi status", 1, 0, 0 },	{ WFC_FIRMWARE_VERSION, "report firmware version", 2, 0, 0 },	{ WFC_HARDWARE_VERSION, "report hardware version", 2, 0, 0 },	{ WFC_GET_NSAMPLES, "report number of samples", 2, 0, 0 },	{ WFC_INSTOUT_LEVELS, "report instantaneous output levels", 7, 0, 0 },	{ WFC_PEAKOUT_LEVELS, "report peak output levels", 7, 0, 0 },	{ WFC_DOWNLOAD_SAMPLE, "download sample",	  0, WF_SAMPLE_BYTES, NEEDS_ACK },	{ WFC_DOWNLOAD_BLOCK, "download block", 0, 0, NEEDS_ACK},	{ WFC_DOWNLOAD_SAMPLE_HEADER, "download sample header",	  0, WF_SAMPLE_HDR_BYTES, NEEDS_ACK },	{ WFC_UPLOAD_SAMPLE_HEADER, "upload sample header", 13, 2, 0 },	/* This command requires a variable number of bytes to be written.	   There is a hack in snd_wavefront_cmd() to support this. The actual	   count is passed in as the read buffer ptr, cast appropriately.	   Ugh.	*/	{ WFC_DOWNLOAD_MULTISAMPLE, "download multisample", 0, 0, NEEDS_ACK },	/* This one is a hack as well. We just read the first byte of the	   response, don't fetch an ACK, and leave the rest to the 	   calling function. Ugly, ugly, ugly.	*/	{ WFC_UPLOAD_MULTISAMPLE, "upload multisample", 2, 1, 0 },	{ WFC_DOWNLOAD_SAMPLE_ALIAS, "download sample alias",	  0, WF_ALIAS_BYTES, NEEDS_ACK },	{ WFC_UPLOAD_SAMPLE_ALIAS, "upload sample alias", WF_ALIAS_BYTES, 2, 0},	{ WFC_DELETE_SAMPLE, "delete sample", 0, 2, NEEDS_ACK },	{ WFC_IDENTIFY_SAMPLE_TYPE, "identify sample type", 5, 2, 0 },	{ WFC_UPLOAD_SAMPLE_PARAMS, "upload sample parameters" },	{ WFC_REPORT_FREE_MEMORY, "report free memory", 4, 0, 0 },	{ WFC_DOWNLOAD_PATCH, "download patch", 0, 134, NEEDS_ACK },	{ WFC_UPLOAD_PATCH, "upload patch", 132, 2, 0 },	{ WFC_DOWNLOAD_PROGRAM, "download program", 0, 33, NEEDS_ACK },	{ WFC_UPLOAD_PROGRAM, "upload program", 32, 1, 0 },	{ WFC_DOWNLOAD_EDRUM_PROGRAM, "download enhanced drum program", 0, 9,	  NEEDS_ACK},	{ WFC_UPLOAD_EDRUM_PROGRAM, "upload enhanced drum program", 8, 1, 0},	{ WFC_SET_EDRUM_CHANNEL, "set enhanced drum program channel",	  0, 1, NEEDS_ACK },	{ WFC_DISABLE_DRUM_PROGRAM, "disable drum program", 0, 1, NEEDS_ACK },	{ WFC_REPORT_CHANNEL_PROGRAMS, "report channel program numbers",	  32, 0, 0 },	{ WFC_NOOP, "the no-op command", 0, 0, NEEDS_ACK },	{ 0x00 }};static const char *wavefront_errorstr (int errnum){	int i;	for (i = 0; wavefront_errors[i].errstr; i++) {		if (wavefront_errors[i].errno == errnum) {			return wavefront_errors[i].errstr;		}	}	return "Unknown WaveFront error";}static wavefront_command *wavefront_get_command (int cmd) {	int i;	for (i = 0; wavefront_commands[i].cmd != 0; i++) {		if (cmd == wavefront_commands[i].cmd) {			return &wavefront_commands[i];		}	}	return (wavefront_command *) 0;}static inline intwavefront_status (snd_wavefront_t *dev) {	return inb (dev->status_port);}static intwavefront_sleep (int limit){	schedule_timeout_interruptible(limit);	return signal_pending(current);}static intwavefront_wait (snd_wavefront_t *dev, int mask){	int             i;	/* Spin for a short period of time, because >99% of all	   requests to the WaveFront can be serviced inline like this.	*/	for (i = 0; i < wait_usecs; i += 5) {		if (wavefront_status (dev) & mask) {			return 1;		}		udelay(5);	}	for (i = 0; i < sleep_tries; i++) {		if (wavefront_status (dev) & mask) {			return 1;		}		if (wavefront_sleep (HZ/sleep_interval)) {			return (0);		}	}	return (0);}static intwavefront_read (snd_wavefront_t *dev){	if (wavefront_wait (dev, STAT_CAN_READ))		return inb (dev->data_port);	DPRINT (WF_DEBUG_DATA, "read timeout.\n");	return -1;}static intwavefront_write (snd_wavefront_t *dev, unsigned char data){	if (wavefront_wait (dev, STAT_CAN_WRITE)) {		outb (data, dev->data_port);		return 0;	}	DPRINT (WF_DEBUG_DATA, "write timeout.\n");	return -1;}intsnd_wavefront_cmd (snd_wavefront_t *dev, 		   int cmd, unsigned char *rbuf, unsigned char *wbuf){	int ack;	unsigned int i;	int c;	wavefront_command *wfcmd;	if ((wfcmd = wavefront_get_command (cmd)) == (wavefront_command *) 0) {		snd_printk ("command 0x%x not supported.\n",			cmd);		return 1;	}	/* Hack to handle the one variable-size write command. See	   wavefront_send_multisample() for the other half of this	   gross and ugly strategy.	*/	if (cmd == WFC_DOWNLOAD_MULTISAMPLE) {		wfcmd->write_cnt = (unsigned long) rbuf;		rbuf = NULL;	}	DPRINT (WF_DEBUG_CMD, "0x%x [%s] (%d,%d,%d)\n",			       cmd, wfcmd->action, wfcmd->read_cnt,			       wfcmd->write_cnt, wfcmd->need_ack);    	if (wavefront_write (dev, cmd)) { 		DPRINT ((WF_DEBUG_IO|WF_DEBUG_CMD), "cannot request "						     "0x%x [%s].\n",						     cmd, wfcmd->action);		return 1;	} 	if (wfcmd->write_cnt > 0) {		DPRINT (WF_DEBUG_DATA, "writing %d bytes "					"for 0x%x\n",					wfcmd->write_cnt, cmd);		for (i = 0; i < wfcmd->write_cnt; i++) {			if (wavefront_write (dev, wbuf[i])) {				DPRINT (WF_DEBUG_IO, "bad write for byte "						      "%d of 0x%x [%s].\n",						      i, cmd, wfcmd->action);				return 1;			}			DPRINT (WF_DEBUG_DATA, "write[%d] = 0x%x\n",						i, wbuf[i]);		}	}	if (wfcmd->read_cnt > 0) {		DPRINT (WF_DEBUG_DATA, "reading %d ints "					"for 0x%x\n",					wfcmd->read_cnt, cmd);		for (i = 0; i < wfcmd->read_cnt; i++) {			if ((c = wavefront_read (dev)) == -1) {				DPRINT (WF_DEBUG_IO, "bad read for byte "						      "%d of 0x%x [%s].\n",						      i, cmd, wfcmd->action);				return 1;			}			/* Now handle errors. Lots of special cases here */	    			if (c == 0xff) { 				if ((c = wavefront_read (dev)) == -1) {					DPRINT (WF_DEBUG_IO, "bad read for "							      "error byte at "							      "read byte %d "							      "of 0x%x [%s].\n",							      i, cmd,							      wfcmd->action);					return 1;				}				/* Can you believe this madness ? */				if (c == 1 &&				    wfcmd->cmd == WFC_IDENTIFY_SAMPLE_TYPE) {					rbuf[0] = WF_ST_EMPTY;					return (0);				} else if (c == 3 &&					   wfcmd->cmd == WFC_UPLOAD_PATCH) {					return 3;				} else if (c == 1 &&					   wfcmd->cmd == WFC_UPLOAD_PROGRAM) {					return 1;				} else {					DPRINT (WF_DEBUG_IO, "error %d (%s) "							      "during "							      "read for byte "							      "%d of 0x%x "							      "[%s].\n",							      c,							      wavefront_errorstr (c),							      i, cmd,							      wfcmd->action);					return 1;				}				} else {				rbuf[i] = c;			}						DPRINT (WF_DEBUG_DATA, "read[%d] = 0x%x\n",i, rbuf[i]);		}	}		if ((wfcmd->read_cnt == 0 && wfcmd->write_cnt == 0) || wfcmd->need_ack) {		DPRINT (WF_DEBUG_CMD, "reading ACK for 0x%x\n", cmd);		/* Some commands need an ACK, but return zero instead		   of the standard value.		*/	    		if ((ack = wavefront_read (dev)) == 0) {			ack = WF_ACK;		}			if (ack != WF_ACK) {			if (ack == -1) {				DPRINT (WF_DEBUG_IO, "cannot read ack for "						      "0x%x [%s].\n",						      cmd, wfcmd->action);				return 1;					} else {				int err = -1; /* something unknown */				if (ack == 0xff) { /* explicit error */		    					if ((err = wavefront_read (dev)) == -1) {						DPRINT (WF_DEBUG_DATA,							"cannot read err "							"for 0x%x [%s].\n",							cmd, wfcmd->action);					}				}								DPRINT (WF_DEBUG_IO, "0x%x [%s] "					"failed (0x%x, 0x%x, %s)\n",					cmd, wfcmd->action, ack, err,					wavefront_errorstr (err));								return -err;			}		}				DPRINT (WF_DEBUG_DATA, "ack received "					"for 0x%x [%s]\n",					cmd, wfcmd->action);	} else {		DPRINT (WF_DEBUG_CMD, "0x%x [%s] does not need "				       "ACK (%d,%d,%d)\n",				       cmd, wfcmd->action, wfcmd->read_cnt,				       wfcmd->write_cnt, wfcmd->need_ack);	}	return 0;	}/***********************************************************************WaveFront data munging   Things here are weird. All data written to the board cannot have its most significant bit set. Any data item with values potentially > 0x7F (127) must be split across multiple bytes.Sometimes, we need to munge numeric values that are represented onthe x86 side as 8-32 bit values. Sometimes, we need to munge datathat is represented on the x86 side as an array of bytes. The mostefficient approach to handling both cases seems to be to use 2different functions for munging and 2 for de-munging. This avoidsweird casting and worrying about bit-level offsets.**********************************************************************/static unsigned char *munge_int32 (unsigned int src,	     unsigned char *dst,	     unsigned int dst_size){	unsigned int i;	for (i = 0; i < dst_size; i++) {		*dst = src & 0x7F;  /* Mask high bit of LSB */		src = src >> 7;     /* Rotate Right 7 bits  */	                            /* Note: we leave the upper bits in place */ 		dst++; 	};	return dst;};static int demunge_int32 (unsigned char* src, int src_size){	int i; 	int outval = 0;	 	for (i = src_size - 1; i >= 0; i--) {		outval=(outval<<7)+src[i];	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -