Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6] / sound / pci / nm256 / nm256.c
1 /* 
2  * Driver for NeoMagic 256AV and 256ZX chipsets.
3  * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
4  *
5  * Based on nm256_audio.c OSS driver in linux kernel.
6  * The original author of OSS nm256 driver wishes to remain anonymous,
7  * so I just put my acknoledgment to him/her here.
8  * The original author's web page is found at
9  *      http://www.uglx.org/sony.html
10  *
11  *
12  *   This program is free software; you can redistribute it and/or modify
13  *   it under the terms of the GNU General Public License as published by
14  *   the Free Software Foundation; either version 2 of the License, or
15  *   (at your option) any later version.
16  *
17  *   This program is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with this program; if not, write to the Free Software
24  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  */
26   
27 #include <sound/driver.h>
28 #include <asm/io.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/init.h>
32 #include <linux/pci.h>
33 #include <linux/slab.h>
34 #include <linux/moduleparam.h>
35 #include <sound/core.h>
36 #include <sound/info.h>
37 #include <sound/control.h>
38 #include <sound/pcm.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/initval.h>
41
42 #define CARD_NAME "NeoMagic 256AV/ZX"
43 #define DRIVER_NAME "NM256"
44
45 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
46 MODULE_DESCRIPTION("NeoMagic NM256AV/ZX");
47 MODULE_LICENSE("GPL");
48 MODULE_SUPPORTED_DEVICE("{{NeoMagic,NM256AV},"
49                 "{NeoMagic,NM256ZX}}");
50
51 /*
52  * some compile conditions.
53  */
54
55 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
56 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
57 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
58 static int playback_bufsize[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 16};
59 static int capture_bufsize[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 16};
60 static int force_ac97[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; /* disabled as default */
61 static int buffer_top[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; /* not specified */
62 static int use_cache[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; /* disabled */
63 static int vaio_hack[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; /* disabled */
64 static int reset_workaround[SNDRV_CARDS];
65
66 module_param_array(index, int, NULL, 0444);
67 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
68 module_param_array(id, charp, NULL, 0444);
69 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
70 module_param_array(enable, bool, NULL, 0444);
71 MODULE_PARM_DESC(enable, "Enable this soundcard.");
72 module_param_array(playback_bufsize, int, NULL, 0444);
73 MODULE_PARM_DESC(playback_bufsize, "DAC frame size in kB for " CARD_NAME " soundcard.");
74 module_param_array(capture_bufsize, int, NULL, 0444);
75 MODULE_PARM_DESC(capture_bufsize, "ADC frame size in kB for " CARD_NAME " soundcard.");
76 module_param_array(force_ac97, bool, NULL, 0444);
77 MODULE_PARM_DESC(force_ac97, "Force to use AC97 codec for " CARD_NAME " soundcard.");
78 module_param_array(buffer_top, int, NULL, 0444);
79 MODULE_PARM_DESC(buffer_top, "Set the top address of audio buffer for " CARD_NAME " soundcard.");
80 module_param_array(use_cache, bool, NULL, 0444);
81 MODULE_PARM_DESC(use_cache, "Enable the cache for coefficient table access.");
82 module_param_array(vaio_hack, bool, NULL, 0444);
83 MODULE_PARM_DESC(vaio_hack, "Enable workaround for Sony VAIO notebooks.");
84 module_param_array(reset_workaround, bool, NULL, 0444);
85 MODULE_PARM_DESC(reset_workaround, "Enable AC97 RESET workaround for some laptops.");
86
87 /*
88  * hw definitions
89  */
90
91 /* The BIOS signature. */
92 #define NM_SIGNATURE 0x4e4d0000
93 /* Signature mask. */
94 #define NM_SIG_MASK 0xffff0000
95
96 /* Size of the second memory area. */
97 #define NM_PORT2_SIZE 4096
98
99 /* The base offset of the mixer in the second memory area. */
100 #define NM_MIXER_OFFSET 0x600
101
102 /* The maximum size of a coefficient entry. */
103 #define NM_MAX_PLAYBACK_COEF_SIZE       0x5000
104 #define NM_MAX_RECORD_COEF_SIZE         0x1260
105
106 /* The interrupt register. */
107 #define NM_INT_REG 0xa04
108 /* And its bits. */
109 #define NM_PLAYBACK_INT 0x40
110 #define NM_RECORD_INT 0x100
111 #define NM_MISC_INT_1 0x4000
112 #define NM_MISC_INT_2 0x1
113 #define NM_ACK_INT(chip, X) snd_nm256_writew(chip, NM_INT_REG, (X) << 1)
114
115 /* The AV's "mixer ready" status bit and location. */
116 #define NM_MIXER_STATUS_OFFSET 0xa04
117 #define NM_MIXER_READY_MASK 0x0800
118 #define NM_MIXER_PRESENCE 0xa06
119 #define NM_PRESENCE_MASK 0x0050
120 #define NM_PRESENCE_VALUE 0x0040
121
122 /*
123  * For the ZX.  It uses the same interrupt register, but it holds 32
124  * bits instead of 16.
125  */
126 #define NM2_PLAYBACK_INT 0x10000
127 #define NM2_RECORD_INT 0x80000
128 #define NM2_MISC_INT_1 0x8
129 #define NM2_MISC_INT_2 0x2
130 #define NM2_ACK_INT(chip, X) snd_nm256_writel(chip, NM_INT_REG, (X))
131
132 /* The ZX's "mixer ready" status bit and location. */
133 #define NM2_MIXER_STATUS_OFFSET 0xa06
134 #define NM2_MIXER_READY_MASK 0x0800
135
136 /* The playback registers start from here. */
137 #define NM_PLAYBACK_REG_OFFSET 0x0
138 /* The record registers start from here. */
139 #define NM_RECORD_REG_OFFSET 0x200
140
141 /* The rate register is located 2 bytes from the start of the register area. */
142 #define NM_RATE_REG_OFFSET 2
143
144 /* Mono/stereo flag, number of bits on playback, and rate mask. */
145 #define NM_RATE_STEREO 1
146 #define NM_RATE_BITS_16 2
147 #define NM_RATE_MASK 0xf0
148
149 /* Playback enable register. */
150 #define NM_PLAYBACK_ENABLE_REG (NM_PLAYBACK_REG_OFFSET + 0x1)
151 #define NM_PLAYBACK_ENABLE_FLAG 1
152 #define NM_PLAYBACK_ONESHOT 2
153 #define NM_PLAYBACK_FREERUN 4
154
155 /* Mutes the audio output. */
156 #define NM_AUDIO_MUTE_REG (NM_PLAYBACK_REG_OFFSET + 0x18)
157 #define NM_AUDIO_MUTE_LEFT 0x8000
158 #define NM_AUDIO_MUTE_RIGHT 0x0080
159
160 /* Recording enable register. */
161 #define NM_RECORD_ENABLE_REG (NM_RECORD_REG_OFFSET + 0)
162 #define NM_RECORD_ENABLE_FLAG 1
163 #define NM_RECORD_FREERUN 2
164
165 /* coefficient buffer pointer */
166 #define NM_COEFF_START_OFFSET   0x1c
167 #define NM_COEFF_END_OFFSET     0x20
168
169 /* DMA buffer offsets */
170 #define NM_RBUFFER_START (NM_RECORD_REG_OFFSET + 0x4)
171 #define NM_RBUFFER_END   (NM_RECORD_REG_OFFSET + 0x10)
172 #define NM_RBUFFER_WMARK (NM_RECORD_REG_OFFSET + 0xc)
173 #define NM_RBUFFER_CURRP (NM_RECORD_REG_OFFSET + 0x8)
174
175 #define NM_PBUFFER_START (NM_PLAYBACK_REG_OFFSET + 0x4)
176 #define NM_PBUFFER_END   (NM_PLAYBACK_REG_OFFSET + 0x14)
177 #define NM_PBUFFER_WMARK (NM_PLAYBACK_REG_OFFSET + 0xc)
178 #define NM_PBUFFER_CURRP (NM_PLAYBACK_REG_OFFSET + 0x8)
179
180 /*
181  * type definitions
182  */
183
184 typedef struct snd_nm256 nm256_t;
185 typedef struct snd_nm256_stream nm256_stream_t;
186
187 struct snd_nm256_stream {
188
189         nm256_t *chip;
190         snd_pcm_substream_t *substream;
191         int running;
192         
193         u32 buf;        /* offset from chip->buffer */
194         int bufsize;    /* buffer size in bytes */
195         void __iomem *bufptr;           /* mapped pointer */
196         unsigned long bufptr_addr;      /* physical address of the mapped pointer */
197
198         int dma_size;           /* buffer size of the substream in bytes */
199         int period_size;        /* period size in bytes */
200         int periods;            /* # of periods */
201         int shift;              /* bit shifts */
202         int cur_period;         /* current period # */
203
204 };
205
206 struct snd_nm256 {
207         
208         snd_card_t *card;
209
210         void __iomem *cport;            /* control port */
211         struct resource *res_cport;     /* its resource */
212         unsigned long cport_addr;       /* physical address */
213
214         void __iomem *buffer;           /* buffer */
215         struct resource *res_buffer;    /* its resource */
216         unsigned long buffer_addr;      /* buffer phyiscal address */
217
218         u32 buffer_start;               /* start offset from pci resource 0 */
219         u32 buffer_end;                 /* end offset */
220         u32 buffer_size;                /* total buffer size */
221
222         u32 all_coeff_buf;              /* coefficient buffer */
223         u32 coeff_buf[2];               /* coefficient buffer for each stream */
224
225         unsigned int coeffs_current: 1; /* coeff. table is loaded? */
226         unsigned int use_cache: 1;      /* use one big coef. table */
227         unsigned int reset_workaround: 1; /* Workaround for some laptops to avoid freeze */
228
229         int mixer_base;                 /* register offset of ac97 mixer */
230         int mixer_status_offset;        /* offset of mixer status reg. */
231         int mixer_status_mask;          /* bit mask to test the mixer status */
232
233         int irq;
234         irqreturn_t (*interrupt)(int, void *, struct pt_regs *);
235         int badintrcount;               /* counter to check bogus interrupts */
236
237         nm256_stream_t streams[2];
238
239         ac97_t *ac97;
240
241         snd_pcm_t *pcm;
242
243         struct pci_dev *pci;
244
245         spinlock_t reg_lock;
246
247 };
248
249
250 /*
251  * include coefficient table
252  */
253 #include "nm256_coef.c"
254
255
256 /*
257  * PCI ids
258  */
259
260 #ifndef PCI_VENDOR_ID_NEOMAGIC
261 #define PCI_VENDOR_ID_NEOMEGIC 0x10c8
262 #endif
263 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO
264 #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
265 #endif
266 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO
267 #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
268 #endif
269 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO
270 #define PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO 0x8016
271 #endif
272
273
274 static struct pci_device_id snd_nm256_ids[] = {
275         {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
276         {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
277         {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
278         {0,},
279 };
280
281 MODULE_DEVICE_TABLE(pci, snd_nm256_ids);
282
283
284 /*
285  * lowlvel stuffs
286  */
287
288 static inline u8
289 snd_nm256_readb(nm256_t *chip, int offset)
290 {
291         return readb(chip->cport + offset);
292 }
293
294 static inline u16
295 snd_nm256_readw(nm256_t *chip, int offset)
296 {
297         return readw(chip->cport + offset);
298 }
299
300 static inline u32
301 snd_nm256_readl(nm256_t *chip, int offset)
302 {
303         return readl(chip->cport + offset);
304 }
305
306 static inline void
307 snd_nm256_writeb(nm256_t *chip, int offset, u8 val)
308 {
309         writeb(val, chip->cport + offset);
310 }
311
312 static inline void
313 snd_nm256_writew(nm256_t *chip, int offset, u16 val)
314 {
315         writew(val, chip->cport + offset);
316 }
317
318 static inline void
319 snd_nm256_writel(nm256_t *chip, int offset, u32 val)
320 {
321         writel(val, chip->cport + offset);
322 }
323
324 static inline void
325 snd_nm256_write_buffer(nm256_t *chip, void *src, int offset, int size)
326 {
327         offset -= chip->buffer_start;
328 #ifdef SNDRV_CONFIG_DEBUG
329         if (offset < 0 || offset >= chip->buffer_size) {
330                 snd_printk("write_buffer invalid offset = %d size = %d\n", offset, size);
331                 return;
332         }
333 #endif
334         memcpy_toio(chip->buffer + offset, src, size);
335 }
336
337 /*
338  * coefficient handlers -- what a magic!
339  */
340
341 static u16
342 snd_nm256_get_start_offset(int which)
343 {
344         u16 offset = 0;
345         while (which-- > 0)
346                 offset += coefficient_sizes[which];
347         return offset;
348 }
349
350 static void
351 snd_nm256_load_one_coefficient(nm256_t *chip, int stream, u32 port, int which)
352 {
353         u32 coeff_buf = chip->coeff_buf[stream];
354         u16 offset = snd_nm256_get_start_offset(which);
355         u16 size = coefficient_sizes[which];
356
357         snd_nm256_write_buffer(chip, coefficients + offset, coeff_buf, size);
358         snd_nm256_writel(chip, port, coeff_buf);
359         /* ???  Record seems to behave differently than playback.  */
360         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
361                 size--;
362         snd_nm256_writel(chip, port + 4, coeff_buf + size);
363 }
364
365 static void
366 snd_nm256_load_coefficient(nm256_t *chip, int stream, int number)
367 {
368         /* The enable register for the specified engine.  */
369         u32 poffset = (stream == SNDRV_PCM_STREAM_CAPTURE ? NM_RECORD_ENABLE_REG : NM_PLAYBACK_ENABLE_REG);
370         u32 addr = NM_COEFF_START_OFFSET;
371
372         addr += (stream == SNDRV_PCM_STREAM_CAPTURE ? NM_RECORD_REG_OFFSET : NM_PLAYBACK_REG_OFFSET);
373
374         if (snd_nm256_readb(chip, poffset) & 1) {
375                 snd_printd("NM256: Engine was enabled while loading coefficients!\n");
376                 return;
377         }
378
379         /* The recording engine uses coefficient values 8-15.  */
380         number &= 7;
381         if (stream == SNDRV_PCM_STREAM_CAPTURE)
382                 number += 8;
383
384         if (! chip->use_cache) {
385                 snd_nm256_load_one_coefficient(chip, stream, addr, number);
386                 return;
387         }
388         if (! chip->coeffs_current) {
389                 snd_nm256_write_buffer(chip, coefficients, chip->all_coeff_buf,
390                                        NM_TOTAL_COEFF_COUNT * 4);
391                 chip->coeffs_current = 1;
392         } else {
393                 u32 base = chip->all_coeff_buf;
394                 u32 offset = snd_nm256_get_start_offset(number);
395                 u32 end_offset = offset + coefficient_sizes[number];
396                 snd_nm256_writel(chip, addr, base + offset);
397                 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
398                         end_offset--;
399                 snd_nm256_writel(chip, addr + 4, base + end_offset);
400         }
401 }
402
403
404 /* The actual rates supported by the card. */
405 static unsigned int samplerates[8] = {
406         8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
407 };
408 static snd_pcm_hw_constraint_list_t constraints_rates = {
409         .count = ARRAY_SIZE(samplerates), 
410         .list = samplerates,
411         .mask = 0,
412 };
413
414 /*
415  * return the index of the target rate
416  */
417 static int
418 snd_nm256_fixed_rate(unsigned int rate)
419 {
420         unsigned int i;
421         for (i = 0; i < ARRAY_SIZE(samplerates); i++) {
422                 if (rate == samplerates[i])
423                         return i;
424         }
425         snd_BUG();
426         return 0;
427 }
428
429 /*
430  * set sample rate and format
431  */
432 static void
433 snd_nm256_set_format(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
434 {
435         snd_pcm_runtime_t *runtime = substream->runtime;
436         int rate_index = snd_nm256_fixed_rate(runtime->rate);
437         unsigned char ratebits = (rate_index << 4) & NM_RATE_MASK;
438
439         s->shift = 0;
440         if (snd_pcm_format_width(runtime->format) == 16) {
441                 ratebits |= NM_RATE_BITS_16;
442                 s->shift++;
443         }
444         if (runtime->channels > 1) {
445                 ratebits |= NM_RATE_STEREO;
446                 s->shift++;
447         }
448
449         runtime->rate = samplerates[rate_index];
450
451         switch (substream->stream) {
452         case SNDRV_PCM_STREAM_PLAYBACK:
453                 snd_nm256_load_coefficient(chip, 0, rate_index); /* 0 = playback */
454                 snd_nm256_writeb(chip,
455                                  NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET,
456                                  ratebits);
457                 break;
458         case SNDRV_PCM_STREAM_CAPTURE:
459                 snd_nm256_load_coefficient(chip, 1, rate_index); /* 1 = record */
460                 snd_nm256_writeb(chip,
461                                  NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET,
462                                  ratebits);
463                 break;
464         }
465 }
466
467 /*
468  * start / stop
469  */
470
471 /* update the watermark (current period) */
472 static void snd_nm256_pcm_mark(nm256_t *chip, nm256_stream_t *s, int reg)
473 {
474         s->cur_period++;
475         s->cur_period %= s->periods;
476         snd_nm256_writel(chip, reg, s->buf + s->cur_period * s->period_size);
477 }
478
479 #define snd_nm256_playback_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_PBUFFER_WMARK)
480 #define snd_nm256_capture_mark(chip, s)  snd_nm256_pcm_mark(chip, s, NM_RBUFFER_WMARK)
481
482 static void
483 snd_nm256_playback_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
484 {
485         /* program buffer pointers */
486         snd_nm256_writel(chip, NM_PBUFFER_START, s->buf);
487         snd_nm256_writel(chip, NM_PBUFFER_END, s->buf + s->dma_size - (1 << s->shift));
488         snd_nm256_writel(chip, NM_PBUFFER_CURRP, s->buf);
489         snd_nm256_playback_mark(chip, s);
490
491         /* Enable playback engine and interrupts. */
492         snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG,
493                          NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);
494         /* Enable both channels. */
495         snd_nm256_writew(chip, NM_AUDIO_MUTE_REG, 0x0);
496 }
497
498 static void
499 snd_nm256_capture_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
500 {
501         /* program buffer pointers */
502         snd_nm256_writel(chip, NM_RBUFFER_START, s->buf);
503         snd_nm256_writel(chip, NM_RBUFFER_END, s->buf + s->dma_size);
504         snd_nm256_writel(chip, NM_RBUFFER_CURRP, s->buf);
505         snd_nm256_capture_mark(chip, s);
506
507         /* Enable playback engine and interrupts. */
508         snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG,
509                          NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
510 }
511
512 /* Stop the play engine. */
513 static void
514 snd_nm256_playback_stop(nm256_t *chip)
515 {
516         /* Shut off sound from both channels. */
517         snd_nm256_writew(chip, NM_AUDIO_MUTE_REG,
518                          NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT);
519         /* Disable play engine. */
520         snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG, 0);
521 }
522
523 static void
524 snd_nm256_capture_stop(nm256_t *chip)
525 {
526         /* Disable recording engine. */
527         snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG, 0);
528 }
529
530 static int
531 snd_nm256_playback_trigger(snd_pcm_substream_t *substream, int cmd)
532 {
533         nm256_t *chip = snd_pcm_substream_chip(substream);
534         nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
535         int err = 0;
536
537         snd_assert(s != NULL, return -ENXIO);
538
539         spin_lock(&chip->reg_lock);
540         switch (cmd) {
541         case SNDRV_PCM_TRIGGER_START:
542         case SNDRV_PCM_TRIGGER_RESUME:
543                 if (! s->running) {
544                         snd_nm256_playback_start(chip, s, substream);
545                         s->running = 1;
546                 }
547                 break;
548         case SNDRV_PCM_TRIGGER_STOP:
549         case SNDRV_PCM_TRIGGER_SUSPEND:
550                 if (s->running) {
551                         snd_nm256_playback_stop(chip);
552                         s->running = 0;
553                 }
554                 break;
555         default:
556                 err = -EINVAL;
557                 break;
558         }
559         spin_unlock(&chip->reg_lock);
560         return err;
561 }
562
563 static int
564 snd_nm256_capture_trigger(snd_pcm_substream_t *substream, int cmd)
565 {
566         nm256_t *chip = snd_pcm_substream_chip(substream);
567         nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
568         int err = 0;
569
570         snd_assert(s != NULL, return -ENXIO);
571
572         spin_lock(&chip->reg_lock);
573         switch (cmd) {
574         case SNDRV_PCM_TRIGGER_START:
575         case SNDRV_PCM_TRIGGER_RESUME:
576                 if (! s->running) {
577                         snd_nm256_capture_start(chip, s, substream);
578                         s->running = 1;
579                 }
580                 break;
581         case SNDRV_PCM_TRIGGER_STOP:
582         case SNDRV_PCM_TRIGGER_SUSPEND:
583                 if (s->running) {
584                         snd_nm256_capture_stop(chip);
585                         s->running = 0;
586                 }
587                 break;
588         default:
589                 err = -EINVAL;
590                 break;
591         }
592         spin_unlock(&chip->reg_lock);
593         return err;
594 }
595
596
597 /*
598  * prepare playback/capture channel
599  */
600 static int snd_nm256_pcm_prepare(snd_pcm_substream_t *substream)
601 {
602         nm256_t *chip = snd_pcm_substream_chip(substream);
603         snd_pcm_runtime_t *runtime = substream->runtime;
604         nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
605
606         snd_assert(s, return -ENXIO);
607         s->dma_size = frames_to_bytes(runtime, substream->runtime->buffer_size);
608         s->period_size = frames_to_bytes(runtime, substream->runtime->period_size);
609         s->periods = substream->runtime->periods;
610         s->cur_period = 0;
611
612         spin_lock_irq(&chip->reg_lock);
613         s->running = 0;
614         snd_nm256_set_format(chip, s, substream);
615         spin_unlock_irq(&chip->reg_lock);
616
617         return 0;
618 }
619
620
621 /*
622  * get the current pointer
623  */
624 static snd_pcm_uframes_t
625 snd_nm256_playback_pointer(snd_pcm_substream_t * substream)
626 {
627         nm256_t *chip = snd_pcm_substream_chip(substream);
628         nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
629         unsigned long curp;
630
631         snd_assert(s, return 0);
632         curp = snd_nm256_readl(chip, NM_PBUFFER_CURRP) - (unsigned long)s->buf;
633         curp %= s->dma_size;
634         return bytes_to_frames(substream->runtime, curp);
635 }
636
637 static snd_pcm_uframes_t
638 snd_nm256_capture_pointer(snd_pcm_substream_t * substream)
639 {
640         nm256_t *chip = snd_pcm_substream_chip(substream);
641         nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
642         unsigned long curp;
643
644         snd_assert(s != NULL, return 0);
645         curp = snd_nm256_readl(chip, NM_RBUFFER_CURRP) - (unsigned long)s->buf;
646         curp %= s->dma_size;    
647         return bytes_to_frames(substream->runtime, curp);
648 }
649
650 /* Remapped I/O space can be accessible as pointer on i386 */
651 /* This might be changed in the future */
652 #ifndef __i386__
653 /*
654  * silence / copy for playback
655  */
656 static int
657 snd_nm256_playback_silence(snd_pcm_substream_t *substream,
658                            int channel, /* not used (interleaved data) */
659                            snd_pcm_uframes_t pos,
660                            snd_pcm_uframes_t count)
661 {
662         snd_pcm_runtime_t *runtime = substream->runtime;
663         nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
664         count = frames_to_bytes(runtime, count);
665         pos = frames_to_bytes(runtime, pos);
666         memset_io(s->bufptr + pos, 0, count);
667         return 0;
668 }
669
670 static int
671 snd_nm256_playback_copy(snd_pcm_substream_t *substream,
672                         int channel, /* not used (interleaved data) */
673                         snd_pcm_uframes_t pos,
674                         void __user *src,
675                         snd_pcm_uframes_t count)
676 {
677         snd_pcm_runtime_t *runtime = substream->runtime;
678         nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
679         count = frames_to_bytes(runtime, count);
680         pos = frames_to_bytes(runtime, pos);
681         if (copy_from_user_toio(s->bufptr + pos, src, count))
682                 return -EFAULT;
683         return 0;
684 }
685
686 /*
687  * copy to user
688  */
689 static int
690 snd_nm256_capture_copy(snd_pcm_substream_t *substream,
691                        int channel, /* not used (interleaved data) */
692                        snd_pcm_uframes_t pos,
693                        void __user *dst,
694                        snd_pcm_uframes_t count)
695 {
696         snd_pcm_runtime_t *runtime = substream->runtime;
697         nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
698         count = frames_to_bytes(runtime, count);
699         pos = frames_to_bytes(runtime, pos);
700         if (copy_to_user_fromio(dst, s->bufptr + pos, count))
701                 return -EFAULT;
702         return 0;
703 }
704
705 #endif /* !__i386__ */
706
707
708 /*
709  * update playback/capture watermarks
710  */
711
712 /* spinlock held! */
713 static void
714 snd_nm256_playback_update(nm256_t *chip)
715 {
716         nm256_stream_t *s;
717
718         s = &chip->streams[SNDRV_PCM_STREAM_PLAYBACK];
719         if (s->running && s->substream) {
720                 spin_unlock(&chip->reg_lock);
721                 snd_pcm_period_elapsed(s->substream);
722                 spin_lock(&chip->reg_lock);
723                 snd_nm256_playback_mark(chip, s);
724         }
725 }
726
727 /* spinlock held! */
728 static void
729 snd_nm256_capture_update(nm256_t *chip)
730 {
731         nm256_stream_t *s;
732
733         s = &chip->streams[SNDRV_PCM_STREAM_CAPTURE];
734         if (s->running && s->substream) {
735                 spin_unlock(&chip->reg_lock);
736                 snd_pcm_period_elapsed(s->substream);
737                 spin_lock(&chip->reg_lock);
738                 snd_nm256_capture_mark(chip, s);
739         }
740 }
741
742 /*
743  * hardware info
744  */
745 static snd_pcm_hardware_t snd_nm256_playback =
746 {
747         .info =                 SNDRV_PCM_INFO_MMAP_IOMEM |SNDRV_PCM_INFO_MMAP_VALID |
748                                 SNDRV_PCM_INFO_INTERLEAVED |
749                                 /*SNDRV_PCM_INFO_PAUSE |*/
750                                 SNDRV_PCM_INFO_RESUME,
751         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
752         .rates =                SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000,
753         .rate_min =             8000,
754         .rate_max =             48000,
755         .channels_min =         1,
756         .channels_max =         2,
757         .periods_min =          2,
758         .periods_max =          1024,
759         .buffer_bytes_max =     128 * 1024,
760         .period_bytes_min =     256,
761         .period_bytes_max =     128 * 1024,
762 };
763
764 static snd_pcm_hardware_t snd_nm256_capture =
765 {
766         .info =                 SNDRV_PCM_INFO_MMAP_IOMEM | SNDRV_PCM_INFO_MMAP_VALID |
767                                 SNDRV_PCM_INFO_INTERLEAVED |
768                                 /*SNDRV_PCM_INFO_PAUSE |*/
769                                 SNDRV_PCM_INFO_RESUME,
770         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
771         .rates =                SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000,
772         .rate_min =             8000,
773         .rate_max =             48000,
774         .channels_min =         1,
775         .channels_max =         2,
776         .periods_min =          2,
777         .periods_max =          1024,
778         .buffer_bytes_max =     128 * 1024,
779         .period_bytes_min =     256,
780         .period_bytes_max =     128 * 1024,
781 };
782
783
784 /* set dma transfer size */
785 static int snd_nm256_pcm_hw_params(snd_pcm_substream_t *substream, snd_pcm_hw_params_t *hw_params)
786 {
787         /* area and addr are already set and unchanged */
788         substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
789         return 0;
790 }
791
792 /*
793  * open
794  */
795 static void snd_nm256_setup_stream(nm256_t *chip, nm256_stream_t *s,
796                                    snd_pcm_substream_t *substream,
797                                    snd_pcm_hardware_t *hw_ptr)
798 {
799         snd_pcm_runtime_t *runtime = substream->runtime;
800
801         s->running = 0;
802         runtime->hw = *hw_ptr;
803         runtime->hw.buffer_bytes_max = s->bufsize;
804         runtime->hw.period_bytes_max = s->bufsize / 2;
805         runtime->dma_area = (void*) s->bufptr;
806         runtime->dma_addr = s->bufptr_addr;
807         runtime->dma_bytes = s->bufsize;
808         runtime->private_data = s;
809         s->substream = substream;
810
811         snd_pcm_set_sync(substream);
812         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
813                                    &constraints_rates);
814 }
815
816 static int
817 snd_nm256_playback_open(snd_pcm_substream_t *substream)
818 {
819         nm256_t *chip = snd_pcm_substream_chip(substream);
820
821         snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK],
822                                substream, &snd_nm256_playback);
823         return 0;
824 }
825
826 static int
827 snd_nm256_capture_open(snd_pcm_substream_t *substream)
828 {
829         nm256_t *chip = snd_pcm_substream_chip(substream);
830
831         snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_CAPTURE],
832                                substream, &snd_nm256_capture);
833         return 0;
834 }
835
836 /*
837  * close - we don't have to do special..
838  */
839 static int
840 snd_nm256_playback_close(snd_pcm_substream_t *substream)
841 {
842         return 0;
843 }
844
845
846 static int
847 snd_nm256_capture_close(snd_pcm_substream_t *substream)
848 {
849         return 0;
850 }
851
852 /*
853  * create a pcm instance
854  */
855 static snd_pcm_ops_t snd_nm256_playback_ops = {
856         .open =         snd_nm256_playback_open,
857         .close =        snd_nm256_playback_close,
858         .ioctl =        snd_pcm_lib_ioctl,
859         .hw_params =    snd_nm256_pcm_hw_params,
860         .prepare =      snd_nm256_pcm_prepare,
861         .trigger =      snd_nm256_playback_trigger,
862         .pointer =      snd_nm256_playback_pointer,
863 #ifndef __i386__
864         .copy =         snd_nm256_playback_copy,
865         .silence =      snd_nm256_playback_silence,
866 #endif
867         .mmap =         snd_pcm_lib_mmap_iomem,
868 };
869
870 static snd_pcm_ops_t snd_nm256_capture_ops = {
871         .open =         snd_nm256_capture_open,
872         .close =        snd_nm256_capture_close,
873         .ioctl =        snd_pcm_lib_ioctl,
874         .hw_params =    snd_nm256_pcm_hw_params,
875         .prepare =      snd_nm256_pcm_prepare,
876         .trigger =      snd_nm256_capture_trigger,
877         .pointer =      snd_nm256_capture_pointer,
878 #ifndef __i386__
879         .copy =         snd_nm256_capture_copy,
880 #endif
881         .mmap =         snd_pcm_lib_mmap_iomem,
882 };
883
884 static int __devinit
885 snd_nm256_pcm(nm256_t *chip, int device)
886 {
887         snd_pcm_t *pcm;
888         int i, err;
889
890         for (i = 0; i < 2; i++) {
891                 nm256_stream_t *s = &chip->streams[i];
892                 s->bufptr = chip->buffer + (s->buf - chip->buffer_start);
893                 s->bufptr_addr = chip->buffer_addr + (s->buf - chip->buffer_start);
894         }
895
896         err = snd_pcm_new(chip->card, chip->card->driver, device,
897                           1, 1, &pcm);
898         if (err < 0)
899                 return err;
900
901         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_nm256_playback_ops);
902         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_nm256_capture_ops);
903
904         pcm->private_data = chip;
905         pcm->info_flags = 0;
906         chip->pcm = pcm;
907
908         return 0;
909 }
910
911
912 /* 
913  * Initialize the hardware. 
914  */
915 static void
916 snd_nm256_init_chip(nm256_t *chip)
917 {
918         spin_lock_irq(&chip->reg_lock);
919         /* Reset everything. */
920         snd_nm256_writeb(chip, 0x0, 0x11);
921         snd_nm256_writew(chip, 0x214, 0);
922         /* stop sounds.. */
923         //snd_nm256_playback_stop(chip);
924         //snd_nm256_capture_stop(chip);
925         spin_unlock_irq(&chip->reg_lock);
926 }
927
928
929 static inline void
930 snd_nm256_intr_check(nm256_t *chip)
931 {
932         if (chip->badintrcount++ > 1000) {
933                 /*
934                  * I'm not sure if the best thing is to stop the card from
935                  * playing or just release the interrupt (after all, we're in
936                  * a bad situation, so doing fancy stuff may not be such a good
937                  * idea).
938                  *
939                  * I worry about the card engine continuing to play noise
940                  * over and over, however--that could become a very
941                  * obnoxious problem.  And we know that when this usually
942                  * happens things are fairly safe, it just means the user's
943                  * inserted a PCMCIA card and someone's spamming us with IRQ 9s.
944                  */
945                 if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
946                         snd_nm256_playback_stop(chip);
947                 if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
948                         snd_nm256_capture_stop(chip);
949                 chip->badintrcount = 0;
950         }
951 }
952
953 /* 
954  * Handle a potential interrupt for the device referred to by DEV_ID. 
955  *
956  * I don't like the cut-n-paste job here either between the two routines,
957  * but there are sufficient differences between the two interrupt handlers
958  * that parameterizing it isn't all that great either.  (Could use a macro,
959  * I suppose...yucky bleah.)
960  */
961
962 static irqreturn_t
963 snd_nm256_interrupt(int irq, void *dev_id, struct pt_regs *dummy)
964 {
965         nm256_t *chip = dev_id;
966         u16 status;
967         u8 cbyte;
968
969         status = snd_nm256_readw(chip, NM_INT_REG);
970
971         /* Not ours. */
972         if (status == 0) {
973                 snd_nm256_intr_check(chip);
974                 return IRQ_NONE;
975         }
976
977         chip->badintrcount = 0;
978
979         /* Rather boring; check for individual interrupts and process them. */
980
981         spin_lock(&chip->reg_lock);
982         if (status & NM_PLAYBACK_INT) {
983                 status &= ~NM_PLAYBACK_INT;
984                 NM_ACK_INT(chip, NM_PLAYBACK_INT);
985                 snd_nm256_playback_update(chip);
986         }
987
988         if (status & NM_RECORD_INT) {
989                 status &= ~NM_RECORD_INT;
990                 NM_ACK_INT(chip, NM_RECORD_INT);
991                 snd_nm256_capture_update(chip);
992         }
993
994         if (status & NM_MISC_INT_1) {
995                 status &= ~NM_MISC_INT_1;
996                 NM_ACK_INT(chip, NM_MISC_INT_1);
997                 snd_printd("NM256: Got misc interrupt #1\n");
998                 snd_nm256_writew(chip, NM_INT_REG, 0x8000);
999                 cbyte = snd_nm256_readb(chip, 0x400);
1000                 snd_nm256_writeb(chip, 0x400, cbyte | 2);
1001         }
1002
1003         if (status & NM_MISC_INT_2) {
1004                 status &= ~NM_MISC_INT_2;
1005                 NM_ACK_INT(chip, NM_MISC_INT_2);
1006                 snd_printd("NM256: Got misc interrupt #2\n");
1007                 cbyte = snd_nm256_readb(chip, 0x400);
1008                 snd_nm256_writeb(chip, 0x400, cbyte & ~2);
1009         }
1010
1011         /* Unknown interrupt. */
1012         if (status) {
1013                 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1014                            status);
1015                 /* Pray. */
1016                 NM_ACK_INT(chip, status);
1017         }
1018
1019         spin_unlock(&chip->reg_lock);
1020         return IRQ_HANDLED;
1021 }
1022
1023 /*
1024  * Handle a potential interrupt for the device referred to by DEV_ID.
1025  * This handler is for the 256ZX, and is very similar to the non-ZX
1026  * routine.
1027  */
1028
1029 static irqreturn_t
1030 snd_nm256_interrupt_zx(int irq, void *dev_id, struct pt_regs *dummy)
1031 {
1032         nm256_t *chip = dev_id;
1033         u32 status;
1034         u8 cbyte;
1035
1036         status = snd_nm256_readl(chip, NM_INT_REG);
1037
1038         /* Not ours. */
1039         if (status == 0) {
1040                 snd_nm256_intr_check(chip);
1041                 return IRQ_NONE;
1042         }
1043
1044         chip->badintrcount = 0;
1045
1046         /* Rather boring; check for individual interrupts and process them. */
1047
1048         spin_lock(&chip->reg_lock);
1049         if (status & NM2_PLAYBACK_INT) {
1050                 status &= ~NM2_PLAYBACK_INT;
1051                 NM2_ACK_INT(chip, NM2_PLAYBACK_INT);
1052                 snd_nm256_playback_update(chip);
1053         }
1054
1055         if (status & NM2_RECORD_INT) {
1056                 status &= ~NM2_RECORD_INT;
1057                 NM2_ACK_INT(chip, NM2_RECORD_INT);
1058                 snd_nm256_capture_update(chip);
1059         }
1060
1061         if (status & NM2_MISC_INT_1) {
1062                 status &= ~NM2_MISC_INT_1;
1063                 NM2_ACK_INT(chip, NM2_MISC_INT_1);
1064                 snd_printd("NM256: Got misc interrupt #1\n");
1065                 cbyte = snd_nm256_readb(chip, 0x400);
1066                 snd_nm256_writeb(chip, 0x400, cbyte | 2);
1067         }
1068
1069         if (status & NM2_MISC_INT_2) {
1070                 status &= ~NM2_MISC_INT_2;
1071                 NM2_ACK_INT(chip, NM2_MISC_INT_2);
1072                 snd_printd("NM256: Got misc interrupt #2\n");
1073                 cbyte = snd_nm256_readb(chip, 0x400);
1074                 snd_nm256_writeb(chip, 0x400, cbyte & ~2);
1075         }
1076
1077         /* Unknown interrupt. */
1078         if (status) {
1079                 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1080                            status);
1081                 /* Pray. */
1082                 NM2_ACK_INT(chip, status);
1083         }
1084
1085         spin_unlock(&chip->reg_lock);
1086         return IRQ_HANDLED;
1087 }
1088
1089 /*
1090  * AC97 interface
1091  */
1092
1093 /*
1094  * Waits for the mixer to become ready to be written; returns a zero value
1095  * if it timed out.
1096  */
1097 static int
1098 snd_nm256_ac97_ready(nm256_t *chip)
1099 {
1100         int timeout = 10;
1101         u32 testaddr;
1102         u16 testb;
1103
1104         testaddr = chip->mixer_status_offset;
1105         testb = chip->mixer_status_mask;
1106
1107         /* 
1108          * Loop around waiting for the mixer to become ready. 
1109          */
1110         while (timeout-- > 0) {
1111                 if ((snd_nm256_readw(chip, testaddr) & testb) == 0)
1112                         return 1;
1113                 udelay(100);
1114         }
1115         return 0;
1116 }
1117
1118 /*
1119  */
1120 static unsigned short
1121 snd_nm256_ac97_read(ac97_t *ac97, unsigned short reg)
1122 {
1123         nm256_t *chip = ac97->private_data;
1124         int res;
1125
1126         if (reg >= 128)
1127                 return 0;
1128
1129         if (! snd_nm256_ac97_ready(chip))
1130                 return 0;
1131         res = snd_nm256_readw(chip, chip->mixer_base + reg);
1132         /* Magic delay.  Bleah yucky.  */
1133         msleep(1);
1134         return res;
1135 }
1136
1137 /* 
1138  */
1139 static void
1140 snd_nm256_ac97_write(ac97_t *ac97,
1141                      unsigned short reg, unsigned short val)
1142 {
1143         nm256_t *chip = ac97->private_data;
1144         int tries = 2;
1145         u32 base;
1146
1147         base = chip->mixer_base;
1148
1149         snd_nm256_ac97_ready(chip);
1150
1151         /* Wait for the write to take, too. */
1152         while (tries-- > 0) {
1153                 snd_nm256_writew(chip, base + reg, val);
1154                 msleep(1);  /* a little delay here seems better.. */
1155                 if (snd_nm256_ac97_ready(chip))
1156                         return;
1157         }
1158         snd_printd("nm256: ac97 codec not ready..\n");
1159 }
1160
1161 /* initialize the ac97 into a known state */
1162 static void
1163 snd_nm256_ac97_reset(ac97_t *ac97)
1164 {
1165         nm256_t *chip = ac97->private_data;
1166
1167         /* Reset the mixer.  'Tis magic!  */
1168         snd_nm256_writeb(chip, 0x6c0, 1);
1169         if (! chip->reset_workaround) {
1170                 /* Dell latitude LS will lock up by this */
1171                 snd_nm256_writeb(chip, 0x6cc, 0x87);
1172         }
1173         snd_nm256_writeb(chip, 0x6cc, 0x80);
1174         snd_nm256_writeb(chip, 0x6cc, 0x0);
1175 }
1176
1177 /* create an ac97 mixer interface */
1178 static int __devinit
1179 snd_nm256_mixer(nm256_t *chip)
1180 {
1181         ac97_bus_t *pbus;
1182         ac97_template_t ac97;
1183         int i, err;
1184         static ac97_bus_ops_t ops = {
1185                 .reset = snd_nm256_ac97_reset,
1186                 .write = snd_nm256_ac97_write,
1187                 .read = snd_nm256_ac97_read,
1188         };
1189         /* looks like nm256 hangs up when unexpected registers are touched... */
1190         static int mixer_regs[] = {
1191                 AC97_MASTER, AC97_HEADPHONE, AC97_MASTER_MONO,
1192                 AC97_PC_BEEP, AC97_PHONE, AC97_MIC, AC97_LINE, AC97_CD,
1193                 AC97_VIDEO, AC97_AUX, AC97_PCM, AC97_REC_SEL,
1194                 AC97_REC_GAIN, AC97_GENERAL_PURPOSE, AC97_3D_CONTROL,
1195                 AC97_EXTENDED_ID,
1196                 AC97_VENDOR_ID1, AC97_VENDOR_ID2,
1197                 -1
1198         };
1199
1200         if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
1201                 return err;
1202
1203         memset(&ac97, 0, sizeof(ac97));
1204         ac97.scaps = AC97_SCAP_AUDIO; /* we support audio! */
1205         ac97.limited_regs = 1;
1206         for (i = 0; mixer_regs[i] >= 0; i++)
1207                 set_bit(mixer_regs[i], ac97.reg_accessed);
1208         ac97.private_data = chip;
1209         err = snd_ac97_mixer(pbus, &ac97, &chip->ac97);
1210         if (err < 0)
1211                 return err;
1212         if (! (chip->ac97->id & (0xf0000000))) {
1213                 /* looks like an invalid id */
1214                 sprintf(chip->card->mixername, "%s AC97", chip->card->driver);
1215         }
1216         return 0;
1217 }
1218
1219 /* 
1220  * See if the signature left by the NM256 BIOS is intact; if so, we use
1221  * the associated address as the end of our audio buffer in the video
1222  * RAM.
1223  */
1224
1225 static int __devinit
1226 snd_nm256_peek_for_sig(nm256_t *chip)
1227 {
1228         /* The signature is located 1K below the end of video RAM.  */
1229         void __iomem *temp;
1230         /* Default buffer end is 5120 bytes below the top of RAM.  */
1231         unsigned long pointer_found = chip->buffer_end - 0x1400;
1232         u32 sig;
1233
1234         temp = ioremap_nocache(chip->buffer_addr + chip->buffer_end - 0x400, 16);
1235         if (temp == NULL) {
1236                 snd_printk("Unable to scan for card signature in video RAM\n");
1237                 return -EBUSY;
1238         }
1239
1240         sig = readl(temp);
1241         if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1242                 u32 pointer = readl(temp + 4);
1243
1244                 /*
1245                  * If it's obviously invalid, don't use it
1246                  */
1247                 if (pointer == 0xffffffff ||
1248                     pointer < chip->buffer_size ||
1249                     pointer > chip->buffer_end) {
1250                         snd_printk("invalid signature found: 0x%x\n", pointer);
1251                         iounmap(temp);
1252                         return -ENODEV;
1253                 } else {
1254                         pointer_found = pointer;
1255                         printk(KERN_INFO "nm256: found card signature in video RAM: 0x%x\n", pointer);
1256                 }
1257         }
1258
1259         iounmap(temp);
1260         chip->buffer_end = pointer_found;
1261
1262         return 0;
1263 }
1264
1265 #ifdef CONFIG_PM
1266 /*
1267  * APM event handler, so the card is properly reinitialized after a power
1268  * event.
1269  */
1270 static int nm256_suspend(snd_card_t *card, pm_message_t state)
1271 {
1272         nm256_t *chip = card->pm_private_data;
1273
1274         snd_pcm_suspend_all(chip->pcm);
1275         snd_ac97_suspend(chip->ac97);
1276         chip->coeffs_current = 0;
1277         pci_disable_device(chip->pci);
1278         return 0;
1279 }
1280
1281 static int nm256_resume(snd_card_t *card)
1282 {
1283         nm256_t *chip = card->pm_private_data;
1284
1285         /* Perform a full reset on the hardware */
1286         pci_enable_device(chip->pci);
1287         snd_nm256_init_chip(chip);
1288
1289         /* restore ac97 */
1290         snd_ac97_resume(chip->ac97);
1291
1292         return 0;
1293 }
1294 #endif /* CONFIG_PM */
1295
1296 static int snd_nm256_free(nm256_t *chip)
1297 {
1298         if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
1299                 snd_nm256_playback_stop(chip);
1300         if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
1301                 snd_nm256_capture_stop(chip);
1302
1303         if (chip->irq >= 0)
1304                 synchronize_irq(chip->irq);
1305
1306         if (chip->cport)
1307                 iounmap(chip->cport);
1308         if (chip->buffer)
1309                 iounmap(chip->buffer);
1310         if (chip->res_cport) {
1311                 release_resource(chip->res_cport);
1312                 kfree_nocheck(chip->res_cport);
1313         }
1314         if (chip->res_buffer) {
1315                 release_resource(chip->res_buffer);
1316                 kfree_nocheck(chip->res_buffer);
1317         }
1318         if (chip->irq >= 0)
1319                 free_irq(chip->irq, (void*)chip);
1320
1321         pci_disable_device(chip->pci);
1322         kfree(chip);
1323         return 0;
1324 }
1325
1326 static int snd_nm256_dev_free(snd_device_t *device)
1327 {
1328         nm256_t *chip = device->device_data;
1329         return snd_nm256_free(chip);
1330 }
1331
1332 static int __devinit
1333 snd_nm256_create(snd_card_t *card, struct pci_dev *pci,
1334                  int play_bufsize, int capt_bufsize,
1335                  int force_load,
1336                  u32 buffertop,
1337                  int usecache,
1338                  nm256_t **chip_ret)
1339 {
1340         nm256_t *chip;
1341         int err, pval;
1342         static snd_device_ops_t ops = {
1343                 .dev_free =     snd_nm256_dev_free,
1344         };
1345         u32 addr;
1346
1347         *chip_ret = NULL;
1348
1349         if ((err = pci_enable_device(pci)) < 0)
1350                 return err;
1351
1352         chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
1353         if (chip == NULL) {
1354                 pci_disable_device(pci);
1355                 return -ENOMEM;
1356         }
1357
1358         chip->card = card;
1359         chip->pci = pci;
1360         chip->use_cache = usecache;
1361         spin_lock_init(&chip->reg_lock);
1362         chip->irq = -1;
1363
1364         chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize = play_bufsize;
1365         chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize = capt_bufsize;
1366
1367         /* 
1368          * The NM256 has two memory ports.  The first port is nothing
1369          * more than a chunk of video RAM, which is used as the I/O ring
1370          * buffer.  The second port has the actual juicy stuff (like the
1371          * mixer and the playback engine control registers).
1372          */
1373
1374         chip->buffer_addr = pci_resource_start(pci, 0);
1375         chip->cport_addr = pci_resource_start(pci, 1);
1376
1377         /* Init the memory port info.  */
1378         /* remap control port (#2) */
1379         chip->res_cport = request_mem_region(chip->cport_addr, NM_PORT2_SIZE,
1380                                              card->driver);
1381         if (chip->res_cport == NULL) {
1382                 snd_printk("memory region 0x%lx (size 0x%x) busy\n",
1383                            chip->cport_addr, NM_PORT2_SIZE);
1384                 err = -EBUSY;
1385                 goto __error;
1386         }
1387         chip->cport = ioremap_nocache(chip->cport_addr, NM_PORT2_SIZE);
1388         if (chip->cport == NULL) {
1389                 snd_printk("unable to map control port %lx\n", chip->cport_addr);
1390                 err = -ENOMEM;
1391                 goto __error;
1392         }
1393
1394         if (!strcmp(card->driver, "NM256AV")) {
1395                 /* Ok, try to see if this is a non-AC97 version of the hardware. */
1396                 pval = snd_nm256_readw(chip, NM_MIXER_PRESENCE);
1397                 if ((pval & NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) {
1398                         if (! force_load) {
1399                                 printk(KERN_ERR "nm256: no ac97 is found!\n");
1400                                 printk(KERN_ERR "  force the driver to load by passing in the module parameter\n");
1401                                 printk(KERN_ERR "    force_ac97=1\n");
1402                                 printk(KERN_ERR "  or try sb16 or cs423x drivers instead.\n");
1403                                 err = -ENXIO;
1404                                 goto __error;
1405                         }
1406                 }
1407                 chip->buffer_end = 2560 * 1024;
1408                 chip->interrupt = snd_nm256_interrupt;
1409                 chip->mixer_status_offset = NM_MIXER_STATUS_OFFSET;
1410                 chip->mixer_status_mask = NM_MIXER_READY_MASK;
1411         } else {
1412                 /* Not sure if there is any relevant detect for the ZX or not.  */
1413                 if (snd_nm256_readb(chip, 0xa0b) != 0)
1414                         chip->buffer_end = 6144 * 1024;
1415                 else
1416                         chip->buffer_end = 4096 * 1024;
1417
1418                 chip->interrupt = snd_nm256_interrupt_zx;
1419                 chip->mixer_status_offset = NM2_MIXER_STATUS_OFFSET;
1420                 chip->mixer_status_mask = NM2_MIXER_READY_MASK;
1421         }
1422         
1423         chip->buffer_size = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize + chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize;
1424         if (chip->use_cache)
1425                 chip->buffer_size += NM_TOTAL_COEFF_COUNT * 4;
1426         else
1427                 chip->buffer_size += NM_MAX_PLAYBACK_COEF_SIZE + NM_MAX_RECORD_COEF_SIZE;
1428
1429         if (buffertop >= chip->buffer_size && buffertop < chip->buffer_end)
1430                 chip->buffer_end = buffertop;
1431         else {
1432                 /* get buffer end pointer from signature */
1433                 if ((err = snd_nm256_peek_for_sig(chip)) < 0)
1434                         goto __error;
1435         }
1436
1437         chip->buffer_start = chip->buffer_end - chip->buffer_size;
1438         chip->buffer_addr += chip->buffer_start;
1439
1440         printk(KERN_INFO "nm256: Mapping port 1 from 0x%x - 0x%x\n",
1441                chip->buffer_start, chip->buffer_end);
1442
1443         chip->res_buffer = request_mem_region(chip->buffer_addr,
1444                                               chip->buffer_size,
1445                                               card->driver);
1446         if (chip->res_buffer == NULL) {
1447                 snd_printk("nm256: buffer 0x%lx (size 0x%x) busy\n",
1448                            chip->buffer_addr, chip->buffer_size);
1449                 err = -EBUSY;
1450                 goto __error;
1451         }
1452         chip->buffer = ioremap_nocache(chip->buffer_addr, chip->buffer_size);
1453         if (chip->buffer == NULL) {
1454                 err = -ENOMEM;
1455                 snd_printk("unable to map ring buffer at %lx\n", chip->buffer_addr);
1456                 goto __error;
1457         }
1458
1459         /* set offsets */
1460         addr = chip->buffer_start;
1461         chip->streams[SNDRV_PCM_STREAM_PLAYBACK].buf = addr;
1462         addr += chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize;
1463         chip->streams[SNDRV_PCM_STREAM_CAPTURE].buf = addr;
1464         addr += chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize;
1465         if (chip->use_cache) {
1466                 chip->all_coeff_buf = addr;
1467         } else {
1468                 chip->coeff_buf[SNDRV_PCM_STREAM_PLAYBACK] = addr;
1469                 addr += NM_MAX_PLAYBACK_COEF_SIZE;
1470                 chip->coeff_buf[SNDRV_PCM_STREAM_CAPTURE] = addr;
1471         }
1472
1473         /* acquire interrupt */
1474         if (request_irq(pci->irq, chip->interrupt, SA_INTERRUPT|SA_SHIRQ,
1475                         card->driver, (void*)chip)) {
1476                 err = -EBUSY;
1477                 snd_printk("unable to grab IRQ %d\n", pci->irq);
1478                 goto __error;
1479         }
1480         chip->irq = pci->irq;
1481
1482         /* Fixed setting. */
1483         chip->mixer_base = NM_MIXER_OFFSET;
1484
1485         chip->coeffs_current = 0;
1486
1487         snd_nm256_init_chip(chip);
1488
1489         // pci_set_master(pci); /* needed? */
1490         
1491         snd_card_set_pm_callback(card, nm256_suspend, nm256_resume, chip);
1492
1493         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1494                 goto __error;
1495
1496         snd_card_set_dev(card, &pci->dev);
1497
1498         *chip_ret = chip;
1499         return 0;
1500
1501 __error:
1502         snd_nm256_free(chip);
1503         return err;
1504 }
1505
1506
1507 struct nm256_quirk {
1508         unsigned short vendor;
1509         unsigned short device;
1510         int type;
1511 };
1512
1513 enum { NM_BLACKLISTED, NM_RESET_WORKAROUND };
1514
1515 static struct nm256_quirk nm256_quirks[] __devinitdata = {
1516         /* HP omnibook 4150 has cs4232 codec internally */
1517         { .vendor = 0x103c, .device = 0x0007, .type = NM_BLACKLISTED },
1518         /* Sony PCG-F305 */
1519         { .vendor = 0x104d, .device = 0x8041, .type = NM_RESET_WORKAROUND },
1520         /* Dell Latitude LS */
1521         { .vendor = 0x1028, .device = 0x0080, .type = NM_RESET_WORKAROUND },
1522         { } /* terminator */
1523 };
1524
1525
1526 static int __devinit snd_nm256_probe(struct pci_dev *pci,
1527                                      const struct pci_device_id *pci_id)
1528 {
1529         static int dev;
1530         snd_card_t *card;
1531         nm256_t *chip;
1532         int err;
1533         unsigned int xbuffer_top;
1534         struct nm256_quirk *q;
1535         u16 subsystem_vendor, subsystem_device;
1536
1537         if (dev >= SNDRV_CARDS)
1538                 return -ENODEV;
1539         if (!enable[dev]) {
1540                 dev++;
1541                 return -ENOENT;
1542         }
1543
1544         pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1545         pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1546
1547         for (q = nm256_quirks; q->vendor; q++) {
1548                 if (q->vendor == subsystem_vendor && q->device == subsystem_device) {
1549                         switch (q->type) {
1550                         case NM_BLACKLISTED:
1551                                 printk(KERN_INFO "nm256: The device is blacklisted.  Loading stopped\n");
1552                                 return -ENODEV;
1553                         case NM_RESET_WORKAROUND:
1554                                 reset_workaround[dev] = 1;
1555                                 break;
1556                         }
1557                 }
1558         }
1559
1560         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1561         if (card == NULL)
1562                 return -ENOMEM;
1563
1564         switch (pci->device) {
1565         case PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO:
1566                 strcpy(card->driver, "NM256AV");
1567                 break;
1568         case PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO:
1569                 strcpy(card->driver, "NM256ZX");
1570                 break;
1571         case PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO:
1572                 strcpy(card->driver, "NM256XL+");
1573                 break;
1574         default:
1575                 snd_printk("invalid device id 0x%x\n", pci->device);
1576                 snd_card_free(card);
1577                 return -EINVAL;
1578         }
1579
1580         if (vaio_hack[dev])
1581                 xbuffer_top = 0x25a800; /* this avoids conflicts with XFree86 server */
1582         else
1583                 xbuffer_top = buffer_top[dev];
1584
1585         if (playback_bufsize[dev] < 4)
1586                 playback_bufsize[dev] = 4;
1587         if (playback_bufsize[dev] > 128)
1588                 playback_bufsize[dev] = 128;
1589         if (capture_bufsize[dev] < 4)
1590                 capture_bufsize[dev] = 4;
1591         if (capture_bufsize[dev] > 128)
1592                 capture_bufsize[dev] = 128;
1593         if ((err = snd_nm256_create(card, pci,
1594                                     playback_bufsize[dev] * 1024, /* in bytes */
1595                                     capture_bufsize[dev] * 1024,  /* in bytes */
1596                                     force_ac97[dev],
1597                                     xbuffer_top,
1598                                     use_cache[dev],
1599                                     &chip)) < 0) {
1600                 snd_card_free(card);
1601                 return err;
1602         }
1603
1604         if (reset_workaround[dev]) {
1605                 snd_printdd(KERN_INFO "nm256: reset_workaround activated\n");
1606                 chip->reset_workaround = 1;
1607         }
1608
1609         if ((err = snd_nm256_pcm(chip, 0)) < 0 ||
1610             (err = snd_nm256_mixer(chip)) < 0) {
1611                 snd_card_free(card);
1612                 return err;
1613         }
1614
1615         sprintf(card->shortname, "NeoMagic %s", card->driver);
1616         sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %d",
1617                 card->shortname,
1618                 chip->buffer_addr, chip->cport_addr, chip->irq);
1619
1620         if ((err = snd_card_register(card)) < 0) {
1621                 snd_card_free(card);
1622                 return err;
1623         }
1624
1625         pci_set_drvdata(pci, card);
1626         dev++;
1627         return 0;
1628 }
1629
1630 static void __devexit snd_nm256_remove(struct pci_dev *pci)
1631 {
1632         snd_card_free(pci_get_drvdata(pci));
1633         pci_set_drvdata(pci, NULL);
1634 }
1635
1636
1637 static struct pci_driver driver = {
1638         .name = "NeoMagic 256",
1639         .id_table = snd_nm256_ids,
1640         .probe = snd_nm256_probe,
1641         .remove = __devexit_p(snd_nm256_remove),
1642         SND_PCI_PM_CALLBACKS
1643 };
1644
1645
1646 static int __init alsa_card_nm256_init(void)
1647 {
1648         return pci_register_driver(&driver);
1649 }
1650
1651 static void __exit alsa_card_nm256_exit(void)
1652 {
1653         pci_unregister_driver(&driver);
1654 }
1655
1656 module_init(alsa_card_nm256_init)
1657 module_exit(alsa_card_nm256_exit)