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