2 * Audio driver for the NeoMagic 256AV and 256ZX chipsets in native
3 * mode, with AC97 mixer support.
5 * Overall design and parts of this code stolen from vidc_*.c and
8 * Yeah, there are a lot of magic constants in here. You tell ME what
9 * they are. I just get this stuff psychically, remember?
11 * This driver was written by someone who wishes to remain anonymous.
12 * It is in the public domain, so share and enjoy. Try to make a profit
13 * off of it; go on, I dare you.
16 * 11-10-2000 Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
18 * 19-04-2001 Marcus Meissner <mm@caldera.de>
19 * Ported to 2.4 PCI API.
22 #define __NO_VERSION__
23 #include <linux/pci.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
27 #include <linux/delay.h>
28 #include "sound_config.h"
30 #include "nm256_coeff.h"
33 static int force_load;
36 * The size of the playback reserve. When the playback buffer has less
37 * than NM256_PLAY_WMARK_SIZE bytes to output, we request a new
40 #define NM256_PLAY_WMARK_SIZE 512
42 static struct audio_driver nm256_audio_driver;
44 static int nm256_grabInterrupt (struct nm256_info *card);
45 static int nm256_releaseInterrupt (struct nm256_info *card);
46 static void nm256_interrupt (int irq, void *dev_id, struct pt_regs *dummy);
47 static void nm256_interrupt_zx (int irq, void *dev_id, struct pt_regs *dummy);
48 static int handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data);
50 /* These belong in linux/pci.h. */
51 #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
52 #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
55 static struct nm256_info *nmcard_list;
57 /* Release the mapped-in memory for CARD. */
59 nm256_release_ports (struct nm256_info *card)
63 for (x = 0; x < 2; x++) {
64 if (card->port[x].ptr != NULL) {
65 iounmap (card->port[x].ptr);
66 card->port[x].ptr = NULL;
72 * Map in the memory ports for CARD, if they aren't already mapped in
73 * and have been configured. If successful, a zero value is returned;
74 * otherwise any previously mapped-in areas are released and a non-zero
77 * This is invoked twice, once for each port. Ideally it would only be
78 * called once, but we now need to map in the second port in order to
79 * check how much memory the card has on the 256ZX.
82 nm256_remap_ports (struct nm256_info *card)
86 for (x = 0; x < 2; x++) {
87 if (card->port[x].ptr == NULL && card->port[x].end_offset > 0) {
89 = card->port[x].physaddr + card->port[x].start_offset;
91 = card->port[x].end_offset - card->port[x].start_offset;
93 card->port[x].ptr = ioremap_nocache (physaddr, size);
95 if (card->port[x].ptr == NULL) {
96 printk (KERN_ERR "NM256: Unable to remap port %d\n", x + 1);
97 nm256_release_ports (card);
105 /* Locate the card in our list. */
106 static struct nm256_info *
107 nm256_find_card (int dev)
109 struct nm256_info *card;
111 for (card = nmcard_list; card != NULL; card = card->next_card)
112 if (card->dev[0] == dev || card->dev[1] == dev)
119 * Ditto, but find the card struct corresponding to the mixer device DEV
122 static struct nm256_info *
123 nm256_find_card_for_mixer (int dev)
125 struct nm256_info *card;
127 for (card = nmcard_list; card != NULL; card = card->next_card)
128 if (card->mixer_oss_dev == dev)
135 static int buffertop;
137 /* Check to see if we're using the bank of cached coefficients. */
139 nm256_cachedCoefficients (struct nm256_info *card)
144 /* The actual rates supported by the card. */
145 static int samplerates[9] = {
146 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 99999999
150 * Set the card samplerate, word size and stereo mode to correspond to
151 * the settings in the CARD struct for the specified device in DEV.
152 * We keep two separate sets of information, one for each device; the
153 * hardware is not actually configured until a read or write is
158 nm256_setInfo (int dev, struct nm256_info *card)
164 if (card->dev[0] == dev)
166 else if (card->dev[1] == dev)
171 targetrate = card->sinfo[w].samplerate;
173 if ((card->sinfo[w].bits != 8 && card->sinfo[w].bits != 16)
174 || targetrate < samplerates[0]
175 || targetrate > samplerates[7])
178 for (x = 0; x < 8; x++)
179 if (targetrate < ((samplerates[x] + samplerates[x + 1]) / 2))
183 u8 ratebits = ((x << 4) & NM_RATE_MASK);
184 if (card->sinfo[w].bits == 16)
185 ratebits |= NM_RATE_BITS_16;
186 if (card->sinfo[w].stereo)
187 ratebits |= NM_RATE_STEREO;
189 card->sinfo[w].samplerate = samplerates[x];
192 if (card->dev_for_play == dev && card->playing) {
194 printk (KERN_DEBUG "Setting play ratebits to 0x%x\n",
196 nm256_loadCoefficient (card, 0, x);
197 nm256_writePort8 (card, 2,
198 NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET,
202 if (card->dev_for_record == dev && card->recording) {
204 printk (KERN_DEBUG "Setting record ratebits to 0x%x\n",
206 nm256_loadCoefficient (card, 1, x);
207 nm256_writePort8 (card, 2,
208 NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET,
217 /* Start the play process going. */
219 startPlay (struct nm256_info *card)
221 if (! card->playing) {
223 if (nm256_grabInterrupt (card) == 0) {
224 nm256_setInfo (card->dev_for_play, card);
226 /* Enable playback engine and interrupts. */
227 nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG,
228 NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);
230 /* Enable both channels. */
231 nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG, 0x0);
237 * Request one chunk of AMT bytes from the recording device. When the
238 * operation is complete, the data will be copied into BUFFER and the
239 * function DMAbuf_inputintr will be invoked.
243 nm256_startRecording (struct nm256_info *card, char *buffer, u32 amt)
246 int enableEngine = 0;
247 u32 ringsize = card->recordBufferSize;
250 if (amt > (ringsize / 2)) {
252 * Of course this won't actually work right, because the
253 * caller is going to assume we will give what we got asked
256 printk (KERN_ERR "NM256: Read request too large: %d\n", amt);
261 printk (KERN_ERR "NM256: Read request too small; %d\n", amt);
268 * If we're not currently recording, set up the start and end registers
269 * for the recording engine.
271 if (! card->recording) {
273 if (nm256_grabInterrupt (card) == 0) {
275 nm256_setInfo (card->dev_for_record, card);
276 nm256_writePort32 (card, 2, NM_RBUFFER_START, card->abuf2);
277 nm256_writePort32 (card, 2, NM_RBUFFER_END,
278 card->abuf2 + ringsize);
280 nm256_writePort32 (card, 2, NM_RBUFFER_CURRP,
281 card->abuf2 + card->curRecPos);
285 /* Not sure what else to do here. */
286 restore_flags (flags);
292 * If we happen to go past the end of the buffer a bit (due to a
293 * delayed interrupt) it's OK. So might as well set the watermark
294 * right at the end of the data we want.
296 endpos = card->abuf2 + ((card->curRecPos + amt) % ringsize);
298 card->recBuf = buffer;
299 card->requestedRecAmt = amt;
300 nm256_writePort32 (card, 2, NM_RBUFFER_WMARK, endpos);
301 /* Enable recording engine and interrupts. */
303 nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG,
304 NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
306 restore_flags (flags);
309 /* Stop the play engine. */
311 stopPlay (struct nm256_info *card)
313 /* Shut off sound from both channels. */
314 nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG,
315 NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT);
316 /* Disable play engine. */
317 nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG, 0);
319 nm256_releaseInterrupt (card);
321 /* Reset the relevant state bits. */
323 card->curPlayPos = 0;
327 /* Stop recording. */
329 stopRecord (struct nm256_info *card)
331 /* Disable recording engine. */
332 nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG, 0);
334 if (card->recording) {
335 nm256_releaseInterrupt (card);
343 * Ring buffers, man. That's where the hip-hop, wild-n-wooly action's at.
344 * 1972? (Well, I suppose it was cheep-n-easy to implement.)
346 * Write AMT bytes of BUFFER to the playback ring buffer, and start the
347 * playback engine running. It will only accept up to 1/2 of the total
348 * size of the ring buffer. No check is made that we're about to overwrite
349 * the currently-playing sample.
353 nm256_write_block (struct nm256_info *card, char *buffer, u32 amt)
355 u32 ringsize = card->playbackBufferSize;
359 if (amt > (ringsize / 2)) {
360 printk (KERN_ERR "NM256: Write request too large: %d\n", amt);
361 amt = (ringsize / 2);
364 if (amt < NM256_PLAY_WMARK_SIZE) {
365 printk (KERN_ERR "NM256: Write request too small: %d\n", amt);
369 card->curPlayPos %= ringsize;
371 card->requested_amt = amt;
376 if ((card->curPlayPos + amt) >= ringsize) {
377 u32 rem = ringsize - card->curPlayPos;
379 nm256_writeBuffer8 (card, buffer, 1,
380 card->abuf1 + card->curPlayPos,
383 nm256_writeBuffer8 (card, buffer + rem, 1, card->abuf1,
387 nm256_writeBuffer8 (card, buffer, 1,
388 card->abuf1 + card->curPlayPos,
392 * Setup the start-n-stop-n-limit registers, and start that engine
395 * Normally we just let it wrap around to avoid the click-click
398 if (! card->playing) {
399 /* The PBUFFER_END register in this case points to one sample
400 before the end of the buffer. */
401 int w = (card->dev_for_play == card->dev[0] ? 0 : 1);
402 int sampsize = (card->sinfo[w].bits == 16 ? 2 : 1);
404 if (card->sinfo[w].stereo)
407 /* Need to set the not-normally-changing-registers up. */
408 nm256_writePort32 (card, 2, NM_PBUFFER_START,
409 card->abuf1 + card->curPlayPos);
410 nm256_writePort32 (card, 2, NM_PBUFFER_END,
411 card->abuf1 + ringsize - sampsize);
412 nm256_writePort32 (card, 2, NM_PBUFFER_CURRP,
413 card->abuf1 + card->curPlayPos);
415 endstop = (card->curPlayPos + amt - NM256_PLAY_WMARK_SIZE) % ringsize;
416 nm256_writePort32 (card, 2, NM_PBUFFER_WMARK, card->abuf1 + endstop);
421 restore_flags (flags);
424 /* We just got a card playback interrupt; process it. */
426 nm256_get_new_block (struct nm256_info *card)
428 /* Check to see how much got played so far. */
429 u32 amt = nm256_readPort32 (card, 2, NM_PBUFFER_CURRP) - card->abuf1;
431 if (amt >= card->playbackBufferSize) {
432 printk (KERN_ERR "NM256: Sound playback pointer invalid!\n");
436 if (amt < card->curPlayPos)
437 amt = (card->playbackBufferSize - card->curPlayPos) + amt;
439 amt -= card->curPlayPos;
441 if (card->requested_amt > (amt + NM256_PLAY_WMARK_SIZE)) {
443 card->curPlayPos + card->requested_amt - NM256_PLAY_WMARK_SIZE;
444 nm256_writePort32 (card, 2, NM_PBUFFER_WMARK, card->abuf1 + endstop);
447 card->curPlayPos += card->requested_amt;
448 /* Get a new block to write. This will eventually invoke
449 nm256_write_block () or stopPlay (). */
450 DMAbuf_outputintr (card->dev_for_play, 1);
454 /* Ultra cheez-whiz. But I'm too lazy to grep headers. */
455 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
458 * Read the last-recorded block from the ring buffer, copy it into the
459 * saved buffer pointer, and invoke DMAuf_inputintr() with the recording
464 nm256_read_block (struct nm256_info *card)
466 /* Grab the current position of the recording pointer. */
467 u32 currptr = nm256_readPort32 (card, 2, NM_RBUFFER_CURRP) - card->abuf2;
468 u32 amtToRead = card->requestedRecAmt;
469 u32 ringsize = card->recordBufferSize;
471 if (currptr >= card->recordBufferSize) {
472 printk (KERN_ERR "NM256: Sound buffer record pointer invalid!\n");
477 * This test is probably redundant; we shouldn't be here unless
480 if (card->recording) {
481 /* If we wrapped around, copy everything from the start of our
482 recording buffer to the end of the buffer. */
483 if (currptr < card->curRecPos) {
484 u32 amt = MIN (ringsize - card->curRecPos, amtToRead);
486 nm256_readBuffer8 (card, card->recBuf, 1,
487 card->abuf2 + card->curRecPos,
490 card->curRecPos += amt;
492 if (card->curRecPos == ringsize)
496 if ((card->curRecPos < currptr) && (amtToRead > 0)) {
497 u32 amt = MIN (currptr - card->curRecPos, amtToRead);
498 nm256_readBuffer8 (card, card->recBuf, 1,
499 card->abuf2 + card->curRecPos, amt);
500 card->curRecPos = ((card->curRecPos + amt) % ringsize);
503 card->requestedRecAmt = 0;
504 DMAbuf_inputintr (card->dev_for_record);
510 * Initialize the hardware.
513 nm256_initHw (struct nm256_info *card)
515 /* Reset everything. */
516 nm256_writePort8 (card, 2, 0x0, 0x11);
517 nm256_writePort16 (card, 2, 0x214, 0);
524 * Handle a potential interrupt for the device referred to by DEV_ID.
526 * I don't like the cut-n-paste job here either between the two routines,
527 * but there are sufficient differences between the two interrupt handlers
528 * that parameterizing it isn't all that great either. (Could use a macro,
529 * I suppose...yucky bleah.)
533 nm256_interrupt (int irq, void *dev_id, struct pt_regs *dummy)
535 struct nm256_info *card = (struct nm256_info *)dev_id;
537 static int badintrcount = 0;
539 if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
540 printk (KERN_ERR "NM256: Bad card pointer\n");
544 status = nm256_readPort16 (card, 2, NM_INT_REG);
548 if (badintrcount++ > 1000) {
550 * I'm not sure if the best thing is to stop the card from
551 * playing or just release the interrupt (after all, we're in
552 * a bad situation, so doing fancy stuff may not be such a good
555 * I worry about the card engine continuing to play noise
556 * over and over, however--that could become a very
557 * obnoxious problem. And we know that when this usually
558 * happens things are fairly safe, it just means the user's
559 * inserted a PCMCIA card and someone's spamming us with IRQ 9s.
573 /* Rather boring; check for individual interrupts and process them. */
575 if (status & NM_PLAYBACK_INT) {
576 status &= ~NM_PLAYBACK_INT;
577 NM_ACK_INT (card, NM_PLAYBACK_INT);
580 nm256_get_new_block (card);
583 if (status & NM_RECORD_INT) {
584 status &= ~NM_RECORD_INT;
585 NM_ACK_INT (card, NM_RECORD_INT);
588 nm256_read_block (card);
591 if (status & NM_MISC_INT_1) {
594 status &= ~NM_MISC_INT_1;
595 printk (KERN_ERR "NM256: Got misc interrupt #1\n");
596 NM_ACK_INT (card, NM_MISC_INT_1);
597 nm256_writePort16 (card, 2, NM_INT_REG, 0x8000);
598 cbyte = nm256_readPort8 (card, 2, 0x400);
599 nm256_writePort8 (card, 2, 0x400, cbyte | 2);
602 if (status & NM_MISC_INT_2) {
605 status &= ~NM_MISC_INT_2;
606 printk (KERN_ERR "NM256: Got misc interrupt #2\n");
607 NM_ACK_INT (card, NM_MISC_INT_2);
608 cbyte = nm256_readPort8 (card, 2, 0x400);
609 nm256_writePort8 (card, 2, 0x400, cbyte & ~2);
612 /* Unknown interrupt. */
614 printk (KERN_ERR "NM256: Fire in the hole! Unknown status 0x%x\n",
617 NM_ACK_INT (card, status);
622 * Handle a potential interrupt for the device referred to by DEV_ID.
623 * This handler is for the 256ZX, and is very similar to the non-ZX
628 nm256_interrupt_zx (int irq, void *dev_id, struct pt_regs *dummy)
630 struct nm256_info *card = (struct nm256_info *)dev_id;
632 static int badintrcount = 0;
634 if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
635 printk (KERN_ERR "NM256: Bad card pointer\n");
639 status = nm256_readPort32 (card, 2, NM_INT_REG);
643 if (badintrcount++ > 1000) {
644 printk (KERN_ERR "NM256: Releasing interrupt, over 1000 invalid interrupts\n");
646 * I'm not sure if the best thing is to stop the card from
647 * playing or just release the interrupt (after all, we're in
648 * a bad situation, so doing fancy stuff may not be such a good
651 * I worry about the card engine continuing to play noise
652 * over and over, however--that could become a very
653 * obnoxious problem. And we know that when this usually
654 * happens things are fairly safe, it just means the user's
655 * inserted a PCMCIA card and someone's spamming us with
670 /* Rather boring; check for individual interrupts and process them. */
672 if (status & NM2_PLAYBACK_INT) {
673 status &= ~NM2_PLAYBACK_INT;
674 NM2_ACK_INT (card, NM2_PLAYBACK_INT);
677 nm256_get_new_block (card);
680 if (status & NM2_RECORD_INT) {
681 status &= ~NM2_RECORD_INT;
682 NM2_ACK_INT (card, NM2_RECORD_INT);
685 nm256_read_block (card);
688 if (status & NM2_MISC_INT_1) {
691 status &= ~NM2_MISC_INT_1;
692 printk (KERN_ERR "NM256: Got misc interrupt #1\n");
693 NM2_ACK_INT (card, NM2_MISC_INT_1);
694 cbyte = nm256_readPort8 (card, 2, 0x400);
695 nm256_writePort8 (card, 2, 0x400, cbyte | 2);
698 if (status & NM2_MISC_INT_2) {
701 status &= ~NM2_MISC_INT_2;
702 printk (KERN_ERR "NM256: Got misc interrupt #2\n");
703 NM2_ACK_INT (card, NM2_MISC_INT_2);
704 cbyte = nm256_readPort8 (card, 2, 0x400);
705 nm256_writePort8 (card, 2, 0x400, cbyte & ~2);
708 /* Unknown interrupt. */
710 printk (KERN_ERR "NM256: Fire in the hole! Unknown status 0x%x\n",
713 NM2_ACK_INT (card, status);
718 * Request our interrupt.
721 nm256_grabInterrupt (struct nm256_info *card)
723 if (card->has_irq++ == 0) {
724 if (request_irq (card->irq, card->introutine, SA_SHIRQ,
725 "NM256_audio", card) < 0) {
726 printk (KERN_ERR "NM256: can't obtain IRQ %d\n", card->irq);
734 * Release our interrupt.
737 nm256_releaseInterrupt (struct nm256_info *card)
739 if (card->has_irq <= 0) {
740 printk (KERN_ERR "nm256: too many calls to releaseInterrupt\n");
744 if (card->has_irq == 0) {
745 free_irq (card->irq, card);
751 * Waits for the mixer to become ready to be written; returns a zero value
756 nm256_isReady (struct ac97_hwint *dev)
758 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
764 if (card->magsig != NM_MAGIC_SIG) {
765 printk (KERN_ERR "NM256: Bad magic signature in isReady!\n");
769 testaddr = card->mixer_status_offset;
770 testb = card->mixer_status_mask;
773 * Loop around waiting for the mixer to become ready.
775 while (! done && t2-- > 0) {
776 if ((nm256_readPort16 (card, 2, testaddr) & testb) == 0)
785 * Return the contents of the AC97 mixer register REG. Returns a positive
786 * value if successful, or a negative error code.
789 nm256_readAC97Reg (struct ac97_hwint *dev, u8 reg)
791 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
793 if (card->magsig != NM_MAGIC_SIG) {
794 printk (KERN_ERR "NM256: Bad magic signature in readAC97Reg!\n");
802 res = nm256_readPort16 (card, 2, card->mixer + reg);
803 /* Magic delay. Bleah yucky. */
812 * Writes VALUE to AC97 mixer register REG. Returns 0 if successful, or
813 * a negative error code.
816 nm256_writeAC97Reg (struct ac97_hwint *dev, u8 reg, u16 value)
823 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
825 if (card->magsig != NM_MAGIC_SIG) {
826 printk (KERN_ERR "NM256: Bad magic signature in writeAC97Reg!\n");
837 /* Wait for the write to take, too. */
838 while ((tries-- > 0) && !done) {
839 nm256_writePort16 (card, 2, base + reg, value);
840 if (nm256_isReady (dev)) {
847 restore_flags (flags);
854 * Initial register values to be written to the AC97 mixer.
855 * While most of these are identical to the reset values, we do this
856 * so that we have most of the register contents cached--this avoids
857 * reading from the mixer directly (which seems to be problematic,
858 * probably due to ignorance).
863 unsigned short value;
866 static struct initialValues nm256_ac97_initial_values[] =
868 { AC97_MASTER_VOL_STEREO, 0x8000 },
869 { AC97_HEADPHONE_VOL, 0x8000 },
870 { AC97_MASTER_VOL_MONO, 0x0000 },
871 { AC97_PCBEEP_VOL, 0x0000 },
872 { AC97_PHONE_VOL, 0x0008 },
873 { AC97_MIC_VOL, 0x8000 },
874 { AC97_LINEIN_VOL, 0x8808 },
875 { AC97_CD_VOL, 0x8808 },
876 { AC97_VIDEO_VOL, 0x8808 },
877 { AC97_AUX_VOL, 0x8808 },
878 { AC97_PCMOUT_VOL, 0x0808 },
879 { AC97_RECORD_SELECT, 0x0000 },
880 { AC97_RECORD_GAIN, 0x0B0B },
881 { AC97_GENERAL_PURPOSE, 0x0000 },
885 /* Initialize the AC97 into a known state. */
887 nm256_resetAC97 (struct ac97_hwint *dev)
889 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
892 if (card->magsig != NM_MAGIC_SIG) {
893 printk (KERN_ERR "NM256: Bad magic signature in resetAC97!\n");
897 /* Reset the mixer. 'Tis magic! */
898 nm256_writePort8 (card, 2, 0x6c0, 1);
899 // nm256_writePort8 (card, 2, 0x6cc, 0x87); /* This crashes Dell latitudes */
900 nm256_writePort8 (card, 2, 0x6cc, 0x80);
901 nm256_writePort8 (card, 2, 0x6cc, 0x0);
903 if (! card->mixer_values_init) {
904 for (x = 0; nm256_ac97_initial_values[x].port != 0xffff; x++) {
905 ac97_put_register (dev,
906 nm256_ac97_initial_values[x].port,
907 nm256_ac97_initial_values[x].value);
908 card->mixer_values_init = 1;
916 * We don't do anything particularly special here; it just passes the
917 * mixer ioctl to the AC97 driver.
920 nm256_default_mixer_ioctl (int dev, unsigned int cmd, caddr_t arg)
922 struct nm256_info *card = nm256_find_card_for_mixer (dev);
924 return ac97_mixer_ioctl (&(card->mdev), cmd, arg);
929 static struct mixer_operations nm256_mixer_operations = {
932 name: "NM256AC97Mixer",
933 ioctl: nm256_default_mixer_ioctl
937 * Default settings for the OSS mixer. These are set last, after the
938 * mixer is initialized.
940 * I "love" C sometimes. Got braces?
942 static struct ac97_mixer_value_list mixer_defaults[] = {
943 { SOUND_MIXER_VOLUME, { { 85, 85 } } },
944 { SOUND_MIXER_SPEAKER, { { 100 } } },
945 { SOUND_MIXER_PCM, { { 65, 65 } } },
946 { SOUND_MIXER_CD, { { 65, 65 } } },
951 /* Installs the AC97 mixer into CARD. */
953 nm256_install_mixer (struct nm256_info *card)
957 card->mdev.reset_device = nm256_resetAC97;
958 card->mdev.read_reg = nm256_readAC97Reg;
959 card->mdev.write_reg = nm256_writeAC97Reg;
960 card->mdev.driver_private = (void *)card;
962 if (ac97_init (&(card->mdev)))
965 mixer = sound_alloc_mixerdev();
966 if (num_mixers >= MAX_MIXER_DEV) {
967 printk ("NM256 mixer: Unable to alloc mixerdev\n");
971 mixer_devs[mixer] = &nm256_mixer_operations;
972 card->mixer_oss_dev = mixer;
974 /* Some reasonable default values. */
975 ac97_set_values (&(card->mdev), mixer_defaults);
977 printk(KERN_INFO "Initialized AC97 mixer\n");
981 /* Perform a full reset on the hardware; this is invoked when an APM
982 resume event occurs. */
984 nm256_full_reset (struct nm256_info *card)
987 ac97_reset (&(card->mdev));
991 * See if the signature left by the NM256 BIOS is intact; if so, we use
992 * the associated address as the end of our audio buffer in the video
997 nm256_peek_for_sig (struct nm256_info *card)
1000 = card->port[0].physaddr + card->port[0].end_offset - 0x0400;
1001 /* The signature is located 1K below the end of video RAM. */
1002 char *temp = ioremap_nocache (port1offset, 16);
1003 /* Default buffer end is 5120 bytes below the top of RAM. */
1004 u32 default_value = card->port[0].end_offset - 0x1400;
1007 /* Install the default value first, so we don't have to repeatedly
1008 do it if there is a problem. */
1009 card->port[0].end_offset = default_value;
1012 printk (KERN_ERR "NM256: Unable to scan for card signature in video RAM\n");
1016 if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1017 u32 pointer = readl (temp + 4);
1020 * If it's obviously invalid, don't use it (the port already has a
1021 * suitable default value set).
1023 if (pointer != 0xffffffff)
1024 card->port[0].end_offset = pointer;
1026 printk (KERN_INFO "NM256: Found card signature in video RAM: 0x%x\n",
1034 * Install a driver for the PCI device referenced by PCIDEV.
1035 * VERSTR is a human-readable version string.
1039 nm256_install(struct pci_dev *pcidev, enum nm256rev rev, char *verstr)
1041 struct nm256_info *card;
1042 struct pm_dev *pmdev;
1045 if (pci_enable_device(pcidev))
1048 card = kmalloc (sizeof (struct nm256_info), GFP_KERNEL);
1050 printk (KERN_ERR "NM256: out of memory!\n");
1054 card->magsig = NM_MAGIC_SIG;
1056 card->recording = 0;
1059 /* Init the memory port info. */
1060 for (x = 0; x < 2; x++) {
1061 card->port[x].physaddr = pci_resource_start (pcidev, x);
1062 card->port[x].ptr = NULL;
1063 card->port[x].start_offset = 0;
1064 card->port[x].end_offset = 0;
1067 /* Port 2 is easy. */
1068 card->port[1].start_offset = 0;
1069 card->port[1].end_offset = NM_PORT2_SIZE;
1071 /* Yuck. But we have to map in port 2 so we can check how much RAM the
1073 if (nm256_remap_ports (card)) {
1079 * The NM256 has two memory ports. The first port is nothing
1080 * more than a chunk of video RAM, which is used as the I/O ring
1081 * buffer. The second port has the actual juicy stuff (like the
1082 * mixer and the playback engine control registers).
1085 if (card->rev == REV_NM256AV) {
1086 /* Ok, try to see if this is a non-AC97 version of the hardware. */
1087 int pval = nm256_readPort16 (card, 2, NM_MIXER_PRESENCE);
1088 if ((pval & NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) {
1090 printk (KERN_ERR "NM256: This doesn't look to me like the AC97-compatible version.\n");
1091 printk (KERN_ERR " You can force the driver to load by passing in the module\n");
1092 printk (KERN_ERR " parameter:\n");
1093 printk (KERN_ERR " force_ac97 = 1\n");
1094 printk (KERN_ERR "\n");
1095 printk (KERN_ERR " More likely, you should be using the appropriate SB-16 or\n");
1096 printk (KERN_ERR " CS4232 driver instead. (If your BIOS has settings for\n");
1097 printk (KERN_ERR " IRQ and/or DMA for the sound card, this is *not* the correct\n");
1098 printk (KERN_ERR " driver to use.)\n");
1099 nm256_release_ports (card);
1104 printk (KERN_INFO "NM256: Forcing driver load as per user request.\n");
1108 /* printk (KERN_INFO "NM256: Congratulations. You're not running Eunice.\n")*/;
1110 card->port[0].end_offset = 2560 * 1024;
1111 card->introutine = nm256_interrupt;
1112 card->mixer_status_offset = NM_MIXER_STATUS_OFFSET;
1113 card->mixer_status_mask = NM_MIXER_READY_MASK;
1116 /* Not sure if there is any relevant detect for the ZX or not. */
1117 if (nm256_readPort8 (card, 2, 0xa0b) != 0)
1118 card->port[0].end_offset = 6144 * 1024;
1120 card->port[0].end_offset = 4096 * 1024;
1122 card->introutine = nm256_interrupt_zx;
1123 card->mixer_status_offset = NM2_MIXER_STATUS_OFFSET;
1124 card->mixer_status_mask = NM2_MIXER_READY_MASK;
1127 if (buffertop >= 98304 && buffertop < card->port[0].end_offset)
1128 card->port[0].end_offset = buffertop;
1130 nm256_peek_for_sig (card);
1132 card->port[0].start_offset = card->port[0].end_offset - 98304;
1134 printk (KERN_INFO "NM256: Mapping port 1 from 0x%x - 0x%x\n",
1135 card->port[0].start_offset, card->port[0].end_offset);
1137 if (nm256_remap_ports (card)) {
1142 /* See if we can get the interrupt. */
1144 card->irq = pcidev->irq;
1147 if (nm256_grabInterrupt (card) != 0) {
1148 nm256_release_ports (card);
1153 nm256_releaseInterrupt (card);
1159 card->playbackBufferSize = 16384;
1160 card->recordBufferSize = 16384;
1162 card->coeffBuf = card->port[0].end_offset - NM_MAX_COEFFICIENT;
1163 card->abuf2 = card->coeffBuf - card->recordBufferSize;
1164 card->abuf1 = card->abuf2 - card->playbackBufferSize;
1165 card->allCoeffBuf = card->abuf2 - (NM_TOTAL_COEFF_COUNT * 4);
1167 /* Fixed setting. */
1168 card->mixer = NM_MIXER_OFFSET;
1169 card->mixer_values_init = 0;
1171 card->is_open_play = 0;
1172 card->is_open_record = 0;
1174 card->coeffsCurrent = 0;
1176 card->opencnt[0] = 0; card->opencnt[1] = 0;
1178 /* Reasonable default settings, but largely unnecessary. */
1179 for (x = 0; x < 2; x++) {
1180 card->sinfo[x].bits = 8;
1181 card->sinfo[x].stereo = 0;
1182 card->sinfo[x].samplerate = 8000;
1185 nm256_initHw (card);
1187 for (x = 0; x < 2; x++) {
1189 sound_install_audiodrv(AUDIO_DRIVER_VERSION,
1190 "NM256", &nm256_audio_driver,
1191 sizeof(struct audio_driver),
1192 DMA_NODMA, AFMT_U8 | AFMT_S16_LE,
1193 NULL, -1, -1)) >= 0) {
1194 /* 1K minimum buffer size. */
1195 audio_devs[card->dev[x]]->min_fragment = 10;
1196 /* Maximum of 8K buffer size. */
1197 audio_devs[card->dev[x]]->max_fragment = 13;
1200 printk(KERN_ERR "NM256: Too many PCM devices available\n");
1201 nm256_release_ports (card);
1207 pci_set_drvdata(pcidev,card);
1209 /* Insert the card in the list. */
1210 card->next_card = nmcard_list;
1213 printk(KERN_INFO "Initialized NeoMagic %s audio in PCI native mode\n",
1217 * And our mixer. (We should allow support for other mixers, maybe.)
1220 nm256_install_mixer (card);
1222 pmdev = pm_register(PM_PCI_DEV, PM_PCI_ID(pcidev), handle_pm_event);
1231 * PM event handler, so the card is properly reinitialized after a power
1235 handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data)
1237 struct nm256_info *crd = (struct nm256_info*) dev->data;
1244 int playing = crd->playing;
1245 nm256_full_reset (crd);
1247 * A little ugly, but that's ok; pretend the
1248 * block we were playing is done.
1251 DMAbuf_outputintr (crd->dev_for_play, 1);
1259 static int __devinit
1260 nm256_probe(struct pci_dev *pcidev,const struct pci_device_id *pciid)
1262 if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO)
1263 return nm256_install(pcidev, REV_NM256AV, "256AV");
1264 if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO)
1265 return nm256_install(pcidev, REV_NM256ZX, "256ZX");
1266 return -1; /* should not come here ... */
1269 static void __devinit
1270 nm256_remove(struct pci_dev *pcidev) {
1271 struct nm256_info *xcard = pci_get_drvdata(pcidev);
1272 struct nm256_info *card,*next_card = NULL;
1274 for (card = nmcard_list; card != NULL; card = next_card) {
1275 next_card = card->next_card;
1276 if (card == xcard) {
1280 free_irq (card->irq, card);
1281 nm256_release_ports (card);
1282 sound_unload_mixerdev (card->mixer_oss_dev);
1283 sound_unload_audiodev (card->dev[0]);
1284 sound_unload_audiodev (card->dev[1]);
1289 if (nmcard_list == card)
1290 nmcard_list = next_card;
1297 * MODE - mode to open device (logical OR of OPEN_READ and OPEN_WRITE)
1299 * Called when opening the DMAbuf (dmabuf.c:259)
1302 nm256_audio_open(int dev, int mode)
1304 struct nm256_info *card = nm256_find_card (dev);
1310 if (card->dev[0] == dev)
1312 else if (card->dev[1] == dev)
1317 if (card->opencnt[w] > 0)
1320 /* No bits set? Huh? */
1321 if (! ((mode & OPEN_READ) || (mode & OPEN_WRITE)))
1325 * If it's open for both read and write, and the card's currently
1326 * being read or written to, then do the opposite of what has
1327 * already been done. Otherwise, don't specify any mode until the
1328 * user actually tries to do I/O. (Some programs open the device
1329 * for both read and write, but only actually do reading or writing.)
1332 if ((mode & OPEN_WRITE) && (mode & OPEN_READ)) {
1333 if (card->is_open_play)
1335 else if (card->is_open_record)
1340 if (mode & OPEN_WRITE) {
1341 if (card->is_open_play == 0) {
1342 card->dev_for_play = dev;
1343 card->is_open_play = 1;
1349 if (mode & OPEN_READ) {
1350 if (card->is_open_record == 0) {
1351 card->dev_for_record = dev;
1352 card->is_open_record = 1;
1367 * Called when closing the DMAbuf (dmabuf.c:477)
1371 nm256_audio_close(int dev)
1373 struct nm256_info *card = nm256_find_card (dev);
1378 if (card->dev[0] == dev)
1380 else if (card->dev[1] == dev)
1386 if (card->opencnt[w] <= 0) {
1387 card->opencnt[w] = 0;
1389 if (card->dev_for_play == dev) {
1391 card->is_open_play = 0;
1392 card->dev_for_play = -1;
1395 if (card->dev_for_record == dev) {
1397 card->is_open_record = 0;
1398 card->dev_for_record = -1;
1404 /* Standard ioctl handler. */
1406 nm256_audio_ioctl(int dev, unsigned int cmd, caddr_t arg)
1412 struct nm256_info *card = nm256_find_card (dev);
1417 if (dev == card->dev[0])
1423 * The code here is messy. There are probably better ways to do
1424 * it. (It should be possible to handle it the same way the AC97 mixer
1429 case SOUND_PCM_WRITE_RATE:
1430 if (get_user(ret, (int *) arg))
1434 oldinfo = card->sinfo[w].samplerate;
1435 card->sinfo[w].samplerate = ret;
1436 ret = nm256_setInfo(dev, card);
1438 card->sinfo[w].samplerate = oldinfo;
1441 ret = card->sinfo[w].samplerate;
1444 case SOUND_PCM_READ_RATE:
1445 ret = card->sinfo[w].samplerate;
1448 case SNDCTL_DSP_STEREO:
1449 if (get_user(ret, (int *) arg))
1452 card->sinfo[w].stereo = ret ? 1 : 0;
1453 ret = nm256_setInfo (dev, card);
1455 ret = card->sinfo[w].stereo;
1459 case SOUND_PCM_WRITE_CHANNELS:
1460 if (get_user(ret, (int *) arg))
1463 if (ret < 1 || ret > 3)
1464 ret = card->sinfo[w].stereo + 1;
1466 card->sinfo[w].stereo = ret - 1;
1467 ret = nm256_setInfo (dev, card);
1469 ret = card->sinfo[w].stereo + 1;
1473 case SOUND_PCM_READ_CHANNELS:
1474 ret = card->sinfo[w].stereo + 1;
1477 case SNDCTL_DSP_SETFMT:
1478 if (get_user(ret, (int *) arg))
1482 oldinfo = card->sinfo[w].bits;
1483 card->sinfo[w].bits = ret;
1484 ret = nm256_setInfo (dev, card);
1486 card->sinfo[w].bits = oldinfo;
1489 ret = card->sinfo[w].bits;
1492 case SOUND_PCM_READ_BITS:
1493 ret = card->sinfo[w].bits;
1499 return put_user(ret, (int *) arg);
1503 * Given the sound device DEV and an associated physical buffer PHYSBUF,
1504 * return a pointer to the actual buffer in kernel space.
1506 * This routine should exist as part of the soundcore routines.
1510 nm256_getDMAbuffer (int dev, unsigned long physbuf)
1512 struct audio_operations *adev = audio_devs[dev];
1513 struct dma_buffparms *dmap = adev->dmap_out;
1515 (char *)(physbuf - (unsigned long)dmap->raw_buf_phys
1516 + (unsigned long)dmap->raw_buf);
1523 * Output a block to sound device
1525 * dev - device number
1526 * buf - physical address of buffer
1527 * total_count - total byte count in buffer
1528 * intrflag - set if this has been called from an interrupt
1529 * (via DMAbuf_outputintr)
1530 * restart_dma - set if engine needs to be re-initialised
1533 * 1. Starting output (dmabuf.c:1327)
1534 * 2. (dmabuf.c:1504)
1535 * 3. A new buffer needs to be sent to the device (dmabuf.c:1579)
1538 nm256_audio_output_block(int dev, unsigned long physbuf,
1539 int total_count, int intrflag)
1541 struct nm256_info *card = nm256_find_card (dev);
1544 char *dma_buf = nm256_getDMAbuffer (dev, physbuf);
1545 card->is_open_play = 1;
1546 card->dev_for_play = dev;
1547 nm256_write_block (card, dma_buf, total_count);
1551 /* Ditto, but do recording instead. */
1553 nm256_audio_start_input(int dev, unsigned long physbuf, int count,
1556 struct nm256_info *card = nm256_find_card (dev);
1559 char *dma_buf = nm256_getDMAbuffer (dev, physbuf);
1560 card->is_open_record = 1;
1561 card->dev_for_record = dev;
1562 nm256_startRecording (card, dma_buf, count);
1567 * Prepare for inputting samples to DEV.
1568 * Each requested buffer will be BSIZE byes long, with a total of
1573 nm256_audio_prepare_for_input(int dev, int bsize, int bcount)
1575 struct nm256_info *card = nm256_find_card (dev);
1580 if (card->is_open_record && card->dev_for_record != dev)
1583 audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
1588 * Prepare for outputting samples to `dev'
1590 * Each buffer that will be passed will be `bsize' bytes long,
1591 * with a total of `bcount' buffers.
1594 * 1. A trigger enables audio output (dmabuf.c:978)
1595 * 2. We get a write buffer without dma_mode setup (dmabuf.c:1152)
1596 * 3. We restart a transfer (dmabuf.c:1324)
1600 nm256_audio_prepare_for_output(int dev, int bsize, int bcount)
1602 struct nm256_info *card = nm256_find_card (dev);
1607 if (card->is_open_play && card->dev_for_play != dev)
1610 audio_devs[dev]->dmap_out->flags |= DMA_NODMA;
1614 /* Stop the current operations associated with DEV. */
1616 nm256_audio_reset(int dev)
1618 struct nm256_info *card = nm256_find_card (dev);
1621 if (card->dev_for_play == dev)
1623 if (card->dev_for_record == dev)
1629 nm256_audio_local_qlen(int dev)
1634 static struct audio_driver nm256_audio_driver =
1637 open: nm256_audio_open,
1638 close: nm256_audio_close,
1639 output_block: nm256_audio_output_block,
1640 start_input: nm256_audio_start_input,
1641 ioctl: nm256_audio_ioctl,
1642 prepare_for_input: nm256_audio_prepare_for_input,
1643 prepare_for_output:nm256_audio_prepare_for_output,
1644 halt_io: nm256_audio_reset,
1645 local_qlen: nm256_audio_local_qlen,
1648 static struct pci_device_id nm256_pci_tbl[] __devinitdata = {
1649 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO,
1650 PCI_ANY_ID, PCI_ANY_ID, 0, 0},
1651 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO,
1652 PCI_ANY_ID, PCI_ANY_ID, 0, 0},
1655 MODULE_DEVICE_TABLE(pci, nm256_pci_tbl);
1656 MODULE_LICENSE("GPL");
1659 struct pci_driver nm256_pci_driver = {
1661 id_table:nm256_pci_tbl,
1663 remove:nm256_remove,
1666 MODULE_PARM (usecache, "i");
1667 MODULE_PARM (buffertop, "i");
1668 MODULE_PARM (nm256_debug, "i");
1669 MODULE_PARM (force_load, "i");
1671 static int __init do_init_nm256(void)
1673 printk (KERN_INFO "NeoMagic 256AV/256ZX audio driver, version 1.1p\n");
1674 return pci_module_init(&nm256_pci_driver);
1677 static void __exit cleanup_nm256 (void)
1679 pci_unregister_driver(&nm256_pci_driver);
1680 pm_unregister_all (&handle_pm_event);
1683 module_init(do_init_nm256);
1684 module_exit(cleanup_nm256);