[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / sound / oss / nm256_audio.c
blobf9166e13519201f9118185e919489b1e2d1cbc66
1 /*
2 * Audio driver for the NeoMagic 256AV and 256ZX chipsets in native
3 * mode, with AC97 mixer support.
5 * Overall design and parts of this code stolen from vidc_*.c and
6 * skeleton.c.
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?
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.
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.
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"
34 int nm256_debug;
35 static int force_load;
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.
42 #define NM256_PLAY_WMARK_SIZE 512
44 static struct audio_driver nm256_audio_driver;
46 static int nm256_grabInterrupt (struct nm256_info *card);
47 static int nm256_releaseInterrupt (struct nm256_info *card);
48 static irqreturn_t nm256_interrupt (int irq, void *dev_id, struct pt_regs *dummy);
49 static irqreturn_t nm256_interrupt_zx (int irq, void *dev_id, struct pt_regs *dummy);
50 static int handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data);
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
57 /* List of cards. */
58 static struct nm256_info *nmcard_list;
60 /* Release the mapped-in memory for CARD. */
61 static void
62 nm256_release_ports (struct nm256_info *card)
64 int x;
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;
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.
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.
84 static int
85 nm256_remap_ports (struct nm256_info *card)
87 int x;
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;
96 card->port[x].ptr = ioremap_nocache (physaddr, size);
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;
105 return 0;
108 /* Locate the card in our list. */
109 static struct nm256_info *
110 nm256_find_card (int dev)
112 struct nm256_info *card;
114 for (card = nmcard_list; card != NULL; card = card->next_card)
115 if (card->dev[0] == dev || card->dev[1] == dev)
116 return card;
118 return NULL;
122 * Ditto, but find the card struct corresponding to the mixer device DEV
123 * instead.
125 static struct nm256_info *
126 nm256_find_card_for_mixer (int dev)
128 struct nm256_info *card;
130 for (card = nmcard_list; card != NULL; card = card->next_card)
131 if (card->mixer_oss_dev == dev)
132 return card;
134 return NULL;
137 static int usecache;
138 static int buffertop;
140 /* Check to see if we're using the bank of cached coefficients. */
142 nm256_cachedCoefficients (struct nm256_info *card)
144 return usecache;
147 /* The actual rates supported by the card. */
148 static int samplerates[9] = {
149 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 99999999
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.
160 static int
161 nm256_setInfo (int dev, struct nm256_info *card)
163 int x;
164 int w;
165 int targetrate;
167 if (card->dev[0] == dev)
168 w = 0;
169 else if (card->dev[1] == dev)
170 w = 1;
171 else
172 return -ENODEV;
174 targetrate = card->sinfo[w].samplerate;
176 if ((card->sinfo[w].bits != 8 && card->sinfo[w].bits != 16)
177 || targetrate < samplerates[0]
178 || targetrate > samplerates[7])
179 return -EINVAL;
181 for (x = 0; x < 8; x++)
182 if (targetrate < ((samplerates[x] + samplerates[x + 1]) / 2))
183 break;
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;
192 card->sinfo[w].samplerate = samplerates[x];
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);
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);
214 return 0;
216 else
217 return -EINVAL;
220 /* Start the play process going. */
221 static void
222 startPlay (struct nm256_info *card)
224 if (! card->playing) {
225 card->playing = 1;
226 if (nm256_grabInterrupt (card) == 0) {
227 nm256_setInfo (card->dev_for_play, card);
229 /* Enable playback engine and interrupts. */
230 nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG,
231 NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);
233 /* Enable both channels. */
234 nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG, 0x0);
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.
245 static void
246 nm256_startRecording (struct nm256_info *card, char *buffer, u32 amt)
248 u32 endpos;
249 int enableEngine = 0;
250 u32 ringsize = card->recordBufferSize;
251 unsigned long flags;
253 if (amt > (ringsize / 2)) {
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.
259 printk (KERN_ERR "NM256: Read request too large: %d\n", amt);
260 amt = ringsize / 2;
263 if (amt < 8) {
264 printk (KERN_ERR "NM256: Read request too small; %d\n", amt);
265 return;
268 spin_lock_irqsave(&card->lock,flags);
270 * If we're not currently recording, set up the start and end registers
271 * for the recording engine.
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);
282 nm256_writePort32 (card, 2, NM_RBUFFER_CURRP,
283 card->abuf2 + card->curRecPos);
284 enableEngine = 1;
286 else {
287 /* Not sure what else to do here. */
288 spin_unlock_irqrestore(&card->lock,flags);
289 return;
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.
298 endpos = card->abuf2 + ((card->curRecPos + amt) % ringsize);
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);
308 spin_unlock_irqrestore(&card->lock,flags);
311 /* Stop the play engine. */
312 static void
313 stopPlay (struct nm256_info *card)
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);
323 /* Reset the relevant state bits. */
324 card->playing = 0;
325 card->curPlayPos = 0;
329 /* Stop recording. */
330 static void
331 stopRecord (struct nm256_info *card)
333 /* Disable recording engine. */
334 nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG, 0);
336 if (card->recording) {
337 nm256_releaseInterrupt (card);
339 card->recording = 0;
340 card->curRecPos = 0;
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.)
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.
354 static void
355 nm256_write_block (struct nm256_info *card, char *buffer, u32 amt)
357 u32 ringsize = card->playbackBufferSize;
358 u32 endstop;
359 unsigned long flags;
361 if (amt > (ringsize / 2)) {
362 printk (KERN_ERR "NM256: Write request too large: %d\n", amt);
363 amt = (ringsize / 2);
366 if (amt < NM256_PLAY_WMARK_SIZE) {
367 printk (KERN_ERR "NM256: Write request too small: %d\n", amt);
368 return;
371 card->curPlayPos %= ringsize;
373 card->requested_amt = amt;
375 spin_lock_irqsave(&card->lock,flags);
377 if ((card->curPlayPos + amt) >= ringsize) {
378 u32 rem = ringsize - card->curPlayPos;
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);
387 else
388 nm256_writeBuffer8 (card, buffer, 1,
389 card->abuf1 + card->curPlayPos,
390 amt);
393 * Setup the start-n-stop-n-limit registers, and start that engine
394 * goin'.
396 * Normally we just let it wrap around to avoid the click-click
397 * action scene.
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);
405 if (card->sinfo[w].stereo)
406 sampsize *= 2;
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);
416 endstop = (card->curPlayPos + amt - NM256_PLAY_WMARK_SIZE) % ringsize;
417 nm256_writePort32 (card, 2, NM_PBUFFER_WMARK, card->abuf1 + endstop);
419 if (! card->playing)
420 startPlay (card);
422 spin_unlock_irqrestore(&card->lock,flags);
425 /* We just got a card playback interrupt; process it. */
426 static void
427 nm256_get_new_block (struct nm256_info *card)
429 /* Check to see how much got played so far. */
430 u32 amt = nm256_readPort32 (card, 2, NM_PBUFFER_CURRP) - card->abuf1;
432 if (amt >= card->playbackBufferSize) {
433 printk (KERN_ERR "NM256: Sound playback pointer invalid!\n");
434 amt = 0;
437 if (amt < card->curPlayPos)
438 amt = (card->playbackBufferSize - card->curPlayPos) + amt;
439 else
440 amt -= card->curPlayPos;
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);
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);
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.
461 static void
462 nm256_read_block (struct nm256_info *card)
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;
469 if (currptr >= card->recordBufferSize) {
470 printk (KERN_ERR "NM256: Sound buffer record pointer invalid!\n");
471 currptr = 0;
475 * This test is probably redundant; we shouldn't be here unless
476 * it's true.
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);
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;
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);
500 card->recBuf = NULL;
501 card->requestedRecAmt = 0;
502 DMAbuf_inputintr (card->dev_for_record);
507 * Initialize the hardware.
509 static void
510 nm256_initHw (struct nm256_info *card)
512 /* Reset everything. */
513 nm256_writePort8 (card, 2, 0x0, 0x11);
514 nm256_writePort16 (card, 2, 0x214, 0);
516 stopRecord (card);
517 stopPlay (card);
521 * Handle a potential interrupt for the device referred to by DEV_ID.
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.)
529 static irqreturn_t
530 nm256_interrupt (int irq, void *dev_id, struct pt_regs *dummy)
532 struct nm256_info *card = (struct nm256_info *)dev_id;
533 u16 status;
534 static int badintrcount;
535 int handled = 0;
537 if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
538 printk (KERN_ERR "NM256: Bad card pointer\n");
539 return IRQ_NONE;
542 status = nm256_readPort16 (card, 2, NM_INT_REG);
544 /* Not ours. */
545 if (status == 0) {
546 if (badintrcount++ > 1000) {
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).
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.
560 handled = 1;
561 if (card->playing)
562 stopPlay (card);
563 if (card->recording)
564 stopRecord (card);
565 badintrcount = 0;
567 return IRQ_RETVAL(handled);
570 badintrcount = 0;
572 /* Rather boring; check for individual interrupts and process them. */
574 if (status & NM_PLAYBACK_INT) {
575 handled = 1;
576 status &= ~NM_PLAYBACK_INT;
577 NM_ACK_INT (card, NM_PLAYBACK_INT);
579 if (card->playing)
580 nm256_get_new_block (card);
583 if (status & NM_RECORD_INT) {
584 handled = 1;
585 status &= ~NM_RECORD_INT;
586 NM_ACK_INT (card, NM_RECORD_INT);
588 if (card->recording)
589 nm256_read_block (card);
592 if (status & NM_MISC_INT_1) {
593 u8 cbyte;
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);
604 if (status & NM_MISC_INT_2) {
605 u8 cbyte;
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);
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);
623 return IRQ_RETVAL(handled);
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.
632 static irqreturn_t
633 nm256_interrupt_zx (int irq, void *dev_id, struct pt_regs *dummy)
635 struct nm256_info *card = (struct nm256_info *)dev_id;
636 u32 status;
637 static int badintrcount;
638 int handled = 0;
640 if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
641 printk (KERN_ERR "NM256: Bad card pointer\n");
642 return IRQ_NONE;
645 status = nm256_readPort32 (card, 2, NM_INT_REG);
647 /* Not ours. */
648 if (status == 0) {
649 if (badintrcount++ > 1000) {
650 printk (KERN_ERR "NM256: Releasing interrupt, over 1000 invalid interrupts\n");
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).
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.
665 handled = 1;
666 if (card->playing)
667 stopPlay (card);
668 if (card->recording)
669 stopRecord (card);
670 badintrcount = 0;
672 return IRQ_RETVAL(handled);
675 badintrcount = 0;
677 /* Rather boring; check for individual interrupts and process them. */
679 if (status & NM2_PLAYBACK_INT) {
680 handled = 1;
681 status &= ~NM2_PLAYBACK_INT;
682 NM2_ACK_INT (card, NM2_PLAYBACK_INT);
684 if (card->playing)
685 nm256_get_new_block (card);
688 if (status & NM2_RECORD_INT) {
689 handled = 1;
690 status &= ~NM2_RECORD_INT;
691 NM2_ACK_INT (card, NM2_RECORD_INT);
693 if (card->recording)
694 nm256_read_block (card);
697 if (status & NM2_MISC_INT_1) {
698 u8 cbyte;
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);
708 if (status & NM2_MISC_INT_2) {
709 u8 cbyte;
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);
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);
727 return IRQ_RETVAL(handled);
731 * Request our interrupt.
733 static int
734 nm256_grabInterrupt (struct nm256_info *card)
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;
743 return 0;
747 * Release our interrupt.
749 static int
750 nm256_releaseInterrupt (struct nm256_info *card)
752 if (card->has_irq <= 0) {
753 printk (KERN_ERR "nm256: too many calls to releaseInterrupt\n");
754 return -1;
756 card->has_irq--;
757 if (card->has_irq == 0) {
758 free_irq (card->irq, card);
760 return 0;
764 * Waits for the mixer to become ready to be written; returns a zero value
765 * if it timed out.
768 static int
769 nm256_isReady (struct ac97_hwint *dev)
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;
777 if (card->magsig != NM_MAGIC_SIG) {
778 printk (KERN_ERR "NM256: Bad magic signature in isReady!\n");
779 return 0;
782 testaddr = card->mixer_status_offset;
783 testb = card->mixer_status_mask;
786 * Loop around waiting for the mixer to become ready.
788 while (! done && t2-- > 0) {
789 if ((nm256_readPort16 (card, 2, testaddr) & testb) == 0)
790 done = 1;
791 else
792 udelay (100);
794 return done;
798 * Return the contents of the AC97 mixer register REG. Returns a positive
799 * value if successful, or a negative error code.
801 static int
802 nm256_readAC97Reg (struct ac97_hwint *dev, u8 reg)
804 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
806 if (card->magsig != NM_MAGIC_SIG) {
807 printk (KERN_ERR "NM256: Bad magic signature in readAC97Reg!\n");
808 return -EINVAL;
811 if (reg < 128) {
812 int res;
814 nm256_isReady (dev);
815 res = nm256_readPort16 (card, 2, card->mixer + reg);
816 /* Magic delay. Bleah yucky. */
817 udelay (1000);
818 return res;
820 else
821 return -EINVAL;
825 * Writes VALUE to AC97 mixer register REG. Returns 0 if successful, or
826 * a negative error code.
828 static int
829 nm256_writeAC97Reg (struct ac97_hwint *dev, u8 reg, u16 value)
831 unsigned long flags;
832 int tries = 2;
833 int done = 0;
834 u32 base;
836 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
838 if (card->magsig != NM_MAGIC_SIG) {
839 printk (KERN_ERR "NM256: Bad magic signature in writeAC97Reg!\n");
840 return -EINVAL;
843 base = card->mixer;
845 spin_lock_irqsave(&card->lock,flags);
847 nm256_isReady (dev);
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;
859 spin_unlock_irqrestore(&card->lock,flags);
860 udelay (1000);
862 return ! done;
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).
872 struct initialValues
874 unsigned short port;
875 unsigned short value;
878 static struct initialValues nm256_ac97_initial_values[] =
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 }
897 /* Initialize the AC97 into a known state. */
898 static int
899 nm256_resetAC97 (struct ac97_hwint *dev)
901 struct nm256_info *card = (struct nm256_info *)dev->driver_private;
902 int x;
904 if (card->magsig != NM_MAGIC_SIG) {
905 printk (KERN_ERR "NM256: Bad magic signature in resetAC97!\n");
906 return -EINVAL;
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);
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;
924 return 0;
928 * We don't do anything particularly special here; it just passes the
929 * mixer ioctl to the AC97 driver.
931 static int
932 nm256_default_mixer_ioctl (int dev, unsigned int cmd, void __user *arg)
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;
941 static struct mixer_operations nm256_mixer_operations = {
942 .owner = THIS_MODULE,
943 .id = "NeoMagic",
944 .name = "NM256AC97Mixer",
945 .ioctl = nm256_default_mixer_ioctl
949 * Default settings for the OSS mixer. These are set last, after the
950 * mixer is initialized.
952 * I "love" C sometimes. Got braces?
954 static 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 } } }
963 /* Installs the AC97 mixer into CARD. */
964 static int __init
965 nm256_install_mixer (struct nm256_info *card)
967 int mixer;
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;
974 if (ac97_init (&(card->mdev)))
975 return -1;
977 mixer = sound_alloc_mixerdev();
978 if (num_mixers >= MAX_MIXER_DEV) {
979 printk ("NM256 mixer: Unable to alloc mixerdev\n");
980 return -1;
983 mixer_devs[mixer] = &nm256_mixer_operations;
984 card->mixer_oss_dev = mixer;
986 /* Some reasonable default values. */
987 ac97_set_values (&(card->mdev), mixer_defaults);
989 printk(KERN_INFO "Initialized AC97 mixer\n");
990 return 0;
993 /* Perform a full reset on the hardware; this is invoked when an APM
994 resume event occurs. */
995 static void
996 nm256_full_reset (struct nm256_info *card)
998 nm256_initHw (card);
999 ac97_reset (&(card->mdev));
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.
1008 static void __init
1009 nm256_peek_for_sig (struct nm256_info *card)
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;
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;
1023 if (temp == NULL) {
1024 printk (KERN_ERR "NM256: Unable to scan for card signature in video RAM\n");
1025 return;
1027 sig = readl (temp);
1028 if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1029 u32 pointer = readl (temp + 4);
1032 * If it's obviously invalid, don't use it (the port already has a
1033 * suitable default value set).
1035 if (pointer != 0xffffffff)
1036 card->port[0].end_offset = pointer;
1038 printk (KERN_INFO "NM256: Found card signature in video RAM: 0x%x\n",
1039 pointer);
1042 iounmap (temp);
1046 * Install a driver for the PCI device referenced by PCIDEV.
1047 * VERSTR is a human-readable version string.
1050 static int __devinit
1051 nm256_install(struct pci_dev *pcidev, enum nm256rev rev, char *verstr)
1053 struct nm256_info *card;
1054 struct pm_dev *pmdev;
1055 int x;
1057 if (pci_enable_device(pcidev))
1058 return 0;
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;
1066 card->magsig = NM_MAGIC_SIG;
1067 card->playing = 0;
1068 card->recording = 0;
1069 card->rev = rev;
1070 spin_lock_init(&card->lock);
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;
1080 /* Port 2 is easy. */
1081 card->port[1].start_offset = 0;
1082 card->port[1].end_offset = NM_PORT2_SIZE;
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;
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).
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;
1116 else {
1117 printk (KERN_INFO "NM256: Forcing driver load as per user request.\n");
1120 else {
1121 /* printk (KERN_INFO "NM256: Congratulations. You're not running Eunice.\n")*/;
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;
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;
1135 card->introutine = nm256_interrupt_zx;
1136 card->mixer_status_offset = NM2_MIXER_STATUS_OFFSET;
1137 card->mixer_status_mask = NM2_MIXER_READY_MASK;
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);
1145 card->port[0].start_offset = card->port[0].end_offset - 98304;
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);
1150 if (nm256_remap_ports (card)) {
1151 kfree (card);
1152 return 0;
1155 /* See if we can get the interrupt. */
1157 card->irq = pcidev->irq;
1158 card->has_irq = 0;
1160 if (nm256_grabInterrupt (card) != 0) {
1161 nm256_release_ports (card);
1162 kfree (card);
1163 return 0;
1166 nm256_releaseInterrupt (card);
1169 * Init the board.
1172 card->playbackBufferSize = 16384;
1173 card->recordBufferSize = 16384;
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);
1180 /* Fixed setting. */
1181 card->mixer = NM_MIXER_OFFSET;
1182 card->mixer_values_init = 0;
1184 card->is_open_play = 0;
1185 card->is_open_record = 0;
1187 card->coeffsCurrent = 0;
1189 card->opencnt[0] = 0; card->opencnt[1] = 0;
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;
1198 nm256_initHw (card);
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;
1212 else {
1213 printk(KERN_ERR "NM256: Too many PCM devices available\n");
1214 nm256_release_ports (card);
1215 kfree (card);
1216 return 0;
1220 pci_set_drvdata(pcidev,card);
1222 /* Insert the card in the list. */
1223 card->next_card = nmcard_list;
1224 nmcard_list = card;
1226 printk(KERN_INFO "Initialized NeoMagic %s audio in PCI native mode\n",
1227 verstr);
1230 * And our mixer. (We should allow support for other mixers, maybe.)
1233 nm256_install_mixer (card);
1235 pmdev = pm_register(PM_PCI_DEV, PM_PCI_ID(pcidev), handle_pm_event);
1236 if (pmdev)
1237 pmdev->data = card;
1239 return 1;
1244 * PM event handler, so the card is properly reinitialized after a power
1245 * event.
1247 static int
1248 handle_pm_event (struct pm_dev *dev, pm_request_t rqst, void *data)
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:
1257 int playing = crd->playing;
1258 nm256_full_reset (crd);
1260 * A little ugly, but that's ok; pretend the
1261 * block we were playing is done.
1263 if (playing)
1264 DMAbuf_outputintr (crd->dev_for_play, 1);
1266 break;
1269 return 0;
1272 static int __devinit
1273 nm256_probe(struct pci_dev *pcidev,const struct pci_device_id *pciid)
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 ... */
1284 static void __devinit
1285 nm256_remove(struct pci_dev *pcidev) {
1286 struct nm256_info *xcard = pci_get_drvdata(pcidev);
1287 struct nm256_info *card,*next_card = NULL;
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;
1304 if (nmcard_list == card)
1305 nmcard_list = next_card;
1309 * Open the device
1311 * DEV - device
1312 * MODE - mode to open device (logical OR of OPEN_READ and OPEN_WRITE)
1314 * Called when opening the DMAbuf (dmabuf.c:259)
1316 static int
1317 nm256_audio_open(int dev, int mode)
1319 struct nm256_info *card = nm256_find_card (dev);
1320 int w;
1322 if (card == NULL)
1323 return -ENODEV;
1325 if (card->dev[0] == dev)
1326 w = 0;
1327 else if (card->dev[1] == dev)
1328 w = 1;
1329 else
1330 return -ENODEV;
1332 if (card->opencnt[w] > 0)
1333 return -EBUSY;
1335 /* No bits set? Huh? */
1336 if (! ((mode & OPEN_READ) || (mode & OPEN_WRITE)))
1337 return -EIO;
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.)
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;
1355 if (mode & OPEN_WRITE) {
1356 if (card->is_open_play == 0) {
1357 card->dev_for_play = dev;
1358 card->is_open_play = 1;
1360 else
1361 return -EBUSY;
1364 if (mode & OPEN_READ) {
1365 if (card->is_open_record == 0) {
1366 card->dev_for_record = dev;
1367 card->is_open_record = 1;
1369 else
1370 return -EBUSY;
1373 card->opencnt[w]++;
1374 return 0;
1378 * Close the device
1380 * DEV - device
1382 * Called when closing the DMAbuf (dmabuf.c:477)
1383 * after halt_xfer
1385 static void
1386 nm256_audio_close(int dev)
1388 struct nm256_info *card = nm256_find_card (dev);
1390 if (card != NULL) {
1391 int w;
1393 if (card->dev[0] == dev)
1394 w = 0;
1395 else if (card->dev[1] == dev)
1396 w = 1;
1397 else
1398 return;
1400 card->opencnt[w]--;
1401 if (card->opencnt[w] <= 0) {
1402 card->opencnt[w] = 0;
1404 if (card->dev_for_play == dev) {
1405 stopPlay (card);
1406 card->is_open_play = 0;
1407 card->dev_for_play = -1;
1410 if (card->dev_for_record == dev) {
1411 stopRecord (card);
1412 card->is_open_record = 0;
1413 card->dev_for_record = -1;
1419 /* Standard ioctl handler. */
1420 static int
1421 nm256_audio_ioctl(int dev, unsigned int cmd, void __user *arg)
1423 int ret;
1424 u32 oldinfo;
1425 int w;
1427 struct nm256_info *card = nm256_find_card (dev);
1429 if (card == NULL)
1430 return -ENODEV;
1432 if (dev == card->dev[0])
1433 w = 0;
1434 else
1435 w = 1;
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.)
1442 switch (cmd)
1444 case SOUND_PCM_WRITE_RATE:
1445 if (get_user(ret, (int __user *) arg))
1446 return -EFAULT;
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;
1455 if (ret == 0)
1456 ret = card->sinfo[w].samplerate;
1457 break;
1459 case SOUND_PCM_READ_RATE:
1460 ret = card->sinfo[w].samplerate;
1461 break;
1463 case SNDCTL_DSP_STEREO:
1464 if (get_user(ret, (int __user *) arg))
1465 return -EFAULT;
1467 card->sinfo[w].stereo = ret ? 1 : 0;
1468 ret = nm256_setInfo (dev, card);
1469 if (ret == 0)
1470 ret = card->sinfo[w].stereo;
1472 break;
1474 case SOUND_PCM_WRITE_CHANNELS:
1475 if (get_user(ret, (int __user *) arg))
1476 return -EFAULT;
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;
1486 break;
1488 case SOUND_PCM_READ_CHANNELS:
1489 ret = card->sinfo[w].stereo + 1;
1490 break;
1492 case SNDCTL_DSP_SETFMT:
1493 if (get_user(ret, (int __user *) arg))
1494 return -EFAULT;
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;
1503 if (ret == 0)
1504 ret = card->sinfo[w].bits;
1505 break;
1507 case SOUND_PCM_READ_BITS:
1508 ret = card->sinfo[w].bits;
1509 break;
1511 default:
1512 return -EINVAL;
1514 return put_user(ret, (int __user *) arg);
1518 * Given the sound device DEV and an associated physical buffer PHYSBUF,
1519 * return a pointer to the actual buffer in kernel space.
1521 * This routine should exist as part of the soundcore routines.
1524 static char *
1525 nm256_getDMAbuffer (int dev, unsigned long physbuf)
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);
1533 return dma_start;
1538 * Output a block to sound device
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
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)
1552 static void
1553 nm256_audio_output_block(int dev, unsigned long physbuf,
1554 int total_count, int intrflag)
1556 struct nm256_info *card = nm256_find_card (dev);
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);
1566 /* Ditto, but do recording instead. */
1567 static void
1568 nm256_audio_start_input(int dev, unsigned long physbuf, int count,
1569 int intrflag)
1571 struct nm256_info *card = nm256_find_card (dev);
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);
1582 * Prepare for inputting samples to DEV.
1583 * Each requested buffer will be BSIZE byes long, with a total of
1584 * BCOUNT buffers.
1587 static int
1588 nm256_audio_prepare_for_input(int dev, int bsize, int bcount)
1590 struct nm256_info *card = nm256_find_card (dev);
1592 if (card == NULL)
1593 return -ENODEV;
1595 if (card->is_open_record && card->dev_for_record != dev)
1596 return -EBUSY;
1598 audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
1599 return 0;
1603 * Prepare for outputting samples to `dev'
1605 * Each buffer that will be passed will be `bsize' bytes long,
1606 * with a total of `bcount' buffers.
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)
1614 static int
1615 nm256_audio_prepare_for_output(int dev, int bsize, int bcount)
1617 struct nm256_info *card = nm256_find_card (dev);
1619 if (card == NULL)
1620 return -ENODEV;
1622 if (card->is_open_play && card->dev_for_play != dev)
1623 return -EBUSY;
1625 audio_devs[dev]->dmap_out->flags |= DMA_NODMA;
1626 return 0;
1629 /* Stop the current operations associated with DEV. */
1630 static void
1631 nm256_audio_reset(int dev)
1633 struct nm256_info *card = nm256_find_card (dev);
1635 if (card != NULL) {
1636 if (card->dev_for_play == dev)
1637 stopPlay (card);
1638 if (card->dev_for_record == dev)
1639 stopRecord (card);
1643 static int
1644 nm256_audio_local_qlen(int dev)
1646 return 0;
1649 static struct audio_driver nm256_audio_driver =
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,
1663 static 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,}
1672 MODULE_DEVICE_TABLE(pci, nm256_pci_tbl);
1673 MODULE_LICENSE("GPL");
1676 static struct pci_driver nm256_pci_driver = {
1677 .name = "nm256_audio",
1678 .id_table = nm256_pci_tbl,
1679 .probe = nm256_probe,
1680 .remove = nm256_remove,
1683 module_param(usecache, bool, 0);
1684 module_param(buffertop, int, 0);
1685 module_param(nm256_debug, bool, 0644);
1686 module_param(force_load, bool, 0);
1688 static int __init do_init_nm256(void)
1690 printk (KERN_INFO "NeoMagic 256AV/256ZX audio driver, version 1.1p\n");
1691 return pci_module_init(&nm256_pci_driver);
1694 static void __exit cleanup_nm256 (void)
1696 pci_unregister_driver(&nm256_pci_driver);
1697 pm_unregister_all (&handle_pm_event);
1700 module_init(do_init_nm256);
1701 module_exit(cleanup_nm256);
1704 * Local variables:
1705 * c-basic-offset: 4
1706 * End: