1 /* Audio driver for the NeoMagic 256AV and 256ZX chipsets in native
2 mode, with AC97 mixer support.
4 Overall design and parts of this code stolen from vidc_*.c and
7 Yeah, there are a lot of magic constants in here. You tell ME what
8 they are. I just get this stuff psychically, remember?
10 This driver was written by someone who wishes to remain anonymous.
11 It is in the public domain, so share and enjoy. Try to make a profit
12 off of it; go on, I dare you. */
14 #include <linux/config.h>
15 #include <linux/pci.h>
16 #include <linux/module.h>
17 #include "sound_config.h"
18 #include "soundmodule.h"
20 #include "nm256_coeff.h"
24 /* The size of the playback reserve. */
25 #define NM256_PLAY_WMARK_SIZE 512
27 static struct audio_driver nm256_audio_driver
;
29 static int nm256_grabInterrupt (struct nm256_info
*card
);
30 static int nm256_releaseInterrupt (struct nm256_info
*card
);
31 static void nm256_interrupt (int irq
, void *dev_id
, struct pt_regs
*dummy
);
32 static void nm256_interrupt_zx (int irq
, void *dev_id
,
33 struct pt_regs
*dummy
);
35 /* These belong in linux/pci.h. */
36 #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
37 #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
40 static struct nm256_info
*nmcard_list
;
42 /* Locate the card in our list. */
43 static struct nm256_info
*
44 nm256_find_card (int dev
)
46 struct nm256_info
*card
;
48 for (card
= nmcard_list
; card
!= NULL
; card
= card
->next_card
)
49 if (card
->dev
[0] == dev
|| card
->dev
[1] == dev
)
55 /* Ditto, but find the card struct corresponding to the mixer device DEV
57 static struct nm256_info
*
58 nm256_find_card_for_mixer (int dev
)
60 struct nm256_info
*card
;
62 for (card
= nmcard_list
; card
!= NULL
; card
= card
->next_card
)
63 if (card
->mixer_oss_dev
== dev
)
69 static int usecache
= 0;
70 static int buffertop
= 0;
72 /* Check to see if we're using the bank of cached coefficients. */
74 nm256_cachedCoefficients (struct nm256_info
*card
)
79 /* The actual rates supported by the card. */
80 static int samplerates
[9] = {
81 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 99999999
84 /* Set the card samplerate, word size and stereo mode to correspond to
85 the settings in the CARD struct for the specified device in DEV.
86 We keep two separate sets of information, one for each device; the
87 hardware is not actually configured until a read or write is
91 nm256_setInfo (int dev
, struct nm256_info
*card
)
97 if (card
->dev
[0] == dev
)
99 else if (card
->dev
[1] == dev
)
104 targetrate
= card
->sinfo
[w
].samplerate
;
106 if ((card
->sinfo
[w
].bits
!= 8 && card
->sinfo
[w
].bits
!= 16)
107 || targetrate
< samplerates
[0]
108 || targetrate
> samplerates
[7])
111 for (x
= 0; x
< 8; x
++)
112 if (targetrate
< ((samplerates
[x
] + samplerates
[x
+ 1]) / 2))
116 u8 speedbits
= ((x
<< 4) & NM_RATE_MASK
)
117 | (card
->sinfo
[w
].bits
== 16 ? NM_RATE_BITS_16
: 0)
118 | (card
->sinfo
[w
].stereo
? NM_RATE_STEREO
: 0);
120 card
->sinfo
[w
].samplerate
= samplerates
[x
];
122 if (card
->dev_for_play
== dev
&& card
->playing
) {
123 nm256_loadCoefficient (card
, 0, x
);
124 nm256_writePort8 (card
, 2,
125 NM_PLAYBACK_REG_OFFSET
+ NM_RATE_REG_OFFSET
,
129 if (card
->dev_for_record
== dev
&& card
->recording
) {
130 nm256_loadCoefficient (card
, 1, x
);
131 nm256_writePort8 (card
, 2,
132 NM_RECORD_REG_OFFSET
+ NM_RATE_REG_OFFSET
,
141 /* Start the play process going. */
143 startPlay (struct nm256_info
*card
)
145 if (! card
->playing
) {
147 if (nm256_grabInterrupt (card
) == 0) {
148 nm256_setInfo (card
->dev_for_play
, card
);
150 /* Enable playback engine and interrupts. */
151 nm256_writePort8 (card
, 2, NM_PLAYBACK_ENABLE_REG
,
152 NM_PLAYBACK_ENABLE_FLAG
| NM_PLAYBACK_FREERUN
);
154 /* Enable both channels. */
155 nm256_writePort16 (card
, 2, NM_AUDIO_MUTE_REG
, 0x0);
160 /* Request one chunk of AMT bytes from the recording device. When the
161 operation is complete, the data will be copied into BUFFER and the
162 function DMAbuf_inputintr will be invoked. */
165 nm256_startRecording (struct nm256_info
*card
, char *buffer
, u32 amt
)
168 int enableEngine
= 0;
169 u32 ringsize
= card
->recordBufferSize
;
171 if (amt
> (ringsize
/ 2)) {
172 /* Of course this won't actually work right, because the
173 caller is going to assume we will give what we got asked
175 printk (KERN_ERR
"NM256: Read request too large: %d\n", amt
);
180 printk (KERN_ERR
"NM256: Read request too small; %d\n", amt
);
184 /* If we're not currently recording, set up the start and end registers
185 for the recording engine. */
186 if (! card
->recording
) {
188 if (nm256_grabInterrupt (card
) == 0) {
190 nm256_setInfo (card
->dev_for_record
, card
);
191 nm256_writePort32 (card
, 2, NM_RBUFFER_START
, card
->abuf2
);
192 nm256_writePort32 (card
, 2, NM_RBUFFER_END
,
193 card
->abuf2
+ ringsize
);
195 nm256_writePort32 (card
, 2, NM_RBUFFER_CURRP
,
196 card
->abuf2
+ card
->curRecPos
);
200 /* Not sure what else to do here. */
205 endpos
= card
->abuf2
+ ((card
->curRecPos
+ amt
) % ringsize
);
207 card
->recBuf
= buffer
;
208 card
->requestedRecAmt
= amt
;
209 nm256_writePort32 (card
, 2, NM_RBUFFER_WMARK
, endpos
);
210 /* Enable recording engine and interrupts. */
212 nm256_writePort8 (card
, 2, NM_RECORD_ENABLE_REG
,
213 NM_RECORD_ENABLE_FLAG
| NM_RECORD_FREERUN
);
216 /* Stop the play engine. */
218 stopPlay (struct nm256_info
*card
)
220 /* Shut off sound from both channels. */
221 nm256_writePort16 (card
, 2, NM_AUDIO_MUTE_REG
,
222 NM_AUDIO_MUTE_LEFT
| NM_AUDIO_MUTE_RIGHT
);
223 /* Disable play engine. */
224 nm256_writePort8 (card
, 2, NM_PLAYBACK_ENABLE_REG
, 0);
226 nm256_releaseInterrupt (card
);
228 /* Reset the relevant state bits. */
230 card
->curPlayPos
= 0;
234 /* Stop recording. */
236 stopRecord (struct nm256_info
*card
)
238 /* Disable recording engine. */
239 nm256_writePort8 (card
, 2, NM_RECORD_ENABLE_REG
, 0);
241 if (card
->recording
) {
242 nm256_releaseInterrupt (card
);
249 /* Ring buffers, man. That's where the hip-hop, wild-n-wooly action's at.
252 Write AMT bytes of BUFFER to the playback ring buffer, and start the
253 playback engine running. It will only accept up to 1/2 of the total
254 size of the ring buffer. */
257 nm256_write_block (struct nm256_info
*card
, char *buffer
, u32 amt
)
259 u32 ringsize
= card
->playbackBufferSize
;
262 if (amt
> (ringsize
/ 2)) {
263 printk (KERN_ERR
"NM256: Write request too large: %d\n", amt
);
264 amt
= (ringsize
/ 2);
267 if (amt
< NM256_PLAY_WMARK_SIZE
) {
268 printk (KERN_ERR
"NM256: Write request too small: %d\n", amt
);
272 card
->curPlayPos
%= ringsize
;
274 card
->requested_amt
= amt
;
276 if ((card
->curPlayPos
+ amt
) >= ringsize
) {
277 u32 rem
= ringsize
- card
->curPlayPos
;
279 nm256_writeBuffer8 (card
, buffer
, 1,
280 card
->abuf1
+ card
->curPlayPos
,
283 nm256_writeBuffer8 (card
, buffer
, 1, card
->abuf1
,
287 nm256_writeBuffer8 (card
, buffer
, 1,
288 card
->abuf1
+ card
->curPlayPos
,
291 /* Setup the start-n-stop-n-limit registers, and start that engine
294 Normally we just let it wrap around to avoid the click-click
296 if (! card
->playing
) {
297 /* The PBUFFER_END register in this case points to one "word"
298 before the end of the buffer. */
299 int w
= (card
->dev_for_play
== card
->dev
[0] ? 0 : 1);
300 int wordsize
= (card
->sinfo
[w
].bits
== 16 ? 2 : 1)
301 * (card
->sinfo
[w
].stereo
? 2 : 1);
303 /* Need to set the not-normally-changing-registers up. */
304 nm256_writePort32 (card
, 2, NM_PBUFFER_START
,
305 card
->abuf1
+ card
->curPlayPos
);
306 nm256_writePort32 (card
, 2, NM_PBUFFER_END
,
307 card
->abuf1
+ ringsize
- wordsize
);
308 nm256_writePort32 (card
, 2, NM_PBUFFER_CURRP
,
309 card
->abuf1
+ card
->curPlayPos
);
311 endstop
= (card
->curPlayPos
+ amt
- NM256_PLAY_WMARK_SIZE
) % ringsize
;
312 nm256_writePort32 (card
, 2, NM_PBUFFER_WMARK
, card
->abuf1
+ endstop
);
317 /* We just got a card playback interrupt; process it. */
319 nm256_get_new_block (struct nm256_info
*card
)
321 /* Check to see how much got played so far. */
322 u32 amt
= nm256_readPort32 (card
, 2, NM_PBUFFER_CURRP
) - card
->abuf1
;
324 if (amt
>= card
->playbackBufferSize
) {
325 printk (KERN_ERR
"NM256: Sound playback pointer invalid!\n");
329 if (amt
< card
->curPlayPos
)
330 amt
= (card
->playbackBufferSize
- card
->curPlayPos
) + amt
;
332 amt
-= card
->curPlayPos
;
334 if (card
->requested_amt
> (amt
+ NM256_PLAY_WMARK_SIZE
)) {
336 card
->curPlayPos
+ card
->requested_amt
- NM256_PLAY_WMARK_SIZE
;
337 nm256_writePort32 (card
, 2, NM_PBUFFER_WMARK
, card
->abuf1
+ endstop
);
339 card
->curPlayPos
+= card
->requested_amt
;
340 /* Get a new block to write. This will eventually invoke
341 nm256_write_block (). */
342 DMAbuf_outputintr (card
->dev_for_play
, 1);
346 /* Ultra cheez-whiz. But I'm too lazy to grep headers. */
347 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
349 /* Read the last-recorded block from the ring buffer, copy it into the
350 saved buffer pointer, and invoke DMAuf_inputintr() with the recording
354 nm256_read_block (struct nm256_info
*card
)
356 /* Grab the current position of the recording pointer. */
357 u32 currptr
= nm256_readPort32 (card
, 2, NM_RBUFFER_CURRP
) - card
->abuf2
;
358 u32 amtToRead
= card
->requestedRecAmt
;
359 u32 ringsize
= card
->recordBufferSize
;
361 if (currptr
>= card
->recordBufferSize
) {
362 printk (KERN_ERR
"NM256: Sound buffer record pointer invalid!\n");
366 /* This test is probably redundant; we shouldn't be here unless
368 if (card
->recording
) {
369 /* If we wrapped around, copy everything from the start of our
370 recording buffer to the end of the buffer. */
371 if (currptr
< card
->curRecPos
) {
372 u32 amt
= MIN (ringsize
- card
->curRecPos
, amtToRead
);
374 nm256_readBuffer8 (card
, card
->recBuf
, 1,
375 card
->abuf2
+ card
->curRecPos
,
378 card
->curRecPos
+= amt
;
380 if (card
->curRecPos
== ringsize
)
384 if ((card
->curRecPos
< currptr
) && (amtToRead
> 0)) {
385 u32 amt
= MIN (currptr
- card
->curRecPos
, amtToRead
);
386 nm256_readBuffer8 (card
, card
->recBuf
, 1,
387 card
->abuf2
+ card
->curRecPos
, amt
);
388 card
->curRecPos
= ((card
->curRecPos
+ amt
) % ringsize
);
391 card
->requestedRecAmt
= 0;
392 DMAbuf_inputintr (card
->dev_for_record
);
397 /* Initialize the hardware and various other card data we'll need
400 nm256_initHw (struct nm256_info
*card
)
404 card
->playbackBufferSize
= 16384;
405 card
->recordBufferSize
= 16384;
407 card
->coeffBuf
= card
->bufend
- NM_MAX_COEFFICIENT
;
408 card
->abuf2
= card
->coeffBuf
- card
->recordBufferSize
;
409 card
->abuf1
= card
->abuf2
- card
->playbackBufferSize
;
410 card
->allCoeffBuf
= card
->abuf2
- (NM_TOTAL_COEFF_COUNT
* 4);
413 card
->mixer
= NM_MIXER_BASE
;
416 card
->is_open_play
= 0;
417 card
->curPlayPos
= 0;
420 card
->is_open_record
= 0;
423 card
->coeffsCurrent
= 0;
425 card
->opencnt
[0] = 0; card
->opencnt
[1] = 0;
427 /* Reset everything. */
428 nm256_writePort8 (card
, 2, 0, 0x11);
430 /* Disable recording. */
431 nm256_writePort8 (card
, 2, NM_RECORD_ENABLE_REG
, 0);
432 nm256_writePort16 (card
, 2, 0x214, 0);
434 /* Reasonable default settings, but largely unnecessary. */
435 for (x
= 0; x
< 2; x
++) {
436 card
->sinfo
[x
].bits
= 8;
437 card
->sinfo
[x
].stereo
= 0;
438 card
->sinfo
[x
].samplerate
= 8000;
442 /* Handle a potential interrupt for the device referred to by DEV_ID. */
445 nm256_interrupt (int irq
, void *dev_id
, struct pt_regs
*dummy
)
447 struct nm256_info
*card
= (struct nm256_info
*)dev_id
;
449 static int badintrcount
= 0;
451 if ((card
== NULL
) || (card
->magsig
!= NM_MAGIC_SIG
)) {
452 printk (KERN_ERR
"NM256: Bad card pointer\n");
456 status
= nm256_readPort16 (card
, 2, NM_INT_REG
);
460 if (badintrcount
++ > 1000) {
461 printk (KERN_ERR
"NM256: Releasing interrupt, over 1000 invalid interrupts\n");
462 nm256_releaseInterrupt (card
);
469 if (status
& NM_PLAYBACK_INT
) {
470 status
&= ~NM_PLAYBACK_INT
;
471 NM_ACK_INT (card
, NM_PLAYBACK_INT
);
474 nm256_get_new_block (card
);
477 if (status
& NM_RECORD_INT
) {
478 status
&= ~NM_RECORD_INT
;
479 NM_ACK_INT (card
, NM_RECORD_INT
);
482 nm256_read_block (card
);
485 if (status
& NM_MISC_INT_1
) {
488 status
&= ~NM_MISC_INT_1
;
489 printk (KERN_ERR
"NM256: Got misc interrupt #1\n");
490 NM_ACK_INT (card
, NM_MISC_INT_1
);
491 nm256_writePort16 (card
, 2, NM_INT_REG
, 0x8000);
492 cbyte
= nm256_readPort8 (card
, 2, 0x400);
493 nm256_writePort8 (card
, 2, 0x400, cbyte
| 2);
496 if (status
& NM_MISC_INT_2
) {
499 status
&= ~NM_MISC_INT_2
;
500 printk (KERN_ERR
"NM256: Got misc interrupt #2\n");
501 NM_ACK_INT (card
, NM_MISC_INT_2
);
502 cbyte
= nm256_readPort8 (card
, 2, 0x400);
503 nm256_writePort8 (card
, 2, 0x400, cbyte
& ~2);
507 printk (KERN_ERR
"NM256: Fire in the hole! Unknown status 0x%x\n",
510 NM_ACK_INT (card
, status
);
514 /* Handle a potential interrupt for the device referred to by DEV_ID.
515 This handler is for the 256ZX. */
518 nm256_interrupt_zx (int irq
, void *dev_id
, struct pt_regs
*dummy
)
520 struct nm256_info
*card
= (struct nm256_info
*)dev_id
;
522 static int badintrcount
= 0;
524 if ((card
== NULL
) || (card
->magsig
!= NM_MAGIC_SIG
)) {
525 printk (KERN_ERR
"NM256: Bad card pointer\n");
529 status
= nm256_readPort32 (card
, 2, NM_INT_REG
);
533 if (badintrcount
++ > 1000) {
534 printk (KERN_ERR
"NM256: Releasing interrupt, over 1000 invalid interrupts\n");
535 nm256_releaseInterrupt (card
);
542 if (status
& NM2_PLAYBACK_INT
) {
543 status
&= ~NM2_PLAYBACK_INT
;
544 NM2_ACK_INT (card
, NM2_PLAYBACK_INT
);
547 nm256_get_new_block (card
);
550 if (status
& NM2_RECORD_INT
) {
551 status
&= ~NM2_RECORD_INT
;
552 NM2_ACK_INT (card
, NM2_RECORD_INT
);
555 nm256_read_block (card
);
558 if (status
& NM2_MISC_INT_1
) {
561 status
&= ~NM2_MISC_INT_1
;
562 printk (KERN_ERR
"NM256: Got misc interrupt #1\n");
563 NM2_ACK_INT (card
, NM2_MISC_INT_1
);
564 cbyte
= nm256_readPort8 (card
, 2, 0x400);
565 nm256_writePort8 (card
, 2, 0x400, cbyte
| 2);
568 if (status
& NM2_MISC_INT_2
) {
571 status
&= ~NM2_MISC_INT_2
;
572 printk (KERN_ERR
"NM256: Got misc interrupt #2\n");
573 NM2_ACK_INT (card
, NM2_MISC_INT_2
);
574 cbyte
= nm256_readPort8 (card
, 2, 0x400);
575 nm256_writePort8 (card
, 2, 0x400, cbyte
& ~2);
579 printk (KERN_ERR
"NM256: Fire in the hole! Unknown status 0x%x\n",
582 NM2_ACK_INT (card
, status
);
586 /* Request our interrupt. */
588 nm256_grabInterrupt (struct nm256_info
*card
)
590 if (card
->has_irq
++ == 0) {
591 if (request_irq (card
->irq
, card
->introutine
, SA_SHIRQ
,
592 "NM256_audio", card
) < 0) {
593 printk (KERN_ERR
"NM256: can't obtain IRQ %d\n", card
->irq
);
600 /* Release our interrupt. */
602 nm256_releaseInterrupt (struct nm256_info
*card
)
604 if (card
->has_irq
<= 0) {
605 printk (KERN_ERR
"nm256: too many calls to releaseInterrupt\n");
609 if (card
->has_irq
== 0) {
610 free_irq (card
->irq
, card
);
616 nm256_isReady (struct ac97_hwint
*dev
)
618 struct nm256_info
*card
= (struct nm256_info
*)dev
->driver_private
;
624 if (card
->magsig
!= NM_MAGIC_SIG
) {
625 printk (KERN_ERR
"NM256: Bad magic signature in isReady!\n");
629 if (card
->rev
== REV_NM256AV
) {
632 } else if (card
->rev
== REV_NM256ZX
) {
640 if ((nm256_readPort16 (card
, 2, testaddr
) & testb
) == 0) {
650 nm256_readAC97Reg (struct ac97_hwint
*dev
, u8 reg
)
652 struct nm256_info
*card
= (struct nm256_info
*)dev
->driver_private
;
654 if (card
->magsig
!= NM_MAGIC_SIG
) {
655 printk (KERN_ERR
"NM256: Bad magic signature in readAC97Reg!\n");
663 res
= nm256_readPort16 (card
, 2, card
->mixer
+ reg
);
672 nm256_writeAC97Reg (struct ac97_hwint
*dev
, u8 reg
, u16 value
)
679 struct nm256_info
*card
= (struct nm256_info
*)dev
->driver_private
;
681 if (card
->magsig
!= NM_MAGIC_SIG
) {
682 printk (KERN_ERR
"NM256: Bad magic signature in writeAC97Reg!\n");
693 /* Wait for the write to take, too. */
694 while ((tries
-- > 0) && !done
) {
695 nm256_writePort16 (card
, 2, base
+ reg
, value
);
696 if (nm256_isReady (dev
)) {
703 restore_flags (flags
);
712 unsigned short value
;
715 static struct initialValues nm256_ac97_initial_values
[] =
735 nm256_resetAC97 (struct ac97_hwint
*dev
)
737 struct nm256_info
*card
= (struct nm256_info
*)dev
->driver_private
;
740 if (card
->magsig
!= NM_MAGIC_SIG
) {
741 printk (KERN_ERR
"NM256: Bad magic signature in resetAC97!\n");
745 /* Reset the card. 'Tis magic! */
746 nm256_writePort8 (card
, 2, 0x6c0, 1);
747 nm256_writePort8 (card
, 2, 0x6cc, 0x87);
748 nm256_writePort8 (card
, 2, 0x6cc, 0x80);
749 nm256_writePort8 (card
, 2, 0x6cc, 0x0);
751 for (x
= 0; nm256_ac97_initial_values
[x
].port
!= 0xffff; x
++) {
752 ac97_put_register (dev
,
753 nm256_ac97_initial_values
[x
].port
,
754 nm256_ac97_initial_values
[x
].value
);
760 /* We don't do anything special here. */
762 nm256_default_mixer_ioctl (int dev
, unsigned int cmd
, caddr_t arg
)
764 struct nm256_info
*card
= nm256_find_card_for_mixer (dev
);
766 return ac97_mixer_ioctl (&(card
->mdev
), cmd
, arg
);
771 static struct mixer_operations nm256_mixer_operations
= {
774 nm256_default_mixer_ioctl
777 /* I "love" C sometimes. Got braces? */
778 static struct ac97_mixer_value_list mixer_defaults
[] = {
779 { SOUND_MIXER_VOLUME
, { { 85, 85 } } },
780 { SOUND_MIXER_SPEAKER
, { { 100 } } },
781 { SOUND_MIXER_PCM
, { { 65, 65 } } },
782 { SOUND_MIXER_CD
, { { 65, 65 } } },
787 nm256_install_mixer (struct nm256_info
*card
)
791 card
->mdev
.reset_device
= nm256_resetAC97
;
792 card
->mdev
.read_reg
= nm256_readAC97Reg
;
793 card
->mdev
.write_reg
= nm256_writeAC97Reg
;
794 card
->mdev
.driver_private
= (void *)card
;
796 if (ac97_init (&(card
->mdev
)))
799 mixer
= sound_alloc_mixerdev();
800 if (num_mixers
>= MAX_MIXER_DEV
) {
801 printk ("NM256 mixer: Unable to alloc mixerdev\n");
805 mixer_devs
[mixer
] = &nm256_mixer_operations
;
806 card
->mixer_oss_dev
= mixer
;
808 /* Some reasonable default values. */
809 ac97_set_values (&(card
->mdev
), mixer_defaults
);
811 printk(KERN_INFO
"Initialized AC97 mixer\n");
815 /* See if the signature left by the NM256 BIOS is intact; if so, we use
816 the associated address as the end of our buffer. */
818 nm256_peek_for_sig (struct nm256_info
*card
, u32 port1addr
)
820 char *temp
= ioremap_nocache (port1addr
+ card
->port1_end
- 0x0400, 16);
824 printk (KERN_ERR
"NM256: Unable to scan for card signature in video RAM\n");
827 memcpy_fromio (&sig
, temp
, sizeof (u32
));
828 if ((sig
& 0xffff0000) == 0x4e4d0000) {
829 memcpy_fromio (&(card
->bufend
), temp
+ 4, sizeof (u32
));
830 printk (KERN_INFO
"NM256: Found card signature in video RAM: 0x%x\n",
834 release_region ((unsigned long) temp
, 16);
837 /* Install a driver for the soundcard referenced by PCIDEV. */
840 nm256_install(struct pci_dev
*pcidev
, enum nm256rev rev
, char *verstr
)
842 struct nm256_info
*card
;
843 u32 port1addr
= pcidev
->resource
[0].start
;
844 u32 port2addr
= pcidev
->resource
[1].start
;
847 card
= kmalloc (sizeof (struct nm256_info
), GFP_KERNEL
);
849 printk (KERN_ERR
"NM256: out of memory!\n");
853 card
->magsig
= NM_MAGIC_SIG
;
858 /* The NM256 has two memory ports. The first port is nothing
859 more than a chunk of video RAM, which is used as the I/O ring
860 buffer. The second port has the actual juicy stuff (like the
861 mixer and the playback engine control registers). */
863 card
->ports
[1] = ioremap_nocache (port2addr
, NM_PORT2_SIZE
);
865 if (card
->ports
[1] == NULL
) {
866 printk (KERN_ERR
"NM256: Unable to remap port 2\n");
867 kfree_s (card
, sizeof (struct nm256_info
));
871 if (card
->rev
== REV_NM256AV
) {
872 card
->port1_end
= 2560 * 1024;
873 card
->introutine
= nm256_interrupt
;
876 if (nm256_readPort8 (card
, 2, 0xa0b) != 0)
877 card
->port1_end
= 6144 * 1024;
879 card
->port1_end
= 4096 * 1024;
881 card
->introutine
= nm256_interrupt_zx
;
885 card
->bufend
= card
->port1_end
- 0x1400;
887 if (buffertop
>= 98304 && buffertop
< card
->port1_end
)
888 card
->bufend
= buffertop
;
890 nm256_peek_for_sig (card
, port1addr
);
892 card
->port1_start
= card
->bufend
- 98304;
894 printk (KERN_INFO
"NM256: Mapping port 1 from 0x%x - 0x%x\n",
895 card
->port1_start
, card
->port1_end
);
898 ioremap_nocache (port1addr
+ card
->port1_start
,
899 card
->port1_end
- card
->port1_start
);
901 if (card
->ports
[0] == NULL
) {
902 printk (KERN_ERR
"NM256: Unable to remap port 1\n");
903 release_region ((unsigned long) card
->ports
[1], NM_PORT2_SIZE
);
904 kfree_s (card
, sizeof (struct nm256_info
));
908 /* See if we can get the interrupt. */
910 card
->irq
= pcidev
->irq
;
913 if (nm256_grabInterrupt (card
) != 0) {
914 release_region ((unsigned long) card
->ports
[0],
915 card
->port1_end
- card
->port1_start
);
916 release_region ((unsigned long) card
->ports
[1], NM_PORT2_SIZE
);
917 kfree_s (card
, sizeof (struct nm256_info
));
921 nm256_releaseInterrupt (card
);
929 for (x
= 0; x
< 2; x
++) {
931 sound_install_audiodrv(AUDIO_DRIVER_VERSION
,
932 "NM256", &nm256_audio_driver
,
933 sizeof(struct audio_driver
),
934 DMA_NODMA
, AFMT_U8
| AFMT_S16_LE
,
935 NULL
, -1, -1)) >= 0) {
936 /* 1K minimum buffer size. */
937 audio_devs
[card
->dev
[x
]]->min_fragment
= 10;
938 /* Maximum of 8K buffer size. */
939 audio_devs
[card
->dev
[x
]]->max_fragment
= 13;
942 printk(KERN_ERR
"NM256: Too many PCM devices available\n");
943 release_region ((unsigned long) card
->ports
[0],
944 card
->port1_end
- card
->port1_start
);
945 release_region ((unsigned long) card
->ports
[1], NM_PORT2_SIZE
);
946 kfree_s (card
, sizeof (struct nm256_info
));
951 /* Insert the card in the list. */
952 card
->next_card
= nmcard_list
;
955 printk(KERN_INFO
"Initialized NeoMagic %s audio in PCI native mode\n",
959 * And our mixer. (We should allow support for other mixers, maybe.)
962 nm256_install_mixer (card
);
968 * This loop walks the PCI configuration database and finds where
969 * the sound cards are.
975 struct pci_dev
*pcidev
= NULL
;
981 while((pcidev
= pci_find_device(PCI_VENDOR_ID_NEOMAGIC
,
982 PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO
,
984 count
+= nm256_install(pcidev
, REV_NM256AV
, "256AV");
987 while((pcidev
= pci_find_device(PCI_VENDOR_ID_NEOMAGIC
,
988 PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO
,
990 count
+= nm256_install(pcidev
, REV_NM256ZX
, "256ZX");
996 printk (KERN_INFO
"Done installing NM256 audio driver.\n");
1004 * MODE - mode to open device (logical OR of OPEN_READ and OPEN_WRITE)
1006 * Called when opening the DMAbuf (dmabuf.c:259)
1009 nm256_audio_open(int dev
, int mode
)
1011 struct nm256_info
*card
= nm256_find_card (dev
);
1017 if (card
->dev
[0] == dev
)
1019 else if (card
->dev
[1] == dev
)
1024 if (card
->opencnt
[w
] > 0)
1027 /* No bits set? Huh? */
1028 if (! ((mode
& OPEN_READ
) || (mode
& OPEN_WRITE
)))
1031 /* If it's open for both read and write, and the card's currently
1032 being read or written to, then do the opposite of what has
1033 already been done. Otherwise, don't specify any mode until the
1034 user actually tries to do I/O. */
1036 if ((mode
& OPEN_WRITE
) && (mode
& OPEN_READ
)) {
1037 if (card
->is_open_play
)
1039 else if (card
->is_open_record
)
1044 if (mode
& OPEN_WRITE
) {
1045 if (card
->is_open_play
== 0) {
1046 card
->dev_for_play
= dev
;
1047 card
->is_open_play
= 1;
1053 if (mode
& OPEN_READ
) {
1054 if (card
->is_open_record
== 0) {
1055 card
->dev_for_record
= dev
;
1056 card
->is_open_record
= 1;
1071 * Called when closing the DMAbuf (dmabuf.c:477)
1075 nm256_audio_close(int dev
)
1077 struct nm256_info
*card
= nm256_find_card (dev
);
1082 if (card
->dev
[0] == dev
)
1084 else if (card
->dev
[1] == dev
)
1090 if (card
->opencnt
[w
] <= 0) {
1091 card
->opencnt
[w
] = 0;
1093 if (card
->dev_for_play
== dev
) {
1095 card
->is_open_play
= 0;
1096 card
->dev_for_play
= -1;
1099 if (card
->dev_for_record
== dev
) {
1101 card
->is_open_record
= 0;
1102 card
->dev_for_record
= -1;
1109 nm256_audio_ioctl(int dev
, unsigned int cmd
, caddr_t arg
)
1115 struct nm256_info
*card
= nm256_find_card (dev
);
1120 if (dev
== card
->dev
[0])
1127 case SOUND_PCM_WRITE_RATE
:
1128 get_user_ret(ret
, (int *) arg
, -EFAULT
);
1131 oldinfo
= card
->sinfo
[w
].samplerate
;
1132 card
->sinfo
[w
].samplerate
= ret
;
1133 ret
= nm256_setInfo(dev
, card
);
1135 card
->sinfo
[w
].samplerate
= oldinfo
;
1138 ret
= card
->sinfo
[w
].samplerate
;
1141 case SOUND_PCM_READ_RATE
:
1142 ret
= card
->sinfo
[w
].samplerate
;
1145 case SNDCTL_DSP_STEREO
:
1146 get_user_ret(ret
, (int *) arg
, -EFAULT
);
1148 card
->sinfo
[w
].stereo
= ret
? 1 : 0;
1149 ret
= nm256_setInfo (dev
, card
);
1151 ret
= card
->sinfo
[w
].stereo
;
1155 case SOUND_PCM_WRITE_CHANNELS
:
1156 get_user_ret(ret
, (int *) arg
, -EFAULT
);
1158 if (ret
< 1 || ret
> 3)
1159 ret
= card
->sinfo
[w
].stereo
+ 1;
1161 card
->sinfo
[w
].stereo
= ret
- 1;
1162 ret
= nm256_setInfo (dev
, card
);
1164 ret
= card
->sinfo
[w
].stereo
+ 1;
1168 case SOUND_PCM_READ_CHANNELS
:
1169 ret
= card
->sinfo
[w
].stereo
+ 1;
1172 case SNDCTL_DSP_SETFMT
:
1173 get_user_ret(ret
, (int *) arg
, -EFAULT
);
1176 oldinfo
= card
->sinfo
[w
].bits
;
1177 card
->sinfo
[w
].bits
= ret
;
1178 ret
= nm256_setInfo (dev
, card
);
1180 card
->sinfo
[w
].bits
= oldinfo
;
1183 ret
= card
->sinfo
[w
].bits
;
1186 case SOUND_PCM_READ_BITS
:
1187 ret
= card
->sinfo
[w
].bits
;
1193 return put_user(ret
, (int *) arg
);
1196 /* Given the dev DEV and an associated physical buffer PHYSBUF, return
1197 a pointer to the actual buffer in kernel space. */
1200 nm256_getDMAbuffer (int dev
, unsigned long physbuf
)
1202 struct audio_operations
*adev
= audio_devs
[dev
];
1203 struct dma_buffparms
*dmap
= adev
->dmap_out
;
1205 (char *)(physbuf
- (unsigned long)dmap
->raw_buf_phys
1206 + (unsigned long)dmap
->raw_buf
);
1213 * Output a block to sound device
1215 * dev - device number
1216 * buf - physical address of buffer
1217 * total_count - total byte count in buffer
1218 * intrflag - set if this has been called from an interrupt
1219 * (via DMAbuf_outputintr)
1220 * restart_dma - set if engine needs to be re-initialised
1223 * 1. Starting output (dmabuf.c:1327)
1224 * 2. (dmabuf.c:1504)
1225 * 3. A new buffer needs to be sent to the device (dmabuf.c:1579)
1228 nm256_audio_output_block(int dev
, unsigned long physbuf
,
1229 int total_count
, int intrflag
)
1231 struct nm256_info
*card
= nm256_find_card (dev
);
1234 char *dma_buf
= nm256_getDMAbuffer (dev
, physbuf
);
1235 card
->is_open_play
= 1;
1236 card
->dev_for_play
= dev
;
1237 nm256_write_block (card
, dma_buf
, total_count
);
1242 nm256_audio_start_input(int dev
, unsigned long physbuf
, int count
,
1245 struct nm256_info
*card
= nm256_find_card (dev
);
1248 char *dma_buf
= nm256_getDMAbuffer (dev
, physbuf
);
1249 card
->is_open_record
= 1;
1250 card
->dev_for_record
= dev
;
1251 nm256_startRecording (card
, dma_buf
, count
);
1256 nm256_audio_prepare_for_input(int dev
, int bsize
, int bcount
)
1258 struct nm256_info
*card
= nm256_find_card (dev
);
1263 if (card
->is_open_record
&& card
->dev_for_record
!= dev
)
1266 audio_devs
[dev
]->dmap_in
->flags
|= DMA_NODMA
;
1271 * Prepare for outputting samples to `dev'
1273 * Each buffer that will be passed will be `bsize' bytes long,
1274 * with a total of `bcount' buffers.
1277 * 1. A trigger enables audio output (dmabuf.c:978)
1278 * 2. We get a write buffer without dma_mode setup (dmabuf.c:1152)
1279 * 3. We restart a transfer (dmabuf.c:1324)
1282 nm256_audio_prepare_for_output(int dev
, int bsize
, int bcount
)
1284 struct nm256_info
*card
= nm256_find_card (dev
);
1289 if (card
->is_open_play
&& card
->dev_for_play
!= dev
)
1292 audio_devs
[dev
]->dmap_out
->flags
|= DMA_NODMA
;
1296 /* Stop the current operations associated with DEV. */
1298 nm256_audio_reset(int dev
)
1300 struct nm256_info
*card
= nm256_find_card (dev
);
1303 if (card
->dev_for_play
== dev
)
1305 if (card
->dev_for_record
== dev
)
1311 nm256_audio_local_qlen(int dev
)
1316 static struct audio_driver nm256_audio_driver
=
1318 nm256_audio_open
, /* open */
1319 nm256_audio_close
, /* close */
1320 nm256_audio_output_block
, /* output_block */
1321 nm256_audio_start_input
, /* start_input */
1322 nm256_audio_ioctl
, /* ioctl */
1323 nm256_audio_prepare_for_input
, /* prepare_for_input */
1324 nm256_audio_prepare_for_output
, /* prepare_for_output */
1325 nm256_audio_reset
, /* reset */
1326 nm256_audio_local_qlen
, /*+local_qlen */
1327 NULL
, /*+copy_from_user */
1328 NULL
, /*+halt_input */
1329 NULL
, /* halt_output */
1331 NULL
, /*+set_speed */
1332 NULL
, /*+set_bits */
1333 NULL
, /*+set_channels */
1336 EXPORT_SYMBOL(init_nm256
);
1340 static int loaded
= 0;
1342 MODULE_PARM (usecache
, "i");
1343 MODULE_PARM (buffertop
, "i");
1344 MODULE_PARM (nm256_debug
, "i");
1350 printk (KERN_INFO
"NeoMagic 256AV/256ZX audio driver, version 1.0\n");
1352 if (init_nm256 () == 0) {
1362 cleanup_module (void)
1365 struct nm256_info
*card
;
1366 struct nm256_info
*next_card
;
1370 for (card
= nmcard_list
; card
!= NULL
; card
= next_card
) {
1374 free_irq (card
->irq
, card
);
1375 release_region ((unsigned long) card
->ports
[0],
1376 card
->port1_end
- card
->port1_start
);
1377 release_region ((unsigned long) card
->ports
[1], NM_PORT2_SIZE
);
1378 sound_unload_mixerdev (card
->mixer_oss_dev
);
1379 sound_unload_audiodev (card
->dev
[0]);
1380 sound_unload_audiodev (card
->dev
[1]);
1381 next_card
= card
->next_card
;
1382 kfree_s (card
, sizeof (struct nm256_info
));