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 #define __NO_VERSION__
17 #include <linux/pci.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
21 #include <linux/delay.h>
22 #include "sound_config.h"
24 #include "nm256_coeff.h"
27 static int force_load
= 0;
30 * The size of the playback reserve. When the playback buffer has less
31 * than NM256_PLAY_WMARK_SIZE bytes to output, we request a new
34 #define NM256_PLAY_WMARK_SIZE 512
36 static struct audio_driver nm256_audio_driver
;
38 static int nm256_grabInterrupt (struct nm256_info
*card
);
39 static int nm256_releaseInterrupt (struct nm256_info
*card
);
40 static void nm256_interrupt (int irq
, void *dev_id
, struct pt_regs
*dummy
);
41 static void nm256_interrupt_zx (int irq
, void *dev_id
, struct pt_regs
*dummy
);
42 static int handle_pm_event (struct pm_dev
*dev
, pm_request_t rqst
, void *data
);
44 /* These belong in linux/pci.h. */
45 #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
46 #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
48 #define RSRCADDRESS(dev,num) ((dev)->resource[(num)].start)
51 static struct nm256_info
*nmcard_list
;
53 /* Release the mapped-in memory for CARD. */
55 nm256_release_ports (struct nm256_info
*card
)
59 for (x
= 0; x
< 2; x
++) {
60 if (card
->port
[x
].ptr
!= NULL
) {
62 card
->port
[x
].end_offset
- card
->port
[x
].start_offset
;
63 release_region ((unsigned long) card
->port
[x
].ptr
, size
);
64 card
->port
[x
].ptr
= NULL
;
70 * Map in the memory ports for CARD, if they aren't already mapped in
71 * and have been configured. If successful, a zero value is returned;
72 * otherwise any previously mapped-in areas are released and a non-zero
75 * This is invoked twice, once for each port. Ideally it would only be
76 * called once, but we now need to map in the second port in order to
77 * check how much memory the card has on the 256ZX.
80 nm256_remap_ports (struct nm256_info
*card
)
84 for (x
= 0; x
< 2; x
++) {
85 if (card
->port
[x
].ptr
== NULL
&& card
->port
[x
].end_offset
> 0) {
87 = card
->port
[x
].physaddr
+ card
->port
[x
].start_offset
;
89 = card
->port
[x
].end_offset
- card
->port
[x
].start_offset
;
91 card
->port
[x
].ptr
= ioremap_nocache (physaddr
, size
);
93 if (card
->port
[x
].ptr
== NULL
) {
94 printk (KERN_ERR
"NM256: Unable to remap port %d\n", x
+ 1);
95 nm256_release_ports (card
);
103 /* Locate the card in our list. */
104 static struct nm256_info
*
105 nm256_find_card (int dev
)
107 struct nm256_info
*card
;
109 for (card
= nmcard_list
; card
!= NULL
; card
= card
->next_card
)
110 if (card
->dev
[0] == dev
|| card
->dev
[1] == dev
)
117 * Ditto, but find the card struct corresponding to the mixer device DEV
120 static struct nm256_info
*
121 nm256_find_card_for_mixer (int dev
)
123 struct nm256_info
*card
;
125 for (card
= nmcard_list
; card
!= NULL
; card
= card
->next_card
)
126 if (card
->mixer_oss_dev
== dev
)
132 static int usecache
= 0;
133 static int buffertop
= 0;
135 /* Check to see if we're using the bank of cached coefficients. */
137 nm256_cachedCoefficients (struct nm256_info
*card
)
142 /* The actual rates supported by the card. */
143 static int samplerates
[9] = {
144 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 99999999
148 * Set the card samplerate, word size and stereo mode to correspond to
149 * the settings in the CARD struct for the specified device in DEV.
150 * We keep two separate sets of information, one for each device; the
151 * hardware is not actually configured until a read or write is
156 nm256_setInfo (int dev
, struct nm256_info
*card
)
162 if (card
->dev
[0] == dev
)
164 else if (card
->dev
[1] == dev
)
169 targetrate
= card
->sinfo
[w
].samplerate
;
171 if ((card
->sinfo
[w
].bits
!= 8 && card
->sinfo
[w
].bits
!= 16)
172 || targetrate
< samplerates
[0]
173 || targetrate
> samplerates
[7])
176 for (x
= 0; x
< 8; x
++)
177 if (targetrate
< ((samplerates
[x
] + samplerates
[x
+ 1]) / 2))
181 u8 ratebits
= ((x
<< 4) & NM_RATE_MASK
);
182 if (card
->sinfo
[w
].bits
== 16)
183 ratebits
|= NM_RATE_BITS_16
;
184 if (card
->sinfo
[w
].stereo
)
185 ratebits
|= NM_RATE_STEREO
;
187 card
->sinfo
[w
].samplerate
= samplerates
[x
];
190 if (card
->dev_for_play
== dev
&& card
->playing
) {
192 printk (KERN_DEBUG
"Setting play ratebits to 0x%x\n",
194 nm256_loadCoefficient (card
, 0, x
);
195 nm256_writePort8 (card
, 2,
196 NM_PLAYBACK_REG_OFFSET
+ NM_RATE_REG_OFFSET
,
200 if (card
->dev_for_record
== dev
&& card
->recording
) {
202 printk (KERN_DEBUG
"Setting record ratebits to 0x%x\n",
204 nm256_loadCoefficient (card
, 1, x
);
205 nm256_writePort8 (card
, 2,
206 NM_RECORD_REG_OFFSET
+ NM_RATE_REG_OFFSET
,
215 /* Start the play process going. */
217 startPlay (struct nm256_info
*card
)
219 if (! card
->playing
) {
221 if (nm256_grabInterrupt (card
) == 0) {
222 nm256_setInfo (card
->dev_for_play
, card
);
224 /* Enable playback engine and interrupts. */
225 nm256_writePort8 (card
, 2, NM_PLAYBACK_ENABLE_REG
,
226 NM_PLAYBACK_ENABLE_FLAG
| NM_PLAYBACK_FREERUN
);
228 /* Enable both channels. */
229 nm256_writePort16 (card
, 2, NM_AUDIO_MUTE_REG
, 0x0);
235 * Request one chunk of AMT bytes from the recording device. When the
236 * operation is complete, the data will be copied into BUFFER and the
237 * function DMAbuf_inputintr will be invoked.
241 nm256_startRecording (struct nm256_info
*card
, char *buffer
, u32 amt
)
244 int enableEngine
= 0;
245 u32 ringsize
= card
->recordBufferSize
;
248 if (amt
> (ringsize
/ 2)) {
250 * Of course this won't actually work right, because the
251 * caller is going to assume we will give what we got asked
254 printk (KERN_ERR
"NM256: Read request too large: %d\n", amt
);
259 printk (KERN_ERR
"NM256: Read request too small; %d\n", amt
);
266 * If we're not currently recording, set up the start and end registers
267 * for the recording engine.
269 if (! card
->recording
) {
271 if (nm256_grabInterrupt (card
) == 0) {
273 nm256_setInfo (card
->dev_for_record
, card
);
274 nm256_writePort32 (card
, 2, NM_RBUFFER_START
, card
->abuf2
);
275 nm256_writePort32 (card
, 2, NM_RBUFFER_END
,
276 card
->abuf2
+ ringsize
);
278 nm256_writePort32 (card
, 2, NM_RBUFFER_CURRP
,
279 card
->abuf2
+ card
->curRecPos
);
283 /* Not sure what else to do here. */
284 restore_flags (flags
);
290 * If we happen to go past the end of the buffer a bit (due to a
291 * delayed interrupt) it's OK. So might as well set the watermark
292 * right at the end of the data we want.
294 endpos
= card
->abuf2
+ ((card
->curRecPos
+ amt
) % ringsize
);
296 card
->recBuf
= buffer
;
297 card
->requestedRecAmt
= amt
;
298 nm256_writePort32 (card
, 2, NM_RBUFFER_WMARK
, endpos
);
299 /* Enable recording engine and interrupts. */
301 nm256_writePort8 (card
, 2, NM_RECORD_ENABLE_REG
,
302 NM_RECORD_ENABLE_FLAG
| NM_RECORD_FREERUN
);
304 restore_flags (flags
);
307 /* Stop the play engine. */
309 stopPlay (struct nm256_info
*card
)
311 /* Shut off sound from both channels. */
312 nm256_writePort16 (card
, 2, NM_AUDIO_MUTE_REG
,
313 NM_AUDIO_MUTE_LEFT
| NM_AUDIO_MUTE_RIGHT
);
314 /* Disable play engine. */
315 nm256_writePort8 (card
, 2, NM_PLAYBACK_ENABLE_REG
, 0);
317 nm256_releaseInterrupt (card
);
319 /* Reset the relevant state bits. */
321 card
->curPlayPos
= 0;
325 /* Stop recording. */
327 stopRecord (struct nm256_info
*card
)
329 /* Disable recording engine. */
330 nm256_writePort8 (card
, 2, NM_RECORD_ENABLE_REG
, 0);
332 if (card
->recording
) {
333 nm256_releaseInterrupt (card
);
341 * Ring buffers, man. That's where the hip-hop, wild-n-wooly action's at.
342 * 1972? (Well, I suppose it was cheep-n-easy to implement.)
344 * Write AMT bytes of BUFFER to the playback ring buffer, and start the
345 * playback engine running. It will only accept up to 1/2 of the total
346 * size of the ring buffer. No check is made that we're about to overwrite
347 * the currently-playing sample.
351 nm256_write_block (struct nm256_info
*card
, char *buffer
, u32 amt
)
353 u32 ringsize
= card
->playbackBufferSize
;
357 if (amt
> (ringsize
/ 2)) {
358 printk (KERN_ERR
"NM256: Write request too large: %d\n", amt
);
359 amt
= (ringsize
/ 2);
362 if (amt
< NM256_PLAY_WMARK_SIZE
) {
363 printk (KERN_ERR
"NM256: Write request too small: %d\n", amt
);
367 card
->curPlayPos
%= ringsize
;
369 card
->requested_amt
= amt
;
374 if ((card
->curPlayPos
+ amt
) >= ringsize
) {
375 u32 rem
= ringsize
- card
->curPlayPos
;
377 nm256_writeBuffer8 (card
, buffer
, 1,
378 card
->abuf1
+ card
->curPlayPos
,
381 nm256_writeBuffer8 (card
, buffer
+ rem
, 1, card
->abuf1
,
385 nm256_writeBuffer8 (card
, buffer
, 1,
386 card
->abuf1
+ card
->curPlayPos
,
390 * Setup the start-n-stop-n-limit registers, and start that engine
393 * Normally we just let it wrap around to avoid the click-click
396 if (! card
->playing
) {
397 /* The PBUFFER_END register in this case points to one sample
398 before the end of the buffer. */
399 int w
= (card
->dev_for_play
== card
->dev
[0] ? 0 : 1);
400 int sampsize
= (card
->sinfo
[w
].bits
== 16 ? 2 : 1);
402 if (card
->sinfo
[w
].stereo
)
405 /* Need to set the not-normally-changing-registers up. */
406 nm256_writePort32 (card
, 2, NM_PBUFFER_START
,
407 card
->abuf1
+ card
->curPlayPos
);
408 nm256_writePort32 (card
, 2, NM_PBUFFER_END
,
409 card
->abuf1
+ ringsize
- sampsize
);
410 nm256_writePort32 (card
, 2, NM_PBUFFER_CURRP
,
411 card
->abuf1
+ card
->curPlayPos
);
413 endstop
= (card
->curPlayPos
+ amt
- NM256_PLAY_WMARK_SIZE
) % ringsize
;
414 nm256_writePort32 (card
, 2, NM_PBUFFER_WMARK
, card
->abuf1
+ endstop
);
419 restore_flags (flags
);
422 /* We just got a card playback interrupt; process it. */
424 nm256_get_new_block (struct nm256_info
*card
)
426 /* Check to see how much got played so far. */
427 u32 amt
= nm256_readPort32 (card
, 2, NM_PBUFFER_CURRP
) - card
->abuf1
;
429 if (amt
>= card
->playbackBufferSize
) {
430 printk (KERN_ERR
"NM256: Sound playback pointer invalid!\n");
434 if (amt
< card
->curPlayPos
)
435 amt
= (card
->playbackBufferSize
- card
->curPlayPos
) + amt
;
437 amt
-= card
->curPlayPos
;
439 if (card
->requested_amt
> (amt
+ NM256_PLAY_WMARK_SIZE
)) {
441 card
->curPlayPos
+ card
->requested_amt
- NM256_PLAY_WMARK_SIZE
;
442 nm256_writePort32 (card
, 2, NM_PBUFFER_WMARK
, card
->abuf1
+ endstop
);
445 card
->curPlayPos
+= card
->requested_amt
;
446 /* Get a new block to write. This will eventually invoke
447 nm256_write_block () or stopPlay (). */
448 DMAbuf_outputintr (card
->dev_for_play
, 1);
452 /* Ultra cheez-whiz. But I'm too lazy to grep headers. */
453 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
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
);
508 * Initialize the hardware.
511 nm256_initHw (struct nm256_info
*card
)
513 /* Reset everything. */
514 nm256_writePort8 (card
, 2, 0x0, 0x11);
515 nm256_writePort16 (card
, 2, 0x214, 0);
522 * Handle a potential interrupt for the device referred to by DEV_ID.
524 * I don't like the cut-n-paste job here either between the two routines,
525 * but there are sufficient differences between the two interrupt handlers
526 * that parameterizing it isn't all that great either. (Could use a macro,
527 * I suppose...yucky bleah.)
531 nm256_interrupt (int irq
, void *dev_id
, struct pt_regs
*dummy
)
533 struct nm256_info
*card
= (struct nm256_info
*)dev_id
;
535 static int badintrcount
= 0;
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.
571 /* Rather boring; check for individual interrupts and process them. */
573 if (status
& NM_PLAYBACK_INT
) {
574 status
&= ~NM_PLAYBACK_INT
;
575 NM_ACK_INT (card
, NM_PLAYBACK_INT
);
578 nm256_get_new_block (card
);
581 if (status
& NM_RECORD_INT
) {
582 status
&= ~NM_RECORD_INT
;
583 NM_ACK_INT (card
, NM_RECORD_INT
);
586 nm256_read_block (card
);
589 if (status
& NM_MISC_INT_1
) {
592 status
&= ~NM_MISC_INT_1
;
593 printk (KERN_ERR
"NM256: Got misc interrupt #1\n");
594 NM_ACK_INT (card
, NM_MISC_INT_1
);
595 nm256_writePort16 (card
, 2, NM_INT_REG
, 0x8000);
596 cbyte
= nm256_readPort8 (card
, 2, 0x400);
597 nm256_writePort8 (card
, 2, 0x400, cbyte
| 2);
600 if (status
& NM_MISC_INT_2
) {
603 status
&= ~NM_MISC_INT_2
;
604 printk (KERN_ERR
"NM256: Got misc interrupt #2\n");
605 NM_ACK_INT (card
, NM_MISC_INT_2
);
606 cbyte
= nm256_readPort8 (card
, 2, 0x400);
607 nm256_writePort8 (card
, 2, 0x400, cbyte
& ~2);
610 /* Unknown interrupt. */
612 printk (KERN_ERR
"NM256: Fire in the hole! Unknown status 0x%x\n",
615 NM_ACK_INT (card
, status
);
620 * Handle a potential interrupt for the device referred to by DEV_ID.
621 * This handler is for the 256ZX, and is very similar to the non-ZX
626 nm256_interrupt_zx (int irq
, void *dev_id
, struct pt_regs
*dummy
)
628 struct nm256_info
*card
= (struct nm256_info
*)dev_id
;
630 static int badintrcount
= 0;
632 if ((card
== NULL
) || (card
->magsig
!= NM_MAGIC_SIG
)) {
633 printk (KERN_ERR
"NM256: Bad card pointer\n");
637 status
= nm256_readPort32 (card
, 2, NM_INT_REG
);
641 if (badintrcount
++ > 1000) {
642 printk (KERN_ERR
"NM256: Releasing interrupt, over 1000 invalid interrupts\n");
644 * I'm not sure if the best thing is to stop the card from
645 * playing or just release the interrupt (after all, we're in
646 * a bad situation, so doing fancy stuff may not be such a good
649 * I worry about the card engine continuing to play noise
650 * over and over, however--that could become a very
651 * obnoxious problem. And we know that when this usually
652 * happens things are fairly safe, it just means the user's
653 * inserted a PCMCIA card and someone's spamming us with
668 /* Rather boring; check for individual interrupts and process them. */
670 if (status
& NM2_PLAYBACK_INT
) {
671 status
&= ~NM2_PLAYBACK_INT
;
672 NM2_ACK_INT (card
, NM2_PLAYBACK_INT
);
675 nm256_get_new_block (card
);
678 if (status
& NM2_RECORD_INT
) {
679 status
&= ~NM2_RECORD_INT
;
680 NM2_ACK_INT (card
, NM2_RECORD_INT
);
683 nm256_read_block (card
);
686 if (status
& NM2_MISC_INT_1
) {
689 status
&= ~NM2_MISC_INT_1
;
690 printk (KERN_ERR
"NM256: Got misc interrupt #1\n");
691 NM2_ACK_INT (card
, NM2_MISC_INT_1
);
692 cbyte
= nm256_readPort8 (card
, 2, 0x400);
693 nm256_writePort8 (card
, 2, 0x400, cbyte
| 2);
696 if (status
& NM2_MISC_INT_2
) {
699 status
&= ~NM2_MISC_INT_2
;
700 printk (KERN_ERR
"NM256: Got misc interrupt #2\n");
701 NM2_ACK_INT (card
, NM2_MISC_INT_2
);
702 cbyte
= nm256_readPort8 (card
, 2, 0x400);
703 nm256_writePort8 (card
, 2, 0x400, cbyte
& ~2);
706 /* Unknown interrupt. */
708 printk (KERN_ERR
"NM256: Fire in the hole! Unknown status 0x%x\n",
711 NM2_ACK_INT (card
, status
);
716 * Request our interrupt.
719 nm256_grabInterrupt (struct nm256_info
*card
)
721 if (card
->has_irq
++ == 0) {
722 if (request_irq (card
->irq
, card
->introutine
, SA_SHIRQ
,
723 "NM256_audio", card
) < 0) {
724 printk (KERN_ERR
"NM256: can't obtain IRQ %d\n", card
->irq
);
732 * Release our interrupt.
735 nm256_releaseInterrupt (struct nm256_info
*card
)
737 if (card
->has_irq
<= 0) {
738 printk (KERN_ERR
"nm256: too many calls to releaseInterrupt\n");
742 if (card
->has_irq
== 0) {
743 free_irq (card
->irq
, card
);
749 * Waits for the mixer to become ready to be written; returns a zero value
754 nm256_isReady (struct ac97_hwint
*dev
)
756 struct nm256_info
*card
= (struct nm256_info
*)dev
->driver_private
;
762 if (card
->magsig
!= NM_MAGIC_SIG
) {
763 printk (KERN_ERR
"NM256: Bad magic signature in isReady!\n");
767 testaddr
= card
->mixer_status_offset
;
768 testb
= card
->mixer_status_mask
;
771 * Loop around waiting for the mixer to become ready.
773 while (! done
&& t2
-- > 0) {
774 if ((nm256_readPort16 (card
, 2, testaddr
) & testb
) == 0)
783 * Return the contents of the AC97 mixer register REG. Returns a positive
784 * value if successful, or a negative error code.
787 nm256_readAC97Reg (struct ac97_hwint
*dev
, u8 reg
)
789 struct nm256_info
*card
= (struct nm256_info
*)dev
->driver_private
;
791 if (card
->magsig
!= NM_MAGIC_SIG
) {
792 printk (KERN_ERR
"NM256: Bad magic signature in readAC97Reg!\n");
800 res
= nm256_readPort16 (card
, 2, card
->mixer
+ reg
);
801 /* Magic delay. Bleah yucky. */
810 * Writes VALUE to AC97 mixer register REG. Returns 0 if successful, or
811 * a negative error code.
814 nm256_writeAC97Reg (struct ac97_hwint
*dev
, u8 reg
, u16 value
)
821 struct nm256_info
*card
= (struct nm256_info
*)dev
->driver_private
;
823 if (card
->magsig
!= NM_MAGIC_SIG
) {
824 printk (KERN_ERR
"NM256: Bad magic signature in writeAC97Reg!\n");
835 /* Wait for the write to take, too. */
836 while ((tries
-- > 0) && !done
) {
837 nm256_writePort16 (card
, 2, base
+ reg
, value
);
838 if (nm256_isReady (dev
)) {
845 restore_flags (flags
);
852 * Initial register values to be written to the AC97 mixer.
853 * While most of these are identical to the reset values, we do this
854 * so that we have most of the register contents cached--this avoids
855 * reading from the mixer directly (which seems to be problematic,
856 * probably due to ignorance).
861 unsigned short value
;
864 static struct initialValues nm256_ac97_initial_values
[] =
866 { AC97_MASTER_VOL_STEREO
, 0x8000 },
867 { AC97_HEADPHONE_VOL
, 0x8000 },
868 { AC97_MASTER_VOL_MONO
, 0x0000 },
869 { AC97_PCBEEP_VOL
, 0x0000 },
870 { AC97_PHONE_VOL
, 0x0008 },
871 { AC97_MIC_VOL
, 0x8000 },
872 { AC97_LINEIN_VOL
, 0x8808 },
873 { AC97_CD_VOL
, 0x8808 },
874 { AC97_VIDEO_VOL
, 0x8808 },
875 { AC97_AUX_VOL
, 0x8808 },
876 { AC97_PCMOUT_VOL
, 0x0808 },
877 { AC97_RECORD_SELECT
, 0x0000 },
878 { AC97_RECORD_GAIN
, 0x0B0B },
879 { AC97_GENERAL_PURPOSE
, 0x0000 },
883 /* Initialize the AC97 into a known state. */
885 nm256_resetAC97 (struct ac97_hwint
*dev
)
887 struct nm256_info
*card
= (struct nm256_info
*)dev
->driver_private
;
890 if (card
->magsig
!= NM_MAGIC_SIG
) {
891 printk (KERN_ERR
"NM256: Bad magic signature in resetAC97!\n");
895 /* Reset the mixer. 'Tis magic! */
896 nm256_writePort8 (card
, 2, 0x6c0, 1);
897 nm256_writePort8 (card
, 2, 0x6cc, 0x87);
898 nm256_writePort8 (card
, 2, 0x6cc, 0x80);
899 nm256_writePort8 (card
, 2, 0x6cc, 0x0);
901 if (! card
->mixer_values_init
) {
902 for (x
= 0; nm256_ac97_initial_values
[x
].port
!= 0xffff; x
++) {
903 ac97_put_register (dev
,
904 nm256_ac97_initial_values
[x
].port
,
905 nm256_ac97_initial_values
[x
].value
);
906 card
->mixer_values_init
= 1;
914 * We don't do anything particularly special here; it just passes the
915 * mixer ioctl to the AC97 driver.
918 nm256_default_mixer_ioctl (int dev
, unsigned int cmd
, caddr_t arg
)
920 struct nm256_info
*card
= nm256_find_card_for_mixer (dev
);
922 return ac97_mixer_ioctl (&(card
->mdev
), cmd
, arg
);
927 static struct mixer_operations nm256_mixer_operations
= {
930 name
: "NM256AC97Mixer",
931 ioctl
: nm256_default_mixer_ioctl
935 * Default settings for the OSS mixer. These are set last, after the
936 * mixer is initialized.
938 * I "love" C sometimes. Got braces?
940 static struct ac97_mixer_value_list mixer_defaults
[] = {
941 { SOUND_MIXER_VOLUME
, { { 85, 85 } } },
942 { SOUND_MIXER_SPEAKER
, { { 100 } } },
943 { SOUND_MIXER_PCM
, { { 65, 65 } } },
944 { SOUND_MIXER_CD
, { { 65, 65 } } },
949 /* Installs the AC97 mixer into CARD. */
951 nm256_install_mixer (struct nm256_info
*card
)
955 card
->mdev
.reset_device
= nm256_resetAC97
;
956 card
->mdev
.read_reg
= nm256_readAC97Reg
;
957 card
->mdev
.write_reg
= nm256_writeAC97Reg
;
958 card
->mdev
.driver_private
= (void *)card
;
960 if (ac97_init (&(card
->mdev
)))
963 mixer
= sound_alloc_mixerdev();
964 if (num_mixers
>= MAX_MIXER_DEV
) {
965 printk ("NM256 mixer: Unable to alloc mixerdev\n");
969 mixer_devs
[mixer
] = &nm256_mixer_operations
;
970 card
->mixer_oss_dev
= mixer
;
972 /* Some reasonable default values. */
973 ac97_set_values (&(card
->mdev
), mixer_defaults
);
975 printk(KERN_INFO
"Initialized AC97 mixer\n");
979 /* Perform a full reset on the hardware; this is invoked when an APM
980 resume event occurs. */
982 nm256_full_reset (struct nm256_info
*card
)
985 ac97_reset (&(card
->mdev
));
989 * See if the signature left by the NM256 BIOS is intact; if so, we use
990 * the associated address as the end of our audio buffer in the video
995 nm256_peek_for_sig (struct nm256_info
*card
)
998 = card
->port
[0].physaddr
+ card
->port
[0].end_offset
- 0x0400;
999 /* The signature is located 1K below the end of video RAM. */
1000 char *temp
= ioremap_nocache (port1offset
, 16);
1001 /* Default buffer end is 5120 bytes below the top of RAM. */
1002 u32 default_value
= card
->port
[0].end_offset
- 0x1400;
1005 /* Install the default value first, so we don't have to repeatedly
1006 do it if there is a problem. */
1007 card
->port
[0].end_offset
= default_value
;
1010 printk (KERN_ERR
"NM256: Unable to scan for card signature in video RAM\n");
1014 if ((sig
& NM_SIG_MASK
) == NM_SIGNATURE
) {
1015 u32 pointer
= readl (temp
+ 4);
1018 * If it's obviously invalid, don't use it (the port already has a
1019 * suitable default value set).
1021 if (pointer
!= 0xffffffff)
1022 card
->port
[0].end_offset
= pointer
;
1024 printk (KERN_INFO
"NM256: Found card signature in video RAM: 0x%x\n",
1028 release_region ((unsigned long) temp
, 16);
1032 * Install a driver for the PCI device referenced by PCIDEV.
1033 * VERSTR is a human-readable version string.
1037 nm256_install(struct pci_dev
*pcidev
, enum nm256rev rev
, char *verstr
)
1039 struct nm256_info
*card
;
1040 struct pm_dev
*pmdev
;
1043 card
= kmalloc (sizeof (struct nm256_info
), GFP_KERNEL
);
1045 printk (KERN_ERR
"NM256: out of memory!\n");
1049 card
->magsig
= NM_MAGIC_SIG
;
1051 card
->recording
= 0;
1054 /* Init the memory port info. */
1055 for (x
= 0; x
< 2; x
++) {
1056 card
->port
[x
].physaddr
= RSRCADDRESS (pcidev
, x
);
1057 card
->port
[x
].ptr
= NULL
;
1058 card
->port
[x
].start_offset
= 0;
1059 card
->port
[x
].end_offset
= 0;
1062 /* Port 2 is easy. */
1063 card
->port
[1].start_offset
= 0;
1064 card
->port
[1].end_offset
= NM_PORT2_SIZE
;
1066 /* Yuck. But we have to map in port 2 so we can check how much RAM the
1068 if (nm256_remap_ports (card
)) {
1074 * The NM256 has two memory ports. The first port is nothing
1075 * more than a chunk of video RAM, which is used as the I/O ring
1076 * buffer. The second port has the actual juicy stuff (like the
1077 * mixer and the playback engine control registers).
1080 if (card
->rev
== REV_NM256AV
) {
1081 /* Ok, try to see if this is a non-AC97 version of the hardware. */
1082 int pval
= nm256_readPort16 (card
, 2, NM_MIXER_PRESENCE
);
1083 if ((pval
& NM_PRESENCE_MASK
) != NM_PRESENCE_VALUE
) {
1085 printk (KERN_ERR
"NM256: This doesn't look to me like the AC97-compatible version.\n");
1086 printk (KERN_ERR
" You can force the driver to load by passing in the module\n");
1087 printk (KERN_ERR
" parameter:\n");
1088 printk (KERN_ERR
" force_ac97 = 1\n");
1089 printk (KERN_ERR
"\n");
1090 printk (KERN_ERR
" More likely, you should be using the appropriate SB-16 or\n");
1091 printk (KERN_ERR
" CS4232 driver instead. (If your BIOS has settings for\n");
1092 printk (KERN_ERR
" IRQ and/or DMA for the sound card, this is *not* the correct\n");
1093 printk (KERN_ERR
" driver to use.)\n");
1094 nm256_release_ports (card
);
1099 printk (KERN_INFO
"NM256: Forcing driver load as per user request.\n");
1103 /* printk (KERN_INFO "NM256: Congratulations. You're not running Eunice.\n")*/;
1105 card
->port
[0].end_offset
= 2560 * 1024;
1106 card
->introutine
= nm256_interrupt
;
1107 card
->mixer_status_offset
= NM_MIXER_STATUS_OFFSET
;
1108 card
->mixer_status_mask
= NM_MIXER_READY_MASK
;
1111 /* Not sure if there is any relevant detect for the ZX or not. */
1112 if (nm256_readPort8 (card
, 2, 0xa0b) != 0)
1113 card
->port
[0].end_offset
= 6144 * 1024;
1115 card
->port
[0].end_offset
= 4096 * 1024;
1117 card
->introutine
= nm256_interrupt_zx
;
1118 card
->mixer_status_offset
= NM2_MIXER_STATUS_OFFSET
;
1119 card
->mixer_status_mask
= NM2_MIXER_READY_MASK
;
1122 if (buffertop
>= 98304 && buffertop
< card
->port
[0].end_offset
)
1123 card
->port
[0].end_offset
= buffertop
;
1125 nm256_peek_for_sig (card
);
1127 card
->port
[0].start_offset
= card
->port
[0].end_offset
- 98304;
1129 printk (KERN_INFO
"NM256: Mapping port 1 from 0x%x - 0x%x\n",
1130 card
->port
[0].start_offset
, card
->port
[0].end_offset
);
1132 if (nm256_remap_ports (card
)) {
1137 /* See if we can get the interrupt. */
1139 card
->irq
= pcidev
->irq
;
1142 if (nm256_grabInterrupt (card
) != 0) {
1143 nm256_release_ports (card
);
1148 nm256_releaseInterrupt (card
);
1154 card
->playbackBufferSize
= 16384;
1155 card
->recordBufferSize
= 16384;
1157 card
->coeffBuf
= card
->port
[0].end_offset
- NM_MAX_COEFFICIENT
;
1158 card
->abuf2
= card
->coeffBuf
- card
->recordBufferSize
;
1159 card
->abuf1
= card
->abuf2
- card
->playbackBufferSize
;
1160 card
->allCoeffBuf
= card
->abuf2
- (NM_TOTAL_COEFF_COUNT
* 4);
1162 /* Fixed setting. */
1163 card
->mixer
= NM_MIXER_OFFSET
;
1164 card
->mixer_values_init
= 0;
1166 card
->is_open_play
= 0;
1167 card
->is_open_record
= 0;
1169 card
->coeffsCurrent
= 0;
1171 card
->opencnt
[0] = 0; card
->opencnt
[1] = 0;
1173 /* Reasonable default settings, but largely unnecessary. */
1174 for (x
= 0; x
< 2; x
++) {
1175 card
->sinfo
[x
].bits
= 8;
1176 card
->sinfo
[x
].stereo
= 0;
1177 card
->sinfo
[x
].samplerate
= 8000;
1180 nm256_initHw (card
);
1182 for (x
= 0; x
< 2; x
++) {
1184 sound_install_audiodrv(AUDIO_DRIVER_VERSION
,
1185 "NM256", &nm256_audio_driver
,
1186 sizeof(struct audio_driver
),
1187 DMA_NODMA
, AFMT_U8
| AFMT_S16_LE
,
1188 NULL
, -1, -1)) >= 0) {
1189 /* 1K minimum buffer size. */
1190 audio_devs
[card
->dev
[x
]]->min_fragment
= 10;
1191 /* Maximum of 8K buffer size. */
1192 audio_devs
[card
->dev
[x
]]->max_fragment
= 13;
1195 printk(KERN_ERR
"NM256: Too many PCM devices available\n");
1196 nm256_release_ports (card
);
1202 /* Insert the card in the list. */
1203 card
->next_card
= nmcard_list
;
1206 printk(KERN_INFO
"Initialized NeoMagic %s audio in PCI native mode\n",
1210 * And our mixer. (We should allow support for other mixers, maybe.)
1213 nm256_install_mixer (card
);
1215 pmdev
= pm_register(PM_PCI_DEV
, PM_PCI_ID(pcidev
), handle_pm_event
);
1224 * PM event handler, so the card is properly reinitialized after a power
1228 handle_pm_event (struct pm_dev
*dev
, pm_request_t rqst
, void *data
)
1230 struct nm256_info
*crd
= (struct nm256_info
*) dev
->data
;
1237 int playing
= crd
->playing
;
1238 nm256_full_reset (crd
);
1240 * A little ugly, but that's ok; pretend the
1241 * block we were playing is done.
1244 DMAbuf_outputintr (crd
->dev_for_play
, 1);
1253 * This loop walks the PCI configuration database and finds where
1254 * the sound cards are.
1260 struct pci_dev
*pcidev
= NULL
;
1266 while((pcidev
= pci_find_device(PCI_VENDOR_ID_NEOMAGIC
,
1267 PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO
,
1269 count
+= nm256_install(pcidev
, REV_NM256AV
, "256AV");
1272 while((pcidev
= pci_find_device(PCI_VENDOR_ID_NEOMAGIC
,
1273 PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO
,
1275 count
+= nm256_install(pcidev
, REV_NM256ZX
, "256ZX");
1281 printk (KERN_INFO
"Done installing NM256 audio driver.\n");
1289 * MODE - mode to open device (logical OR of OPEN_READ and OPEN_WRITE)
1291 * Called when opening the DMAbuf (dmabuf.c:259)
1294 nm256_audio_open(int dev
, int mode
)
1296 struct nm256_info
*card
= nm256_find_card (dev
);
1302 if (card
->dev
[0] == dev
)
1304 else if (card
->dev
[1] == dev
)
1309 if (card
->opencnt
[w
] > 0)
1312 /* No bits set? Huh? */
1313 if (! ((mode
& OPEN_READ
) || (mode
& OPEN_WRITE
)))
1317 * If it's open for both read and write, and the card's currently
1318 * being read or written to, then do the opposite of what has
1319 * already been done. Otherwise, don't specify any mode until the
1320 * user actually tries to do I/O. (Some programs open the device
1321 * for both read and write, but only actually do reading or writing.)
1324 if ((mode
& OPEN_WRITE
) && (mode
& OPEN_READ
)) {
1325 if (card
->is_open_play
)
1327 else if (card
->is_open_record
)
1332 if (mode
& OPEN_WRITE
) {
1333 if (card
->is_open_play
== 0) {
1334 card
->dev_for_play
= dev
;
1335 card
->is_open_play
= 1;
1341 if (mode
& OPEN_READ
) {
1342 if (card
->is_open_record
== 0) {
1343 card
->dev_for_record
= dev
;
1344 card
->is_open_record
= 1;
1359 * Called when closing the DMAbuf (dmabuf.c:477)
1363 nm256_audio_close(int dev
)
1365 struct nm256_info
*card
= nm256_find_card (dev
);
1370 if (card
->dev
[0] == dev
)
1372 else if (card
->dev
[1] == dev
)
1378 if (card
->opencnt
[w
] <= 0) {
1379 card
->opencnt
[w
] = 0;
1381 if (card
->dev_for_play
== dev
) {
1383 card
->is_open_play
= 0;
1384 card
->dev_for_play
= -1;
1387 if (card
->dev_for_record
== dev
) {
1389 card
->is_open_record
= 0;
1390 card
->dev_for_record
= -1;
1396 /* Standard ioctl handler. */
1398 nm256_audio_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
1404 struct nm256_info
*card
= nm256_find_card (dev
);
1409 if (dev
== card
->dev
[0])
1415 * The code here is messy. There are probably better ways to do
1416 * it. (It should be possible to handle it the same way the AC97 mixer
1421 case SOUND_PCM_WRITE_RATE
:
1422 if (get_user(ret
, (int *) arg
))
1426 oldinfo
= card
->sinfo
[w
].samplerate
;
1427 card
->sinfo
[w
].samplerate
= ret
;
1428 ret
= nm256_setInfo(dev
, card
);
1430 card
->sinfo
[w
].samplerate
= oldinfo
;
1433 ret
= card
->sinfo
[w
].samplerate
;
1436 case SOUND_PCM_READ_RATE
:
1437 ret
= card
->sinfo
[w
].samplerate
;
1440 case SNDCTL_DSP_STEREO
:
1441 if (get_user(ret
, (int *) arg
))
1444 card
->sinfo
[w
].stereo
= ret
? 1 : 0;
1445 ret
= nm256_setInfo (dev
, card
);
1447 ret
= card
->sinfo
[w
].stereo
;
1451 case SOUND_PCM_WRITE_CHANNELS
:
1452 if (get_user(ret
, (int *) arg
))
1455 if (ret
< 1 || ret
> 3)
1456 ret
= card
->sinfo
[w
].stereo
+ 1;
1458 card
->sinfo
[w
].stereo
= ret
- 1;
1459 ret
= nm256_setInfo (dev
, card
);
1461 ret
= card
->sinfo
[w
].stereo
+ 1;
1465 case SOUND_PCM_READ_CHANNELS
:
1466 ret
= card
->sinfo
[w
].stereo
+ 1;
1469 case SNDCTL_DSP_SETFMT
:
1470 if (get_user(ret
, (int *) arg
))
1474 oldinfo
= card
->sinfo
[w
].bits
;
1475 card
->sinfo
[w
].bits
= ret
;
1476 ret
= nm256_setInfo (dev
, card
);
1478 card
->sinfo
[w
].bits
= oldinfo
;
1481 ret
= card
->sinfo
[w
].bits
;
1484 case SOUND_PCM_READ_BITS
:
1485 ret
= card
->sinfo
[w
].bits
;
1491 return put_user(ret
, (int *) arg
);
1495 * Given the sound device DEV and an associated physical buffer PHYSBUF,
1496 * return a pointer to the actual buffer in kernel space.
1498 * This routine should exist as part of the soundcore routines.
1502 nm256_getDMAbuffer (int dev
, unsigned long physbuf
)
1504 struct audio_operations
*adev
= audio_devs
[dev
];
1505 struct dma_buffparms
*dmap
= adev
->dmap_out
;
1507 (char *)(physbuf
- (unsigned long)dmap
->raw_buf_phys
1508 + (unsigned long)dmap
->raw_buf
);
1515 * Output a block to sound device
1517 * dev - device number
1518 * buf - physical address of buffer
1519 * total_count - total byte count in buffer
1520 * intrflag - set if this has been called from an interrupt
1521 * (via DMAbuf_outputintr)
1522 * restart_dma - set if engine needs to be re-initialised
1525 * 1. Starting output (dmabuf.c:1327)
1526 * 2. (dmabuf.c:1504)
1527 * 3. A new buffer needs to be sent to the device (dmabuf.c:1579)
1530 nm256_audio_output_block(int dev
, unsigned long physbuf
,
1531 int total_count
, int intrflag
)
1533 struct nm256_info
*card
= nm256_find_card (dev
);
1536 char *dma_buf
= nm256_getDMAbuffer (dev
, physbuf
);
1537 card
->is_open_play
= 1;
1538 card
->dev_for_play
= dev
;
1539 nm256_write_block (card
, dma_buf
, total_count
);
1543 /* Ditto, but do recording instead. */
1545 nm256_audio_start_input(int dev
, unsigned long physbuf
, int count
,
1548 struct nm256_info
*card
= nm256_find_card (dev
);
1551 char *dma_buf
= nm256_getDMAbuffer (dev
, physbuf
);
1552 card
->is_open_record
= 1;
1553 card
->dev_for_record
= dev
;
1554 nm256_startRecording (card
, dma_buf
, count
);
1559 * Prepare for inputting samples to DEV.
1560 * Each requested buffer will be BSIZE byes long, with a total of
1565 nm256_audio_prepare_for_input(int dev
, int bsize
, int bcount
)
1567 struct nm256_info
*card
= nm256_find_card (dev
);
1572 if (card
->is_open_record
&& card
->dev_for_record
!= dev
)
1575 audio_devs
[dev
]->dmap_in
->flags
|= DMA_NODMA
;
1580 * Prepare for outputting samples to `dev'
1582 * Each buffer that will be passed will be `bsize' bytes long,
1583 * with a total of `bcount' buffers.
1586 * 1. A trigger enables audio output (dmabuf.c:978)
1587 * 2. We get a write buffer without dma_mode setup (dmabuf.c:1152)
1588 * 3. We restart a transfer (dmabuf.c:1324)
1592 nm256_audio_prepare_for_output(int dev
, int bsize
, int bcount
)
1594 struct nm256_info
*card
= nm256_find_card (dev
);
1599 if (card
->is_open_play
&& card
->dev_for_play
!= dev
)
1602 audio_devs
[dev
]->dmap_out
->flags
|= DMA_NODMA
;
1606 /* Stop the current operations associated with DEV. */
1608 nm256_audio_reset(int dev
)
1610 struct nm256_info
*card
= nm256_find_card (dev
);
1613 if (card
->dev_for_play
== dev
)
1615 if (card
->dev_for_record
== dev
)
1621 nm256_audio_local_qlen(int dev
)
1626 static struct audio_driver nm256_audio_driver
=
1629 open
: nm256_audio_open
,
1630 close
: nm256_audio_close
,
1631 output_block
: nm256_audio_output_block
,
1632 start_input
: nm256_audio_start_input
,
1633 ioctl
: nm256_audio_ioctl
,
1634 prepare_for_input
: nm256_audio_prepare_for_input
,
1635 prepare_for_output
:nm256_audio_prepare_for_output
,
1636 halt_io
: nm256_audio_reset
,
1637 local_qlen
: nm256_audio_local_qlen
,
1640 EXPORT_SYMBOL(init_nm256
);
1642 static int loaded
= 0;
1644 MODULE_PARM (usecache
, "i");
1645 MODULE_PARM (buffertop
, "i");
1646 MODULE_PARM (nm256_debug
, "i");
1647 MODULE_PARM (force_load
, "i");
1649 static int __init
do_init_nm256(void)
1652 printk (KERN_INFO
"NeoMagic 256AV/256ZX audio driver, version 1.1\n");
1654 if (init_nm256 () == 0) {
1662 static void __exit
cleanup_nm256 (void)
1665 struct nm256_info
*card
;
1666 struct nm256_info
*next_card
;
1668 for (card
= nmcard_list
; card
!= NULL
; card
= next_card
) {
1672 free_irq (card
->irq
, card
);
1673 nm256_release_ports (card
);
1674 sound_unload_mixerdev (card
->mixer_oss_dev
);
1675 sound_unload_audiodev (card
->dev
[0]);
1676 sound_unload_audiodev (card
->dev
[1]);
1677 next_card
= card
->next_card
;
1682 pm_unregister_all (&handle_pm_event
);
1685 module_init(do_init_nm256
);
1686 module_exit(cleanup_nm256
);