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