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