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