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 #include <linux/pci.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
28 #include <linux/pm_legacy.h>
29 #include <linux/delay.h>
30 #include <linux/spinlock.h>
31 #include "sound_config.h"
33 static int nm256_debug;
34 static int force_load;
37 #include "nm256_coeff.h"
40 * The size of the playback reserve. When the playback buffer has less
41 * than NM256_PLAY_WMARK_SIZE bytes to output, we request a new
44 #define NM256_PLAY_WMARK_SIZE 512
46 static struct audio_driver nm256_audio_driver;
48 static int nm256_grabInterrupt (struct nm256_info *card);
49 static int nm256_releaseInterrupt (struct nm256_info *card);
50 static irqreturn_t nm256_interrupt (int irq, void *dev_id, struct pt_regs *dummy);
51 static irqreturn_t nm256_interrupt_zx (int irq, void *dev_id, struct pt_regs *dummy);
52 static int handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data);
54 /* These belong in linux/pci.h. */
55 #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
56 #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
57 #define PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO 0x8016
60 static struct nm256_info *nmcard_list;
62 /* Release the mapped-in memory for CARD. */
64 nm256_release_ports (struct nm256_info *card)
68 for (x = 0; x < 2; x++) {
69 if (card->port[x].ptr != NULL) {
70 iounmap (card->port[x].ptr);
71 card->port[x].ptr = NULL;
77 * Map in the memory ports for CARD, if they aren't already mapped in
78 * and have been configured. If successful, a zero value is returned;
79 * otherwise any previously mapped-in areas are released and a non-zero
82 * This is invoked twice, once for each port. Ideally it would only be
83 * called once, but we now need to map in the second port in order to
84 * check how much memory the card has on the 256ZX.
87 nm256_remap_ports (struct nm256_info *card)
91 for (x = 0; x < 2; x++) {
92 if (card->port[x].ptr == NULL && card->port[x].end_offset > 0) {
94 = card->port[x].physaddr + card->port[x].start_offset;
96 = card->port[x].end_offset - card->port[x].start_offset;
98 card->port[x].ptr = ioremap_nocache (physaddr, size);
100 if (card->port[x].ptr == NULL) {
101 printk (KERN_ERR "NM256: Unable to remap port %d\n", x + 1);
102 nm256_release_ports (card);
110 /* Locate the card in our list. */
111 static struct nm256_info *
112 nm256_find_card (int dev)
114 struct nm256_info *card;
116 for (card = nmcard_list; card != NULL; card = card->next_card)
117 if (card->dev[0] == dev || card->dev[1] == dev)
124 * Ditto, but find the card struct corresponding to the mixer device DEV
127 static struct nm256_info *
128 nm256_find_card_for_mixer (int dev)
130 struct nm256_info *card;
132 for (card = nmcard_list; card != NULL; card = card->next_card)
133 if (card->mixer_oss_dev == dev)
140 static int buffertop;
142 /* Check to see if we're using the bank of cached coefficients. */
144 nm256_cachedCoefficients (struct nm256_info *card)
149 /* The actual rates supported by the card. */
150 static int samplerates[9] = {
151 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 99999999
155 * Set the card samplerate, word size and stereo mode to correspond to
156 * the settings in the CARD struct for the specified device in DEV.
157 * We keep two separate sets of information, one for each device; the
158 * hardware is not actually configured until a read or write is
163 nm256_setInfo (int dev, struct nm256_info *card)
169 if (card->dev[0] == dev)
171 else if (card->dev[1] == dev)
176 targetrate = card->sinfo[w].samplerate;
178 if ((card->sinfo[w].bits != 8 && card->sinfo[w].bits != 16)
179 || targetrate < samplerates[0]
180 || targetrate > samplerates[7])
183 for (x = 0; x < 8; x++)
184 if (targetrate < ((samplerates[x] + samplerates[x + 1]) / 2))
188 u8 ratebits = ((x << 4) & NM_RATE_MASK);
189 if (card->sinfo[w].bits == 16)
190 ratebits |= NM_RATE_BITS_16;
191 if (card->sinfo[w].stereo)
192 ratebits |= NM_RATE_STEREO;
194 card->sinfo[w].samplerate = samplerates[x];
197 if (card->dev_for_play == dev && card->playing) {
199 printk (KERN_DEBUG "Setting play ratebits to 0x%x\n",
201 nm256_loadCoefficient (card, 0, x);
202 nm256_writePort8 (card, 2,
203 NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET,
207 if (card->dev_for_record == dev && card->recording) {
209 printk (KERN_DEBUG "Setting record ratebits to 0x%x\n",
211 nm256_loadCoefficient (card, 1, x);
212 nm256_writePort8 (card, 2,
213 NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET,
222 /* Start the play process going. */
224 startPlay (struct nm256_info *card)
226 if (! card->playing) {
228 if (nm256_grabInterrupt (card) == 0) {
229 nm256_setInfo (card->dev_for_play, card);
231 /* Enable playback engine and interrupts. */
232 nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG,
233 NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);
235 /* Enable both channels. */
236 nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG, 0x0);
242 * Request one chunk of AMT bytes from the recording device. When the
243 * operation is complete, the data will be copied into BUFFER and the
244 * function DMAbuf_inputintr will be invoked.
248 nm256_startRecording (struct nm256_info *card, char *buffer, u32 amt)
251 int enableEngine = 0;
252 u32 ringsize = card->recordBufferSize;
255 if (amt > (ringsize / 2)) {
257 * Of course this won't actually work right, because the
258 * caller is going to assume we will give what we got asked
261 printk (KERN_ERR "NM256: Read request too large: %d\n", amt);
266 printk (KERN_ERR "NM256: Read request too small; %d\n", amt);
270 spin_lock_irqsave(&card->lock,flags);
272 * If we're not currently recording, set up the start and end registers
273 * for the recording engine.
275 if (! card->recording) {
277 if (nm256_grabInterrupt (card) == 0) {
279 nm256_setInfo (card->dev_for_record, card);
280 nm256_writePort32 (card, 2, NM_RBUFFER_START, card->abuf2);
281 nm256_writePort32 (card, 2, NM_RBUFFER_END,
282 card->abuf2 + ringsize);
284 nm256_writePort32 (card, 2, NM_RBUFFER_CURRP,
285 card->abuf2 + card->curRecPos);
289 /* Not sure what else to do here. */
290 spin_unlock_irqrestore(&card->lock,flags);
296 * If we happen to go past the end of the buffer a bit (due to a
297 * delayed interrupt) it's OK. So might as well set the watermark
298 * right at the end of the data we want.
300 endpos = card->abuf2 + ((card->curRecPos + amt) % ringsize);
302 card->recBuf = buffer;
303 card->requestedRecAmt = amt;
304 nm256_writePort32 (card, 2, NM_RBUFFER_WMARK, endpos);
305 /* Enable recording engine and interrupts. */
307 nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG,
308 NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
310 spin_unlock_irqrestore(&card->lock,flags);
313 /* Stop the play engine. */
315 stopPlay (struct nm256_info *card)
317 /* Shut off sound from both channels. */
318 nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG,
319 NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT);
320 /* Disable play engine. */
321 nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG, 0);
323 nm256_releaseInterrupt (card);
325 /* Reset the relevant state bits. */
327 card->curPlayPos = 0;
331 /* Stop recording. */
333 stopRecord (struct nm256_info *card)
335 /* Disable recording engine. */
336 nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG, 0);
338 if (card->recording) {
339 nm256_releaseInterrupt (card);
347 * Ring buffers, man. That's where the hip-hop, wild-n-wooly action's at.
348 * 1972? (Well, I suppose it was cheep-n-easy to implement.)
350 * Write AMT bytes of BUFFER to the playback ring buffer, and start the
351 * playback engine running. It will only accept up to 1/2 of the total
352 * size of the ring buffer. No check is made that we're about to overwrite
353 * the currently-playing sample.
357 nm256_write_block (struct nm256_info *card, char *buffer, u32 amt)
359 u32 ringsize = card->playbackBufferSize;
363 if (amt > (ringsize / 2)) {
364 printk (KERN_ERR "NM256: Write request too large: %d\n", amt);
365 amt = (ringsize / 2);
368 if (amt < NM256_PLAY_WMARK_SIZE) {
369 printk (KERN_ERR "NM256: Write request too small: %d\n", amt);
373 card->curPlayPos %= ringsize;
375 card->requested_amt = amt;
377 spin_lock_irqsave(&card->lock,flags);
379 if ((card->curPlayPos + amt) >= ringsize) {
380 u32 rem = ringsize - card->curPlayPos;
382 nm256_writeBuffer8 (card, buffer, 1,
383 card->abuf1 + card->curPlayPos,
386 nm256_writeBuffer8 (card, buffer + rem, 1, card->abuf1,
390 nm256_writeBuffer8 (card, buffer, 1,
391 card->abuf1 + card->curPlayPos,
395 * Setup the start-n-stop-n-limit registers, and start that engine
398 * Normally we just let it wrap around to avoid the click-click
401 if (! card->playing) {
402 /* The PBUFFER_END register in this case points to one sample
403 before the end of the buffer. */
404 int w = (card->dev_for_play == card->dev[0] ? 0 : 1);
405 int sampsize = (card->sinfo[w].bits == 16 ? 2 : 1);
407 if (card->sinfo[w].stereo)
410 /* Need to set the not-normally-changing-registers up. */
411 nm256_writePort32 (card, 2, NM_PBUFFER_START,
412 card->abuf1 + card->curPlayPos);
413 nm256_writePort32 (card, 2, NM_PBUFFER_END,
414 card->abuf1 + ringsize - sampsize);
415 nm256_writePort32 (card, 2, NM_PBUFFER_CURRP,
416 card->abuf1 + card->curPlayPos);
418 endstop = (card->curPlayPos + amt - NM256_PLAY_WMARK_SIZE) % ringsize;
419 nm256_writePort32 (card, 2, NM_PBUFFER_WMARK, card->abuf1 + endstop);
424 spin_unlock_irqrestore(&card->lock,flags);
427 /* We just got a card playback interrupt; process it. */
429 nm256_get_new_block (struct nm256_info *card)
431 /* Check to see how much got played so far. */
432 u32 amt = nm256_readPort32 (card, 2, NM_PBUFFER_CURRP) - card->abuf1;
434 if (amt >= card->playbackBufferSize) {
435 printk (KERN_ERR "NM256: Sound playback pointer invalid!\n");
439 if (amt < card->curPlayPos)
440 amt = (card->playbackBufferSize - card->curPlayPos) + amt;
442 amt -= card->curPlayPos;
444 if (card->requested_amt > (amt + NM256_PLAY_WMARK_SIZE)) {
446 card->curPlayPos + card->requested_amt - NM256_PLAY_WMARK_SIZE;
447 nm256_writePort32 (card, 2, NM_PBUFFER_WMARK, card->abuf1 + endstop);
450 card->curPlayPos += card->requested_amt;
451 /* Get a new block to write. This will eventually invoke
452 nm256_write_block () or stopPlay (). */
453 DMAbuf_outputintr (card->dev_for_play, 1);
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);
509 * Initialize the hardware.
512 nm256_initHw (struct nm256_info *card)
514 /* Reset everything. */
515 nm256_writePort8 (card, 2, 0x0, 0x11);
516 nm256_writePort16 (card, 2, 0x214, 0);
523 * Handle a potential interrupt for the device referred to by DEV_ID.
525 * I don't like the cut-n-paste job here either between the two routines,
526 * but there are sufficient differences between the two interrupt handlers
527 * that parameterizing it isn't all that great either. (Could use a macro,
528 * I suppose...yucky bleah.)
532 nm256_interrupt (int irq, void *dev_id, struct pt_regs *dummy)
534 struct nm256_info *card = (struct nm256_info *)dev_id;
536 static int badintrcount;
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.
569 return IRQ_RETVAL(handled);
574 /* Rather boring; check for individual interrupts and process them. */
576 if (status & NM_PLAYBACK_INT) {
578 status &= ~NM_PLAYBACK_INT;
579 NM_ACK_INT (card, NM_PLAYBACK_INT);
582 nm256_get_new_block (card);
585 if (status & NM_RECORD_INT) {
587 status &= ~NM_RECORD_INT;
588 NM_ACK_INT (card, NM_RECORD_INT);
591 nm256_read_block (card);
594 if (status & NM_MISC_INT_1) {
598 status &= ~NM_MISC_INT_1;
599 printk (KERN_ERR "NM256: Got misc interrupt #1\n");
600 NM_ACK_INT (card, NM_MISC_INT_1);
601 nm256_writePort16 (card, 2, NM_INT_REG, 0x8000);
602 cbyte = nm256_readPort8 (card, 2, 0x400);
603 nm256_writePort8 (card, 2, 0x400, cbyte | 2);
606 if (status & NM_MISC_INT_2) {
610 status &= ~NM_MISC_INT_2;
611 printk (KERN_ERR "NM256: Got misc interrupt #2\n");
612 NM_ACK_INT (card, NM_MISC_INT_2);
613 cbyte = nm256_readPort8 (card, 2, 0x400);
614 nm256_writePort8 (card, 2, 0x400, cbyte & ~2);
617 /* Unknown interrupt. */
620 printk (KERN_ERR "NM256: Fire in the hole! Unknown status 0x%x\n",
623 NM_ACK_INT (card, status);
625 return IRQ_RETVAL(handled);
629 * Handle a potential interrupt for the device referred to by DEV_ID.
630 * This handler is for the 256ZX, and is very similar to the non-ZX
635 nm256_interrupt_zx (int irq, void *dev_id, struct pt_regs *dummy)
637 struct nm256_info *card = (struct nm256_info *)dev_id;
639 static int badintrcount;
642 if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
643 printk (KERN_ERR "NM256: Bad card pointer\n");
647 status = nm256_readPort32 (card, 2, NM_INT_REG);
651 if (badintrcount++ > 1000) {
652 printk (KERN_ERR "NM256: Releasing interrupt, over 1000 invalid interrupts\n");
654 * I'm not sure if the best thing is to stop the card from
655 * playing or just release the interrupt (after all, we're in
656 * a bad situation, so doing fancy stuff may not be such a good
659 * I worry about the card engine continuing to play noise
660 * over and over, however--that could become a very
661 * obnoxious problem. And we know that when this usually
662 * happens things are fairly safe, it just means the user's
663 * inserted a PCMCIA card and someone's spamming us with
674 return IRQ_RETVAL(handled);
679 /* Rather boring; check for individual interrupts and process them. */
681 if (status & NM2_PLAYBACK_INT) {
683 status &= ~NM2_PLAYBACK_INT;
684 NM2_ACK_INT (card, NM2_PLAYBACK_INT);
687 nm256_get_new_block (card);
690 if (status & NM2_RECORD_INT) {
692 status &= ~NM2_RECORD_INT;
693 NM2_ACK_INT (card, NM2_RECORD_INT);
696 nm256_read_block (card);
699 if (status & NM2_MISC_INT_1) {
703 status &= ~NM2_MISC_INT_1;
704 printk (KERN_ERR "NM256: Got misc interrupt #1\n");
705 NM2_ACK_INT (card, NM2_MISC_INT_1);
706 cbyte = nm256_readPort8 (card, 2, 0x400);
707 nm256_writePort8 (card, 2, 0x400, cbyte | 2);
710 if (status & NM2_MISC_INT_2) {
714 status &= ~NM2_MISC_INT_2;
715 printk (KERN_ERR "NM256: Got misc interrupt #2\n");
716 NM2_ACK_INT (card, NM2_MISC_INT_2);
717 cbyte = nm256_readPort8 (card, 2, 0x400);
718 nm256_writePort8 (card, 2, 0x400, cbyte & ~2);
721 /* Unknown interrupt. */
724 printk (KERN_ERR "NM256: Fire in the hole! Unknown status 0x%x\n",
727 NM2_ACK_INT (card, status);
729 return IRQ_RETVAL(handled);
733 * Request our interrupt.
736 nm256_grabInterrupt (struct nm256_info *card)
738 if (card->has_irq++ == 0) {
739 if (request_irq (card->irq, card->introutine, SA_SHIRQ,
740 "NM256_audio", card) < 0) {
741 printk (KERN_ERR "NM256: can't obtain IRQ %d\n", card->irq);
749 * Release our interrupt.
752 nm256_releaseInterrupt (struct nm256_info *card)
754 if (card->has_irq <= 0) {
755 printk (KERN_ERR "nm256: too many calls to releaseInterrupt\n");
759 if (card->has_irq == 0) {
760 free_irq (card->irq, card);
766 * Waits for the mixer to become ready to be written; returns a zero value
771 nm256_isReady (struct ac97_hwint *dev)
773 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
779 if (card->magsig != NM_MAGIC_SIG) {
780 printk (KERN_ERR "NM256: Bad magic signature in isReady!\n");
784 testaddr = card->mixer_status_offset;
785 testb = card->mixer_status_mask;
788 * Loop around waiting for the mixer to become ready.
790 while (! done && t2-- > 0) {
791 if ((nm256_readPort16 (card, 2, testaddr) & testb) == 0)
800 * Return the contents of the AC97 mixer register REG. Returns a positive
801 * value if successful, or a negative error code.
804 nm256_readAC97Reg (struct ac97_hwint *dev, u8 reg)
806 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
808 if (card->magsig != NM_MAGIC_SIG) {
809 printk (KERN_ERR "NM256: Bad magic signature in readAC97Reg!\n");
817 res = nm256_readPort16 (card, 2, card->mixer + reg);
818 /* Magic delay. Bleah yucky. */
827 * Writes VALUE to AC97 mixer register REG. Returns 0 if successful, or
828 * a negative error code.
831 nm256_writeAC97Reg (struct ac97_hwint *dev, u8 reg, u16 value)
838 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
840 if (card->magsig != NM_MAGIC_SIG) {
841 printk (KERN_ERR "NM256: Bad magic signature in writeAC97Reg!\n");
847 spin_lock_irqsave(&card->lock,flags);
851 /* Wait for the write to take, too. */
852 while ((tries-- > 0) && !done) {
853 nm256_writePort16 (card, 2, base + reg, value);
854 if (nm256_isReady (dev)) {
861 spin_unlock_irqrestore(&card->lock,flags);
868 * Initial register values to be written to the AC97 mixer.
869 * While most of these are identical to the reset values, we do this
870 * so that we have most of the register contents cached--this avoids
871 * reading from the mixer directly (which seems to be problematic,
872 * probably due to ignorance).
877 unsigned short value;
880 static struct initialValues nm256_ac97_initial_values[] =
882 { AC97_MASTER_VOL_STEREO, 0x8000 },
883 { AC97_HEADPHONE_VOL, 0x8000 },
884 { AC97_MASTER_VOL_MONO, 0x0000 },
885 { AC97_PCBEEP_VOL, 0x0000 },
886 { AC97_PHONE_VOL, 0x0008 },
887 { AC97_MIC_VOL, 0x8000 },
888 { AC97_LINEIN_VOL, 0x8808 },
889 { AC97_CD_VOL, 0x8808 },
890 { AC97_VIDEO_VOL, 0x8808 },
891 { AC97_AUX_VOL, 0x8808 },
892 { AC97_PCMOUT_VOL, 0x0808 },
893 { AC97_RECORD_SELECT, 0x0000 },
894 { AC97_RECORD_GAIN, 0x0B0B },
895 { AC97_GENERAL_PURPOSE, 0x0000 },
899 /* Initialize the AC97 into a known state. */
901 nm256_resetAC97 (struct ac97_hwint *dev)
903 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
906 if (card->magsig != NM_MAGIC_SIG) {
907 printk (KERN_ERR "NM256: Bad magic signature in resetAC97!\n");
911 /* Reset the mixer. 'Tis magic! */
912 nm256_writePort8 (card, 2, 0x6c0, 1);
913 // nm256_writePort8 (card, 2, 0x6cc, 0x87); /* This crashes Dell latitudes */
914 nm256_writePort8 (card, 2, 0x6cc, 0x80);
915 nm256_writePort8 (card, 2, 0x6cc, 0x0);
917 if (! card->mixer_values_init) {
918 for (x = 0; nm256_ac97_initial_values[x].port != 0xffff; x++) {
919 ac97_put_register (dev,
920 nm256_ac97_initial_values[x].port,
921 nm256_ac97_initial_values[x].value);
922 card->mixer_values_init = 1;
930 * We don't do anything particularly special here; it just passes the
931 * mixer ioctl to the AC97 driver.
934 nm256_default_mixer_ioctl (int dev, unsigned int cmd, void __user *arg)
936 struct nm256_info *card = nm256_find_card_for_mixer (dev);
938 return ac97_mixer_ioctl (&(card->mdev), cmd, arg);
943 static struct mixer_operations nm256_mixer_operations = {
944 .owner = THIS_MODULE,
946 .name = "NM256AC97Mixer",
947 .ioctl = nm256_default_mixer_ioctl
951 * Default settings for the OSS mixer. These are set last, after the
952 * mixer is initialized.
954 * I "love" C sometimes. Got braces?
956 static struct ac97_mixer_value_list mixer_defaults[] = {
957 { SOUND_MIXER_VOLUME, { { 85, 85 } } },
958 { SOUND_MIXER_SPEAKER, { { 100 } } },
959 { SOUND_MIXER_PCM, { { 65, 65 } } },
960 { SOUND_MIXER_CD, { { 65, 65 } } },
965 /* Installs the AC97 mixer into CARD. */
967 nm256_install_mixer (struct nm256_info *card)
971 card->mdev.reset_device = nm256_resetAC97;
972 card->mdev.read_reg = nm256_readAC97Reg;
973 card->mdev.write_reg = nm256_writeAC97Reg;
974 card->mdev.driver_private = (void *)card;
976 if (ac97_init (&(card->mdev)))
979 mixer = sound_alloc_mixerdev();
980 if (num_mixers >= MAX_MIXER_DEV) {
981 printk ("NM256 mixer: Unable to alloc mixerdev\n");
985 mixer_devs[mixer] = &nm256_mixer_operations;
986 card->mixer_oss_dev = mixer;
988 /* Some reasonable default values. */
989 ac97_set_values (&(card->mdev), mixer_defaults);
991 printk(KERN_INFO "Initialized AC97 mixer\n");
995 /* Perform a full reset on the hardware; this is invoked when an APM
996 resume event occurs. */
998 nm256_full_reset (struct nm256_info *card)
1000 nm256_initHw (card);
1001 ac97_reset (&(card->mdev));
1005 * See if the signature left by the NM256 BIOS is intact; if so, we use
1006 * the associated address as the end of our audio buffer in the video
1011 nm256_peek_for_sig (struct nm256_info *card)
1014 = card->port[0].physaddr + card->port[0].end_offset - 0x0400;
1015 /* The signature is located 1K below the end of video RAM. */
1016 char __iomem *temp = ioremap_nocache (port1offset, 16);
1017 /* Default buffer end is 5120 bytes below the top of RAM. */
1018 u32 default_value = card->port[0].end_offset - 0x1400;
1021 /* Install the default value first, so we don't have to repeatedly
1022 do it if there is a problem. */
1023 card->port[0].end_offset = default_value;
1026 printk (KERN_ERR "NM256: Unable to scan for card signature in video RAM\n");
1030 if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1031 u32 pointer = readl (temp + 4);
1034 * If it's obviously invalid, don't use it (the port already has a
1035 * suitable default value set).
1037 if (pointer != 0xffffffff)
1038 card->port[0].end_offset = pointer;
1040 printk (KERN_INFO "NM256: Found card signature in video RAM: 0x%x\n",
1048 * Install a driver for the PCI device referenced by PCIDEV.
1049 * VERSTR is a human-readable version string.
1052 static int __devinit
1053 nm256_install(struct pci_dev *pcidev, enum nm256rev rev, char *verstr)
1055 struct nm256_info *card;
1056 struct pm_dev *pmdev;
1059 if (pci_enable_device(pcidev))
1062 card = kmalloc (sizeof (struct nm256_info), GFP_KERNEL);
1064 printk (KERN_ERR "NM256: out of memory!\n");
1068 card->magsig = NM_MAGIC_SIG;
1070 card->recording = 0;
1072 spin_lock_init(&card->lock);
1074 /* Init the memory port info. */
1075 for (x = 0; x < 2; x++) {
1076 card->port[x].physaddr = pci_resource_start (pcidev, x);
1077 card->port[x].ptr = NULL;
1078 card->port[x].start_offset = 0;
1079 card->port[x].end_offset = 0;
1082 /* Port 2 is easy. */
1083 card->port[1].start_offset = 0;
1084 card->port[1].end_offset = NM_PORT2_SIZE;
1086 /* Yuck. But we have to map in port 2 so we can check how much RAM the
1088 if (nm256_remap_ports (card)) {
1094 * The NM256 has two memory ports. The first port is nothing
1095 * more than a chunk of video RAM, which is used as the I/O ring
1096 * buffer. The second port has the actual juicy stuff (like the
1097 * mixer and the playback engine control registers).
1100 if (card->rev == REV_NM256AV) {
1101 /* Ok, try to see if this is a non-AC97 version of the hardware. */
1102 int pval = nm256_readPort16 (card, 2, NM_MIXER_PRESENCE);
1103 if ((pval & NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) {
1105 printk (KERN_ERR "NM256: This doesn't look to me like the AC97-compatible version.\n");
1106 printk (KERN_ERR " You can force the driver to load by passing in the module\n");
1107 printk (KERN_ERR " parameter:\n");
1108 printk (KERN_ERR " force_load = 1\n");
1109 printk (KERN_ERR "\n");
1110 printk (KERN_ERR " More likely, you should be using the appropriate SB-16 or\n");
1111 printk (KERN_ERR " CS4232 driver instead. (If your BIOS has settings for\n");
1112 printk (KERN_ERR " IRQ and/or DMA for the sound card, this is *not* the correct\n");
1113 printk (KERN_ERR " driver to use.)\n");
1114 nm256_release_ports (card);
1119 printk (KERN_INFO "NM256: Forcing driver load as per user request.\n");
1123 /* printk (KERN_INFO "NM256: Congratulations. You're not running Eunice.\n")*/;
1125 card->port[0].end_offset = 2560 * 1024;
1126 card->introutine = nm256_interrupt;
1127 card->mixer_status_offset = NM_MIXER_STATUS_OFFSET;
1128 card->mixer_status_mask = NM_MIXER_READY_MASK;
1131 /* Not sure if there is any relevant detect for the ZX or not. */
1132 if (nm256_readPort8 (card, 2, 0xa0b) != 0)
1133 card->port[0].end_offset = 6144 * 1024;
1135 card->port[0].end_offset = 4096 * 1024;
1137 card->introutine = nm256_interrupt_zx;
1138 card->mixer_status_offset = NM2_MIXER_STATUS_OFFSET;
1139 card->mixer_status_mask = NM2_MIXER_READY_MASK;
1142 if (buffertop >= 98304 && buffertop < card->port[0].end_offset)
1143 card->port[0].end_offset = buffertop;
1145 nm256_peek_for_sig (card);
1147 card->port[0].start_offset = card->port[0].end_offset - 98304;
1149 printk (KERN_INFO "NM256: Mapping port 1 from 0x%x - 0x%x\n",
1150 card->port[0].start_offset, card->port[0].end_offset);
1152 if (nm256_remap_ports (card)) {
1157 /* See if we can get the interrupt. */
1159 card->irq = pcidev->irq;
1162 if (nm256_grabInterrupt (card) != 0) {
1163 nm256_release_ports (card);
1168 nm256_releaseInterrupt (card);
1174 card->playbackBufferSize = 16384;
1175 card->recordBufferSize = 16384;
1177 card->coeffBuf = card->port[0].end_offset - NM_MAX_COEFFICIENT;
1178 card->abuf2 = card->coeffBuf - card->recordBufferSize;
1179 card->abuf1 = card->abuf2 - card->playbackBufferSize;
1180 card->allCoeffBuf = card->abuf2 - (NM_TOTAL_COEFF_COUNT * 4);
1182 /* Fixed setting. */
1183 card->mixer = NM_MIXER_OFFSET;
1184 card->mixer_values_init = 0;
1186 card->is_open_play = 0;
1187 card->is_open_record = 0;
1189 card->coeffsCurrent = 0;
1191 card->opencnt[0] = 0; card->opencnt[1] = 0;
1193 /* Reasonable default settings, but largely unnecessary. */
1194 for (x = 0; x < 2; x++) {
1195 card->sinfo[x].bits = 8;
1196 card->sinfo[x].stereo = 0;
1197 card->sinfo[x].samplerate = 8000;
1200 nm256_initHw (card);
1202 for (x = 0; x < 2; x++) {
1204 sound_install_audiodrv(AUDIO_DRIVER_VERSION,
1205 "NM256", &nm256_audio_driver,
1206 sizeof(struct audio_driver),
1207 DMA_NODMA, AFMT_U8 | AFMT_S16_LE,
1208 NULL, -1, -1)) >= 0) {
1209 /* 1K minimum buffer size. */
1210 audio_devs[card->dev[x]]->min_fragment = 10;
1211 /* Maximum of 8K buffer size. */
1212 audio_devs[card->dev[x]]->max_fragment = 13;
1215 printk(KERN_ERR "NM256: Too many PCM devices available\n");
1216 nm256_release_ports (card);
1222 pci_set_drvdata(pcidev,card);
1224 /* Insert the card in the list. */
1225 card->next_card = nmcard_list;
1228 printk(KERN_INFO "Initialized NeoMagic %s audio in PCI native mode\n",
1232 * And our mixer. (We should allow support for other mixers, maybe.)
1235 nm256_install_mixer (card);
1237 pmdev = pm_register(PM_PCI_DEV, PM_PCI_ID(pcidev), handle_pm_event);
1246 * PM event handler, so the card is properly reinitialized after a power
1250 handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data)
1252 struct nm256_info *crd = (struct nm256_info*) dev->data;
1259 int playing = crd->playing;
1260 nm256_full_reset (crd);
1262 * A little ugly, but that's ok; pretend the
1263 * block we were playing is done.
1266 DMAbuf_outputintr (crd->dev_for_play, 1);
1274 static int __devinit
1275 nm256_probe(struct pci_dev *pcidev,const struct pci_device_id *pciid)
1277 if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO)
1278 return nm256_install(pcidev, REV_NM256AV, "256AV");
1279 if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO)
1280 return nm256_install(pcidev, REV_NM256ZX, "256ZX");
1281 if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO)
1282 return nm256_install(pcidev, REV_NM256ZX, "256XL+");
1283 return -1; /* should not come here ... */
1286 static void __devinit
1287 nm256_remove(struct pci_dev *pcidev) {
1288 struct nm256_info *xcard = pci_get_drvdata(pcidev);
1289 struct nm256_info *card,*next_card = NULL;
1291 for (card = nmcard_list; card != NULL; card = next_card) {
1292 next_card = card->next_card;
1293 if (card == xcard) {
1297 free_irq (card->irq, card);
1298 nm256_release_ports (card);
1299 sound_unload_mixerdev (card->mixer_oss_dev);
1300 sound_unload_audiodev (card->dev[0]);
1301 sound_unload_audiodev (card->dev[1]);
1306 if (nmcard_list == card)
1307 nmcard_list = next_card;
1314 * MODE - mode to open device (logical OR of OPEN_READ and OPEN_WRITE)
1316 * Called when opening the DMAbuf (dmabuf.c:259)
1319 nm256_audio_open(int dev, int mode)
1321 struct nm256_info *card = nm256_find_card (dev);
1327 if (card->dev[0] == dev)
1329 else if (card->dev[1] == dev)
1334 if (card->opencnt[w] > 0)
1337 /* No bits set? Huh? */
1338 if (! ((mode & OPEN_READ) || (mode & OPEN_WRITE)))
1342 * If it's open for both read and write, and the card's currently
1343 * being read or written to, then do the opposite of what has
1344 * already been done. Otherwise, don't specify any mode until the
1345 * user actually tries to do I/O. (Some programs open the device
1346 * for both read and write, but only actually do reading or writing.)
1349 if ((mode & OPEN_WRITE) && (mode & OPEN_READ)) {
1350 if (card->is_open_play)
1352 else if (card->is_open_record)
1357 if (mode & OPEN_WRITE) {
1358 if (card->is_open_play == 0) {
1359 card->dev_for_play = dev;
1360 card->is_open_play = 1;
1366 if (mode & OPEN_READ) {
1367 if (card->is_open_record == 0) {
1368 card->dev_for_record = dev;
1369 card->is_open_record = 1;
1384 * Called when closing the DMAbuf (dmabuf.c:477)
1388 nm256_audio_close(int dev)
1390 struct nm256_info *card = nm256_find_card (dev);
1395 if (card->dev[0] == dev)
1397 else if (card->dev[1] == dev)
1403 if (card->opencnt[w] <= 0) {
1404 card->opencnt[w] = 0;
1406 if (card->dev_for_play == dev) {
1408 card->is_open_play = 0;
1409 card->dev_for_play = -1;
1412 if (card->dev_for_record == dev) {
1414 card->is_open_record = 0;
1415 card->dev_for_record = -1;
1421 /* Standard ioctl handler. */
1423 nm256_audio_ioctl(int dev, unsigned int cmd, void __user *arg)
1429 struct nm256_info *card = nm256_find_card (dev);
1434 if (dev == card->dev[0])
1440 * The code here is messy. There are probably better ways to do
1441 * it. (It should be possible to handle it the same way the AC97 mixer
1446 case SOUND_PCM_WRITE_RATE:
1447 if (get_user(ret, (int __user *) arg))
1451 oldinfo = card->sinfo[w].samplerate;
1452 card->sinfo[w].samplerate = ret;
1453 ret = nm256_setInfo(dev, card);
1455 card->sinfo[w].samplerate = oldinfo;
1458 ret = card->sinfo[w].samplerate;
1461 case SOUND_PCM_READ_RATE:
1462 ret = card->sinfo[w].samplerate;
1465 case SNDCTL_DSP_STEREO:
1466 if (get_user(ret, (int __user *) arg))
1469 card->sinfo[w].stereo = ret ? 1 : 0;
1470 ret = nm256_setInfo (dev, card);
1472 ret = card->sinfo[w].stereo;
1476 case SOUND_PCM_WRITE_CHANNELS:
1477 if (get_user(ret, (int __user *) arg))
1480 if (ret < 1 || ret > 3)
1481 ret = card->sinfo[w].stereo + 1;
1483 card->sinfo[w].stereo = ret - 1;
1484 ret = nm256_setInfo (dev, card);
1486 ret = card->sinfo[w].stereo + 1;
1490 case SOUND_PCM_READ_CHANNELS:
1491 ret = card->sinfo[w].stereo + 1;
1494 case SNDCTL_DSP_SETFMT:
1495 if (get_user(ret, (int __user *) arg))
1499 oldinfo = card->sinfo[w].bits;
1500 card->sinfo[w].bits = ret;
1501 ret = nm256_setInfo (dev, card);
1503 card->sinfo[w].bits = oldinfo;
1506 ret = card->sinfo[w].bits;
1509 case SOUND_PCM_READ_BITS:
1510 ret = card->sinfo[w].bits;
1516 return put_user(ret, (int __user *) arg);
1520 * Given the sound device DEV and an associated physical buffer PHYSBUF,
1521 * return a pointer to the actual buffer in kernel space.
1523 * This routine should exist as part of the soundcore routines.
1527 nm256_getDMAbuffer (int dev, unsigned long physbuf)
1529 struct audio_operations *adev = audio_devs[dev];
1530 struct dma_buffparms *dmap = adev->dmap_out;
1532 (char *)(physbuf - (unsigned long)dmap->raw_buf_phys
1533 + (unsigned long)dmap->raw_buf);
1540 * Output a block to sound device
1542 * dev - device number
1543 * buf - physical address of buffer
1544 * total_count - total byte count in buffer
1545 * intrflag - set if this has been called from an interrupt
1546 * (via DMAbuf_outputintr)
1547 * restart_dma - set if engine needs to be re-initialised
1550 * 1. Starting output (dmabuf.c:1327)
1551 * 2. (dmabuf.c:1504)
1552 * 3. A new buffer needs to be sent to the device (dmabuf.c:1579)
1555 nm256_audio_output_block(int dev, unsigned long physbuf,
1556 int total_count, int intrflag)
1558 struct nm256_info *card = nm256_find_card (dev);
1561 char *dma_buf = nm256_getDMAbuffer (dev, physbuf);
1562 card->is_open_play = 1;
1563 card->dev_for_play = dev;
1564 nm256_write_block (card, dma_buf, total_count);
1568 /* Ditto, but do recording instead. */
1570 nm256_audio_start_input(int dev, unsigned long physbuf, int count,
1573 struct nm256_info *card = nm256_find_card (dev);
1576 char *dma_buf = nm256_getDMAbuffer (dev, physbuf);
1577 card->is_open_record = 1;
1578 card->dev_for_record = dev;
1579 nm256_startRecording (card, dma_buf, count);
1584 * Prepare for inputting samples to DEV.
1585 * Each requested buffer will be BSIZE byes long, with a total of
1590 nm256_audio_prepare_for_input(int dev, int bsize, int bcount)
1592 struct nm256_info *card = nm256_find_card (dev);
1597 if (card->is_open_record && card->dev_for_record != dev)
1600 audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
1605 * Prepare for outputting samples to `dev'
1607 * Each buffer that will be passed will be `bsize' bytes long,
1608 * with a total of `bcount' buffers.
1611 * 1. A trigger enables audio output (dmabuf.c:978)
1612 * 2. We get a write buffer without dma_mode setup (dmabuf.c:1152)
1613 * 3. We restart a transfer (dmabuf.c:1324)
1617 nm256_audio_prepare_for_output(int dev, int bsize, int bcount)
1619 struct nm256_info *card = nm256_find_card (dev);
1624 if (card->is_open_play && card->dev_for_play != dev)
1627 audio_devs[dev]->dmap_out->flags |= DMA_NODMA;
1631 /* Stop the current operations associated with DEV. */
1633 nm256_audio_reset(int dev)
1635 struct nm256_info *card = nm256_find_card (dev);
1638 if (card->dev_for_play == dev)
1640 if (card->dev_for_record == dev)
1646 nm256_audio_local_qlen(int dev)
1651 static struct audio_driver nm256_audio_driver =
1653 .owner = THIS_MODULE,
1654 .open = nm256_audio_open,
1655 .close = nm256_audio_close,
1656 .output_block = nm256_audio_output_block,
1657 .start_input = nm256_audio_start_input,
1658 .ioctl = nm256_audio_ioctl,
1659 .prepare_for_input = nm256_audio_prepare_for_input,
1660 .prepare_for_output = nm256_audio_prepare_for_output,
1661 .halt_io = nm256_audio_reset,
1662 .local_qlen = nm256_audio_local_qlen,
1665 static struct pci_device_id nm256_pci_tbl[] = {
1666 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO,
1667 PCI_ANY_ID, PCI_ANY_ID, 0, 0},
1668 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO,
1669 PCI_ANY_ID, PCI_ANY_ID, 0, 0},
1670 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO,
1671 PCI_ANY_ID, PCI_ANY_ID, 0, 0},
1674 MODULE_DEVICE_TABLE(pci, nm256_pci_tbl);
1675 MODULE_LICENSE("GPL");
1678 static struct pci_driver nm256_pci_driver = {
1679 .name = "nm256_audio",
1680 .id_table = nm256_pci_tbl,
1681 .probe = nm256_probe,
1682 .remove = nm256_remove,
1685 module_param(usecache, bool, 0);
1686 module_param(buffertop, int, 0);
1687 module_param(nm256_debug, bool, 0644);
1688 module_param(force_load, bool, 0);
1690 static int __init do_init_nm256(void)
1692 printk (KERN_INFO "NeoMagic 256AV/256ZX audio driver, version 1.1p\n");
1693 return pci_module_init(&nm256_pci_driver);
1696 static void __exit cleanup_nm256 (void)
1698 pci_unregister_driver(&nm256_pci_driver);
1699 pm_unregister_all (&handle_pm_event);
1702 module_init(do_init_nm256);
1703 module_exit(cleanup_nm256);