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