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