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

📄 nm256_audio.c

📁 linux和2410结合开发 用他可以生成2410所需的zImage文件
💻 C
📖 第 1 页 / 共 3 页
字号:
/*  * Audio driver for the NeoMagic 256AV and 256ZX chipsets in native * mode, with AC97 mixer support. * * Overall design and parts of this code stolen from vidc_*.c and * skeleton.c. * * Yeah, there are a lot of magic constants in here.  You tell ME what * they are.  I just get this stuff psychically, remember?  * * This driver was written by someone who wishes to remain anonymous.  * It is in the public domain, so share and enjoy.  Try to make a profit * off of it; go on, I dare you.   * * Changes: * 11-10-2000	Bartlomiej Zolnierkiewicz <bkz@linux-ide.org> *		Added some __init * 19-04-2001	Marcus Meissner <mm@caldera.de> *		Ported to 2.4 PCI API. */#define __NO_VERSION__#include <linux/pci.h>#include <linux/init.h>#include <linux/module.h>#include <linux/pm.h>#include <linux/delay.h>#include "sound_config.h"#include "nm256.h"#include "nm256_coeff.h"int nm256_debug;static int force_load;/*  * The size of the playback reserve.  When the playback buffer has less * than NM256_PLAY_WMARK_SIZE bytes to output, we request a new * buffer. */#define NM256_PLAY_WMARK_SIZE 512static struct audio_driver nm256_audio_driver;static int nm256_grabInterrupt (struct nm256_info *card);static int nm256_releaseInterrupt (struct nm256_info *card);static void nm256_interrupt (int irq, void *dev_id, struct pt_regs *dummy);static void nm256_interrupt_zx (int irq, void *dev_id, struct pt_regs *dummy);static int handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data);/* These belong in linux/pci.h. */#define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005#define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006/* List of cards.  */static struct nm256_info *nmcard_list;/* Release the mapped-in memory for CARD.  */static voidnm256_release_ports (struct nm256_info *card){    int x;    for (x = 0; x < 2; x++) {	if (card->port[x].ptr != NULL) {	    iounmap (card->port[x].ptr);	    card->port[x].ptr = NULL;	}    }}/*  * Map in the memory ports for CARD, if they aren't already mapped in * and have been configured.  If successful, a zero value is returned; * otherwise any previously mapped-in areas are released and a non-zero * value is returned. * * This is invoked twice, once for each port.  Ideally it would only be * called once, but we now need to map in the second port in order to * check how much memory the card has on the 256ZX. */static intnm256_remap_ports (struct nm256_info *card){    int x;    for (x = 0; x < 2; x++) {	if (card->port[x].ptr == NULL && card->port[x].end_offset > 0) {	    u32 physaddr 		= card->port[x].physaddr + card->port[x].start_offset;	    u32 size 		= card->port[x].end_offset - card->port[x].start_offset;	    card->port[x].ptr = ioremap_nocache (physaddr, size);						  	    if (card->port[x].ptr == NULL) {		printk (KERN_ERR "NM256: Unable to remap port %d\n", x + 1);		nm256_release_ports (card);		return -1;	    }	}    }    return 0;}/* Locate the card in our list. */static struct nm256_info *nm256_find_card (int dev){    struct nm256_info *card;    for (card = nmcard_list; card != NULL; card = card->next_card)	if (card->dev[0] == dev || card->dev[1] == dev)	    return card;    return NULL;}/* * Ditto, but find the card struct corresponding to the mixer device DEV  * instead.  */static struct nm256_info *nm256_find_card_for_mixer (int dev){    struct nm256_info *card;    for (card = nmcard_list; card != NULL; card = card->next_card)	if (card->mixer_oss_dev == dev)	    return card;    return NULL;}static int usecache;static int buffertop;/* Check to see if we're using the bank of cached coefficients. */intnm256_cachedCoefficients (struct nm256_info *card){    return usecache;}/* The actual rates supported by the card. */static int samplerates[9] = {    8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 99999999};/* * Set the card samplerate, word size and stereo mode to correspond to * the settings in the CARD struct for the specified device in DEV. * We keep two separate sets of information, one for each device; the * hardware is not actually configured until a read or write is * attempted. */intnm256_setInfo (int dev, struct nm256_info *card){    int x;    int w;    int targetrate;    if (card->dev[0] == dev)	w = 0;    else if (card->dev[1] == dev)	w = 1;    else	return -ENODEV;    targetrate = card->sinfo[w].samplerate;    if ((card->sinfo[w].bits != 8 && card->sinfo[w].bits != 16)	|| targetrate < samplerates[0]	|| targetrate > samplerates[7])	return -EINVAL;    for (x = 0; x < 8; x++)	if (targetrate < ((samplerates[x] + samplerates[x + 1]) / 2))	    break;    if (x < 8) {	u8 ratebits = ((x << 4) & NM_RATE_MASK);	if (card->sinfo[w].bits == 16)	    ratebits |= NM_RATE_BITS_16;	if (card->sinfo[w].stereo)	    ratebits |= NM_RATE_STEREO;	card->sinfo[w].samplerate = samplerates[x];	if (card->dev_for_play == dev && card->playing) {	    if (nm256_debug)		printk (KERN_DEBUG "Setting play ratebits to 0x%x\n",			ratebits);	    nm256_loadCoefficient (card, 0, x);	    nm256_writePort8 (card, 2,			      NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET,			      ratebits);	}	if (card->dev_for_record == dev && card->recording) {	    if (nm256_debug)		printk (KERN_DEBUG "Setting record ratebits to 0x%x\n",			ratebits);	    nm256_loadCoefficient (card, 1, x);	    nm256_writePort8 (card, 2,			      NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET,			      ratebits);	}	return 0;    }    else	return -EINVAL;}/* Start the play process going. */static voidstartPlay (struct nm256_info *card){    if (! card->playing) {	card->playing = 1;	if (nm256_grabInterrupt (card) == 0) {	    nm256_setInfo (card->dev_for_play, card);	    /* Enable playback engine and interrupts. */	    nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG,			      NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);	    /* Enable both channels. */	    nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG, 0x0);	}    }}/*  * Request one chunk of AMT bytes from the recording device.  When the * operation is complete, the data will be copied into BUFFER and the * function DMAbuf_inputintr will be invoked. */static voidnm256_startRecording (struct nm256_info *card, char *buffer, u32 amt){    u32 endpos;    int enableEngine = 0;    u32 ringsize = card->recordBufferSize;    unsigned long flags;    if (amt > (ringsize / 2)) {	/*	 * Of course this won't actually work right, because the	 * caller is going to assume we will give what we got asked	 * for.	 */	printk (KERN_ERR "NM256: Read request too large: %d\n", amt);	amt = ringsize / 2;    }    if (amt < 8) {	printk (KERN_ERR "NM256: Read request too small; %d\n", amt);	return;    }    save_flags (flags);    cli ();    /*     * If we're not currently recording, set up the start and end registers     * for the recording engine.     */    if (! card->recording) {	card->recording = 1;	if (nm256_grabInterrupt (card) == 0) {	    card->curRecPos = 0;	    nm256_setInfo (card->dev_for_record, card);	    nm256_writePort32 (card, 2, NM_RBUFFER_START, card->abuf2);	    nm256_writePort32 (card, 2, NM_RBUFFER_END,				 card->abuf2 + ringsize);	    nm256_writePort32 (card, 2, NM_RBUFFER_CURRP,				 card->abuf2 + card->curRecPos);	    enableEngine = 1;	}	else {	    /* Not sure what else to do here.  */	    restore_flags (flags);	    return;	}    }    /*      * If we happen to go past the end of the buffer a bit (due to a     * delayed interrupt) it's OK.  So might as well set the watermark     * right at the end of the data we want.     */    endpos = card->abuf2 + ((card->curRecPos + amt) % ringsize);    card->recBuf = buffer;    card->requestedRecAmt = amt;    nm256_writePort32 (card, 2, NM_RBUFFER_WMARK, endpos);    /* Enable recording engine and interrupts. */    if (enableEngine)	nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG,			    NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);    restore_flags (flags);}/* Stop the play engine. */static voidstopPlay (struct nm256_info *card){    /* Shut off sound from both channels. */    nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG,		       NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT);    /* Disable play engine. */    nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG, 0);    if (card->playing) {	nm256_releaseInterrupt (card);	/* Reset the relevant state bits. */	card->playing = 0;	card->curPlayPos = 0;    }}/* Stop recording. */static voidstopRecord (struct nm256_info *card){    /* Disable recording engine. */    nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG, 0);    if (card->recording) {	nm256_releaseInterrupt (card);	card->recording = 0;	card->curRecPos = 0;    }}/* * Ring buffers, man.  That's where the hip-hop, wild-n-wooly action's at. * 1972?  (Well, I suppose it was cheep-n-easy to implement.) * * Write AMT bytes of BUFFER to the playback ring buffer, and start the * playback engine running.  It will only accept up to 1/2 of the total * size of the ring buffer.  No check is made that we're about to overwrite * the currently-playing sample. */static voidnm256_write_block (struct nm256_info *card, char *buffer, u32 amt){    u32 ringsize = card->playbackBufferSize;    u32 endstop;    unsigned long flags;    if (amt > (ringsize / 2)) {	printk (KERN_ERR "NM256: Write request too large: %d\n", amt);	amt = (ringsize / 2);    }    if (amt < NM256_PLAY_WMARK_SIZE) {	printk (KERN_ERR "NM256: Write request too small: %d\n", amt);	return;    }    card->curPlayPos %= ringsize;    card->requested_amt = amt;    save_flags (flags);    cli ();    if ((card->curPlayPos + amt) >= ringsize) {	u32 rem = ringsize - card->curPlayPos;	nm256_writeBuffer8 (card, buffer, 1,			      card->abuf1 + card->curPlayPos,			      rem);	if (amt > rem)	    nm256_writeBuffer8 (card, buffer + rem, 1, card->abuf1,				  amt - rem);    }     else	nm256_writeBuffer8 (card, buffer, 1,			      card->abuf1 + card->curPlayPos,			      amt);    /*     * Setup the start-n-stop-n-limit registers, and start that engine     * goin'.      *     * Normally we just let it wrap around to avoid the click-click     * action scene.     */    if (! card->playing) {	/* The PBUFFER_END register in this case points to one sample	   before the end of the buffer. */	int w = (card->dev_for_play == card->dev[0] ? 0 : 1);	int sampsize = (card->sinfo[w].bits == 16 ? 2 : 1);	if (card->sinfo[w].stereo)	    sampsize *= 2;	/* Need to set the not-normally-changing-registers up. */	nm256_writePort32 (card, 2, NM_PBUFFER_START,			     card->abuf1 + card->curPlayPos);	nm256_writePort32 (card, 2, NM_PBUFFER_END,			     card->abuf1 + ringsize - sampsize);	nm256_writePort32 (card, 2, NM_PBUFFER_CURRP,			     card->abuf1 + card->curPlayPos);    }    endstop = (card->curPlayPos + amt - NM256_PLAY_WMARK_SIZE) % ringsize;    nm256_writePort32 (card, 2, NM_PBUFFER_WMARK, card->abuf1 + endstop);    if (! card->playing)	startPlay (card);    restore_flags (flags);}/*  We just got a card playback interrupt; process it.  */static voidnm256_get_new_block (struct nm256_info *card){    /* Check to see how much got played so far. */    u32 amt = nm256_readPort32 (card, 2, NM_PBUFFER_CURRP) - card->abuf1;    if (amt >= card->playbackBufferSize) {	printk (KERN_ERR "NM256: Sound playback pointer invalid!\n");	amt = 0;    }    if (amt < card->curPlayPos)	amt = (card->playbackBufferSize - card->curPlayPos) + amt;    else	amt -= card->curPlayPos;    if (card->requested_amt > (amt + NM256_PLAY_WMARK_SIZE)) {	u32 endstop =	    card->curPlayPos + card->requested_amt - NM256_PLAY_WMARK_SIZE;	nm256_writePort32 (card, 2, NM_PBUFFER_WMARK, card->abuf1 + endstop);    }     else {	card->curPlayPos += card->requested_amt;	/* Get a new block to write.  This will eventually invoke	   nm256_write_block () or stopPlay ().  */	DMAbuf_outputintr (card->dev_for_play, 1);    }}/* Ultra cheez-whiz.  But I'm too lazy to grep headers. */#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))/*  * Read the last-recorded block from the ring buffer, copy it into the * saved buffer pointer, and invoke DMAuf_inputintr() with the recording * device.  */static voidnm256_read_block (struct nm256_info *card){    /* Grab the current position of the recording pointer. */    u32 currptr = nm256_readPort32 (card, 2, NM_RBUFFER_CURRP) - card->abuf2;    u32 amtToRead = card->requestedRecAmt;    u32 ringsize = card->recordBufferSize;    if (currptr >= card->recordBufferSize) {	printk (KERN_ERR "NM256: Sound buffer record pointer invalid!\n");        currptr = 0;    }    /*     * This test is probably redundant; we shouldn't be here unless     * it's true.     */    if (card->recording) {	/* If we wrapped around, copy everything from the start of our	   recording buffer to the end of the buffer. */	if (currptr < card->curRecPos) {	    u32 amt = MIN (ringsize - card->curRecPos, amtToRead);	    nm256_readBuffer8 (card, card->recBuf, 1,				 card->abuf2 + card->curRecPos,				 amt);	    amtToRead -= amt;	    card->curRecPos += amt;	    card->recBuf += amt;	    if (card->curRecPos == ringsize)		card->curRecPos = 0;	}	if ((card->curRecPos < currptr) && (amtToRead > 0)) {	    u32 amt = MIN (currptr - card->curRecPos, amtToRead);	    nm256_readBuffer8 (card, card->recBuf, 1,				 card->abuf2 + card->curRecPos, amt);	    card->curRecPos = ((card->curRecPos + amt) % ringsize);	}	card->recBuf = NULL;	card->requestedRecAmt = 0;	DMAbuf_inputintr (card->dev_for_record);    }}#undef MIN/*  * Initialize the hardware.  */static voidnm256_initHw (struct nm256_info *card){    /* Reset everything. */    nm256_writePort8 (card, 2, 0x0, 0x11);    nm256_writePort16 (card, 2, 0x214, 0);    stopRecord (card);    stopPlay (card);}/*  * Handle a potential interrupt for the device referred to by DEV_ID.  * * I don't like the cut-n-paste job here either between the two routines, * but there are sufficient differences between the two interrupt handlers * that parameterizing it isn't all that great either.  (Could use a macro, * I suppose...yucky bleah.) */static voidnm256_interrupt (int irq, void *dev_id, struct pt_regs *dummy){    struct nm256_info *card = (struct nm256_info *)dev_id;    u16 status;    static int badintrcount = 0;    if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {	printk (KERN_ERR "NM256: Bad card pointer\n");	return;    }    status = nm256_readPort16 (card, 2, NM_INT_REG);    /* Not ours. */    if (status == 0) {	if (badintrcount++ > 1000) {	    /*	     * I'm not sure if the best thing is to stop the card from	     * playing or just release the interrupt (after all, we're in	     * a bad situation, so doing fancy stuff may not be such a good	     * idea).	     *	     * I worry about the card engine continuing to play noise	     * over and over, however--that could become a very	     * obnoxious problem.  And we know that when this usually	     * happens things are fairly safe, it just means the user's	     * inserted a PCMCIA card and someone's spamming us with IRQ 9s.	     */	    if (card->playing)		stopPlay (card);	    if (card->recording)

⌨️ 快捷键说明

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