ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / oss / nm256_audio.c
1 /* 
2  * Audio driver for the NeoMagic 256AV and 256ZX chipsets in native
3  * mode, with AC97 mixer support.
4  *
5  * Overall design and parts of this code stolen from vidc_*.c and
6  * skeleton.c.
7  *
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? 
10  *
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.  
14  *
15  * Changes:
16  * 11-10-2000   Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
17  *              Added some __init
18  * 19-04-2001   Marcus Meissner <mm@caldera.de>
19  *              Ported to 2.4 PCI API.
20  */
21
22 #include <linux/pci.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/pm.h>
28 #include <linux/delay.h>
29 #include <linux/spinlock.h>
30 #include "sound_config.h"
31 #include "nm256.h"
32 #include "nm256_coeff.h"
33
34 int nm256_debug;
35 static int force_load;
36
37 /* 
38  * The size of the playback reserve.  When the playback buffer has less
39  * than NM256_PLAY_WMARK_SIZE bytes to output, we request a new
40  * buffer.
41  */
42 #define NM256_PLAY_WMARK_SIZE 512
43
44 static struct audio_driver nm256_audio_driver;
45
46 static int nm256_grabInterrupt (struct nm256_info *card);
47 static int nm256_releaseInterrupt (struct nm256_info *card);
48 static irqreturn_t nm256_interrupt (int irq, void *dev_id, struct pt_regs *dummy);
49 static irqreturn_t nm256_interrupt_zx (int irq, void *dev_id, struct pt_regs *dummy);
50 static int handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data);
51
52 /* These belong in linux/pci.h. */
53 #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
54 #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
55
56 /* List of cards.  */
57 static struct nm256_info *nmcard_list;
58
59 /* Release the mapped-in memory for CARD.  */
60 static void
61 nm256_release_ports (struct nm256_info *card)
62 {
63     int x;
64
65     for (x = 0; x < 2; x++) {
66         if (card->port[x].ptr != NULL) {
67             iounmap (card->port[x].ptr);
68             card->port[x].ptr = NULL;
69         }
70     }
71 }
72
73 /* 
74  * Map in the memory ports for CARD, if they aren't already mapped in
75  * and have been configured.  If successful, a zero value is returned;
76  * otherwise any previously mapped-in areas are released and a non-zero
77  * value is returned.
78  *
79  * This is invoked twice, once for each port.  Ideally it would only be
80  * called once, but we now need to map in the second port in order to
81  * check how much memory the card has on the 256ZX.
82  */
83 static int
84 nm256_remap_ports (struct nm256_info *card)
85 {
86     int x;
87
88     for (x = 0; x < 2; x++) {
89         if (card->port[x].ptr == NULL && card->port[x].end_offset > 0) {
90             u32 physaddr 
91                 = card->port[x].physaddr + card->port[x].start_offset;
92             u32 size 
93                 = card->port[x].end_offset - card->port[x].start_offset;
94
95             card->port[x].ptr = ioremap_nocache (physaddr, size);
96                                                   
97             if (card->port[x].ptr == NULL) {
98                 printk (KERN_ERR "NM256: Unable to remap port %d\n", x + 1);
99                 nm256_release_ports (card);
100                 return -1;
101             }
102         }
103     }
104     return 0;
105 }
106
107 /* Locate the card in our list. */
108 static struct nm256_info *
109 nm256_find_card (int dev)
110 {
111     struct nm256_info *card;
112
113     for (card = nmcard_list; card != NULL; card = card->next_card)
114         if (card->dev[0] == dev || card->dev[1] == dev)
115             return card;
116
117     return NULL;
118 }
119
120 /*
121  * Ditto, but find the card struct corresponding to the mixer device DEV 
122  * instead. 
123  */
124 static struct nm256_info *
125 nm256_find_card_for_mixer (int dev)
126 {
127     struct nm256_info *card;
128
129     for (card = nmcard_list; card != NULL; card = card->next_card)
130         if (card->mixer_oss_dev == dev)
131             return card;
132
133     return NULL;
134 }
135
136 static int usecache;
137 static int buffertop;
138
139 /* Check to see if we're using the bank of cached coefficients. */
140 int
141 nm256_cachedCoefficients (struct nm256_info *card)
142 {
143     return usecache;
144 }
145
146 /* The actual rates supported by the card. */
147 static int samplerates[9] = {
148     8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 99999999
149 };
150
151 /*
152  * Set the card samplerate, word size and stereo mode to correspond to
153  * the settings in the CARD struct for the specified device in DEV.
154  * We keep two separate sets of information, one for each device; the
155  * hardware is not actually configured until a read or write is
156  * attempted.
157  */
158
159 int
160 nm256_setInfo (int dev, struct nm256_info *card)
161 {
162     int x;
163     int w;
164     int targetrate;
165
166     if (card->dev[0] == dev)
167         w = 0;
168     else if (card->dev[1] == dev)
169         w = 1;
170     else
171         return -ENODEV;
172
173     targetrate = card->sinfo[w].samplerate;
174
175     if ((card->sinfo[w].bits != 8 && card->sinfo[w].bits != 16)
176         || targetrate < samplerates[0]
177         || targetrate > samplerates[7])
178         return -EINVAL;
179
180     for (x = 0; x < 8; x++)
181         if (targetrate < ((samplerates[x] + samplerates[x + 1]) / 2))
182             break;
183
184     if (x < 8) {
185         u8 ratebits = ((x << 4) & NM_RATE_MASK);
186         if (card->sinfo[w].bits == 16)
187             ratebits |= NM_RATE_BITS_16;
188         if (card->sinfo[w].stereo)
189             ratebits |= NM_RATE_STEREO;
190
191         card->sinfo[w].samplerate = samplerates[x];
192
193
194         if (card->dev_for_play == dev && card->playing) {
195             if (nm256_debug)
196                 printk (KERN_DEBUG "Setting play ratebits to 0x%x\n",
197                         ratebits);
198             nm256_loadCoefficient (card, 0, x);
199             nm256_writePort8 (card, 2,
200                               NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET,
201                               ratebits);
202         }
203
204         if (card->dev_for_record == dev && card->recording) {
205             if (nm256_debug)
206                 printk (KERN_DEBUG "Setting record ratebits to 0x%x\n",
207                         ratebits);
208             nm256_loadCoefficient (card, 1, x);
209             nm256_writePort8 (card, 2,
210                               NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET,
211                               ratebits);
212         }
213         return 0;
214     }
215     else
216         return -EINVAL;
217 }
218
219 /* Start the play process going. */
220 static void
221 startPlay (struct nm256_info *card)
222 {
223     if (! card->playing) {
224         card->playing = 1;
225         if (nm256_grabInterrupt (card) == 0) {
226             nm256_setInfo (card->dev_for_play, card);
227
228             /* Enable playback engine and interrupts. */
229             nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG,
230                               NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);
231
232             /* Enable both channels. */
233             nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG, 0x0);
234         }
235     }
236 }
237
238 /* 
239  * Request one chunk of AMT bytes from the recording device.  When the
240  * operation is complete, the data will be copied into BUFFER and the
241  * function DMAbuf_inputintr will be invoked.
242  */
243
244 static void
245 nm256_startRecording (struct nm256_info *card, char *buffer, u32 amt)
246 {
247     u32 endpos;
248     int enableEngine = 0;
249     u32 ringsize = card->recordBufferSize;
250     unsigned long flags;
251
252     if (amt > (ringsize / 2)) {
253         /*
254          * Of course this won't actually work right, because the
255          * caller is going to assume we will give what we got asked
256          * for.
257          */
258         printk (KERN_ERR "NM256: Read request too large: %d\n", amt);
259         amt = ringsize / 2;
260     }
261
262     if (amt < 8) {
263         printk (KERN_ERR "NM256: Read request too small; %d\n", amt);
264         return;
265     }
266
267     spin_lock_irqsave(&card->lock,flags);
268     /*
269      * If we're not currently recording, set up the start and end registers
270      * for the recording engine.
271      */
272     if (! card->recording) {
273         card->recording = 1;
274         if (nm256_grabInterrupt (card) == 0) {
275             card->curRecPos = 0;
276             nm256_setInfo (card->dev_for_record, card);
277             nm256_writePort32 (card, 2, NM_RBUFFER_START, card->abuf2);
278             nm256_writePort32 (card, 2, NM_RBUFFER_END,
279                                  card->abuf2 + ringsize);
280
281             nm256_writePort32 (card, 2, NM_RBUFFER_CURRP,
282                                  card->abuf2 + card->curRecPos);
283             enableEngine = 1;
284         }
285         else {
286             /* Not sure what else to do here.  */
287             spin_unlock_irqrestore(&card->lock,flags);
288             return;
289         }
290     }
291
292     /* 
293      * If we happen to go past the end of the buffer a bit (due to a
294      * delayed interrupt) it's OK.  So might as well set the watermark
295      * right at the end of the data we want.
296      */
297     endpos = card->abuf2 + ((card->curRecPos + amt) % ringsize);
298
299     card->recBuf = buffer;
300     card->requestedRecAmt = amt;
301     nm256_writePort32 (card, 2, NM_RBUFFER_WMARK, endpos);
302     /* Enable recording engine and interrupts. */
303     if (enableEngine)
304         nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG,
305                             NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
306
307     spin_unlock_irqrestore(&card->lock,flags);
308 }
309
310 /* Stop the play engine. */
311 static void
312 stopPlay (struct nm256_info *card)
313 {
314     /* Shut off sound from both channels. */
315     nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG,
316                        NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT);
317     /* Disable play engine. */
318     nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG, 0);
319     if (card->playing) {
320         nm256_releaseInterrupt (card);
321
322         /* Reset the relevant state bits. */
323         card->playing = 0;
324         card->curPlayPos = 0;
325     }
326 }
327
328 /* Stop recording. */
329 static void
330 stopRecord (struct nm256_info *card)
331 {
332     /* Disable recording engine. */
333     nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG, 0);
334
335     if (card->recording) {
336         nm256_releaseInterrupt (card);
337
338         card->recording = 0;
339         card->curRecPos = 0;
340     }
341 }
342
343 /*
344  * Ring buffers, man.  That's where the hip-hop, wild-n-wooly action's at.
345  * 1972?  (Well, I suppose it was cheep-n-easy to implement.)
346  *
347  * Write AMT bytes of BUFFER to the playback ring buffer, and start the
348  * playback engine running.  It will only accept up to 1/2 of the total
349  * size of the ring buffer.  No check is made that we're about to overwrite
350  * the currently-playing sample.
351  */
352
353 static void
354 nm256_write_block (struct nm256_info *card, char *buffer, u32 amt)
355 {
356     u32 ringsize = card->playbackBufferSize;
357     u32 endstop;
358     unsigned long flags;
359
360     if (amt > (ringsize / 2)) {
361         printk (KERN_ERR "NM256: Write request too large: %d\n", amt);
362         amt = (ringsize / 2);
363     }
364
365     if (amt < NM256_PLAY_WMARK_SIZE) {
366         printk (KERN_ERR "NM256: Write request too small: %d\n", amt);
367         return;
368     }
369
370     card->curPlayPos %= ringsize;
371
372     card->requested_amt = amt;
373
374     spin_lock_irqsave(&card->lock,flags);
375
376     if ((card->curPlayPos + amt) >= ringsize) {
377         u32 rem = ringsize - card->curPlayPos;
378
379         nm256_writeBuffer8 (card, buffer, 1,
380                               card->abuf1 + card->curPlayPos,
381                               rem);
382         if (amt > rem)
383             nm256_writeBuffer8 (card, buffer + rem, 1, card->abuf1,
384                                   amt - rem);
385     } 
386     else
387         nm256_writeBuffer8 (card, buffer, 1,
388                               card->abuf1 + card->curPlayPos,
389                               amt);
390
391     /*
392      * Setup the start-n-stop-n-limit registers, and start that engine
393      * goin'. 
394      *
395      * Normally we just let it wrap around to avoid the click-click
396      * action scene.
397      */
398     if (! card->playing) {
399         /* The PBUFFER_END register in this case points to one sample
400            before the end of the buffer. */
401         int w = (card->dev_for_play == card->dev[0] ? 0 : 1);
402         int sampsize = (card->sinfo[w].bits == 16 ? 2 : 1);
403
404         if (card->sinfo[w].stereo)
405             sampsize *= 2;
406
407         /* Need to set the not-normally-changing-registers up. */
408         nm256_writePort32 (card, 2, NM_PBUFFER_START,
409                              card->abuf1 + card->curPlayPos);
410         nm256_writePort32 (card, 2, NM_PBUFFER_END,
411                              card->abuf1 + ringsize - sampsize);
412         nm256_writePort32 (card, 2, NM_PBUFFER_CURRP,
413                              card->abuf1 + card->curPlayPos);
414     }
415     endstop = (card->curPlayPos + amt - NM256_PLAY_WMARK_SIZE) % ringsize;
416     nm256_writePort32 (card, 2, NM_PBUFFER_WMARK, card->abuf1 + endstop);
417
418     if (! card->playing)
419         startPlay (card);
420
421     spin_unlock_irqrestore(&card->lock,flags);
422 }
423
424 /*  We just got a card playback interrupt; process it.  */
425 static void
426 nm256_get_new_block (struct nm256_info *card)
427 {
428     /* Check to see how much got played so far. */
429     u32 amt = nm256_readPort32 (card, 2, NM_PBUFFER_CURRP) - card->abuf1;
430
431     if (amt >= card->playbackBufferSize) {
432         printk (KERN_ERR "NM256: Sound playback pointer invalid!\n");
433         amt = 0;
434     }
435
436     if (amt < card->curPlayPos)
437         amt = (card->playbackBufferSize - card->curPlayPos) + amt;
438     else
439         amt -= card->curPlayPos;
440
441     if (card->requested_amt > (amt + NM256_PLAY_WMARK_SIZE)) {
442         u32 endstop =
443             card->curPlayPos + card->requested_amt - NM256_PLAY_WMARK_SIZE;
444         nm256_writePort32 (card, 2, NM_PBUFFER_WMARK, card->abuf1 + endstop);
445     } 
446     else {
447         card->curPlayPos += card->requested_amt;
448         /* Get a new block to write.  This will eventually invoke
449            nm256_write_block () or stopPlay ().  */
450         DMAbuf_outputintr (card->dev_for_play, 1);
451     }
452 }
453
454 /* 
455  * Read the last-recorded block from the ring buffer, copy it into the
456  * saved buffer pointer, and invoke DMAuf_inputintr() with the recording
457  * device. 
458  */
459
460 static void
461 nm256_read_block (struct nm256_info *card)
462 {
463     /* Grab the current position of the recording pointer. */
464     u32 currptr = nm256_readPort32 (card, 2, NM_RBUFFER_CURRP) - card->abuf2;
465     u32 amtToRead = card->requestedRecAmt;
466     u32 ringsize = card->recordBufferSize;
467
468     if (currptr >= card->recordBufferSize) {
469         printk (KERN_ERR "NM256: Sound buffer record pointer invalid!\n");
470         currptr = 0;
471     }
472
473     /*
474      * This test is probably redundant; we shouldn't be here unless
475      * it's true.
476      */
477     if (card->recording) {
478         /* If we wrapped around, copy everything from the start of our
479            recording buffer to the end of the buffer. */
480         if (currptr < card->curRecPos) {
481             u32 amt = min (ringsize - card->curRecPos, amtToRead);
482
483             nm256_readBuffer8 (card, card->recBuf, 1,
484                                  card->abuf2 + card->curRecPos,
485                                  amt);
486             amtToRead -= amt;
487             card->curRecPos += amt;
488             card->recBuf += amt;
489             if (card->curRecPos == ringsize)
490                 card->curRecPos = 0;
491         }
492
493         if ((card->curRecPos < currptr) && (amtToRead > 0)) {
494             u32 amt = min (currptr - card->curRecPos, amtToRead);
495             nm256_readBuffer8 (card, card->recBuf, 1,
496                                  card->abuf2 + card->curRecPos, amt);
497             card->curRecPos = ((card->curRecPos + amt) % ringsize);
498         }
499         card->recBuf = NULL;
500         card->requestedRecAmt = 0;
501         DMAbuf_inputintr (card->dev_for_record);
502     }
503 }
504
505 /*
506  * Initialize the hardware. 
507  */
508 static void
509 nm256_initHw (struct nm256_info *card)
510 {
511     /* Reset everything. */
512     nm256_writePort8 (card, 2, 0x0, 0x11);
513     nm256_writePort16 (card, 2, 0x214, 0);
514
515     stopRecord (card);
516     stopPlay (card);
517 }
518
519 /* 
520  * Handle a potential interrupt for the device referred to by DEV_ID. 
521  *
522  * I don't like the cut-n-paste job here either between the two routines,
523  * but there are sufficient differences between the two interrupt handlers
524  * that parameterizing it isn't all that great either.  (Could use a macro,
525  * I suppose...yucky bleah.)
526  */
527
528 static irqreturn_t
529 nm256_interrupt (int irq, void *dev_id, struct pt_regs *dummy)
530 {
531     struct nm256_info *card = (struct nm256_info *)dev_id;
532     u16 status;
533     static int badintrcount;
534     int handled = 0;
535
536     if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
537         printk (KERN_ERR "NM256: Bad card pointer\n");
538         return IRQ_NONE;
539     }
540
541     status = nm256_readPort16 (card, 2, NM_INT_REG);
542
543     /* Not ours. */
544     if (status == 0) {
545         if (badintrcount++ > 1000) {
546             /*
547              * I'm not sure if the best thing is to stop the card from
548              * playing or just release the interrupt (after all, we're in
549              * a bad situation, so doing fancy stuff may not be such a good
550              * idea).
551              *
552              * I worry about the card engine continuing to play noise
553              * over and over, however--that could become a very
554              * obnoxious problem.  And we know that when this usually
555              * happens things are fairly safe, it just means the user's
556              * inserted a PCMCIA card and someone's spamming us with IRQ 9s.
557              */
558
559             handled = 1;
560             if (card->playing)
561                 stopPlay (card);
562             if (card->recording)
563                 stopRecord (card);
564             badintrcount = 0;
565         }
566         return IRQ_RETVAL(handled);
567     }
568
569     badintrcount = 0;
570
571     /* Rather boring; check for individual interrupts and process them. */
572
573     if (status & NM_PLAYBACK_INT) {
574         handled = 1;
575         status &= ~NM_PLAYBACK_INT;
576         NM_ACK_INT (card, NM_PLAYBACK_INT);
577
578         if (card->playing)
579             nm256_get_new_block (card);
580     }
581
582     if (status & NM_RECORD_INT) {
583         handled = 1;
584         status &= ~NM_RECORD_INT;
585         NM_ACK_INT (card, NM_RECORD_INT);
586
587         if (card->recording)
588             nm256_read_block (card);
589     }
590
591     if (status & NM_MISC_INT_1) {
592         u8 cbyte;
593
594         handled = 1;
595         status &= ~NM_MISC_INT_1;
596         printk (KERN_ERR "NM256: Got misc interrupt #1\n");
597         NM_ACK_INT (card, NM_MISC_INT_1);
598         nm256_writePort16 (card, 2, NM_INT_REG, 0x8000);
599         cbyte = nm256_readPort8 (card, 2, 0x400);
600         nm256_writePort8 (card, 2, 0x400, cbyte | 2);
601     }
602
603     if (status & NM_MISC_INT_2) {
604         u8 cbyte;
605
606         handled = 1;
607         status &= ~NM_MISC_INT_2;
608         printk (KERN_ERR "NM256: Got misc interrupt #2\n");
609         NM_ACK_INT (card, NM_MISC_INT_2);
610         cbyte = nm256_readPort8 (card, 2, 0x400);
611         nm256_writePort8 (card, 2, 0x400, cbyte & ~2);
612     }
613
614     /* Unknown interrupt. */
615     if (status) {
616         handled = 1;
617         printk (KERN_ERR "NM256: Fire in the hole! Unknown status 0x%x\n",
618                 status);
619         /* Pray. */
620         NM_ACK_INT (card, status);
621     }
622     return IRQ_RETVAL(handled);
623 }
624
625 /*
626  * Handle a potential interrupt for the device referred to by DEV_ID.
627  * This handler is for the 256ZX, and is very similar to the non-ZX
628  * routine.
629  */
630
631 static irqreturn_t
632 nm256_interrupt_zx (int irq, void *dev_id, struct pt_regs *dummy)
633 {
634     struct nm256_info *card = (struct nm256_info *)dev_id;
635     u32 status;
636     static int badintrcount;
637     int handled = 0;
638
639     if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
640         printk (KERN_ERR "NM256: Bad card pointer\n");
641         return IRQ_NONE;
642     }
643
644     status = nm256_readPort32 (card, 2, NM_INT_REG);
645
646     /* Not ours. */
647     if (status == 0) {
648         if (badintrcount++ > 1000) {
649             printk (KERN_ERR "NM256: Releasing interrupt, over 1000 invalid interrupts\n");
650             /*
651              * I'm not sure if the best thing is to stop the card from
652              * playing or just release the interrupt (after all, we're in
653              * a bad situation, so doing fancy stuff may not be such a good
654              * idea).
655              *
656              * I worry about the card engine continuing to play noise
657              * over and over, however--that could become a very
658              * obnoxious problem.  And we know that when this usually
659              * happens things are fairly safe, it just means the user's
660              * inserted a PCMCIA card and someone's spamming us with 
661              * IRQ 9s.
662              */
663
664             handled = 1;
665             if (card->playing)
666                 stopPlay (card);
667             if (card->recording)
668                 stopRecord (card);
669             badintrcount = 0;
670         }
671         return IRQ_RETVAL(handled);
672     }
673
674     badintrcount = 0;
675
676     /* Rather boring; check for individual interrupts and process them. */
677
678     if (status & NM2_PLAYBACK_INT) {
679         handled = 1;
680         status &= ~NM2_PLAYBACK_INT;
681         NM2_ACK_INT (card, NM2_PLAYBACK_INT);
682
683         if (card->playing)
684             nm256_get_new_block (card);
685     }
686
687     if (status & NM2_RECORD_INT) {
688         handled = 1;
689         status &= ~NM2_RECORD_INT;
690         NM2_ACK_INT (card, NM2_RECORD_INT);
691
692         if (card->recording)
693             nm256_read_block (card);
694     }
695
696     if (status & NM2_MISC_INT_1) {
697         u8 cbyte;
698
699         handled = 1;
700         status &= ~NM2_MISC_INT_1;
701         printk (KERN_ERR "NM256: Got misc interrupt #1\n");
702         NM2_ACK_INT (card, NM2_MISC_INT_1);
703         cbyte = nm256_readPort8 (card, 2, 0x400);
704         nm256_writePort8 (card, 2, 0x400, cbyte | 2);
705     }
706
707     if (status & NM2_MISC_INT_2) {
708         u8 cbyte;
709
710         handled = 1;
711         status &= ~NM2_MISC_INT_2;
712         printk (KERN_ERR "NM256: Got misc interrupt #2\n");
713         NM2_ACK_INT (card, NM2_MISC_INT_2);
714         cbyte = nm256_readPort8 (card, 2, 0x400);
715         nm256_writePort8 (card, 2, 0x400, cbyte & ~2);
716     }
717
718     /* Unknown interrupt. */
719     if (status) {
720         handled = 1;
721         printk (KERN_ERR "NM256: Fire in the hole! Unknown status 0x%x\n",
722                 status);
723         /* Pray. */
724         NM2_ACK_INT (card, status);
725     }
726     return IRQ_RETVAL(handled);
727 }
728
729 /* 
730  * Request our interrupt.
731  */
732 static int
733 nm256_grabInterrupt (struct nm256_info *card)
734 {
735     if (card->has_irq++ == 0) {
736         if (request_irq (card->irq, card->introutine, SA_SHIRQ,
737                          "NM256_audio", card) < 0) {
738             printk (KERN_ERR "NM256: can't obtain IRQ %d\n", card->irq);
739             return -1;
740         }
741     }
742     return 0;
743 }
744
745 /* 
746  * Release our interrupt. 
747  */
748 static int
749 nm256_releaseInterrupt (struct nm256_info *card)
750 {
751     if (card->has_irq <= 0) {
752         printk (KERN_ERR "nm256: too many calls to releaseInterrupt\n");
753         return -1;
754     }
755     card->has_irq--;
756     if (card->has_irq == 0) {
757         free_irq (card->irq, card);
758     }
759     return 0;
760 }
761
762 /*
763  * Waits for the mixer to become ready to be written; returns a zero value
764  * if it timed out.
765  */
766
767 static int
768 nm256_isReady (struct ac97_hwint *dev)
769 {
770     struct nm256_info *card = (struct nm256_info *)dev->driver_private;
771     int t2 = 10;
772     u32 testaddr;
773     u16 testb;
774     int done = 0;
775
776     if (card->magsig != NM_MAGIC_SIG) {
777         printk (KERN_ERR "NM256: Bad magic signature in isReady!\n");
778         return 0;
779     }
780
781     testaddr = card->mixer_status_offset;
782     testb = card->mixer_status_mask;
783
784     /* 
785      * Loop around waiting for the mixer to become ready. 
786      */
787     while (! done && t2-- > 0) {
788         if ((nm256_readPort16 (card, 2, testaddr) & testb) == 0)
789             done = 1;
790         else
791             udelay (100);
792     }
793     return done;
794 }
795
796 /*
797  * Return the contents of the AC97 mixer register REG.  Returns a positive
798  * value if successful, or a negative error code.
799  */
800 static int
801 nm256_readAC97Reg (struct ac97_hwint *dev, u8 reg)
802 {
803     struct nm256_info *card = (struct nm256_info *)dev->driver_private;
804
805     if (card->magsig != NM_MAGIC_SIG) {
806         printk (KERN_ERR "NM256: Bad magic signature in readAC97Reg!\n");
807         return -EINVAL;
808     }
809
810     if (reg < 128) {
811         int res;
812
813         nm256_isReady (dev);
814         res = nm256_readPort16 (card, 2, card->mixer + reg);
815         /* Magic delay.  Bleah yucky.  */
816         udelay (1000);
817         return res;
818     }
819     else
820         return -EINVAL;
821 }
822
823 /* 
824  * Writes VALUE to AC97 mixer register REG.  Returns 0 if successful, or
825  * a negative error code. 
826  */
827 static int
828 nm256_writeAC97Reg (struct ac97_hwint *dev, u8 reg, u16 value)
829 {
830     unsigned long flags;
831     int tries = 2;
832     int done = 0;
833     u32 base;
834
835     struct nm256_info *card = (struct nm256_info *)dev->driver_private;
836
837     if (card->magsig != NM_MAGIC_SIG) {
838         printk (KERN_ERR "NM256: Bad magic signature in writeAC97Reg!\n");
839         return -EINVAL;
840     }
841
842     base = card->mixer;
843
844     spin_lock_irqsave(&card->lock,flags);
845
846     nm256_isReady (dev);
847
848     /* Wait for the write to take, too. */
849     while ((tries-- > 0) && !done) {
850         nm256_writePort16 (card, 2, base + reg, value);
851         if (nm256_isReady (dev)) {
852             done = 1;
853             break;
854         }
855
856     }
857
858     spin_unlock_irqrestore(&card->lock,flags);
859     udelay (1000);
860
861     return ! done;
862 }
863
864 /* 
865  * Initial register values to be written to the AC97 mixer.
866  * While most of these are identical to the reset values, we do this
867  * so that we have most of the register contents cached--this avoids
868  * reading from the mixer directly (which seems to be problematic,
869  * probably due to ignorance).
870  */
871 struct initialValues 
872 {
873     unsigned short port;
874     unsigned short value;
875 };
876
877 static struct initialValues nm256_ac97_initial_values[] = 
878 {
879     { AC97_MASTER_VOL_STEREO, 0x8000 },
880     { AC97_HEADPHONE_VOL,     0x8000 },
881     { AC97_MASTER_VOL_MONO,   0x0000 },
882     { AC97_PCBEEP_VOL,        0x0000 },
883     { AC97_PHONE_VOL,         0x0008 },
884     { AC97_MIC_VOL,           0x8000 },
885     { AC97_LINEIN_VOL,        0x8808 },
886     { AC97_CD_VOL,            0x8808 },
887     { AC97_VIDEO_VOL,         0x8808 },
888     { AC97_AUX_VOL,           0x8808 },
889     { AC97_PCMOUT_VOL,        0x0808 },
890     { AC97_RECORD_SELECT,     0x0000 },
891     { AC97_RECORD_GAIN,       0x0B0B },
892     { AC97_GENERAL_PURPOSE,   0x0000 },
893     { 0xffff, 0xffff }
894 };
895
896 /* Initialize the AC97 into a known state.  */
897 static int
898 nm256_resetAC97 (struct ac97_hwint *dev)
899 {
900     struct nm256_info *card = (struct nm256_info *)dev->driver_private;
901     int x;
902
903     if (card->magsig != NM_MAGIC_SIG) {
904         printk (KERN_ERR "NM256: Bad magic signature in resetAC97!\n");
905         return -EINVAL;
906     }
907
908     /* Reset the mixer.  'Tis magic!  */
909     nm256_writePort8 (card, 2, 0x6c0, 1);
910 //  nm256_writePort8 (card, 2, 0x6cc, 0x87);    /* This crashes Dell latitudes */
911     nm256_writePort8 (card, 2, 0x6cc, 0x80);
912     nm256_writePort8 (card, 2, 0x6cc, 0x0);
913
914     if (! card->mixer_values_init) {
915         for (x = 0; nm256_ac97_initial_values[x].port != 0xffff; x++) {
916             ac97_put_register (dev,
917                                nm256_ac97_initial_values[x].port,
918                                nm256_ac97_initial_values[x].value);
919             card->mixer_values_init = 1;
920         }
921     }
922
923     return 0;
924 }
925
926 /*
927  * We don't do anything particularly special here; it just passes the
928  * mixer ioctl to the AC97 driver.
929  */
930 static int
931 nm256_default_mixer_ioctl (int dev, unsigned int cmd, caddr_t arg)
932 {
933     struct nm256_info *card = nm256_find_card_for_mixer (dev);
934     if (card != NULL)
935         return ac97_mixer_ioctl (&(card->mdev), cmd, arg);
936     else
937         return -ENODEV;
938 }
939
940 static struct mixer_operations nm256_mixer_operations = {
941         .owner  = THIS_MODULE,
942         .id     = "NeoMagic",
943         .name   = "NM256AC97Mixer",
944         .ioctl  = nm256_default_mixer_ioctl
945 };
946
947 /*
948  * Default settings for the OSS mixer.  These are set last, after the
949  * mixer is initialized.
950  *
951  * I "love" C sometimes.  Got braces?
952  */
953 static struct ac97_mixer_value_list mixer_defaults[] = {
954     { SOUND_MIXER_VOLUME,  { { 85, 85 } } },
955     { SOUND_MIXER_SPEAKER, { { 100 } } },
956     { SOUND_MIXER_PCM,     { { 65, 65 } } },
957     { SOUND_MIXER_CD,      { { 65, 65 } } },
958     { -1,                  {  { 0,  0 } } }
959 };
960
961
962 /* Installs the AC97 mixer into CARD.  */
963 static int __init
964 nm256_install_mixer (struct nm256_info *card)
965 {
966     int mixer;
967
968     card->mdev.reset_device = nm256_resetAC97;
969     card->mdev.read_reg = nm256_readAC97Reg;
970     card->mdev.write_reg = nm256_writeAC97Reg;
971     card->mdev.driver_private = (void *)card;
972
973     if (ac97_init (&(card->mdev)))
974         return -1;
975
976     mixer = sound_alloc_mixerdev();
977     if (num_mixers >= MAX_MIXER_DEV) {
978         printk ("NM256 mixer: Unable to alloc mixerdev\n");
979         return -1;
980     }
981
982     mixer_devs[mixer] = &nm256_mixer_operations;
983     card->mixer_oss_dev = mixer;
984
985     /* Some reasonable default values.  */
986     ac97_set_values (&(card->mdev), mixer_defaults);
987
988     printk(KERN_INFO "Initialized AC97 mixer\n");
989     return 0;
990 }
991
992 /* Perform a full reset on the hardware; this is invoked when an APM
993    resume event occurs.  */
994 static void
995 nm256_full_reset (struct nm256_info *card)
996 {
997     nm256_initHw (card);
998     ac97_reset (&(card->mdev));
999 }
1000
1001 /* 
1002  * See if the signature left by the NM256 BIOS is intact; if so, we use
1003  * the associated address as the end of our audio buffer in the video
1004  * RAM.
1005  */
1006
1007 static void __init
1008 nm256_peek_for_sig (struct nm256_info *card)
1009 {
1010     u32 port1offset 
1011         = card->port[0].physaddr + card->port[0].end_offset - 0x0400;
1012     /* The signature is located 1K below the end of video RAM.  */
1013     char *temp = ioremap_nocache (port1offset, 16);
1014     /* Default buffer end is 5120 bytes below the top of RAM.  */
1015     u32 default_value = card->port[0].end_offset - 0x1400;
1016     u32 sig;
1017
1018     /* Install the default value first, so we don't have to repeatedly
1019        do it if there is a problem.  */
1020     card->port[0].end_offset = default_value;
1021
1022     if (temp == NULL) {
1023         printk (KERN_ERR "NM256: Unable to scan for card signature in video RAM\n");
1024         return;
1025     }
1026     sig = readl (temp);
1027     if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1028         u32 pointer = readl (temp + 4);
1029
1030         /*
1031          * If it's obviously invalid, don't use it (the port already has a
1032          * suitable default value set).
1033          */
1034         if (pointer != 0xffffffff)
1035             card->port[0].end_offset = pointer;
1036
1037         printk (KERN_INFO "NM256: Found card signature in video RAM: 0x%x\n",
1038                 pointer);
1039     }
1040
1041     iounmap (temp);
1042 }
1043
1044 /* 
1045  * Install a driver for the PCI device referenced by PCIDEV.
1046  * VERSTR is a human-readable version string.
1047  */
1048
1049 static int __init
1050 nm256_install(struct pci_dev *pcidev, enum nm256rev rev, char *verstr)
1051 {
1052     struct nm256_info *card;
1053     struct pm_dev *pmdev;
1054     int x;
1055
1056     if (pci_enable_device(pcidev))
1057             return 0;
1058
1059     card = kmalloc (sizeof (struct nm256_info), GFP_KERNEL);
1060     if (card == NULL) {
1061         printk (KERN_ERR "NM256: out of memory!\n");
1062         return 0;
1063     }
1064
1065     card->magsig = NM_MAGIC_SIG;
1066     card->playing  = 0;
1067     card->recording = 0;
1068     card->rev = rev;
1069         spin_lock_init(&card->lock);
1070
1071     /* Init the memory port info.  */
1072     for (x = 0; x < 2; x++) {
1073         card->port[x].physaddr = pci_resource_start (pcidev, x);
1074         card->port[x].ptr = NULL;
1075         card->port[x].start_offset = 0;
1076         card->port[x].end_offset = 0;
1077     }
1078
1079     /* Port 2 is easy.  */
1080     card->port[1].start_offset = 0;
1081     card->port[1].end_offset = NM_PORT2_SIZE;
1082
1083     /* Yuck.  But we have to map in port 2 so we can check how much RAM the
1084        card has.  */
1085     if (nm256_remap_ports (card)) {
1086         kfree (card);
1087         return 0;
1088     }
1089
1090     /* 
1091      * The NM256 has two memory ports.  The first port is nothing
1092      * more than a chunk of video RAM, which is used as the I/O ring
1093      * buffer.  The second port has the actual juicy stuff (like the
1094      * mixer and the playback engine control registers).
1095      */
1096
1097     if (card->rev == REV_NM256AV) {
1098         /* Ok, try to see if this is a non-AC97 version of the hardware. */
1099         int pval = nm256_readPort16 (card, 2, NM_MIXER_PRESENCE);
1100         if ((pval & NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) {
1101             if (! force_load) {
1102                 printk (KERN_ERR "NM256: This doesn't look to me like the AC97-compatible version.\n");
1103                 printk (KERN_ERR "       You can force the driver to load by passing in the module\n");
1104                 printk (KERN_ERR "       parameter:\n");
1105                 printk (KERN_ERR "              force_load = 1\n");
1106                 printk (KERN_ERR "\n");
1107                 printk (KERN_ERR "       More likely, you should be using the appropriate SB-16 or\n");
1108                 printk (KERN_ERR "       CS4232 driver instead.  (If your BIOS has settings for\n");
1109                 printk (KERN_ERR "       IRQ and/or DMA for the sound card, this is *not* the correct\n");
1110                 printk (KERN_ERR "       driver to use.)\n");
1111                 nm256_release_ports (card);
1112                 kfree (card);
1113                 return 0;
1114             }
1115             else {
1116                 printk (KERN_INFO "NM256: Forcing driver load as per user request.\n");
1117             }
1118         }
1119         else {
1120          /*   printk (KERN_INFO "NM256: Congratulations. You're not running Eunice.\n")*/;
1121         }
1122         card->port[0].end_offset = 2560 * 1024;
1123         card->introutine = nm256_interrupt;
1124         card->mixer_status_offset = NM_MIXER_STATUS_OFFSET;
1125         card->mixer_status_mask = NM_MIXER_READY_MASK;
1126     } 
1127     else {
1128         /* Not sure if there is any relevant detect for the ZX or not.  */
1129         if (nm256_readPort8 (card, 2, 0xa0b) != 0)
1130             card->port[0].end_offset = 6144 * 1024;
1131         else
1132             card->port[0].end_offset = 4096 * 1024;
1133
1134         card->introutine = nm256_interrupt_zx;
1135         card->mixer_status_offset = NM2_MIXER_STATUS_OFFSET;
1136         card->mixer_status_mask = NM2_MIXER_READY_MASK;
1137     }
1138
1139     if (buffertop >= 98304 && buffertop < card->port[0].end_offset)
1140         card->port[0].end_offset = buffertop;
1141     else
1142         nm256_peek_for_sig (card);
1143
1144     card->port[0].start_offset = card->port[0].end_offset - 98304;
1145
1146     printk (KERN_INFO "NM256: Mapping port 1 from 0x%x - 0x%x\n",
1147             card->port[0].start_offset, card->port[0].end_offset);
1148
1149     if (nm256_remap_ports (card)) {
1150         kfree (card);
1151         return 0;
1152     }
1153
1154     /* See if we can get the interrupt. */
1155
1156     card->irq = pcidev->irq;
1157     card->has_irq = 0;
1158
1159     if (nm256_grabInterrupt (card) != 0) {
1160         nm256_release_ports (card);
1161         kfree (card);
1162         return 0;
1163     }
1164
1165     nm256_releaseInterrupt (card);
1166
1167     /*
1168      *  Init the board.
1169      */
1170
1171     card->playbackBufferSize = 16384;
1172     card->recordBufferSize = 16384;
1173
1174     card->coeffBuf = card->port[0].end_offset - NM_MAX_COEFFICIENT;
1175     card->abuf2 = card->coeffBuf - card->recordBufferSize;
1176     card->abuf1 = card->abuf2 - card->playbackBufferSize;
1177     card->allCoeffBuf = card->abuf2 - (NM_TOTAL_COEFF_COUNT * 4);
1178
1179     /* Fixed setting. */
1180     card->mixer = NM_MIXER_OFFSET;
1181     card->mixer_values_init = 0;
1182
1183     card->is_open_play = 0;
1184     card->is_open_record = 0;
1185
1186     card->coeffsCurrent = 0;
1187
1188     card->opencnt[0] = 0; card->opencnt[1] = 0;
1189
1190     /* Reasonable default settings, but largely unnecessary. */
1191     for (x = 0; x < 2; x++) {
1192         card->sinfo[x].bits = 8;
1193         card->sinfo[x].stereo = 0;
1194         card->sinfo[x].samplerate = 8000;
1195     }
1196
1197     nm256_initHw (card);
1198
1199     for (x = 0; x < 2; x++) {
1200         if ((card->dev[x] =
1201              sound_install_audiodrv(AUDIO_DRIVER_VERSION,
1202                                     "NM256", &nm256_audio_driver,
1203                                     sizeof(struct audio_driver),
1204                                     DMA_NODMA, AFMT_U8 | AFMT_S16_LE,
1205                                     NULL, -1, -1)) >= 0) {
1206             /* 1K minimum buffer size. */
1207             audio_devs[card->dev[x]]->min_fragment = 10;
1208             /* Maximum of 8K buffer size. */
1209             audio_devs[card->dev[x]]->max_fragment = 13;
1210         }
1211         else {
1212             printk(KERN_ERR "NM256: Too many PCM devices available\n");
1213             nm256_release_ports (card);
1214             kfree (card);
1215             return 0;
1216         }
1217     }
1218
1219     pci_set_drvdata(pcidev,card);
1220
1221     /* Insert the card in the list.  */
1222     card->next_card = nmcard_list;
1223     nmcard_list = card;
1224
1225     printk(KERN_INFO "Initialized NeoMagic %s audio in PCI native mode\n",
1226            verstr);
1227
1228     /* 
1229      * And our mixer.  (We should allow support for other mixers, maybe.)
1230      */
1231
1232     nm256_install_mixer (card);
1233
1234     pmdev = pm_register(PM_PCI_DEV, PM_PCI_ID(pcidev), handle_pm_event);
1235     if (pmdev)
1236         pmdev->data = card;
1237
1238     return 1;
1239 }
1240
1241
1242 /*
1243  * PM event handler, so the card is properly reinitialized after a power
1244  * event.
1245  */
1246 static int
1247 handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data)
1248 {
1249     struct nm256_info *crd = (struct nm256_info*) dev->data;
1250     if (crd) {
1251         switch (rqst) {
1252         case PM_SUSPEND:
1253             break;
1254         case PM_RESUME:
1255             {
1256                 int playing = crd->playing;
1257                 nm256_full_reset (crd);
1258                 /*
1259                  * A little ugly, but that's ok; pretend the
1260                  * block we were playing is done. 
1261                  */
1262                 if (playing)
1263                     DMAbuf_outputintr (crd->dev_for_play, 1);
1264             }
1265             break;
1266         }
1267     }
1268     return 0;
1269 }
1270
1271 static int __devinit
1272 nm256_probe(struct pci_dev *pcidev,const struct pci_device_id *pciid)
1273 {
1274     if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO)
1275         return nm256_install(pcidev, REV_NM256AV, "256AV");
1276     if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO)
1277         return nm256_install(pcidev, REV_NM256ZX, "256ZX");
1278     return -1; /* should not come here ... */
1279 }
1280
1281 static void __devinit
1282 nm256_remove(struct pci_dev *pcidev) {
1283     struct nm256_info *xcard = pci_get_drvdata(pcidev);
1284     struct nm256_info *card,*next_card = NULL;
1285
1286     for (card = nmcard_list; card != NULL; card = next_card) {
1287         next_card = card->next_card;
1288         if (card == xcard) {
1289             stopPlay (card);
1290             stopRecord (card);
1291             if (card->has_irq)
1292                 free_irq (card->irq, card);
1293             nm256_release_ports (card);
1294             sound_unload_mixerdev (card->mixer_oss_dev);
1295             sound_unload_audiodev (card->dev[0]);
1296             sound_unload_audiodev (card->dev[1]);
1297             kfree (card);
1298             break;
1299         }
1300     }
1301     if (nmcard_list == card)
1302         nmcard_list = next_card;
1303 }
1304
1305 /*
1306  * Open the device
1307  *
1308  * DEV  - device
1309  * MODE - mode to open device (logical OR of OPEN_READ and OPEN_WRITE)
1310  *
1311  * Called when opening the DMAbuf               (dmabuf.c:259)
1312  */
1313 static int
1314 nm256_audio_open(int dev, int mode)
1315 {
1316     struct nm256_info *card = nm256_find_card (dev);
1317     int w;
1318         
1319     if (card == NULL)
1320         return -ENODEV;
1321
1322     if (card->dev[0] == dev)
1323         w = 0;
1324     else if (card->dev[1] == dev)
1325         w = 1;
1326     else
1327         return -ENODEV;
1328
1329     if (card->opencnt[w] > 0)
1330         return -EBUSY;
1331
1332     /* No bits set? Huh? */
1333     if (! ((mode & OPEN_READ) || (mode & OPEN_WRITE)))
1334         return -EIO;
1335
1336     /*
1337      * If it's open for both read and write, and the card's currently
1338      * being read or written to, then do the opposite of what has
1339      * already been done.  Otherwise, don't specify any mode until the
1340      * user actually tries to do I/O.  (Some programs open the device
1341      * for both read and write, but only actually do reading or writing.)
1342      */
1343
1344     if ((mode & OPEN_WRITE) && (mode & OPEN_READ)) {
1345         if (card->is_open_play)
1346             mode = OPEN_WRITE;
1347         else if (card->is_open_record)
1348             mode = OPEN_READ;
1349         else mode = 0;
1350     }
1351         
1352     if (mode & OPEN_WRITE) {
1353         if (card->is_open_play == 0) {
1354             card->dev_for_play = dev;
1355             card->is_open_play = 1;
1356         }
1357         else
1358             return -EBUSY;
1359     }
1360
1361     if (mode & OPEN_READ) {
1362         if (card->is_open_record == 0) {
1363             card->dev_for_record = dev;
1364             card->is_open_record = 1;
1365         }
1366         else
1367             return -EBUSY;
1368     }
1369
1370     card->opencnt[w]++;
1371     return 0;
1372 }
1373
1374 /*
1375  * Close the device
1376  *
1377  * DEV  - device
1378  *
1379  * Called when closing the DMAbuf               (dmabuf.c:477)
1380  *      after halt_xfer
1381  */
1382 static void
1383 nm256_audio_close(int dev)
1384 {
1385     struct nm256_info *card = nm256_find_card (dev);
1386         
1387     if (card != NULL) {
1388         int w;
1389
1390         if (card->dev[0] == dev)
1391             w = 0;
1392         else if (card->dev[1] == dev)
1393             w = 1;
1394         else
1395             return;
1396
1397         card->opencnt[w]--;
1398         if (card->opencnt[w] <= 0) {
1399             card->opencnt[w] = 0;
1400
1401             if (card->dev_for_play == dev) {
1402                 stopPlay (card);
1403                 card->is_open_play = 0;
1404                 card->dev_for_play = -1;
1405             }
1406
1407             if (card->dev_for_record == dev) {
1408                 stopRecord (card);
1409                 card->is_open_record = 0;
1410                 card->dev_for_record = -1;
1411             }
1412         }
1413     }
1414 }
1415
1416 /* Standard ioctl handler. */
1417 static int
1418 nm256_audio_ioctl(int dev, unsigned int cmd, caddr_t arg)
1419 {
1420     int ret;
1421     u32 oldinfo;
1422     int w;
1423
1424     struct nm256_info *card = nm256_find_card (dev);
1425
1426     if (card == NULL)
1427         return -ENODEV;
1428
1429     if (dev == card->dev[0])
1430         w = 0;
1431     else
1432         w = 1;
1433
1434     /* 
1435      * The code here is messy.  There are probably better ways to do
1436      * it.  (It should be possible to handle it the same way the AC97 mixer 
1437      * is done.)
1438      */
1439     switch (cmd)
1440         {
1441         case SOUND_PCM_WRITE_RATE:
1442             if (get_user(ret, (int *) arg))
1443                 return -EFAULT;
1444
1445             if (ret != 0) {
1446                 oldinfo = card->sinfo[w].samplerate;
1447                 card->sinfo[w].samplerate = ret;
1448                 ret = nm256_setInfo(dev, card);
1449                 if (ret != 0)
1450                     card->sinfo[w].samplerate = oldinfo;
1451             }
1452             if (ret == 0)
1453                 ret = card->sinfo[w].samplerate;
1454             break;
1455
1456         case SOUND_PCM_READ_RATE:
1457             ret = card->sinfo[w].samplerate;
1458             break;
1459
1460         case SNDCTL_DSP_STEREO:
1461             if (get_user(ret, (int *) arg))
1462                 return -EFAULT;
1463
1464             card->sinfo[w].stereo = ret ? 1 : 0;
1465             ret = nm256_setInfo (dev, card);
1466             if (ret == 0)
1467                 ret = card->sinfo[w].stereo;
1468
1469             break;
1470
1471         case SOUND_PCM_WRITE_CHANNELS:
1472             if (get_user(ret, (int *) arg))
1473                 return -EFAULT;
1474
1475             if (ret < 1 || ret > 3)
1476                 ret = card->sinfo[w].stereo + 1;
1477             else {
1478                 card->sinfo[w].stereo = ret - 1;
1479                 ret = nm256_setInfo (dev, card);
1480                 if (ret == 0)
1481                     ret = card->sinfo[w].stereo + 1;
1482             }
1483             break;
1484
1485         case SOUND_PCM_READ_CHANNELS:
1486             ret = card->sinfo[w].stereo + 1;
1487             break;
1488
1489         case SNDCTL_DSP_SETFMT:
1490             if (get_user(ret, (int *) arg))
1491                 return -EFAULT;
1492
1493             if (ret != 0) {
1494                 oldinfo = card->sinfo[w].bits;
1495                 card->sinfo[w].bits = ret;
1496                 ret = nm256_setInfo (dev, card);
1497                 if (ret != 0)
1498                     card->sinfo[w].bits = oldinfo;
1499             }
1500             if (ret == 0)
1501                 ret = card->sinfo[w].bits;
1502             break;
1503
1504         case SOUND_PCM_READ_BITS:
1505             ret = card->sinfo[w].bits;
1506             break;
1507
1508         default:
1509             return -EINVAL;
1510         }
1511     return put_user(ret, (int *) arg);
1512 }
1513
1514 /*
1515  * Given the sound device DEV and an associated physical buffer PHYSBUF, 
1516  * return a pointer to the actual buffer in kernel space. 
1517  *
1518  * This routine should exist as part of the soundcore routines.
1519  */
1520
1521 static char *
1522 nm256_getDMAbuffer (int dev, unsigned long physbuf)
1523 {
1524     struct audio_operations *adev = audio_devs[dev];
1525     struct dma_buffparms *dmap = adev->dmap_out;
1526     char *dma_start =
1527         (char *)(physbuf - (unsigned long)dmap->raw_buf_phys 
1528                  + (unsigned long)dmap->raw_buf);
1529
1530     return dma_start;
1531 }
1532
1533
1534 /*
1535  * Output a block to sound device
1536  *
1537  * dev          - device number
1538  * buf          - physical address of buffer
1539  * total_count  - total byte count in buffer
1540  * intrflag     - set if this has been called from an interrupt 
1541  *                                (via DMAbuf_outputintr)
1542  * restart_dma  - set if engine needs to be re-initialised
1543  *
1544  * Called when:
1545  *  1. Starting output                                  (dmabuf.c:1327)
1546  *  2.                                                  (dmabuf.c:1504)
1547  *  3. A new buffer needs to be sent to the device      (dmabuf.c:1579)
1548  */
1549 static void
1550 nm256_audio_output_block(int dev, unsigned long physbuf,
1551                                        int total_count, int intrflag)
1552 {
1553     struct nm256_info *card = nm256_find_card (dev);
1554
1555     if (card != NULL) {
1556         char *dma_buf = nm256_getDMAbuffer (dev, physbuf);
1557         card->is_open_play = 1;
1558         card->dev_for_play = dev;
1559         nm256_write_block (card, dma_buf, total_count);
1560     }
1561 }
1562
1563 /* Ditto, but do recording instead.  */
1564 static void
1565 nm256_audio_start_input(int dev, unsigned long physbuf, int count,
1566                         int intrflag)
1567 {
1568     struct nm256_info *card = nm256_find_card (dev);
1569
1570     if (card != NULL) {
1571         char *dma_buf = nm256_getDMAbuffer (dev, physbuf);
1572         card->is_open_record = 1;
1573         card->dev_for_record = dev;
1574         nm256_startRecording (card, dma_buf, count);
1575     }
1576 }
1577
1578 /* 
1579  * Prepare for inputting samples to DEV. 
1580  * Each requested buffer will be BSIZE byes long, with a total of
1581  * BCOUNT buffers. 
1582  */
1583
1584 static int
1585 nm256_audio_prepare_for_input(int dev, int bsize, int bcount)
1586 {
1587     struct nm256_info *card = nm256_find_card (dev);
1588
1589     if (card == NULL) 
1590         return -ENODEV;
1591
1592     if (card->is_open_record && card->dev_for_record != dev)
1593         return -EBUSY;
1594
1595     audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
1596     return 0;
1597 }
1598
1599 /*
1600  * Prepare for outputting samples to `dev'
1601  *
1602  * Each buffer that will be passed will be `bsize' bytes long,
1603  * with a total of `bcount' buffers.
1604  *
1605  * Called when:
1606  *  1. A trigger enables audio output                   (dmabuf.c:978)
1607  *  2. We get a write buffer without dma_mode setup     (dmabuf.c:1152)
1608  *  3. We restart a transfer                            (dmabuf.c:1324)
1609  */
1610
1611 static int
1612 nm256_audio_prepare_for_output(int dev, int bsize, int bcount)
1613 {
1614     struct nm256_info *card = nm256_find_card (dev);
1615
1616     if (card == NULL)
1617         return -ENODEV;
1618
1619     if (card->is_open_play && card->dev_for_play != dev)
1620         return -EBUSY;
1621
1622     audio_devs[dev]->dmap_out->flags |= DMA_NODMA;
1623     return 0;
1624 }
1625
1626 /* Stop the current operations associated with DEV.  */
1627 static void
1628 nm256_audio_reset(int dev)
1629 {
1630     struct nm256_info *card = nm256_find_card (dev);
1631
1632     if (card != NULL) {
1633         if (card->dev_for_play == dev)
1634             stopPlay (card);
1635         if (card->dev_for_record == dev)
1636             stopRecord (card);
1637     }
1638 }
1639
1640 static int
1641 nm256_audio_local_qlen(int dev)
1642 {
1643     return 0;
1644 }
1645
1646 static struct audio_driver nm256_audio_driver =
1647 {
1648         .owner                  = THIS_MODULE,
1649         .open                   = nm256_audio_open,
1650         .close                  = nm256_audio_close,
1651         .output_block           = nm256_audio_output_block,
1652         .start_input            = nm256_audio_start_input,
1653         .ioctl                  = nm256_audio_ioctl,
1654         .prepare_for_input      = nm256_audio_prepare_for_input,
1655         .prepare_for_output     = nm256_audio_prepare_for_output,
1656         .halt_io                = nm256_audio_reset,
1657         .local_qlen             = nm256_audio_local_qlen,
1658 };
1659
1660 static struct pci_device_id nm256_pci_tbl[] = {
1661         {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO,
1662         PCI_ANY_ID, PCI_ANY_ID, 0, 0},
1663         {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO,
1664         PCI_ANY_ID, PCI_ANY_ID, 0, 0},
1665         {0,}
1666 };
1667 MODULE_DEVICE_TABLE(pci, nm256_pci_tbl);
1668 MODULE_LICENSE("GPL");
1669
1670
1671 struct pci_driver nm256_pci_driver = {
1672         .name           = "nm256_audio",
1673         .id_table       = nm256_pci_tbl,
1674         .probe          = nm256_probe,
1675         .remove         = nm256_remove,
1676 };
1677
1678 MODULE_PARM (usecache, "i");
1679 MODULE_PARM (buffertop, "i");
1680 MODULE_PARM (nm256_debug, "i");
1681 MODULE_PARM (force_load, "i");
1682
1683 static int __init do_init_nm256(void)
1684 {
1685     printk (KERN_INFO "NeoMagic 256AV/256ZX audio driver, version 1.1p\n");
1686     return pci_module_init(&nm256_pci_driver);
1687 }
1688
1689 static void __exit cleanup_nm256 (void)
1690 {
1691     pci_unregister_driver(&nm256_pci_driver);
1692     pm_unregister_all (&handle_pm_event);
1693 }
1694
1695 module_init(do_init_nm256);
1696 module_exit(cleanup_nm256);
1697
1698 /*
1699  * Local variables:
1700  *  c-basic-offset: 4
1701  * End:
1702  */