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