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>
27 #include <linux/delay.h>
28 #include <linux/spinlock.h>
29 #include "sound_config.h"
31 static int nm256_debug;
32 static int force_load;
35 #include "nm256_coeff.h"
38 * The size of the playback reserve. When the playback buffer has less
39 * than NM256_PLAY_WMARK_SIZE bytes to output, we request a new
42 #define NM256_PLAY_WMARK_SIZE 512
44 static struct audio_driver nm256_audio_driver;
46 static int nm256_grabInterrupt (struct nm256_info *card);
47 static int nm256_releaseInterrupt (struct nm256_info *card);
48 static irqreturn_t nm256_interrupt (int irq, void *dev_id);
49 static irqreturn_t nm256_interrupt_zx (int irq, void *dev_id);
51 /* These belong in linux/pci.h. */
52 #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
53 #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
54 #define PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO 0x8016
57 static struct nm256_info *nmcard_list;
59 /* Release the mapped-in memory for CARD. */
61 nm256_release_ports (struct nm256_info *card)
65 for (x = 0; x < 2; x++) {
66 if (card->port[x].ptr != NULL) {
67 iounmap (card->port[x].ptr);
68 card->port[x].ptr = NULL;
74 * Map in the memory ports for CARD, if they aren't already mapped in
75 * and have been configured. If successful, a zero value is returned;
76 * otherwise any previously mapped-in areas are released and a non-zero
79 * This is invoked twice, once for each port. Ideally it would only be
80 * called once, but we now need to map in the second port in order to
81 * check how much memory the card has on the 256ZX.
84 nm256_remap_ports (struct nm256_info *card)
88 for (x = 0; x < 2; x++) {
89 if (card->port[x].ptr == NULL && card->port[x].end_offset > 0) {
91 = card->port[x].physaddr + card->port[x].start_offset;
93 = card->port[x].end_offset - card->port[x].start_offset;
95 card->port[x].ptr = ioremap_nocache (physaddr, size);
97 if (card->port[x].ptr == NULL) {
98 printk (KERN_ERR "NM256: Unable to remap port %d\n", x + 1);
99 nm256_release_ports (card);
107 /* Locate the card in our list. */
108 static struct nm256_info *
109 nm256_find_card (int dev)
111 struct nm256_info *card;
113 for (card = nmcard_list; card != NULL; card = card->next_card)
114 if (card->dev[0] == dev || card->dev[1] == dev)
121 * Ditto, but find the card struct corresponding to the mixer device DEV
124 static struct nm256_info *
125 nm256_find_card_for_mixer (int dev)
127 struct nm256_info *card;
129 for (card = nmcard_list; card != NULL; card = card->next_card)
130 if (card->mixer_oss_dev == dev)
137 static int buffertop;
139 /* Check to see if we're using the bank of cached coefficients. */
141 nm256_cachedCoefficients (struct nm256_info *card)
146 /* The actual rates supported by the card. */
147 static int samplerates[9] = {
148 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 99999999
152 * Set the card samplerate, word size and stereo mode to correspond to
153 * the settings in the CARD struct for the specified device in DEV.
154 * We keep two separate sets of information, one for each device; the
155 * hardware is not actually configured until a read or write is
160 nm256_setInfo (int dev, struct nm256_info *card)
166 if (card->dev[0] == dev)
168 else if (card->dev[1] == dev)
173 targetrate = card->sinfo[w].samplerate;
175 if ((card->sinfo[w].bits != 8 && card->sinfo[w].bits != 16)
176 || targetrate < samplerates[0]
177 || targetrate > samplerates[7])
180 for (x = 0; x < 8; x++)
181 if (targetrate < ((samplerates[x] + samplerates[x + 1]) / 2))
185 u8 ratebits = ((x << 4) & NM_RATE_MASK);
186 if (card->sinfo[w].bits == 16)
187 ratebits |= NM_RATE_BITS_16;
188 if (card->sinfo[w].stereo)
189 ratebits |= NM_RATE_STEREO;
191 card->sinfo[w].samplerate = samplerates[x];
194 if (card->dev_for_play == dev && card->playing) {
196 printk (KERN_DEBUG "Setting play ratebits to 0x%x\n",
198 nm256_loadCoefficient (card, 0, x);
199 nm256_writePort8 (card, 2,
200 NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET,
204 if (card->dev_for_record == dev && card->recording) {
206 printk (KERN_DEBUG "Setting record ratebits to 0x%x\n",
208 nm256_loadCoefficient (card, 1, x);
209 nm256_writePort8 (card, 2,
210 NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET,
219 /* Start the play process going. */
221 startPlay (struct nm256_info *card)
223 if (! card->playing) {
225 if (nm256_grabInterrupt (card) == 0) {
226 nm256_setInfo (card->dev_for_play, card);
228 /* Enable playback engine and interrupts. */
229 nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG,
230 NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);
232 /* Enable both channels. */
233 nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG, 0x0);
239 * Request one chunk of AMT bytes from the recording device. When the
240 * operation is complete, the data will be copied into BUFFER and the
241 * function DMAbuf_inputintr will be invoked.
245 nm256_startRecording (struct nm256_info *card, char *buffer, u32 amt)
248 int enableEngine = 0;
249 u32 ringsize = card->recordBufferSize;
252 if (amt > (ringsize / 2)) {
254 * Of course this won't actually work right, because the
255 * caller is going to assume we will give what we got asked
258 printk (KERN_ERR "NM256: Read request too large: %d\n", amt);
263 printk (KERN_ERR "NM256: Read request too small; %d\n", amt);
267 spin_lock_irqsave(&card->lock,flags);
269 * If we're not currently recording, set up the start and end registers
270 * for the recording engine.
272 if (! card->recording) {
274 if (nm256_grabInterrupt (card) == 0) {
276 nm256_setInfo (card->dev_for_record, card);
277 nm256_writePort32 (card, 2, NM_RBUFFER_START, card->abuf2);
278 nm256_writePort32 (card, 2, NM_RBUFFER_END,
279 card->abuf2 + ringsize);
281 nm256_writePort32 (card, 2, NM_RBUFFER_CURRP,
282 card->abuf2 + card->curRecPos);
286 /* Not sure what else to do here. */
287 spin_unlock_irqrestore(&card->lock,flags);
293 * If we happen to go past the end of the buffer a bit (due to a
294 * delayed interrupt) it's OK. So might as well set the watermark
295 * right at the end of the data we want.
297 endpos = card->abuf2 + ((card->curRecPos + amt) % ringsize);
299 card->recBuf = buffer;
300 card->requestedRecAmt = amt;
301 nm256_writePort32 (card, 2, NM_RBUFFER_WMARK, endpos);
302 /* Enable recording engine and interrupts. */
304 nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG,
305 NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
307 spin_unlock_irqrestore(&card->lock,flags);
310 /* Stop the play engine. */
312 stopPlay (struct nm256_info *card)
314 /* Shut off sound from both channels. */
315 nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG,
316 NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT);
317 /* Disable play engine. */
318 nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG, 0);
320 nm256_releaseInterrupt (card);
322 /* Reset the relevant state bits. */
324 card->curPlayPos = 0;
328 /* Stop recording. */
330 stopRecord (struct nm256_info *card)
332 /* Disable recording engine. */
333 nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG, 0);
335 if (card->recording) {
336 nm256_releaseInterrupt (card);
344 * Ring buffers, man. That's where the hip-hop, wild-n-wooly action's at.
345 * 1972? (Well, I suppose it was cheep-n-easy to implement.)
347 * Write AMT bytes of BUFFER to the playback ring buffer, and start the
348 * playback engine running. It will only accept up to 1/2 of the total
349 * size of the ring buffer. No check is made that we're about to overwrite
350 * the currently-playing sample.
354 nm256_write_block (struct nm256_info *card, char *buffer, u32 amt)
356 u32 ringsize = card->playbackBufferSize;
360 if (amt > (ringsize / 2)) {
361 printk (KERN_ERR "NM256: Write request too large: %d\n", amt);
362 amt = (ringsize / 2);
365 if (amt < NM256_PLAY_WMARK_SIZE) {
366 printk (KERN_ERR "NM256: Write request too small: %d\n", amt);
370 card->curPlayPos %= ringsize;
372 card->requested_amt = amt;
374 spin_lock_irqsave(&card->lock,flags);
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 spin_unlock_irqrestore(&card->lock,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);
455 * Read the last-recorded block from the ring buffer, copy it into the
456 * saved buffer pointer, and invoke DMAuf_inputintr() with the recording
461 nm256_read_block (struct nm256_info *card)
463 /* Grab the current position of the recording pointer. */
464 u32 currptr = nm256_readPort32 (card, 2, NM_RBUFFER_CURRP) - card->abuf2;
465 u32 amtToRead = card->requestedRecAmt;
466 u32 ringsize = card->recordBufferSize;
468 if (currptr >= card->recordBufferSize) {
469 printk (KERN_ERR "NM256: Sound buffer record pointer invalid!\n");
474 * This test is probably redundant; we shouldn't be here unless
477 if (card->recording) {
478 /* If we wrapped around, copy everything from the start of our
479 recording buffer to the end of the buffer. */
480 if (currptr < card->curRecPos) {
481 u32 amt = min (ringsize - card->curRecPos, amtToRead);
483 nm256_readBuffer8 (card, card->recBuf, 1,
484 card->abuf2 + card->curRecPos,
487 card->curRecPos += amt;
489 if (card->curRecPos == ringsize)
493 if ((card->curRecPos < currptr) && (amtToRead > 0)) {
494 u32 amt = min (currptr - card->curRecPos, amtToRead);
495 nm256_readBuffer8 (card, card->recBuf, 1,
496 card->abuf2 + card->curRecPos, amt);
497 card->curRecPos = ((card->curRecPos + amt) % ringsize);
500 card->requestedRecAmt = 0;
501 DMAbuf_inputintr (card->dev_for_record);
506 * Initialize the hardware.
509 nm256_initHw (struct nm256_info *card)
511 /* Reset everything. */
512 nm256_writePort8 (card, 2, 0x0, 0x11);
513 nm256_writePort16 (card, 2, 0x214, 0);
520 * Handle a potential interrupt for the device referred to by DEV_ID.
522 * I don't like the cut-n-paste job here either between the two routines,
523 * but there are sufficient differences between the two interrupt handlers
524 * that parameterizing it isn't all that great either. (Could use a macro,
525 * I suppose...yucky bleah.)
529 nm256_interrupt (int irq, void *dev_id)
531 struct nm256_info *card = (struct nm256_info *)dev_id;
533 static int badintrcount;
536 if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
537 printk (KERN_ERR "NM256: Bad card pointer\n");
541 status = nm256_readPort16 (card, 2, NM_INT_REG);
545 if (badintrcount++ > 1000) {
547 * I'm not sure if the best thing is to stop the card from
548 * playing or just release the interrupt (after all, we're in
549 * a bad situation, so doing fancy stuff may not be such a good
552 * I worry about the card engine continuing to play noise
553 * over and over, however--that could become a very
554 * obnoxious problem. And we know that when this usually
555 * happens things are fairly safe, it just means the user's
556 * inserted a PCMCIA card and someone's spamming us with IRQ 9s.
566 return IRQ_RETVAL(handled);
571 /* Rather boring; check for individual interrupts and process them. */
573 if (status & NM_PLAYBACK_INT) {
575 status &= ~NM_PLAYBACK_INT;
576 NM_ACK_INT (card, NM_PLAYBACK_INT);
579 nm256_get_new_block (card);
582 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) {
595 status &= ~NM_MISC_INT_1;
596 printk (KERN_ERR "NM256: Got misc interrupt #1\n");
597 NM_ACK_INT (card, NM_MISC_INT_1);
598 nm256_writePort16 (card, 2, NM_INT_REG, 0x8000);
599 cbyte = nm256_readPort8 (card, 2, 0x400);
600 nm256_writePort8 (card, 2, 0x400, cbyte | 2);
603 if (status & NM_MISC_INT_2) {
607 status &= ~NM_MISC_INT_2;
608 printk (KERN_ERR "NM256: Got misc interrupt #2\n");
609 NM_ACK_INT (card, NM_MISC_INT_2);
610 cbyte = nm256_readPort8 (card, 2, 0x400);
611 nm256_writePort8 (card, 2, 0x400, cbyte & ~2);
614 /* Unknown interrupt. */
617 printk (KERN_ERR "NM256: Fire in the hole! Unknown status 0x%x\n",
620 NM_ACK_INT (card, status);
622 return IRQ_RETVAL(handled);
626 * Handle a potential interrupt for the device referred to by DEV_ID.
627 * This handler is for the 256ZX, and is very similar to the non-ZX
632 nm256_interrupt_zx (int irq, void *dev_id)
634 struct nm256_info *card = (struct nm256_info *)dev_id;
636 static int badintrcount;
639 if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
640 printk (KERN_ERR "NM256: Bad card pointer\n");
644 status = nm256_readPort32 (card, 2, NM_INT_REG);
648 if (badintrcount++ > 1000) {
649 printk (KERN_ERR "NM256: Releasing interrupt, over 1000 invalid interrupts\n");
651 * I'm not sure if the best thing is to stop the card from
652 * playing or just release the interrupt (after all, we're in
653 * a bad situation, so doing fancy stuff may not be such a good
656 * I worry about the card engine continuing to play noise
657 * over and over, however--that could become a very
658 * obnoxious problem. And we know that when this usually
659 * happens things are fairly safe, it just means the user's
660 * inserted a PCMCIA card and someone's spamming us with
671 return IRQ_RETVAL(handled);
676 /* Rather boring; check for individual interrupts and process them. */
678 if (status & NM2_PLAYBACK_INT) {
680 status &= ~NM2_PLAYBACK_INT;
681 NM2_ACK_INT (card, NM2_PLAYBACK_INT);
684 nm256_get_new_block (card);
687 if (status & NM2_RECORD_INT) {
689 status &= ~NM2_RECORD_INT;
690 NM2_ACK_INT (card, NM2_RECORD_INT);
693 nm256_read_block (card);
696 if (status & NM2_MISC_INT_1) {
700 status &= ~NM2_MISC_INT_1;
701 printk (KERN_ERR "NM256: Got misc interrupt #1\n");
702 NM2_ACK_INT (card, NM2_MISC_INT_1);
703 cbyte = nm256_readPort8 (card, 2, 0x400);
704 nm256_writePort8 (card, 2, 0x400, cbyte | 2);
707 if (status & NM2_MISC_INT_2) {
711 status &= ~NM2_MISC_INT_2;
712 printk (KERN_ERR "NM256: Got misc interrupt #2\n");
713 NM2_ACK_INT (card, NM2_MISC_INT_2);
714 cbyte = nm256_readPort8 (card, 2, 0x400);
715 nm256_writePort8 (card, 2, 0x400, cbyte & ~2);
718 /* Unknown interrupt. */
721 printk (KERN_ERR "NM256: Fire in the hole! Unknown status 0x%x\n",
724 NM2_ACK_INT (card, status);
726 return IRQ_RETVAL(handled);
730 * Request our interrupt.
733 nm256_grabInterrupt (struct nm256_info *card)
735 if (card->has_irq++ == 0) {
736 if (request_irq (card->irq, card->introutine, IRQF_SHARED,
737 "NM256_audio", card) < 0) {
738 printk (KERN_ERR "NM256: can't obtain IRQ %d\n", card->irq);
746 * Release our interrupt.
749 nm256_releaseInterrupt (struct nm256_info *card)
751 if (card->has_irq <= 0) {
752 printk (KERN_ERR "nm256: too many calls to releaseInterrupt\n");
756 if (card->has_irq == 0) {
757 free_irq (card->irq, card);
763 * Waits for the mixer to become ready to be written; returns a zero value
768 nm256_isReady (struct ac97_hwint *dev)
770 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
776 if (card->magsig != NM_MAGIC_SIG) {
777 printk (KERN_ERR "NM256: Bad magic signature in isReady!\n");
781 testaddr = card->mixer_status_offset;
782 testb = card->mixer_status_mask;
785 * Loop around waiting for the mixer to become ready.
787 while (! done && t2-- > 0) {
788 if ((nm256_readPort16 (card, 2, testaddr) & testb) == 0)
797 * Return the contents of the AC97 mixer register REG. Returns a positive
798 * value if successful, or a negative error code.
801 nm256_readAC97Reg (struct ac97_hwint *dev, u8 reg)
803 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
805 if (card->magsig != NM_MAGIC_SIG) {
806 printk (KERN_ERR "NM256: Bad magic signature in readAC97Reg!\n");
814 res = nm256_readPort16 (card, 2, card->mixer + reg);
815 /* Magic delay. Bleah yucky. */
824 * Writes VALUE to AC97 mixer register REG. Returns 0 if successful, or
825 * a negative error code.
828 nm256_writeAC97Reg (struct ac97_hwint *dev, u8 reg, u16 value)
835 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
837 if (card->magsig != NM_MAGIC_SIG) {
838 printk (KERN_ERR "NM256: Bad magic signature in writeAC97Reg!\n");
844 spin_lock_irqsave(&card->lock,flags);
848 /* Wait for the write to take, too. */
849 while ((tries-- > 0) && !done) {
850 nm256_writePort16 (card, 2, base + reg, value);
851 if (nm256_isReady (dev)) {
858 spin_unlock_irqrestore(&card->lock,flags);
865 * Initial register values to be written to the AC97 mixer.
866 * While most of these are identical to the reset values, we do this
867 * so that we have most of the register contents cached--this avoids
868 * reading from the mixer directly (which seems to be problematic,
869 * probably due to ignorance).
874 unsigned short value;
877 static struct initialValues nm256_ac97_initial_values[] =
879 { AC97_MASTER_VOL_STEREO, 0x8000 },
880 { AC97_HEADPHONE_VOL, 0x8000 },
881 { AC97_MASTER_VOL_MONO, 0x0000 },
882 { AC97_PCBEEP_VOL, 0x0000 },
883 { AC97_PHONE_VOL, 0x0008 },
884 { AC97_MIC_VOL, 0x8000 },
885 { AC97_LINEIN_VOL, 0x8808 },
886 { AC97_CD_VOL, 0x8808 },
887 { AC97_VIDEO_VOL, 0x8808 },
888 { AC97_AUX_VOL, 0x8808 },
889 { AC97_PCMOUT_VOL, 0x0808 },
890 { AC97_RECORD_SELECT, 0x0000 },
891 { AC97_RECORD_GAIN, 0x0B0B },
892 { AC97_GENERAL_PURPOSE, 0x0000 },
896 /* Initialize the AC97 into a known state. */
898 nm256_resetAC97 (struct ac97_hwint *dev)
900 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
903 if (card->magsig != NM_MAGIC_SIG) {
904 printk (KERN_ERR "NM256: Bad magic signature in resetAC97!\n");
908 /* Reset the mixer. 'Tis magic! */
909 nm256_writePort8 (card, 2, 0x6c0, 1);
910 // nm256_writePort8 (card, 2, 0x6cc, 0x87); /* This crashes Dell latitudes */
911 nm256_writePort8 (card, 2, 0x6cc, 0x80);
912 nm256_writePort8 (card, 2, 0x6cc, 0x0);
914 if (! card->mixer_values_init) {
915 for (x = 0; nm256_ac97_initial_values[x].port != 0xffff; x++) {
916 ac97_put_register (dev,
917 nm256_ac97_initial_values[x].port,
918 nm256_ac97_initial_values[x].value);
919 card->mixer_values_init = 1;
927 * We don't do anything particularly special here; it just passes the
928 * mixer ioctl to the AC97 driver.
931 nm256_default_mixer_ioctl (int dev, unsigned int cmd, void __user *arg)
933 struct nm256_info *card = nm256_find_card_for_mixer (dev);
935 return ac97_mixer_ioctl (&(card->mdev), cmd, arg);
940 static struct mixer_operations nm256_mixer_operations = {
941 .owner = THIS_MODULE,
943 .name = "NM256AC97Mixer",
944 .ioctl = nm256_default_mixer_ioctl
948 * Default settings for the OSS mixer. These are set last, after the
949 * mixer is initialized.
951 * I "love" C sometimes. Got braces?
953 static struct ac97_mixer_value_list mixer_defaults[] = {
954 { SOUND_MIXER_VOLUME, { { 85, 85 } } },
955 { SOUND_MIXER_SPEAKER, { { 100 } } },
956 { SOUND_MIXER_PCM, { { 65, 65 } } },
957 { SOUND_MIXER_CD, { { 65, 65 } } },
962 /* Installs the AC97 mixer into CARD. */
964 nm256_install_mixer (struct nm256_info *card)
968 card->mdev.reset_device = nm256_resetAC97;
969 card->mdev.read_reg = nm256_readAC97Reg;
970 card->mdev.write_reg = nm256_writeAC97Reg;
971 card->mdev.driver_private = (void *)card;
973 if (ac97_init (&(card->mdev)))
976 mixer = sound_alloc_mixerdev();
977 if (num_mixers >= MAX_MIXER_DEV) {
978 printk ("NM256 mixer: Unable to alloc mixerdev\n");
982 mixer_devs[mixer] = &nm256_mixer_operations;
983 card->mixer_oss_dev = mixer;
985 /* Some reasonable default values. */
986 ac97_set_values (&(card->mdev), mixer_defaults);
988 printk(KERN_INFO "Initialized AC97 mixer\n");
993 * See if the signature left by the NM256 BIOS is intact; if so, we use
994 * the associated address as the end of our audio buffer in the video
998 static void __devinit
999 nm256_peek_for_sig (struct nm256_info *card)
1002 = card->port[0].physaddr + card->port[0].end_offset - 0x0400;
1003 /* The signature is located 1K below the end of video RAM. */
1004 char __iomem *temp = ioremap_nocache (port1offset, 16);
1005 /* Default buffer end is 5120 bytes below the top of RAM. */
1006 u32 default_value = card->port[0].end_offset - 0x1400;
1009 /* Install the default value first, so we don't have to repeatedly
1010 do it if there is a problem. */
1011 card->port[0].end_offset = default_value;
1014 printk (KERN_ERR "NM256: Unable to scan for card signature in video RAM\n");
1018 if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1019 u32 pointer = readl (temp + 4);
1022 * If it's obviously invalid, don't use it (the port already has a
1023 * suitable default value set).
1025 if (pointer != 0xffffffff)
1026 card->port[0].end_offset = pointer;
1028 printk (KERN_INFO "NM256: Found card signature in video RAM: 0x%x\n",
1036 * Install a driver for the PCI device referenced by PCIDEV.
1037 * VERSTR is a human-readable version string.
1040 static int __devinit
1041 nm256_install(struct pci_dev *pcidev, enum nm256rev rev, char *verstr)
1043 struct nm256_info *card;
1046 if (pci_enable_device(pcidev))
1049 card = kmalloc (sizeof (struct nm256_info), GFP_KERNEL);
1051 printk (KERN_ERR "NM256: out of memory!\n");
1055 card->magsig = NM_MAGIC_SIG;
1057 card->recording = 0;
1059 spin_lock_init(&card->lock);
1061 /* Init the memory port info. */
1062 for (x = 0; x < 2; x++) {
1063 card->port[x].physaddr = pci_resource_start (pcidev, x);
1064 card->port[x].ptr = NULL;
1065 card->port[x].start_offset = 0;
1066 card->port[x].end_offset = 0;
1069 /* Port 2 is easy. */
1070 card->port[1].start_offset = 0;
1071 card->port[1].end_offset = NM_PORT2_SIZE;
1073 /* Yuck. But we have to map in port 2 so we can check how much RAM the
1075 if (nm256_remap_ports (card)) {
1081 * The NM256 has two memory ports. The first port is nothing
1082 * more than a chunk of video RAM, which is used as the I/O ring
1083 * buffer. The second port has the actual juicy stuff (like the
1084 * mixer and the playback engine control registers).
1087 if (card->rev == REV_NM256AV) {
1088 /* Ok, try to see if this is a non-AC97 version of the hardware. */
1089 int pval = nm256_readPort16 (card, 2, NM_MIXER_PRESENCE);
1090 if ((pval & NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) {
1092 printk (KERN_ERR "NM256: This doesn't look to me like the AC97-compatible version.\n");
1093 printk (KERN_ERR " You can force the driver to load by passing in the module\n");
1094 printk (KERN_ERR " parameter:\n");
1095 printk (KERN_ERR " force_load = 1\n");
1096 printk (KERN_ERR "\n");
1097 printk (KERN_ERR " More likely, you should be using the appropriate SB-16 or\n");
1098 printk (KERN_ERR " CS4232 driver instead. (If your BIOS has settings for\n");
1099 printk (KERN_ERR " IRQ and/or DMA for the sound card, this is *not* the correct\n");
1100 printk (KERN_ERR " driver to use.)\n");
1101 nm256_release_ports (card);
1106 printk (KERN_INFO "NM256: Forcing driver load as per user request.\n");
1110 /* printk (KERN_INFO "NM256: Congratulations. You're not running Eunice.\n")*/;
1112 card->port[0].end_offset = 2560 * 1024;
1113 card->introutine = nm256_interrupt;
1114 card->mixer_status_offset = NM_MIXER_STATUS_OFFSET;
1115 card->mixer_status_mask = NM_MIXER_READY_MASK;
1118 /* Not sure if there is any relevant detect for the ZX or not. */
1119 if (nm256_readPort8 (card, 2, 0xa0b) != 0)
1120 card->port[0].end_offset = 6144 * 1024;
1122 card->port[0].end_offset = 4096 * 1024;
1124 card->introutine = nm256_interrupt_zx;
1125 card->mixer_status_offset = NM2_MIXER_STATUS_OFFSET;
1126 card->mixer_status_mask = NM2_MIXER_READY_MASK;
1129 if (buffertop >= 98304 && buffertop < card->port[0].end_offset)
1130 card->port[0].end_offset = buffertop;
1132 nm256_peek_for_sig (card);
1134 card->port[0].start_offset = card->port[0].end_offset - 98304;
1136 printk (KERN_INFO "NM256: Mapping port 1 from 0x%x - 0x%x\n",
1137 card->port[0].start_offset, card->port[0].end_offset);
1139 if (nm256_remap_ports (card)) {
1144 /* See if we can get the interrupt. */
1146 card->irq = pcidev->irq;
1149 if (nm256_grabInterrupt (card) != 0) {
1150 nm256_release_ports (card);
1155 nm256_releaseInterrupt (card);
1161 card->playbackBufferSize = 16384;
1162 card->recordBufferSize = 16384;
1164 card->coeffBuf = card->port[0].end_offset - NM_MAX_COEFFICIENT;
1165 card->abuf2 = card->coeffBuf - card->recordBufferSize;
1166 card->abuf1 = card->abuf2 - card->playbackBufferSize;
1167 card->allCoeffBuf = card->abuf2 - (NM_TOTAL_COEFF_COUNT * 4);
1169 /* Fixed setting. */
1170 card->mixer = NM_MIXER_OFFSET;
1171 card->mixer_values_init = 0;
1173 card->is_open_play = 0;
1174 card->is_open_record = 0;
1176 card->coeffsCurrent = 0;
1178 card->opencnt[0] = 0; card->opencnt[1] = 0;
1180 /* Reasonable default settings, but largely unnecessary. */
1181 for (x = 0; x < 2; x++) {
1182 card->sinfo[x].bits = 8;
1183 card->sinfo[x].stereo = 0;
1184 card->sinfo[x].samplerate = 8000;
1187 nm256_initHw (card);
1189 for (x = 0; x < 2; x++) {
1191 sound_install_audiodrv(AUDIO_DRIVER_VERSION,
1192 "NM256", &nm256_audio_driver,
1193 sizeof(struct audio_driver),
1194 DMA_NODMA, AFMT_U8 | AFMT_S16_LE,
1195 NULL, -1, -1)) >= 0) {
1196 /* 1K minimum buffer size. */
1197 audio_devs[card->dev[x]]->min_fragment = 10;
1198 /* Maximum of 8K buffer size. */
1199 audio_devs[card->dev[x]]->max_fragment = 13;
1202 printk(KERN_ERR "NM256: Too many PCM devices available\n");
1203 nm256_release_ports (card);
1209 pci_set_drvdata(pcidev,card);
1211 /* Insert the card in the list. */
1212 card->next_card = nmcard_list;
1215 printk(KERN_INFO "Initialized NeoMagic %s audio in PCI native mode\n",
1219 * And our mixer. (We should allow support for other mixers, maybe.)
1222 nm256_install_mixer (card);
1228 static int __devinit
1229 nm256_probe(struct pci_dev *pcidev,const struct pci_device_id *pciid)
1231 if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO)
1232 return nm256_install(pcidev, REV_NM256AV, "256AV");
1233 if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO)
1234 return nm256_install(pcidev, REV_NM256ZX, "256ZX");
1235 if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO)
1236 return nm256_install(pcidev, REV_NM256ZX, "256XL+");
1237 return -1; /* should not come here ... */
1240 static void __devinit
1241 nm256_remove(struct pci_dev *pcidev) {
1242 struct nm256_info *xcard = pci_get_drvdata(pcidev);
1243 struct nm256_info *card,*next_card = NULL;
1245 for (card = nmcard_list; card != NULL; card = next_card) {
1246 next_card = card->next_card;
1247 if (card == xcard) {
1251 free_irq (card->irq, card);
1252 nm256_release_ports (card);
1253 sound_unload_mixerdev (card->mixer_oss_dev);
1254 sound_unload_audiodev (card->dev[0]);
1255 sound_unload_audiodev (card->dev[1]);
1260 if (nmcard_list == card)
1261 nmcard_list = next_card;
1268 * MODE - mode to open device (logical OR of OPEN_READ and OPEN_WRITE)
1270 * Called when opening the DMAbuf (dmabuf.c:259)
1273 nm256_audio_open(int dev, int mode)
1275 struct nm256_info *card = nm256_find_card (dev);
1281 if (card->dev[0] == dev)
1283 else if (card->dev[1] == dev)
1288 if (card->opencnt[w] > 0)
1291 /* No bits set? Huh? */
1292 if (! ((mode & OPEN_READ) || (mode & OPEN_WRITE)))
1296 * If it's open for both read and write, and the card's currently
1297 * being read or written to, then do the opposite of what has
1298 * already been done. Otherwise, don't specify any mode until the
1299 * user actually tries to do I/O. (Some programs open the device
1300 * for both read and write, but only actually do reading or writing.)
1303 if ((mode & OPEN_WRITE) && (mode & OPEN_READ)) {
1304 if (card->is_open_play)
1306 else if (card->is_open_record)
1311 if (mode & OPEN_WRITE) {
1312 if (card->is_open_play == 0) {
1313 card->dev_for_play = dev;
1314 card->is_open_play = 1;
1320 if (mode & OPEN_READ) {
1321 if (card->is_open_record == 0) {
1322 card->dev_for_record = dev;
1323 card->is_open_record = 1;
1338 * Called when closing the DMAbuf (dmabuf.c:477)
1342 nm256_audio_close(int dev)
1344 struct nm256_info *card = nm256_find_card (dev);
1349 if (card->dev[0] == dev)
1351 else if (card->dev[1] == dev)
1357 if (card->opencnt[w] <= 0) {
1358 card->opencnt[w] = 0;
1360 if (card->dev_for_play == dev) {
1362 card->is_open_play = 0;
1363 card->dev_for_play = -1;
1366 if (card->dev_for_record == dev) {
1368 card->is_open_record = 0;
1369 card->dev_for_record = -1;
1375 /* Standard ioctl handler. */
1377 nm256_audio_ioctl(int dev, unsigned int cmd, void __user *arg)
1383 struct nm256_info *card = nm256_find_card (dev);
1388 if (dev == card->dev[0])
1394 * The code here is messy. There are probably better ways to do
1395 * it. (It should be possible to handle it the same way the AC97 mixer
1400 case SOUND_PCM_WRITE_RATE:
1401 if (get_user(ret, (int __user *) arg))
1405 oldinfo = card->sinfo[w].samplerate;
1406 card->sinfo[w].samplerate = ret;
1407 ret = nm256_setInfo(dev, card);
1409 card->sinfo[w].samplerate = oldinfo;
1412 ret = card->sinfo[w].samplerate;
1415 case SOUND_PCM_READ_RATE:
1416 ret = card->sinfo[w].samplerate;
1419 case SNDCTL_DSP_STEREO:
1420 if (get_user(ret, (int __user *) arg))
1423 card->sinfo[w].stereo = ret ? 1 : 0;
1424 ret = nm256_setInfo (dev, card);
1426 ret = card->sinfo[w].stereo;
1430 case SOUND_PCM_WRITE_CHANNELS:
1431 if (get_user(ret, (int __user *) arg))
1434 if (ret < 1 || ret > 3)
1435 ret = card->sinfo[w].stereo + 1;
1437 card->sinfo[w].stereo = ret - 1;
1438 ret = nm256_setInfo (dev, card);
1440 ret = card->sinfo[w].stereo + 1;
1444 case SOUND_PCM_READ_CHANNELS:
1445 ret = card->sinfo[w].stereo + 1;
1448 case SNDCTL_DSP_SETFMT:
1449 if (get_user(ret, (int __user *) arg))
1453 oldinfo = card->sinfo[w].bits;
1454 card->sinfo[w].bits = ret;
1455 ret = nm256_setInfo (dev, card);
1457 card->sinfo[w].bits = oldinfo;
1460 ret = card->sinfo[w].bits;
1463 case SOUND_PCM_READ_BITS:
1464 ret = card->sinfo[w].bits;
1470 return put_user(ret, (int __user *) arg);
1474 * Given the sound device DEV and an associated physical buffer PHYSBUF,
1475 * return a pointer to the actual buffer in kernel space.
1477 * This routine should exist as part of the soundcore routines.
1481 nm256_getDMAbuffer (int dev, unsigned long physbuf)
1483 struct audio_operations *adev = audio_devs[dev];
1484 struct dma_buffparms *dmap = adev->dmap_out;
1486 (char *)(physbuf - (unsigned long)dmap->raw_buf_phys
1487 + (unsigned long)dmap->raw_buf);
1494 * Output a block to sound device
1496 * dev - device number
1497 * buf - physical address of buffer
1498 * total_count - total byte count in buffer
1499 * intrflag - set if this has been called from an interrupt
1500 * (via DMAbuf_outputintr)
1501 * restart_dma - set if engine needs to be re-initialised
1504 * 1. Starting output (dmabuf.c:1327)
1505 * 2. (dmabuf.c:1504)
1506 * 3. A new buffer needs to be sent to the device (dmabuf.c:1579)
1509 nm256_audio_output_block(int dev, unsigned long physbuf,
1510 int total_count, int intrflag)
1512 struct nm256_info *card = nm256_find_card (dev);
1515 char *dma_buf = nm256_getDMAbuffer (dev, physbuf);
1516 card->is_open_play = 1;
1517 card->dev_for_play = dev;
1518 nm256_write_block (card, dma_buf, total_count);
1522 /* Ditto, but do recording instead. */
1524 nm256_audio_start_input(int dev, unsigned long physbuf, int count,
1527 struct nm256_info *card = nm256_find_card (dev);
1530 char *dma_buf = nm256_getDMAbuffer (dev, physbuf);
1531 card->is_open_record = 1;
1532 card->dev_for_record = dev;
1533 nm256_startRecording (card, dma_buf, count);
1538 * Prepare for inputting samples to DEV.
1539 * Each requested buffer will be BSIZE byes long, with a total of
1544 nm256_audio_prepare_for_input(int dev, int bsize, int bcount)
1546 struct nm256_info *card = nm256_find_card (dev);
1551 if (card->is_open_record && card->dev_for_record != dev)
1554 audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
1559 * Prepare for outputting samples to `dev'
1561 * Each buffer that will be passed will be `bsize' bytes long,
1562 * with a total of `bcount' buffers.
1565 * 1. A trigger enables audio output (dmabuf.c:978)
1566 * 2. We get a write buffer without dma_mode setup (dmabuf.c:1152)
1567 * 3. We restart a transfer (dmabuf.c:1324)
1571 nm256_audio_prepare_for_output(int dev, int bsize, int bcount)
1573 struct nm256_info *card = nm256_find_card (dev);
1578 if (card->is_open_play && card->dev_for_play != dev)
1581 audio_devs[dev]->dmap_out->flags |= DMA_NODMA;
1585 /* Stop the current operations associated with DEV. */
1587 nm256_audio_reset(int dev)
1589 struct nm256_info *card = nm256_find_card (dev);
1592 if (card->dev_for_play == dev)
1594 if (card->dev_for_record == dev)
1600 nm256_audio_local_qlen(int dev)
1605 static struct audio_driver nm256_audio_driver =
1607 .owner = THIS_MODULE,
1608 .open = nm256_audio_open,
1609 .close = nm256_audio_close,
1610 .output_block = nm256_audio_output_block,
1611 .start_input = nm256_audio_start_input,
1612 .ioctl = nm256_audio_ioctl,
1613 .prepare_for_input = nm256_audio_prepare_for_input,
1614 .prepare_for_output = nm256_audio_prepare_for_output,
1615 .halt_io = nm256_audio_reset,
1616 .local_qlen = nm256_audio_local_qlen,
1619 static struct pci_device_id nm256_pci_tbl[] = {
1620 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO,
1621 PCI_ANY_ID, PCI_ANY_ID, 0, 0},
1622 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO,
1623 PCI_ANY_ID, PCI_ANY_ID, 0, 0},
1624 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO,
1625 PCI_ANY_ID, PCI_ANY_ID, 0, 0},
1628 MODULE_DEVICE_TABLE(pci, nm256_pci_tbl);
1629 MODULE_LICENSE("GPL");
1632 static struct pci_driver nm256_pci_driver = {
1633 .name = "nm256_audio",
1634 .id_table = nm256_pci_tbl,
1635 .probe = nm256_probe,
1636 .remove = nm256_remove,
1639 module_param(usecache, bool, 0);
1640 module_param(buffertop, int, 0);
1641 module_param(nm256_debug, bool, 0644);
1642 module_param(force_load, bool, 0);
1644 static int __init do_init_nm256(void)
1646 printk (KERN_INFO "NeoMagic 256AV/256ZX audio driver, version 1.1p\n");
1647 return pci_register_driver(&nm256_pci_driver);
1650 static void __exit cleanup_nm256 (void)
1652 pci_unregister_driver(&nm256_pci_driver);
1655 module_init(do_init_nm256);
1656 module_exit(cleanup_nm256);