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/delay.h>
29 #include <linux/spinlock.h>
30 #include "sound_config.h"
32 #include "nm256_coeff.h"
35 static int force_load
;
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
, struct pt_regs
*dummy
);
49 static irqreturn_t
nm256_interrupt_zx (int irq
, void *dev_id
, struct pt_regs
*dummy
);
50 static int handle_pm_event (struct pm_dev
*dev
, pm_request_t rqst
, void *data
);
52 /* These belong in linux/pci.h. */
53 #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
54 #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
55 #define PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO 0x8016
58 static struct nm256_info
*nmcard_list
;
60 /* Release the mapped-in memory for CARD. */
62 nm256_release_ports (struct nm256_info
*card
)
66 for (x
= 0; x
< 2; x
++) {
67 if (card
->port
[x
].ptr
!= NULL
) {
68 iounmap (card
->port
[x
].ptr
);
69 card
->port
[x
].ptr
= NULL
;
75 * Map in the memory ports for CARD, if they aren't already mapped in
76 * and have been configured. If successful, a zero value is returned;
77 * otherwise any previously mapped-in areas are released and a non-zero
80 * This is invoked twice, once for each port. Ideally it would only be
81 * called once, but we now need to map in the second port in order to
82 * check how much memory the card has on the 256ZX.
85 nm256_remap_ports (struct nm256_info
*card
)
89 for (x
= 0; x
< 2; x
++) {
90 if (card
->port
[x
].ptr
== NULL
&& card
->port
[x
].end_offset
> 0) {
92 = card
->port
[x
].physaddr
+ card
->port
[x
].start_offset
;
94 = card
->port
[x
].end_offset
- card
->port
[x
].start_offset
;
96 card
->port
[x
].ptr
= ioremap_nocache (physaddr
, size
);
98 if (card
->port
[x
].ptr
== NULL
) {
99 printk (KERN_ERR
"NM256: Unable to remap port %d\n", x
+ 1);
100 nm256_release_ports (card
);
108 /* Locate the card in our list. */
109 static struct nm256_info
*
110 nm256_find_card (int dev
)
112 struct nm256_info
*card
;
114 for (card
= nmcard_list
; card
!= NULL
; card
= card
->next_card
)
115 if (card
->dev
[0] == dev
|| card
->dev
[1] == dev
)
122 * Ditto, but find the card struct corresponding to the mixer device DEV
125 static struct nm256_info
*
126 nm256_find_card_for_mixer (int dev
)
128 struct nm256_info
*card
;
130 for (card
= nmcard_list
; card
!= NULL
; card
= card
->next_card
)
131 if (card
->mixer_oss_dev
== dev
)
138 static int buffertop
;
140 /* Check to see if we're using the bank of cached coefficients. */
142 nm256_cachedCoefficients (struct nm256_info
*card
)
147 /* The actual rates supported by the card. */
148 static int samplerates
[9] = {
149 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 99999999
153 * Set the card samplerate, word size and stereo mode to correspond to
154 * the settings in the CARD struct for the specified device in DEV.
155 * We keep two separate sets of information, one for each device; the
156 * hardware is not actually configured until a read or write is
161 nm256_setInfo (int dev
, struct nm256_info
*card
)
167 if (card
->dev
[0] == dev
)
169 else if (card
->dev
[1] == dev
)
174 targetrate
= card
->sinfo
[w
].samplerate
;
176 if ((card
->sinfo
[w
].bits
!= 8 && card
->sinfo
[w
].bits
!= 16)
177 || targetrate
< samplerates
[0]
178 || targetrate
> samplerates
[7])
181 for (x
= 0; x
< 8; x
++)
182 if (targetrate
< ((samplerates
[x
] + samplerates
[x
+ 1]) / 2))
186 u8 ratebits
= ((x
<< 4) & NM_RATE_MASK
);
187 if (card
->sinfo
[w
].bits
== 16)
188 ratebits
|= NM_RATE_BITS_16
;
189 if (card
->sinfo
[w
].stereo
)
190 ratebits
|= NM_RATE_STEREO
;
192 card
->sinfo
[w
].samplerate
= samplerates
[x
];
195 if (card
->dev_for_play
== dev
&& card
->playing
) {
197 printk (KERN_DEBUG
"Setting play ratebits to 0x%x\n",
199 nm256_loadCoefficient (card
, 0, x
);
200 nm256_writePort8 (card
, 2,
201 NM_PLAYBACK_REG_OFFSET
+ NM_RATE_REG_OFFSET
,
205 if (card
->dev_for_record
== dev
&& card
->recording
) {
207 printk (KERN_DEBUG
"Setting record ratebits to 0x%x\n",
209 nm256_loadCoefficient (card
, 1, x
);
210 nm256_writePort8 (card
, 2,
211 NM_RECORD_REG_OFFSET
+ NM_RATE_REG_OFFSET
,
220 /* Start the play process going. */
222 startPlay (struct nm256_info
*card
)
224 if (! card
->playing
) {
226 if (nm256_grabInterrupt (card
) == 0) {
227 nm256_setInfo (card
->dev_for_play
, card
);
229 /* Enable playback engine and interrupts. */
230 nm256_writePort8 (card
, 2, NM_PLAYBACK_ENABLE_REG
,
231 NM_PLAYBACK_ENABLE_FLAG
| NM_PLAYBACK_FREERUN
);
233 /* Enable both channels. */
234 nm256_writePort16 (card
, 2, NM_AUDIO_MUTE_REG
, 0x0);
240 * Request one chunk of AMT bytes from the recording device. When the
241 * operation is complete, the data will be copied into BUFFER and the
242 * function DMAbuf_inputintr will be invoked.
246 nm256_startRecording (struct nm256_info
*card
, char *buffer
, u32 amt
)
249 int enableEngine
= 0;
250 u32 ringsize
= card
->recordBufferSize
;
253 if (amt
> (ringsize
/ 2)) {
255 * Of course this won't actually work right, because the
256 * caller is going to assume we will give what we got asked
259 printk (KERN_ERR
"NM256: Read request too large: %d\n", amt
);
264 printk (KERN_ERR
"NM256: Read request too small; %d\n", amt
);
268 spin_lock_irqsave(&card
->lock
,flags
);
270 * If we're not currently recording, set up the start and end registers
271 * for the recording engine.
273 if (! card
->recording
) {
275 if (nm256_grabInterrupt (card
) == 0) {
277 nm256_setInfo (card
->dev_for_record
, card
);
278 nm256_writePort32 (card
, 2, NM_RBUFFER_START
, card
->abuf2
);
279 nm256_writePort32 (card
, 2, NM_RBUFFER_END
,
280 card
->abuf2
+ ringsize
);
282 nm256_writePort32 (card
, 2, NM_RBUFFER_CURRP
,
283 card
->abuf2
+ card
->curRecPos
);
287 /* Not sure what else to do here. */
288 spin_unlock_irqrestore(&card
->lock
,flags
);
294 * If we happen to go past the end of the buffer a bit (due to a
295 * delayed interrupt) it's OK. So might as well set the watermark
296 * right at the end of the data we want.
298 endpos
= card
->abuf2
+ ((card
->curRecPos
+ amt
) % ringsize
);
300 card
->recBuf
= buffer
;
301 card
->requestedRecAmt
= amt
;
302 nm256_writePort32 (card
, 2, NM_RBUFFER_WMARK
, endpos
);
303 /* Enable recording engine and interrupts. */
305 nm256_writePort8 (card
, 2, NM_RECORD_ENABLE_REG
,
306 NM_RECORD_ENABLE_FLAG
| NM_RECORD_FREERUN
);
308 spin_unlock_irqrestore(&card
->lock
,flags
);
311 /* Stop the play engine. */
313 stopPlay (struct nm256_info
*card
)
315 /* Shut off sound from both channels. */
316 nm256_writePort16 (card
, 2, NM_AUDIO_MUTE_REG
,
317 NM_AUDIO_MUTE_LEFT
| NM_AUDIO_MUTE_RIGHT
);
318 /* Disable play engine. */
319 nm256_writePort8 (card
, 2, NM_PLAYBACK_ENABLE_REG
, 0);
321 nm256_releaseInterrupt (card
);
323 /* Reset the relevant state bits. */
325 card
->curPlayPos
= 0;
329 /* Stop recording. */
331 stopRecord (struct nm256_info
*card
)
333 /* Disable recording engine. */
334 nm256_writePort8 (card
, 2, NM_RECORD_ENABLE_REG
, 0);
336 if (card
->recording
) {
337 nm256_releaseInterrupt (card
);
345 * Ring buffers, man. That's where the hip-hop, wild-n-wooly action's at.
346 * 1972? (Well, I suppose it was cheep-n-easy to implement.)
348 * Write AMT bytes of BUFFER to the playback ring buffer, and start the
349 * playback engine running. It will only accept up to 1/2 of the total
350 * size of the ring buffer. No check is made that we're about to overwrite
351 * the currently-playing sample.
355 nm256_write_block (struct nm256_info
*card
, char *buffer
, u32 amt
)
357 u32 ringsize
= card
->playbackBufferSize
;
361 if (amt
> (ringsize
/ 2)) {
362 printk (KERN_ERR
"NM256: Write request too large: %d\n", amt
);
363 amt
= (ringsize
/ 2);
366 if (amt
< NM256_PLAY_WMARK_SIZE
) {
367 printk (KERN_ERR
"NM256: Write request too small: %d\n", amt
);
371 card
->curPlayPos
%= ringsize
;
373 card
->requested_amt
= amt
;
375 spin_lock_irqsave(&card
->lock
,flags
);
377 if ((card
->curPlayPos
+ amt
) >= ringsize
) {
378 u32 rem
= ringsize
- card
->curPlayPos
;
380 nm256_writeBuffer8 (card
, buffer
, 1,
381 card
->abuf1
+ card
->curPlayPos
,
384 nm256_writeBuffer8 (card
, buffer
+ rem
, 1, card
->abuf1
,
388 nm256_writeBuffer8 (card
, buffer
, 1,
389 card
->abuf1
+ card
->curPlayPos
,
393 * Setup the start-n-stop-n-limit registers, and start that engine
396 * Normally we just let it wrap around to avoid the click-click
399 if (! card
->playing
) {
400 /* The PBUFFER_END register in this case points to one sample
401 before the end of the buffer. */
402 int w
= (card
->dev_for_play
== card
->dev
[0] ? 0 : 1);
403 int sampsize
= (card
->sinfo
[w
].bits
== 16 ? 2 : 1);
405 if (card
->sinfo
[w
].stereo
)
408 /* Need to set the not-normally-changing-registers up. */
409 nm256_writePort32 (card
, 2, NM_PBUFFER_START
,
410 card
->abuf1
+ card
->curPlayPos
);
411 nm256_writePort32 (card
, 2, NM_PBUFFER_END
,
412 card
->abuf1
+ ringsize
- sampsize
);
413 nm256_writePort32 (card
, 2, NM_PBUFFER_CURRP
,
414 card
->abuf1
+ card
->curPlayPos
);
416 endstop
= (card
->curPlayPos
+ amt
- NM256_PLAY_WMARK_SIZE
) % ringsize
;
417 nm256_writePort32 (card
, 2, NM_PBUFFER_WMARK
, card
->abuf1
+ endstop
);
422 spin_unlock_irqrestore(&card
->lock
,flags
);
425 /* We just got a card playback interrupt; process it. */
427 nm256_get_new_block (struct nm256_info
*card
)
429 /* Check to see how much got played so far. */
430 u32 amt
= nm256_readPort32 (card
, 2, NM_PBUFFER_CURRP
) - card
->abuf1
;
432 if (amt
>= card
->playbackBufferSize
) {
433 printk (KERN_ERR
"NM256: Sound playback pointer invalid!\n");
437 if (amt
< card
->curPlayPos
)
438 amt
= (card
->playbackBufferSize
- card
->curPlayPos
) + amt
;
440 amt
-= card
->curPlayPos
;
442 if (card
->requested_amt
> (amt
+ NM256_PLAY_WMARK_SIZE
)) {
444 card
->curPlayPos
+ card
->requested_amt
- NM256_PLAY_WMARK_SIZE
;
445 nm256_writePort32 (card
, 2, NM_PBUFFER_WMARK
, card
->abuf1
+ endstop
);
448 card
->curPlayPos
+= card
->requested_amt
;
449 /* Get a new block to write. This will eventually invoke
450 nm256_write_block () or stopPlay (). */
451 DMAbuf_outputintr (card
->dev_for_play
, 1);
456 * Read the last-recorded block from the ring buffer, copy it into the
457 * saved buffer pointer, and invoke DMAuf_inputintr() with the recording
462 nm256_read_block (struct nm256_info
*card
)
464 /* Grab the current position of the recording pointer. */
465 u32 currptr
= nm256_readPort32 (card
, 2, NM_RBUFFER_CURRP
) - card
->abuf2
;
466 u32 amtToRead
= card
->requestedRecAmt
;
467 u32 ringsize
= card
->recordBufferSize
;
469 if (currptr
>= card
->recordBufferSize
) {
470 printk (KERN_ERR
"NM256: Sound buffer record pointer invalid!\n");
475 * This test is probably redundant; we shouldn't be here unless
478 if (card
->recording
) {
479 /* If we wrapped around, copy everything from the start of our
480 recording buffer to the end of the buffer. */
481 if (currptr
< card
->curRecPos
) {
482 u32 amt
= min (ringsize
- card
->curRecPos
, amtToRead
);
484 nm256_readBuffer8 (card
, card
->recBuf
, 1,
485 card
->abuf2
+ card
->curRecPos
,
488 card
->curRecPos
+= amt
;
490 if (card
->curRecPos
== ringsize
)
494 if ((card
->curRecPos
< currptr
) && (amtToRead
> 0)) {
495 u32 amt
= min (currptr
- card
->curRecPos
, amtToRead
);
496 nm256_readBuffer8 (card
, card
->recBuf
, 1,
497 card
->abuf2
+ card
->curRecPos
, amt
);
498 card
->curRecPos
= ((card
->curRecPos
+ amt
) % ringsize
);
501 card
->requestedRecAmt
= 0;
502 DMAbuf_inputintr (card
->dev_for_record
);
507 * Initialize the hardware.
510 nm256_initHw (struct nm256_info
*card
)
512 /* Reset everything. */
513 nm256_writePort8 (card
, 2, 0x0, 0x11);
514 nm256_writePort16 (card
, 2, 0x214, 0);
521 * Handle a potential interrupt for the device referred to by DEV_ID.
523 * I don't like the cut-n-paste job here either between the two routines,
524 * but there are sufficient differences between the two interrupt handlers
525 * that parameterizing it isn't all that great either. (Could use a macro,
526 * I suppose...yucky bleah.)
530 nm256_interrupt (int irq
, void *dev_id
, struct pt_regs
*dummy
)
532 struct nm256_info
*card
= (struct nm256_info
*)dev_id
;
534 static int badintrcount
;
537 if ((card
== NULL
) || (card
->magsig
!= NM_MAGIC_SIG
)) {
538 printk (KERN_ERR
"NM256: Bad card pointer\n");
542 status
= nm256_readPort16 (card
, 2, NM_INT_REG
);
546 if (badintrcount
++ > 1000) {
548 * I'm not sure if the best thing is to stop the card from
549 * playing or just release the interrupt (after all, we're in
550 * a bad situation, so doing fancy stuff may not be such a good
553 * I worry about the card engine continuing to play noise
554 * over and over, however--that could become a very
555 * obnoxious problem. And we know that when this usually
556 * happens things are fairly safe, it just means the user's
557 * inserted a PCMCIA card and someone's spamming us with IRQ 9s.
567 return IRQ_RETVAL(handled
);
572 /* Rather boring; check for individual interrupts and process them. */
574 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
) {
585 status
&= ~NM_RECORD_INT
;
586 NM_ACK_INT (card
, NM_RECORD_INT
);
589 nm256_read_block (card
);
592 if (status
& NM_MISC_INT_1
) {
596 status
&= ~NM_MISC_INT_1
;
597 printk (KERN_ERR
"NM256: Got misc interrupt #1\n");
598 NM_ACK_INT (card
, NM_MISC_INT_1
);
599 nm256_writePort16 (card
, 2, NM_INT_REG
, 0x8000);
600 cbyte
= nm256_readPort8 (card
, 2, 0x400);
601 nm256_writePort8 (card
, 2, 0x400, cbyte
| 2);
604 if (status
& NM_MISC_INT_2
) {
608 status
&= ~NM_MISC_INT_2
;
609 printk (KERN_ERR
"NM256: Got misc interrupt #2\n");
610 NM_ACK_INT (card
, NM_MISC_INT_2
);
611 cbyte
= nm256_readPort8 (card
, 2, 0x400);
612 nm256_writePort8 (card
, 2, 0x400, cbyte
& ~2);
615 /* Unknown interrupt. */
618 printk (KERN_ERR
"NM256: Fire in the hole! Unknown status 0x%x\n",
621 NM_ACK_INT (card
, status
);
623 return IRQ_RETVAL(handled
);
627 * Handle a potential interrupt for the device referred to by DEV_ID.
628 * This handler is for the 256ZX, and is very similar to the non-ZX
633 nm256_interrupt_zx (int irq
, void *dev_id
, struct pt_regs
*dummy
)
635 struct nm256_info
*card
= (struct nm256_info
*)dev_id
;
637 static int badintrcount
;
640 if ((card
== NULL
) || (card
->magsig
!= NM_MAGIC_SIG
)) {
641 printk (KERN_ERR
"NM256: Bad card pointer\n");
645 status
= nm256_readPort32 (card
, 2, NM_INT_REG
);
649 if (badintrcount
++ > 1000) {
650 printk (KERN_ERR
"NM256: Releasing interrupt, over 1000 invalid interrupts\n");
652 * I'm not sure if the best thing is to stop the card from
653 * playing or just release the interrupt (after all, we're in
654 * a bad situation, so doing fancy stuff may not be such a good
657 * I worry about the card engine continuing to play noise
658 * over and over, however--that could become a very
659 * obnoxious problem. And we know that when this usually
660 * happens things are fairly safe, it just means the user's
661 * inserted a PCMCIA card and someone's spamming us with
672 return IRQ_RETVAL(handled
);
677 /* Rather boring; check for individual interrupts and process them. */
679 if (status
& NM2_PLAYBACK_INT
) {
681 status
&= ~NM2_PLAYBACK_INT
;
682 NM2_ACK_INT (card
, NM2_PLAYBACK_INT
);
685 nm256_get_new_block (card
);
688 if (status
& NM2_RECORD_INT
) {
690 status
&= ~NM2_RECORD_INT
;
691 NM2_ACK_INT (card
, NM2_RECORD_INT
);
694 nm256_read_block (card
);
697 if (status
& NM2_MISC_INT_1
) {
701 status
&= ~NM2_MISC_INT_1
;
702 printk (KERN_ERR
"NM256: Got misc interrupt #1\n");
703 NM2_ACK_INT (card
, NM2_MISC_INT_1
);
704 cbyte
= nm256_readPort8 (card
, 2, 0x400);
705 nm256_writePort8 (card
, 2, 0x400, cbyte
| 2);
708 if (status
& NM2_MISC_INT_2
) {
712 status
&= ~NM2_MISC_INT_2
;
713 printk (KERN_ERR
"NM256: Got misc interrupt #2\n");
714 NM2_ACK_INT (card
, NM2_MISC_INT_2
);
715 cbyte
= nm256_readPort8 (card
, 2, 0x400);
716 nm256_writePort8 (card
, 2, 0x400, cbyte
& ~2);
719 /* Unknown interrupt. */
722 printk (KERN_ERR
"NM256: Fire in the hole! Unknown status 0x%x\n",
725 NM2_ACK_INT (card
, status
);
727 return IRQ_RETVAL(handled
);
731 * Request our interrupt.
734 nm256_grabInterrupt (struct nm256_info
*card
)
736 if (card
->has_irq
++ == 0) {
737 if (request_irq (card
->irq
, card
->introutine
, SA_SHIRQ
,
738 "NM256_audio", card
) < 0) {
739 printk (KERN_ERR
"NM256: can't obtain IRQ %d\n", card
->irq
);
747 * Release our interrupt.
750 nm256_releaseInterrupt (struct nm256_info
*card
)
752 if (card
->has_irq
<= 0) {
753 printk (KERN_ERR
"nm256: too many calls to releaseInterrupt\n");
757 if (card
->has_irq
== 0) {
758 free_irq (card
->irq
, card
);
764 * Waits for the mixer to become ready to be written; returns a zero value
769 nm256_isReady (struct ac97_hwint
*dev
)
771 struct nm256_info
*card
= (struct nm256_info
*)dev
->driver_private
;
777 if (card
->magsig
!= NM_MAGIC_SIG
) {
778 printk (KERN_ERR
"NM256: Bad magic signature in isReady!\n");
782 testaddr
= card
->mixer_status_offset
;
783 testb
= card
->mixer_status_mask
;
786 * Loop around waiting for the mixer to become ready.
788 while (! done
&& t2
-- > 0) {
789 if ((nm256_readPort16 (card
, 2, testaddr
) & testb
) == 0)
798 * Return the contents of the AC97 mixer register REG. Returns a positive
799 * value if successful, or a negative error code.
802 nm256_readAC97Reg (struct ac97_hwint
*dev
, u8 reg
)
804 struct nm256_info
*card
= (struct nm256_info
*)dev
->driver_private
;
806 if (card
->magsig
!= NM_MAGIC_SIG
) {
807 printk (KERN_ERR
"NM256: Bad magic signature in readAC97Reg!\n");
815 res
= nm256_readPort16 (card
, 2, card
->mixer
+ reg
);
816 /* Magic delay. Bleah yucky. */
825 * Writes VALUE to AC97 mixer register REG. Returns 0 if successful, or
826 * a negative error code.
829 nm256_writeAC97Reg (struct ac97_hwint
*dev
, u8 reg
, u16 value
)
836 struct nm256_info
*card
= (struct nm256_info
*)dev
->driver_private
;
838 if (card
->magsig
!= NM_MAGIC_SIG
) {
839 printk (KERN_ERR
"NM256: Bad magic signature in writeAC97Reg!\n");
845 spin_lock_irqsave(&card
->lock
,flags
);
849 /* Wait for the write to take, too. */
850 while ((tries
-- > 0) && !done
) {
851 nm256_writePort16 (card
, 2, base
+ reg
, value
);
852 if (nm256_isReady (dev
)) {
859 spin_unlock_irqrestore(&card
->lock
,flags
);
866 * Initial register values to be written to the AC97 mixer.
867 * While most of these are identical to the reset values, we do this
868 * so that we have most of the register contents cached--this avoids
869 * reading from the mixer directly (which seems to be problematic,
870 * probably due to ignorance).
875 unsigned short value
;
878 static struct initialValues nm256_ac97_initial_values
[] =
880 { AC97_MASTER_VOL_STEREO
, 0x8000 },
881 { AC97_HEADPHONE_VOL
, 0x8000 },
882 { AC97_MASTER_VOL_MONO
, 0x0000 },
883 { AC97_PCBEEP_VOL
, 0x0000 },
884 { AC97_PHONE_VOL
, 0x0008 },
885 { AC97_MIC_VOL
, 0x8000 },
886 { AC97_LINEIN_VOL
, 0x8808 },
887 { AC97_CD_VOL
, 0x8808 },
888 { AC97_VIDEO_VOL
, 0x8808 },
889 { AC97_AUX_VOL
, 0x8808 },
890 { AC97_PCMOUT_VOL
, 0x0808 },
891 { AC97_RECORD_SELECT
, 0x0000 },
892 { AC97_RECORD_GAIN
, 0x0B0B },
893 { AC97_GENERAL_PURPOSE
, 0x0000 },
897 /* Initialize the AC97 into a known state. */
899 nm256_resetAC97 (struct ac97_hwint
*dev
)
901 struct nm256_info
*card
= (struct nm256_info
*)dev
->driver_private
;
904 if (card
->magsig
!= NM_MAGIC_SIG
) {
905 printk (KERN_ERR
"NM256: Bad magic signature in resetAC97!\n");
909 /* Reset the mixer. 'Tis magic! */
910 nm256_writePort8 (card
, 2, 0x6c0, 1);
911 // nm256_writePort8 (card, 2, 0x6cc, 0x87); /* This crashes Dell latitudes */
912 nm256_writePort8 (card
, 2, 0x6cc, 0x80);
913 nm256_writePort8 (card
, 2, 0x6cc, 0x0);
915 if (! card
->mixer_values_init
) {
916 for (x
= 0; nm256_ac97_initial_values
[x
].port
!= 0xffff; x
++) {
917 ac97_put_register (dev
,
918 nm256_ac97_initial_values
[x
].port
,
919 nm256_ac97_initial_values
[x
].value
);
920 card
->mixer_values_init
= 1;
928 * We don't do anything particularly special here; it just passes the
929 * mixer ioctl to the AC97 driver.
932 nm256_default_mixer_ioctl (int dev
, unsigned int cmd
, void __user
*arg
)
934 struct nm256_info
*card
= nm256_find_card_for_mixer (dev
);
936 return ac97_mixer_ioctl (&(card
->mdev
), cmd
, arg
);
941 static struct mixer_operations nm256_mixer_operations
= {
942 .owner
= THIS_MODULE
,
944 .name
= "NM256AC97Mixer",
945 .ioctl
= nm256_default_mixer_ioctl
949 * Default settings for the OSS mixer. These are set last, after the
950 * mixer is initialized.
952 * I "love" C sometimes. Got braces?
954 static struct ac97_mixer_value_list mixer_defaults
[] = {
955 { SOUND_MIXER_VOLUME
, { { 85, 85 } } },
956 { SOUND_MIXER_SPEAKER
, { { 100 } } },
957 { SOUND_MIXER_PCM
, { { 65, 65 } } },
958 { SOUND_MIXER_CD
, { { 65, 65 } } },
963 /* Installs the AC97 mixer into CARD. */
965 nm256_install_mixer (struct nm256_info
*card
)
969 card
->mdev
.reset_device
= nm256_resetAC97
;
970 card
->mdev
.read_reg
= nm256_readAC97Reg
;
971 card
->mdev
.write_reg
= nm256_writeAC97Reg
;
972 card
->mdev
.driver_private
= (void *)card
;
974 if (ac97_init (&(card
->mdev
)))
977 mixer
= sound_alloc_mixerdev();
978 if (num_mixers
>= MAX_MIXER_DEV
) {
979 printk ("NM256 mixer: Unable to alloc mixerdev\n");
983 mixer_devs
[mixer
] = &nm256_mixer_operations
;
984 card
->mixer_oss_dev
= mixer
;
986 /* Some reasonable default values. */
987 ac97_set_values (&(card
->mdev
), mixer_defaults
);
989 printk(KERN_INFO
"Initialized AC97 mixer\n");
993 /* Perform a full reset on the hardware; this is invoked when an APM
994 resume event occurs. */
996 nm256_full_reset (struct nm256_info
*card
)
999 ac97_reset (&(card
->mdev
));
1003 * See if the signature left by the NM256 BIOS is intact; if so, we use
1004 * the associated address as the end of our audio buffer in the video
1009 nm256_peek_for_sig (struct nm256_info
*card
)
1012 = card
->port
[0].physaddr
+ card
->port
[0].end_offset
- 0x0400;
1013 /* The signature is located 1K below the end of video RAM. */
1014 char __iomem
*temp
= ioremap_nocache (port1offset
, 16);
1015 /* Default buffer end is 5120 bytes below the top of RAM. */
1016 u32 default_value
= card
->port
[0].end_offset
- 0x1400;
1019 /* Install the default value first, so we don't have to repeatedly
1020 do it if there is a problem. */
1021 card
->port
[0].end_offset
= default_value
;
1024 printk (KERN_ERR
"NM256: Unable to scan for card signature in video RAM\n");
1028 if ((sig
& NM_SIG_MASK
) == NM_SIGNATURE
) {
1029 u32 pointer
= readl (temp
+ 4);
1032 * If it's obviously invalid, don't use it (the port already has a
1033 * suitable default value set).
1035 if (pointer
!= 0xffffffff)
1036 card
->port
[0].end_offset
= pointer
;
1038 printk (KERN_INFO
"NM256: Found card signature in video RAM: 0x%x\n",
1046 * Install a driver for the PCI device referenced by PCIDEV.
1047 * VERSTR is a human-readable version string.
1050 static int __devinit
1051 nm256_install(struct pci_dev
*pcidev
, enum nm256rev rev
, char *verstr
)
1053 struct nm256_info
*card
;
1054 struct pm_dev
*pmdev
;
1057 if (pci_enable_device(pcidev
))
1060 card
= kmalloc (sizeof (struct nm256_info
), GFP_KERNEL
);
1062 printk (KERN_ERR
"NM256: out of memory!\n");
1066 card
->magsig
= NM_MAGIC_SIG
;
1068 card
->recording
= 0;
1070 spin_lock_init(&card
->lock
);
1072 /* Init the memory port info. */
1073 for (x
= 0; x
< 2; x
++) {
1074 card
->port
[x
].physaddr
= pci_resource_start (pcidev
, x
);
1075 card
->port
[x
].ptr
= NULL
;
1076 card
->port
[x
].start_offset
= 0;
1077 card
->port
[x
].end_offset
= 0;
1080 /* Port 2 is easy. */
1081 card
->port
[1].start_offset
= 0;
1082 card
->port
[1].end_offset
= NM_PORT2_SIZE
;
1084 /* Yuck. But we have to map in port 2 so we can check how much RAM the
1086 if (nm256_remap_ports (card
)) {
1092 * The NM256 has two memory ports. The first port is nothing
1093 * more than a chunk of video RAM, which is used as the I/O ring
1094 * buffer. The second port has the actual juicy stuff (like the
1095 * mixer and the playback engine control registers).
1098 if (card
->rev
== REV_NM256AV
) {
1099 /* Ok, try to see if this is a non-AC97 version of the hardware. */
1100 int pval
= nm256_readPort16 (card
, 2, NM_MIXER_PRESENCE
);
1101 if ((pval
& NM_PRESENCE_MASK
) != NM_PRESENCE_VALUE
) {
1103 printk (KERN_ERR
"NM256: This doesn't look to me like the AC97-compatible version.\n");
1104 printk (KERN_ERR
" You can force the driver to load by passing in the module\n");
1105 printk (KERN_ERR
" parameter:\n");
1106 printk (KERN_ERR
" force_load = 1\n");
1107 printk (KERN_ERR
"\n");
1108 printk (KERN_ERR
" More likely, you should be using the appropriate SB-16 or\n");
1109 printk (KERN_ERR
" CS4232 driver instead. (If your BIOS has settings for\n");
1110 printk (KERN_ERR
" IRQ and/or DMA for the sound card, this is *not* the correct\n");
1111 printk (KERN_ERR
" driver to use.)\n");
1112 nm256_release_ports (card
);
1117 printk (KERN_INFO
"NM256: Forcing driver load as per user request.\n");
1121 /* printk (KERN_INFO "NM256: Congratulations. You're not running Eunice.\n")*/;
1123 card
->port
[0].end_offset
= 2560 * 1024;
1124 card
->introutine
= nm256_interrupt
;
1125 card
->mixer_status_offset
= NM_MIXER_STATUS_OFFSET
;
1126 card
->mixer_status_mask
= NM_MIXER_READY_MASK
;
1129 /* Not sure if there is any relevant detect for the ZX or not. */
1130 if (nm256_readPort8 (card
, 2, 0xa0b) != 0)
1131 card
->port
[0].end_offset
= 6144 * 1024;
1133 card
->port
[0].end_offset
= 4096 * 1024;
1135 card
->introutine
= nm256_interrupt_zx
;
1136 card
->mixer_status_offset
= NM2_MIXER_STATUS_OFFSET
;
1137 card
->mixer_status_mask
= NM2_MIXER_READY_MASK
;
1140 if (buffertop
>= 98304 && buffertop
< card
->port
[0].end_offset
)
1141 card
->port
[0].end_offset
= buffertop
;
1143 nm256_peek_for_sig (card
);
1145 card
->port
[0].start_offset
= card
->port
[0].end_offset
- 98304;
1147 printk (KERN_INFO
"NM256: Mapping port 1 from 0x%x - 0x%x\n",
1148 card
->port
[0].start_offset
, card
->port
[0].end_offset
);
1150 if (nm256_remap_ports (card
)) {
1155 /* See if we can get the interrupt. */
1157 card
->irq
= pcidev
->irq
;
1160 if (nm256_grabInterrupt (card
) != 0) {
1161 nm256_release_ports (card
);
1166 nm256_releaseInterrupt (card
);
1172 card
->playbackBufferSize
= 16384;
1173 card
->recordBufferSize
= 16384;
1175 card
->coeffBuf
= card
->port
[0].end_offset
- NM_MAX_COEFFICIENT
;
1176 card
->abuf2
= card
->coeffBuf
- card
->recordBufferSize
;
1177 card
->abuf1
= card
->abuf2
- card
->playbackBufferSize
;
1178 card
->allCoeffBuf
= card
->abuf2
- (NM_TOTAL_COEFF_COUNT
* 4);
1180 /* Fixed setting. */
1181 card
->mixer
= NM_MIXER_OFFSET
;
1182 card
->mixer_values_init
= 0;
1184 card
->is_open_play
= 0;
1185 card
->is_open_record
= 0;
1187 card
->coeffsCurrent
= 0;
1189 card
->opencnt
[0] = 0; card
->opencnt
[1] = 0;
1191 /* Reasonable default settings, but largely unnecessary. */
1192 for (x
= 0; x
< 2; x
++) {
1193 card
->sinfo
[x
].bits
= 8;
1194 card
->sinfo
[x
].stereo
= 0;
1195 card
->sinfo
[x
].samplerate
= 8000;
1198 nm256_initHw (card
);
1200 for (x
= 0; x
< 2; x
++) {
1202 sound_install_audiodrv(AUDIO_DRIVER_VERSION
,
1203 "NM256", &nm256_audio_driver
,
1204 sizeof(struct audio_driver
),
1205 DMA_NODMA
, AFMT_U8
| AFMT_S16_LE
,
1206 NULL
, -1, -1)) >= 0) {
1207 /* 1K minimum buffer size. */
1208 audio_devs
[card
->dev
[x
]]->min_fragment
= 10;
1209 /* Maximum of 8K buffer size. */
1210 audio_devs
[card
->dev
[x
]]->max_fragment
= 13;
1213 printk(KERN_ERR
"NM256: Too many PCM devices available\n");
1214 nm256_release_ports (card
);
1220 pci_set_drvdata(pcidev
,card
);
1222 /* Insert the card in the list. */
1223 card
->next_card
= nmcard_list
;
1226 printk(KERN_INFO
"Initialized NeoMagic %s audio in PCI native mode\n",
1230 * And our mixer. (We should allow support for other mixers, maybe.)
1233 nm256_install_mixer (card
);
1235 pmdev
= pm_register(PM_PCI_DEV
, PM_PCI_ID(pcidev
), handle_pm_event
);
1244 * PM event handler, so the card is properly reinitialized after a power
1248 handle_pm_event (struct pm_dev
*dev
, pm_request_t rqst
, void *data
)
1250 struct nm256_info
*crd
= (struct nm256_info
*) dev
->data
;
1257 int playing
= crd
->playing
;
1258 nm256_full_reset (crd
);
1260 * A little ugly, but that's ok; pretend the
1261 * block we were playing is done.
1264 DMAbuf_outputintr (crd
->dev_for_play
, 1);
1272 static int __devinit
1273 nm256_probe(struct pci_dev
*pcidev
,const struct pci_device_id
*pciid
)
1275 if (pcidev
->device
== PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO
)
1276 return nm256_install(pcidev
, REV_NM256AV
, "256AV");
1277 if (pcidev
->device
== PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO
)
1278 return nm256_install(pcidev
, REV_NM256ZX
, "256ZX");
1279 if (pcidev
->device
== PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO
)
1280 return nm256_install(pcidev
, REV_NM256ZX
, "256XL+");
1281 return -1; /* should not come here ... */
1284 static void __devinit
1285 nm256_remove(struct pci_dev
*pcidev
) {
1286 struct nm256_info
*xcard
= pci_get_drvdata(pcidev
);
1287 struct nm256_info
*card
,*next_card
= NULL
;
1289 for (card
= nmcard_list
; card
!= NULL
; card
= next_card
) {
1290 next_card
= card
->next_card
;
1291 if (card
== xcard
) {
1295 free_irq (card
->irq
, card
);
1296 nm256_release_ports (card
);
1297 sound_unload_mixerdev (card
->mixer_oss_dev
);
1298 sound_unload_audiodev (card
->dev
[0]);
1299 sound_unload_audiodev (card
->dev
[1]);
1304 if (nmcard_list
== card
)
1305 nmcard_list
= next_card
;
1312 * MODE - mode to open device (logical OR of OPEN_READ and OPEN_WRITE)
1314 * Called when opening the DMAbuf (dmabuf.c:259)
1317 nm256_audio_open(int dev
, int mode
)
1319 struct nm256_info
*card
= nm256_find_card (dev
);
1325 if (card
->dev
[0] == dev
)
1327 else if (card
->dev
[1] == dev
)
1332 if (card
->opencnt
[w
] > 0)
1335 /* No bits set? Huh? */
1336 if (! ((mode
& OPEN_READ
) || (mode
& OPEN_WRITE
)))
1340 * If it's open for both read and write, and the card's currently
1341 * being read or written to, then do the opposite of what has
1342 * already been done. Otherwise, don't specify any mode until the
1343 * user actually tries to do I/O. (Some programs open the device
1344 * for both read and write, but only actually do reading or writing.)
1347 if ((mode
& OPEN_WRITE
) && (mode
& OPEN_READ
)) {
1348 if (card
->is_open_play
)
1350 else if (card
->is_open_record
)
1355 if (mode
& OPEN_WRITE
) {
1356 if (card
->is_open_play
== 0) {
1357 card
->dev_for_play
= dev
;
1358 card
->is_open_play
= 1;
1364 if (mode
& OPEN_READ
) {
1365 if (card
->is_open_record
== 0) {
1366 card
->dev_for_record
= dev
;
1367 card
->is_open_record
= 1;
1382 * Called when closing the DMAbuf (dmabuf.c:477)
1386 nm256_audio_close(int dev
)
1388 struct nm256_info
*card
= nm256_find_card (dev
);
1393 if (card
->dev
[0] == dev
)
1395 else if (card
->dev
[1] == dev
)
1401 if (card
->opencnt
[w
] <= 0) {
1402 card
->opencnt
[w
] = 0;
1404 if (card
->dev_for_play
== dev
) {
1406 card
->is_open_play
= 0;
1407 card
->dev_for_play
= -1;
1410 if (card
->dev_for_record
== dev
) {
1412 card
->is_open_record
= 0;
1413 card
->dev_for_record
= -1;
1419 /* Standard ioctl handler. */
1421 nm256_audio_ioctl(int dev
, unsigned int cmd
, void __user
*arg
)
1427 struct nm256_info
*card
= nm256_find_card (dev
);
1432 if (dev
== card
->dev
[0])
1438 * The code here is messy. There are probably better ways to do
1439 * it. (It should be possible to handle it the same way the AC97 mixer
1444 case SOUND_PCM_WRITE_RATE
:
1445 if (get_user(ret
, (int __user
*) arg
))
1449 oldinfo
= card
->sinfo
[w
].samplerate
;
1450 card
->sinfo
[w
].samplerate
= ret
;
1451 ret
= nm256_setInfo(dev
, card
);
1453 card
->sinfo
[w
].samplerate
= oldinfo
;
1456 ret
= card
->sinfo
[w
].samplerate
;
1459 case SOUND_PCM_READ_RATE
:
1460 ret
= card
->sinfo
[w
].samplerate
;
1463 case SNDCTL_DSP_STEREO
:
1464 if (get_user(ret
, (int __user
*) arg
))
1467 card
->sinfo
[w
].stereo
= ret
? 1 : 0;
1468 ret
= nm256_setInfo (dev
, card
);
1470 ret
= card
->sinfo
[w
].stereo
;
1474 case SOUND_PCM_WRITE_CHANNELS
:
1475 if (get_user(ret
, (int __user
*) arg
))
1478 if (ret
< 1 || ret
> 3)
1479 ret
= card
->sinfo
[w
].stereo
+ 1;
1481 card
->sinfo
[w
].stereo
= ret
- 1;
1482 ret
= nm256_setInfo (dev
, card
);
1484 ret
= card
->sinfo
[w
].stereo
+ 1;
1488 case SOUND_PCM_READ_CHANNELS
:
1489 ret
= card
->sinfo
[w
].stereo
+ 1;
1492 case SNDCTL_DSP_SETFMT
:
1493 if (get_user(ret
, (int __user
*) arg
))
1497 oldinfo
= card
->sinfo
[w
].bits
;
1498 card
->sinfo
[w
].bits
= ret
;
1499 ret
= nm256_setInfo (dev
, card
);
1501 card
->sinfo
[w
].bits
= oldinfo
;
1504 ret
= card
->sinfo
[w
].bits
;
1507 case SOUND_PCM_READ_BITS
:
1508 ret
= card
->sinfo
[w
].bits
;
1514 return put_user(ret
, (int __user
*) arg
);
1518 * Given the sound device DEV and an associated physical buffer PHYSBUF,
1519 * return a pointer to the actual buffer in kernel space.
1521 * This routine should exist as part of the soundcore routines.
1525 nm256_getDMAbuffer (int dev
, unsigned long physbuf
)
1527 struct audio_operations
*adev
= audio_devs
[dev
];
1528 struct dma_buffparms
*dmap
= adev
->dmap_out
;
1530 (char *)(physbuf
- (unsigned long)dmap
->raw_buf_phys
1531 + (unsigned long)dmap
->raw_buf
);
1538 * Output a block to sound device
1540 * dev - device number
1541 * buf - physical address of buffer
1542 * total_count - total byte count in buffer
1543 * intrflag - set if this has been called from an interrupt
1544 * (via DMAbuf_outputintr)
1545 * restart_dma - set if engine needs to be re-initialised
1548 * 1. Starting output (dmabuf.c:1327)
1549 * 2. (dmabuf.c:1504)
1550 * 3. A new buffer needs to be sent to the device (dmabuf.c:1579)
1553 nm256_audio_output_block(int dev
, unsigned long physbuf
,
1554 int total_count
, int intrflag
)
1556 struct nm256_info
*card
= nm256_find_card (dev
);
1559 char *dma_buf
= nm256_getDMAbuffer (dev
, physbuf
);
1560 card
->is_open_play
= 1;
1561 card
->dev_for_play
= dev
;
1562 nm256_write_block (card
, dma_buf
, total_count
);
1566 /* Ditto, but do recording instead. */
1568 nm256_audio_start_input(int dev
, unsigned long physbuf
, int count
,
1571 struct nm256_info
*card
= nm256_find_card (dev
);
1574 char *dma_buf
= nm256_getDMAbuffer (dev
, physbuf
);
1575 card
->is_open_record
= 1;
1576 card
->dev_for_record
= dev
;
1577 nm256_startRecording (card
, dma_buf
, count
);
1582 * Prepare for inputting samples to DEV.
1583 * Each requested buffer will be BSIZE byes long, with a total of
1588 nm256_audio_prepare_for_input(int dev
, int bsize
, int bcount
)
1590 struct nm256_info
*card
= nm256_find_card (dev
);
1595 if (card
->is_open_record
&& card
->dev_for_record
!= dev
)
1598 audio_devs
[dev
]->dmap_in
->flags
|= DMA_NODMA
;
1603 * Prepare for outputting samples to `dev'
1605 * Each buffer that will be passed will be `bsize' bytes long,
1606 * with a total of `bcount' buffers.
1609 * 1. A trigger enables audio output (dmabuf.c:978)
1610 * 2. We get a write buffer without dma_mode setup (dmabuf.c:1152)
1611 * 3. We restart a transfer (dmabuf.c:1324)
1615 nm256_audio_prepare_for_output(int dev
, int bsize
, int bcount
)
1617 struct nm256_info
*card
= nm256_find_card (dev
);
1622 if (card
->is_open_play
&& card
->dev_for_play
!= dev
)
1625 audio_devs
[dev
]->dmap_out
->flags
|= DMA_NODMA
;
1629 /* Stop the current operations associated with DEV. */
1631 nm256_audio_reset(int dev
)
1633 struct nm256_info
*card
= nm256_find_card (dev
);
1636 if (card
->dev_for_play
== dev
)
1638 if (card
->dev_for_record
== dev
)
1644 nm256_audio_local_qlen(int dev
)
1649 static struct audio_driver nm256_audio_driver
=
1651 .owner
= THIS_MODULE
,
1652 .open
= nm256_audio_open
,
1653 .close
= nm256_audio_close
,
1654 .output_block
= nm256_audio_output_block
,
1655 .start_input
= nm256_audio_start_input
,
1656 .ioctl
= nm256_audio_ioctl
,
1657 .prepare_for_input
= nm256_audio_prepare_for_input
,
1658 .prepare_for_output
= nm256_audio_prepare_for_output
,
1659 .halt_io
= nm256_audio_reset
,
1660 .local_qlen
= nm256_audio_local_qlen
,
1663 static struct pci_device_id nm256_pci_tbl
[] = {
1664 {PCI_VENDOR_ID_NEOMAGIC
, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO
,
1665 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0},
1666 {PCI_VENDOR_ID_NEOMAGIC
, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO
,
1667 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0},
1668 {PCI_VENDOR_ID_NEOMAGIC
, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO
,
1669 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0},
1672 MODULE_DEVICE_TABLE(pci
, nm256_pci_tbl
);
1673 MODULE_LICENSE("GPL");
1676 static struct pci_driver nm256_pci_driver
= {
1677 .name
= "nm256_audio",
1678 .id_table
= nm256_pci_tbl
,
1679 .probe
= nm256_probe
,
1680 .remove
= nm256_remove
,
1683 module_param(usecache
, bool, 0);
1684 module_param(buffertop
, int, 0);
1685 module_param(nm256_debug
, bool, 0644);
1686 module_param(force_load
, bool, 0);
1688 static int __init
do_init_nm256(void)
1690 printk (KERN_INFO
"NeoMagic 256AV/256ZX audio driver, version 1.1p\n");
1691 return pci_module_init(&nm256_pci_driver
);
1694 static void __exit
cleanup_nm256 (void)
1696 pci_unregister_driver(&nm256_pci_driver
);
1697 pm_unregister_all (&handle_pm_event
);
1700 module_init(do_init_nm256
);
1701 module_exit(cleanup_nm256
);