* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / sound / nm256_audio.c
blob0c3ee7b37e58be771e957d609367cd163e64d83c
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
5 skeleton.c.
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"
19 #include "nm256.h"
20 #include "nm256_coeff.h"
22 int nm256_debug = 0;
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
39 /* List of cards. */
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)
50 return card;
52 return NULL;
55 /* Ditto, but find the card struct corresponding to the mixer device DEV
56 instead. */
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)
64 return card;
66 return NULL;
69 static int usecache = 0;
70 static int buffertop = 0;
72 /* Check to see if we're using the bank of cached coefficients. */
73 int
74 nm256_cachedCoefficients (struct nm256_info *card)
76 return usecache;
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
88 attempted. */
90 int
91 nm256_setInfo (int dev, struct nm256_info *card)
93 int x;
94 int w;
95 int targetrate;
97 if (card->dev[0] == dev)
98 w = 0;
99 else if (card->dev[1] == dev)
100 w = 1;
101 else
102 return -ENODEV;
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])
109 return -EINVAL;
111 for (x = 0; x < 8; x++)
112 if (targetrate < ((samplerates[x] + samplerates[x + 1]) / 2))
113 break;
115 if (x < 8) {
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,
126 speedbits);
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,
133 speedbits);
135 return 0;
137 else
138 return -EINVAL;
141 /* Start the play process going. */
142 static void
143 startPlay (struct nm256_info *card)
145 if (! card->playing) {
146 card->playing = 1;
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. */
164 static void
165 nm256_startRecording (struct nm256_info *card, char *buffer, u32 amt)
167 u32 endpos;
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
174 for. */
175 printk (KERN_ERR "NM256: Read request too large: %d\n", amt);
176 amt = ringsize / 2;
179 if (amt < 8) {
180 printk (KERN_ERR "NM256: Read request too small; %d\n", amt);
181 return;
184 /* If we're not currently recording, set up the start and end registers
185 for the recording engine. */
186 if (! card->recording) {
187 card->recording = 1;
188 if (nm256_grabInterrupt (card) == 0) {
189 card->curRecPos = 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);
197 enableEngine = 1;
199 else {
200 /* Not sure what else to do here. */
201 return;
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. */
211 if (enableEngine)
212 nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG,
213 NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
216 /* Stop the play engine. */
217 static void
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);
225 if (card->playing) {
226 nm256_releaseInterrupt (card);
228 /* Reset the relevant state bits. */
229 card->playing = 0;
230 card->curPlayPos = 0;
234 /* Stop recording. */
235 static void
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);
244 card->recording = 0;
245 card->curRecPos = 0;
249 /* Ring buffers, man. That's where the hip-hop, wild-n-wooly action's at.
250 1972?
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. */
256 static void
257 nm256_write_block (struct nm256_info *card, char *buffer, u32 amt)
259 u32 ringsize = card->playbackBufferSize;
260 u32 endstop;
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);
269 return;
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,
281 rem);
282 if (amt > rem)
283 nm256_writeBuffer8 (card, buffer, 1, card->abuf1,
284 amt - rem);
286 else
287 nm256_writeBuffer8 (card, buffer, 1,
288 card->abuf1 + card->curPlayPos,
289 amt);
291 /* Setup the start-n-stop-n-limit registers, and start that engine
292 goin'.
294 Normally we just let it wrap around to avoid the click-click
295 action scene. */
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);
313 if (! card->playing)
314 startPlay (card);
317 /* We just got a card playback interrupt; process it. */
318 static void
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");
326 amt = 0;
329 if (amt < card->curPlayPos)
330 amt = (card->playbackBufferSize - card->curPlayPos) + amt;
331 else
332 amt -= card->curPlayPos;
334 if (card->requested_amt > (amt + NM256_PLAY_WMARK_SIZE)) {
335 u32 endstop =
336 card->curPlayPos + card->requested_amt - NM256_PLAY_WMARK_SIZE;
337 nm256_writePort32 (card, 2, NM_PBUFFER_WMARK, card->abuf1 + endstop);
338 } else {
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
351 device. */
353 static void
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");
363 currptr = 0;
366 /* This test is probably redundant; we shouldn't be here unless
367 it's true. */
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,
376 amt);
377 amtToRead -= amt;
378 card->curRecPos += amt;
379 card->recBuf += amt;
380 if (card->curRecPos == ringsize)
381 card->curRecPos = 0;
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);
390 card->recBuf = NULL;
391 card->requestedRecAmt = 0;
392 DMAbuf_inputintr (card->dev_for_record);
395 #undef MIN
397 /* Initialize the hardware and various other card data we'll need
398 later. */
399 static void
400 nm256_initHw (struct nm256_info *card)
402 int x;
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);
412 /* Fixed setting. */
413 card->mixer = NM_MIXER_BASE;
415 card->playing = 0;
416 card->is_open_play = 0;
417 card->curPlayPos = 0;
419 card->recording = 0;
420 card->is_open_record = 0;
421 card->curRecPos = 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. */
444 static void
445 nm256_interrupt (int irq, void *dev_id, struct pt_regs *dummy)
447 struct nm256_info *card = (struct nm256_info *)dev_id;
448 u16 status;
449 static int badintrcount = 0;
451 if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
452 printk (KERN_ERR "NM256: Bad card pointer\n");
453 return;
456 status = nm256_readPort16 (card, 2, NM_INT_REG);
458 /* Not ours. */
459 if (status == 0) {
460 if (badintrcount++ > 1000) {
461 printk (KERN_ERR "NM256: Releasing interrupt, over 1000 invalid interrupts\n");
462 nm256_releaseInterrupt (card);
464 return;
467 badintrcount = 0;
469 if (status & NM_PLAYBACK_INT) {
470 status &= ~NM_PLAYBACK_INT;
471 NM_ACK_INT (card, NM_PLAYBACK_INT);
473 if (card->playing)
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);
481 if (card->recording)
482 nm256_read_block (card);
485 if (status & NM_MISC_INT_1) {
486 u8 cbyte;
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) {
497 u8 cbyte;
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);
506 if (status) {
507 printk (KERN_ERR "NM256: Fire in the hole! Unknown status 0x%x\n",
508 status);
509 /* Pray. */
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. */
517 static void
518 nm256_interrupt_zx (int irq, void *dev_id, struct pt_regs *dummy)
520 struct nm256_info *card = (struct nm256_info *)dev_id;
521 u32 status;
522 static int badintrcount = 0;
524 if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
525 printk (KERN_ERR "NM256: Bad card pointer\n");
526 return;
529 status = nm256_readPort32 (card, 2, NM_INT_REG);
531 /* Not ours. */
532 if (status == 0) {
533 if (badintrcount++ > 1000) {
534 printk (KERN_ERR "NM256: Releasing interrupt, over 1000 invalid interrupts\n");
535 nm256_releaseInterrupt (card);
537 return;
540 badintrcount = 0;
542 if (status & NM2_PLAYBACK_INT) {
543 status &= ~NM2_PLAYBACK_INT;
544 NM2_ACK_INT (card, NM2_PLAYBACK_INT);
546 if (card->playing)
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);
554 if (card->recording)
555 nm256_read_block (card);
558 if (status & NM2_MISC_INT_1) {
559 u8 cbyte;
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) {
569 u8 cbyte;
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);
578 if (status) {
579 printk (KERN_ERR "NM256: Fire in the hole! Unknown status 0x%x\n",
580 status);
581 /* Pray. */
582 NM2_ACK_INT (card, status);
586 /* Request our interrupt. */
587 static int
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);
594 return -1;
597 return 0;
600 /* Release our interrupt. */
601 static int
602 nm256_releaseInterrupt (struct nm256_info *card)
604 if (card->has_irq <= 0) {
605 printk (KERN_ERR "nm256: too many calls to releaseInterrupt\n");
606 return -1;
608 card->has_irq--;
609 if (card->has_irq == 0) {
610 free_irq (card->irq, card);
612 return 0;
615 static int
616 nm256_isReady (struct ac97_hwint *dev)
618 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
619 int t2 = 10;
620 u32 testaddr;
621 u16 testb;
622 int done = 0;
624 if (card->magsig != NM_MAGIC_SIG) {
625 printk (KERN_ERR "NM256: Bad magic signature in isReady!\n");
626 return 0;
629 if (card->rev == REV_NM256AV) {
630 testaddr = 0xa06;
631 testb = 0x0100;
632 } else if (card->rev == REV_NM256ZX) {
633 testaddr = 0xa08;
634 testb = 0x0800;
635 } else {
636 return -1;
639 while (t2-- > 0) {
640 if ((nm256_readPort16 (card, 2, testaddr) & testb) == 0) {
641 done = 1;
642 break;
644 udelay (100);
646 return done;
649 static int
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");
656 return -EINVAL;
659 if (reg < 128) {
660 int res;
662 nm256_isReady (dev);
663 res = nm256_readPort16 (card, 2, card->mixer + reg);
664 udelay (1000);
665 return res;
667 else
668 return -EINVAL;
671 static int
672 nm256_writeAC97Reg (struct ac97_hwint *dev, u8 reg, u16 value)
674 unsigned long flags;
675 int tries = 2;
676 int done = 0;
677 u32 base;
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");
683 return -EINVAL;
686 base = card->mixer;
688 save_flags (flags);
689 cli ();
691 nm256_isReady (dev);
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)) {
697 done = 1;
698 break;
703 restore_flags (flags);
704 udelay (1000);
706 return ! done;
709 struct initialValues
711 unsigned short port;
712 unsigned short value;
715 static struct initialValues nm256_ac97_initial_values[] =
717 { 0x0002, 0x8000 },
718 { 0x0004, 0x0000 },
719 { 0x0006, 0x0000 },
720 { 0x000A, 0x0000 },
721 { 0x000C, 0x0008 },
722 { 0x000E, 0x8008 },
723 { 0x0010, 0x8808 },
724 { 0x0012, 0x8808 },
725 { 0x0014, 0x8808 },
726 { 0x0016, 0x8808 },
727 { 0x0018, 0x0808 },
728 { 0x001A, 0x0000 },
729 { 0x001C, 0x0B0B },
730 { 0x0020, 0x0000 },
731 { 0xffff, 0xffff }
734 static int
735 nm256_resetAC97 (struct ac97_hwint *dev)
737 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
738 int x;
740 if (card->magsig != NM_MAGIC_SIG) {
741 printk (KERN_ERR "NM256: Bad magic signature in resetAC97!\n");
742 return -EINVAL;
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);
757 return 0;
760 /* We don't do anything special here. */
761 static int
762 nm256_default_mixer_ioctl (int dev, unsigned int cmd, caddr_t arg)
764 struct nm256_info *card = nm256_find_card_for_mixer (dev);
765 if (card != NULL)
766 return ac97_mixer_ioctl (&(card->mdev), cmd, arg);
767 else
768 return -ENODEV;
771 static struct mixer_operations nm256_mixer_operations = {
772 "NeoMagic",
773 "NM256AC97Mixer",
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 } } },
783 { -1, { { 0, 0 } } }
786 static int
787 nm256_install_mixer (struct nm256_info *card)
789 int mixer;
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)))
797 return -1;
799 mixer = sound_alloc_mixerdev();
800 if (num_mixers >= MAX_MIXER_DEV) {
801 printk ("NM256 mixer: Unable to alloc mixerdev\n");
802 return -1;
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");
812 return 0;
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. */
817 static void
818 nm256_peek_for_sig (struct nm256_info *card, u32 port1addr)
820 char *temp = ioremap_nocache (port1addr + card->port1_end - 0x0400, 16);
821 u32 sig;
823 if (temp == NULL) {
824 printk (KERN_ERR "NM256: Unable to scan for card signature in video RAM\n");
825 return;
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",
831 card->bufend);
834 release_region ((unsigned long) temp, 16);
837 /* Install a driver for the soundcard referenced by PCIDEV. */
839 static int
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;
845 int x;
847 card = kmalloc (sizeof (struct nm256_info), GFP_KERNEL);
848 if (card == NULL) {
849 printk (KERN_ERR "NM256: out of memory!\n");
850 return 0;
853 card->magsig = NM_MAGIC_SIG;
854 card->playing = 0;
855 card->recording = 0;
856 card->rev = rev;
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));
868 return 0;
871 if (card->rev == REV_NM256AV) {
872 card->port1_end = 2560 * 1024;
873 card->introutine = nm256_interrupt;
875 else {
876 if (nm256_readPort8 (card, 2, 0xa0b) != 0)
877 card->port1_end = 6144 * 1024;
878 else
879 card->port1_end = 4096 * 1024;
881 card->introutine = nm256_interrupt_zx;
884 /* Default value. */
885 card->bufend = card->port1_end - 0x1400;
887 if (buffertop >= 98304 && buffertop < card->port1_end)
888 card->bufend = buffertop;
889 else
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);
897 card->ports[0] =
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));
905 return 0;
908 /* See if we can get the interrupt. */
910 card->irq = pcidev->irq;
911 card->has_irq = 0;
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));
918 return 0;
921 nm256_releaseInterrupt (card);
924 * Init the board.
927 nm256_initHw (card);
929 for (x = 0; x < 2; x++) {
930 if ((card->dev[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;
941 else {
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));
947 return 0;
951 /* Insert the card in the list. */
952 card->next_card = nmcard_list;
953 nmcard_list = card;
955 printk(KERN_INFO "Initialized NeoMagic %s audio in PCI native mode\n",
956 verstr);
959 * And our mixer. (We should allow support for other mixers, maybe.)
962 nm256_install_mixer (card);
964 return 1;
968 * This loop walks the PCI configuration database and finds where
969 * the sound cards are.
973 init_nm256(void)
975 struct pci_dev *pcidev = NULL;
976 int count = 0;
978 if(! pci_present())
979 return -ENODEV;
981 while((pcidev = pci_find_device(PCI_VENDOR_ID_NEOMAGIC,
982 PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO,
983 pcidev)) != NULL) {
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,
989 pcidev)) != NULL) {
990 count += nm256_install(pcidev, REV_NM256ZX, "256ZX");
993 if (count == 0)
994 return -ENODEV;
996 printk (KERN_INFO "Done installing NM256 audio driver.\n");
997 return 0;
1001 * Open the device
1003 * DEV - device
1004 * MODE - mode to open device (logical OR of OPEN_READ and OPEN_WRITE)
1006 * Called when opening the DMAbuf (dmabuf.c:259)
1008 static int
1009 nm256_audio_open(int dev, int mode)
1011 struct nm256_info *card = nm256_find_card (dev);
1012 int w;
1014 if (card == NULL)
1015 return -ENODEV;
1017 if (card->dev[0] == dev)
1018 w = 0;
1019 else if (card->dev[1] == dev)
1020 w = 1;
1021 else
1022 return -ENODEV;
1024 if (card->opencnt[w] > 0)
1025 return -EBUSY;
1027 /* No bits set? Huh? */
1028 if (! ((mode & OPEN_READ) || (mode & OPEN_WRITE)))
1029 return -EIO;
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)
1038 mode = OPEN_WRITE;
1039 else if (card->is_open_record)
1040 mode = OPEN_READ;
1041 else mode = 0;
1044 if (mode & OPEN_WRITE) {
1045 if (card->is_open_play == 0) {
1046 card->dev_for_play = dev;
1047 card->is_open_play = 1;
1049 else
1050 return -EBUSY;
1053 if (mode & OPEN_READ) {
1054 if (card->is_open_record == 0) {
1055 card->dev_for_record = dev;
1056 card->is_open_record = 1;
1058 else
1059 return -EBUSY;
1062 card->opencnt[w]++;
1063 return 0;
1067 * Close the device
1069 * DEV - device
1071 * Called when closing the DMAbuf (dmabuf.c:477)
1072 * after halt_xfer
1074 static void
1075 nm256_audio_close(int dev)
1077 struct nm256_info *card = nm256_find_card (dev);
1079 if (card != NULL) {
1080 int w;
1082 if (card->dev[0] == dev)
1083 w = 0;
1084 else if (card->dev[1] == dev)
1085 w = 1;
1086 else
1087 return;
1089 card->opencnt[w]--;
1090 if (card->opencnt[w] <= 0) {
1091 card->opencnt[w] = 0;
1093 if (card->dev_for_play == dev) {
1094 stopPlay (card);
1095 card->is_open_play = 0;
1096 card->dev_for_play = -1;
1099 if (card->dev_for_record == dev) {
1100 stopRecord (card);
1101 card->is_open_record = 0;
1102 card->dev_for_record = -1;
1108 static int
1109 nm256_audio_ioctl(int dev, unsigned int cmd, caddr_t arg)
1111 int ret;
1112 u32 oldinfo;
1113 int w;
1115 struct nm256_info *card = nm256_find_card (dev);
1117 if (card == NULL)
1118 return -ENODEV;
1120 if (dev == card->dev[0])
1121 w = 0;
1122 else
1123 w = 1;
1125 switch (cmd)
1127 case SOUND_PCM_WRITE_RATE:
1128 get_user_ret(ret, (int *) arg, -EFAULT);
1130 if (ret != 0) {
1131 oldinfo = card->sinfo[w].samplerate;
1132 card->sinfo[w].samplerate = ret;
1133 ret = nm256_setInfo(dev, card);
1134 if (ret != 0)
1135 card->sinfo[w].samplerate = oldinfo;
1137 if (ret == 0)
1138 ret = card->sinfo[w].samplerate;
1139 break;
1141 case SOUND_PCM_READ_RATE:
1142 ret = card->sinfo[w].samplerate;
1143 break;
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);
1150 if (ret == 0)
1151 ret = card->sinfo[w].stereo;
1153 break;
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;
1160 else {
1161 card->sinfo[w].stereo = ret - 1;
1162 ret = nm256_setInfo (dev, card);
1163 if (ret == 0)
1164 ret = card->sinfo[w].stereo + 1;
1166 break;
1168 case SOUND_PCM_READ_CHANNELS:
1169 ret = card->sinfo[w].stereo + 1;
1170 break;
1172 case SNDCTL_DSP_SETFMT:
1173 get_user_ret(ret, (int *) arg, -EFAULT);
1175 if (ret != 0) {
1176 oldinfo = card->sinfo[w].bits;
1177 card->sinfo[w].bits = ret;
1178 ret = nm256_setInfo (dev, card);
1179 if (ret != 0)
1180 card->sinfo[w].bits = oldinfo;
1182 if (ret == 0)
1183 ret = card->sinfo[w].bits;
1184 break;
1186 case SOUND_PCM_READ_BITS:
1187 ret = card->sinfo[w].bits;
1188 break;
1190 default:
1191 return -EINVAL;
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. */
1199 static char *
1200 nm256_getDMAbuffer (int dev, unsigned long physbuf)
1202 struct audio_operations *adev = audio_devs[dev];
1203 struct dma_buffparms *dmap = adev->dmap_out;
1204 char *dma_start =
1205 (char *)(physbuf - (unsigned long)dmap->raw_buf_phys
1206 + (unsigned long)dmap->raw_buf);
1208 return dma_start;
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
1222 * Called when:
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)
1227 static void
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);
1233 if (card != NULL) {
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);
1241 static void
1242 nm256_audio_start_input(int dev, unsigned long physbuf, int count,
1243 int intrflag)
1245 struct nm256_info *card = nm256_find_card (dev);
1247 if (card != NULL) {
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);
1255 static int
1256 nm256_audio_prepare_for_input(int dev, int bsize, int bcount)
1258 struct nm256_info *card = nm256_find_card (dev);
1260 if (card == NULL)
1261 return -ENODEV;
1263 if (card->is_open_record && card->dev_for_record != dev)
1264 return -EBUSY;
1266 audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
1267 return 0;
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.
1276 * Called when:
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)
1281 static int
1282 nm256_audio_prepare_for_output(int dev, int bsize, int bcount)
1284 struct nm256_info *card = nm256_find_card (dev);
1286 if (card == NULL)
1287 return -ENODEV;
1289 if (card->is_open_play && card->dev_for_play != dev)
1290 return -EBUSY;
1292 audio_devs[dev]->dmap_out->flags |= DMA_NODMA;
1293 return 0;
1296 /* Stop the current operations associated with DEV. */
1297 static void
1298 nm256_audio_reset(int dev)
1300 struct nm256_info *card = nm256_find_card (dev);
1302 if (card != NULL) {
1303 if (card->dev_for_play == dev)
1304 stopPlay (card);
1305 if (card->dev_for_record == dev)
1306 stopRecord (card);
1310 static int
1311 nm256_audio_local_qlen(int dev)
1313 return 0;
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 */
1330 NULL, /*+trigger */
1331 NULL, /*+set_speed */
1332 NULL, /*+set_bits */
1333 NULL, /*+set_channels */
1336 EXPORT_SYMBOL(init_nm256);
1338 #ifdef MODULE
1340 static int loaded = 0;
1342 MODULE_PARM (usecache, "i");
1343 MODULE_PARM (buffertop, "i");
1344 MODULE_PARM (nm256_debug, "i");
1347 init_module (void)
1349 nmcard_list = NULL;
1350 printk (KERN_INFO "NeoMagic 256AV/256ZX audio driver, version 1.0\n");
1352 if (init_nm256 () == 0) {
1353 SOUND_LOCK;
1354 loaded = 1;
1355 return 0;
1357 else
1358 return -ENODEV;
1361 void
1362 cleanup_module (void)
1364 if (loaded) {
1365 struct nm256_info *card;
1366 struct nm256_info *next_card;
1368 SOUND_LOCK_END;
1370 for (card = nmcard_list; card != NULL; card = next_card) {
1371 stopPlay (card);
1372 stopRecord (card);
1373 if (card->has_irq)
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));
1384 nmcard_list = NULL;
1387 #endif
1390 * Local variables:
1391 * c-basic-offset: 4
1392 * End: