2 * Driver for NeoMagic 256AV and 256ZX chipsets.
3 * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
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
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.
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.
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
27 #include <sound/driver.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>
42 #define CARD_NAME "NeoMagic 256AV/ZX"
43 #define DRIVER_NAME "NM256"
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}}");
53 * some compile conditions.
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 */
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);
99 /* The BIOS signature. */
100 #define NM_SIGNATURE 0x4e4d0000
101 /* Signature mask. */
102 #define NM_SIG_MASK 0xffff0000
104 /* Size of the second memory area. */
105 #define NM_PORT2_SIZE 4096
107 /* The base offset of the mixer in the second memory area. */
108 #define NM_MIXER_OFFSET 0x600
110 /* The maximum size of a coefficient entry. */
111 #define NM_MAX_PLAYBACK_COEF_SIZE 0x5000
112 #define NM_MAX_RECORD_COEF_SIZE 0x1260
114 /* The interrupt register. */
115 #define NM_INT_REG 0xa04
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)
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
131 * For the ZX. It uses the same interrupt register, but it holds 32
132 * bits instead of 16.
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))
140 /* The ZX's "mixer ready" status bit and location. */
141 #define NM2_MIXER_STATUS_OFFSET 0xa06
142 #define NM2_MIXER_READY_MASK 0x0800
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
149 /* The rate register is located 2 bytes from the start of the register area. */
150 #define NM_RATE_REG_OFFSET 2
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
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
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
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
173 /* coefficient buffer pointer */
174 #define NM_COEFF_START_OFFSET 0x1c
175 #define NM_COEFF_END_OFFSET 0x20
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)
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)
192 typedef struct snd_nm256 nm256_t;
193 typedef struct snd_nm256_stream nm256_stream_t;
194 #define chip_t nm256_t
196 struct snd_nm256_stream {
199 snd_pcm_substream_t *substream;
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 */
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 # */
219 unsigned long cport; /* control port */
220 struct resource *res_cport; /* its resource */
221 unsigned long cport_addr; /* physical address */
223 unsigned long buffer; /* buffer */
224 struct resource *res_buffer; /* its resource */
225 unsigned long buffer_addr; /* buffer phyiscal address */
227 u32 buffer_start; /* start offset from pci resource 0 */
228 u32 buffer_end; /* end offset */
229 u32 buffer_size; /* total buffer size */
231 u32 all_coeff_buf; /* coefficient buffer */
232 u32 coeff_buf[2]; /* coefficient buffer for each stream */
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 */
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 */
243 irqreturn_t (*interrupt)(int, void *, struct pt_regs *);
244 int badintrcount; /* counter to check bogus interrupts */
246 nm256_stream_t streams[2];
260 * include coefficient table
262 #include "nm256_coef.c"
269 #ifndef PCI_VENDOR_ID_NEOMAGIC
270 #define PCI_VENDOR_ID_NEOMEGIC 0x10c8
272 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO
273 #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
275 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO
276 #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
278 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO
279 #define PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO 0x8016
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},
290 MODULE_DEVICE_TABLE(pci, snd_nm256_ids);
298 snd_nm256_readb(nm256_t *chip, int offset)
300 return readb(chip->cport + offset);
304 snd_nm256_readw(nm256_t *chip, int offset)
306 return readw(chip->cport + offset);
310 snd_nm256_readl(nm256_t *chip, int offset)
312 return readl(chip->cport + offset);
316 snd_nm256_writeb(nm256_t *chip, int offset, u8 val)
318 writeb(val, chip->cport + offset);
322 snd_nm256_writew(nm256_t *chip, int offset, u16 val)
324 writew(val, chip->cport + offset);
328 snd_nm256_writel(nm256_t *chip, int offset, u32 val)
330 writel(val, chip->cport + offset);
334 snd_nm256_write_buffer(nm256_t *chip, void *src, int offset, int size)
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);
343 memcpy_toio((void *)chip->buffer + offset, src, size);
347 * coefficient handlers -- what a magic!
351 snd_nm256_get_start_offset(int which)
355 offset += coefficient_sizes[which];
360 snd_nm256_load_one_coefficient(nm256_t *chip, int stream, u32 port, int which)
362 u32 coeff_buf = chip->coeff_buf[stream];
363 u16 offset = snd_nm256_get_start_offset(which);
364 u16 size = coefficient_sizes[which];
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)
371 snd_nm256_writel(chip, port + 4, coeff_buf + size);
375 snd_nm256_load_coefficient(nm256_t *chip, int stream, int number)
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;
381 addr += (stream == SNDRV_PCM_STREAM_CAPTURE ? NM_RECORD_REG_OFFSET : NM_PLAYBACK_REG_OFFSET);
383 if (snd_nm256_readb(chip, poffset) & 1) {
384 snd_printd("NM256: Engine was enabled while loading coefficients!\n");
388 /* The recording engine uses coefficient values 8-15. */
390 if (stream == SNDRV_PCM_STREAM_CAPTURE)
393 if (! chip->use_cache) {
394 snd_nm256_load_one_coefficient(chip, stream, addr, number);
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;
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)
408 snd_nm256_writel(chip, addr + 4, base + end_offset);
413 /* The actual rates supported by the card. */
414 static unsigned int samplerates[8] = {
415 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
417 #define NUM_SAMPLERATES (sizeof(samplerates) / sizeof(samplerates[0]))
418 static snd_pcm_hw_constraint_list_t constraints_rates = {
419 .count = NUM_SAMPLERATES,
425 * return the index of the target rate
428 snd_nm256_fixed_rate(unsigned int rate)
431 for (i = 0; i < NUM_SAMPLERATES; i++) {
432 if (rate == samplerates[i])
440 * set sample rate and format
443 snd_nm256_set_format(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
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;
450 if (snd_pcm_format_width(runtime->format) == 16) {
451 ratebits |= NM_RATE_BITS_16;
454 if (runtime->channels > 1) {
455 ratebits |= NM_RATE_STEREO;
459 runtime->rate = samplerates[rate_index];
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,
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,
481 /* update the watermark (current period) */
482 static void snd_nm256_pcm_mark(nm256_t *chip, nm256_stream_t *s, int reg)
485 s->cur_period %= s->periods;
486 snd_nm256_writel(chip, reg, s->buf + s->cur_period * s->period_size);
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)
493 snd_nm256_playback_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
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);
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);
509 snd_nm256_capture_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
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);
517 /* Enable playback engine and interrupts. */
518 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG,
519 NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
522 /* Stop the play engine. */
524 snd_nm256_playback_stop(nm256_t *chip)
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);
534 snd_nm256_capture_stop(nm256_t *chip)
536 /* Disable recording engine. */
537 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG, 0);
541 snd_nm256_playback_trigger(snd_pcm_substream_t *substream, int cmd)
543 nm256_t *chip = snd_pcm_substream_chip(substream);
544 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
548 snd_assert(s != NULL, return -ENXIO);
550 spin_lock_irqsave(&chip->reg_lock, flags);
552 case SNDRV_PCM_TRIGGER_START:
553 case SNDRV_PCM_TRIGGER_RESUME:
555 snd_nm256_playback_start(chip, s, substream);
559 case SNDRV_PCM_TRIGGER_STOP:
560 case SNDRV_PCM_TRIGGER_SUSPEND:
562 snd_nm256_playback_stop(chip);
570 spin_unlock_irqrestore(&chip->reg_lock, flags);
575 snd_nm256_capture_trigger(snd_pcm_substream_t *substream, int cmd)
577 nm256_t *chip = snd_pcm_substream_chip(substream);
578 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
582 snd_assert(s != NULL, return -ENXIO);
584 spin_lock_irqsave(&chip->reg_lock, flags);
586 case SNDRV_PCM_TRIGGER_START:
587 case SNDRV_PCM_TRIGGER_RESUME:
589 snd_nm256_capture_start(chip, s, substream);
593 case SNDRV_PCM_TRIGGER_STOP:
594 case SNDRV_PCM_TRIGGER_SUSPEND:
596 snd_nm256_capture_stop(chip);
604 spin_unlock_irqrestore(&chip->reg_lock, flags);
610 * prepare playback/capture channel
612 static int snd_nm256_pcm_prepare(snd_pcm_substream_t *substream)
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;
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;
625 spin_lock_irqsave(&chip->reg_lock, flags);
627 snd_nm256_set_format(chip, s, substream);
628 spin_unlock_irqrestore(&chip->reg_lock, flags);
635 * get the current pointer
637 static snd_pcm_uframes_t
638 snd_nm256_playback_pointer(snd_pcm_substream_t * substream)
640 nm256_t *chip = snd_pcm_substream_chip(substream);
641 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
644 snd_assert(s, return 0);
645 curp = snd_nm256_readl(chip, NM_PBUFFER_CURRP) - (unsigned long)s->buf;
647 return bytes_to_frames(substream->runtime, curp);
650 static snd_pcm_uframes_t
651 snd_nm256_capture_pointer(snd_pcm_substream_t * substream)
653 nm256_t *chip = snd_pcm_substream_chip(substream);
654 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
657 snd_assert(s != NULL, return 0);
658 curp = snd_nm256_readl(chip, NM_RBUFFER_CURRP) - (unsigned long)s->buf;
660 return bytes_to_frames(substream->runtime, curp);
664 /* FIXME: I/O space is not accessible via pointers on all architectures */
667 * silence / copy for playback
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)
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);
684 snd_nm256_playback_copy(snd_pcm_substream_t *substream,
685 int channel, /* not used (interleaved data) */
686 snd_pcm_uframes_t pos,
688 snd_pcm_uframes_t count)
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))
703 snd_nm256_capture_copy(snd_pcm_substream_t *substream,
704 int channel, /* not used (interleaved data) */
705 snd_pcm_uframes_t pos,
707 snd_pcm_uframes_t count)
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))
718 #endif /* !__i386__ */
722 * update playback/capture watermarks
727 snd_nm256_playback_update(nm256_t *chip)
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);
742 snd_nm256_capture_update(nm256_t *chip)
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);
758 static snd_pcm_hardware_t snd_nm256_playback =
762 SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID|
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,
775 .buffer_bytes_max = 128 * 1024,
776 .period_bytes_min = 256,
777 .period_bytes_max = 128 * 1024,
780 static snd_pcm_hardware_t snd_nm256_capture =
784 SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID|
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,
797 .buffer_bytes_max = 128 * 1024,
798 .period_bytes_min = 256,
799 .period_bytes_max = 128 * 1024,
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)
806 /* area and addr are already set and unchanged */
807 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
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)
818 snd_pcm_runtime_t *runtime = substream->runtime;
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;
830 snd_pcm_set_sync(substream);
831 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
836 snd_nm256_playback_open(snd_pcm_substream_t *substream)
838 nm256_t *chip = snd_pcm_substream_chip(substream);
840 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK],
841 substream, &snd_nm256_playback);
846 snd_nm256_capture_open(snd_pcm_substream_t *substream)
848 nm256_t *chip = snd_pcm_substream_chip(substream);
850 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_CAPTURE],
851 substream, &snd_nm256_capture);
856 * close - we don't have to do special..
859 snd_nm256_playback_close(snd_pcm_substream_t *substream)
866 snd_nm256_capture_close(snd_pcm_substream_t *substream)
872 * create a pcm instance
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,
883 .copy = snd_nm256_playback_copy,
884 .silence = snd_nm256_playback_silence,
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,
897 .copy = snd_nm256_capture_copy,
902 snd_nm256_pcm(nm256_t *chip, int device)
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;
913 err = snd_pcm_new(chip->card, chip->card->driver, device,
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);
921 pcm->private_data = chip;
930 * Initialize the hardware.
933 snd_nm256_init_chip(nm256_t *chip)
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);
942 //snd_nm256_playback_stop(chip);
943 //snd_nm256_capture_stop(chip);
944 spin_unlock_irqrestore(&chip->reg_lock, flags);
949 snd_nm256_intr_check(nm256_t *chip)
951 if (chip->badintrcount++ > 1000) {
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
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.
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;
973 * Handle a potential interrupt for the device referred to by DEV_ID.
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.)
982 snd_nm256_interrupt(int irq, void *dev_id, struct pt_regs *dummy)
984 nm256_t *chip = snd_magic_cast(nm256_t, dev_id, return IRQ_NONE);
988 status = snd_nm256_readw(chip, NM_INT_REG);
992 snd_nm256_intr_check(chip);
996 chip->badintrcount = 0;
998 /* Rather boring; check for individual interrupts and process them. */
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);
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);
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);
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);
1030 /* Unknown interrupt. */
1032 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1035 NM_ACK_INT(chip, status);
1038 spin_unlock(&chip->reg_lock);
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
1049 snd_nm256_interrupt_zx(int irq, void *dev_id, struct pt_regs *dummy)
1051 nm256_t *chip = snd_magic_cast(nm256_t, dev_id, return IRQ_NONE);
1055 status = snd_nm256_readl(chip, NM_INT_REG);
1059 snd_nm256_intr_check(chip);
1063 chip->badintrcount = 0;
1065 /* Rather boring; check for individual interrupts and process them. */
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);
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);
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);
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);
1096 /* Unknown interrupt. */
1098 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1101 NM2_ACK_INT(chip, status);
1104 spin_unlock(&chip->reg_lock);
1113 * Waits for the mixer to become ready to be written; returns a zero value
1117 snd_nm256_ac97_ready(nm256_t *chip)
1123 testaddr = chip->mixer_status_offset;
1124 testb = chip->mixer_status_mask;
1127 * Loop around waiting for the mixer to become ready.
1129 while (timeout-- > 0) {
1130 if ((snd_nm256_readw(chip, testaddr) & testb) == 0)
1139 static unsigned short
1140 snd_nm256_ac97_read(ac97_t *ac97, unsigned short reg)
1142 nm256_t *chip = snd_magic_cast(nm256_t, ac97->private_data, return -ENXIO);
1148 if (! snd_nm256_ac97_ready(chip))
1150 res = snd_nm256_readw(chip, chip->mixer_base + reg);
1151 /* Magic delay. Bleah yucky. */
1159 snd_nm256_ac97_write(ac97_t *ac97,
1160 unsigned short reg, unsigned short val)
1162 nm256_t *chip = snd_magic_cast(nm256_t, ac97->private_data, return);
1166 base = chip->mixer_base;
1168 snd_nm256_ac97_ready(chip);
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))
1177 snd_printd("nm256: ac97 codec not ready..\n");
1180 /* initialize the ac97 into a known state */
1182 snd_nm256_ac97_reset(ac97_t *ac97)
1184 nm256_t *chip = snd_magic_cast(nm256_t, ac97->private_data, return);
1185 unsigned long flags;
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 on this */
1192 snd_nm256_writeb(chip, 0x6cc, 0x87);
1194 snd_nm256_writeb(chip, 0x6cc, 0x80);
1195 snd_nm256_writeb(chip, 0x6cc, 0x0);
1196 spin_unlock_irqrestore(&chip->reg_lock, flags);
1199 /* create an ac97 mixer interface */
1200 static int __devinit
1201 snd_nm256_mixer(nm256_t *chip)
1203 ac97_bus_t bus, *pbus;
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,
1213 AC97_VENDOR_ID1, AC97_VENDOR_ID2,
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)
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);
1233 if (! (chip->ac97->id & (0xf0000000))) {
1234 /* looks like an invalid id */
1235 sprintf(chip->card->mixername, "%s AC97", chip->card->driver);
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
1246 static int __devinit
1247 snd_nm256_peek_for_sig(nm256_t *chip)
1249 /* The signature is located 1K below the end of video RAM. */
1251 /* Default buffer end is 5120 bytes below the top of RAM. */
1252 unsigned long pointer_found = chip->buffer_end - 0x1400;
1255 temp = (unsigned long) ioremap_nocache(chip->buffer_addr + chip->buffer_end - 0x400, 16);
1257 snd_printk("Unable to scan for card signature in video RAM\n");
1262 if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1263 u32 pointer = readl(temp + 4);
1266 * If it's obviously invalid, don't use it
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);
1275 pointer_found = pointer;
1276 printk(KERN_INFO "nm256: found card signature in video RAM: 0x%x\n", pointer);
1280 iounmap((void *)temp);
1281 chip->buffer_end = pointer_found;
1288 * APM event handler, so the card is properly reinitialized after a power
1291 static int nm256_suspend(snd_card_t *card, unsigned int state)
1293 nm256_t *chip = snd_magic_cast(nm256_t, card->pm_private_data, return -EINVAL);
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);
1302 static int nm256_resume(snd_card_t *card, unsigned int state)
1304 nm256_t *chip = snd_magic_cast(nm256_t, card->pm_private_data, return -EINVAL);
1306 /* Perform a full reset on the hardware */
1307 pci_enable_device(chip->pci);
1308 snd_nm256_init_chip(chip);
1311 snd_ac97_resume(chip->ac97);
1313 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1316 #endif /* CONFIG_PM */
1318 static int snd_nm256_free(nm256_t *chip)
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);
1326 synchronize_irq(chip->irq);
1329 iounmap((void *) chip->cport);
1331 iounmap((void *) chip->buffer);
1332 if (chip->res_cport) {
1333 release_resource(chip->res_cport);
1334 kfree_nocheck(chip->res_cport);
1336 if (chip->res_buffer) {
1337 release_resource(chip->res_buffer);
1338 kfree_nocheck(chip->res_buffer);
1341 free_irq(chip->irq, (void*)chip);
1343 snd_magic_kfree(chip);
1347 static int snd_nm256_dev_free(snd_device_t *device)
1349 nm256_t *chip = snd_magic_cast(nm256_t, device->device_data, return -ENXIO);
1350 return snd_nm256_free(chip);
1353 static int __devinit
1354 snd_nm256_create(snd_card_t *card, struct pci_dev *pci,
1355 int play_bufsize, int capt_bufsize,
1363 static snd_device_ops_t ops = {
1364 .dev_free = snd_nm256_dev_free,
1367 u16 subsystem_vendor, subsystem_device;
1371 chip = snd_magic_kcalloc(nm256_t, 0, GFP_KERNEL);
1377 chip->use_cache = usecache;
1378 spin_lock_init(&chip->reg_lock);
1381 chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize = play_bufsize;
1382 chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize = capt_bufsize;
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).
1391 chip->buffer_addr = pci_resource_start(pci, 0);
1392 chip->cport_addr = pci_resource_start(pci, 1);
1394 /* Init the memory port info. */
1395 /* remap control port (#2) */
1396 chip->res_cport = request_mem_region(chip->cport_addr, NM_PORT2_SIZE,
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);
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);
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) {
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");
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;
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;
1433 chip->buffer_end = 4096 * 1024;
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;
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;
1444 chip->buffer_size += NM_MAX_PLAYBACK_COEF_SIZE + NM_MAX_RECORD_COEF_SIZE;
1446 if (buffertop >= chip->buffer_size && buffertop < chip->buffer_end)
1447 chip->buffer_end = buffertop;
1449 /* get buffer end pointer from signature */
1450 if ((err = snd_nm256_peek_for_sig(chip)) < 0)
1454 chip->buffer_start = chip->buffer_end - chip->buffer_size;
1455 chip->buffer_addr += chip->buffer_start;
1457 printk(KERN_INFO "nm256: Mapping port 1 from 0x%x - 0x%x\n",
1458 chip->buffer_start, chip->buffer_end);
1460 chip->res_buffer = request_mem_region(chip->buffer_addr,
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);
1469 chip->buffer = (unsigned long) ioremap_nocache(chip->buffer_addr, chip->buffer_size);
1470 if (chip->buffer == 0) {
1472 snd_printk("unable to map ring buffer at %lx\n", chip->buffer_addr);
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;
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;
1490 /* acquire interrupt */
1491 if (request_irq(pci->irq, chip->interrupt, SA_INTERRUPT|SA_SHIRQ,
1492 card->driver, (void*)chip)) {
1494 snd_printk("unable to grab IRQ %d\n", pci->irq);
1497 chip->irq = pci->irq;
1499 /* Fixed setting. */
1500 chip->mixer_base = NM_MIXER_OFFSET;
1502 chip->coeffs_current = 0;
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 || (subsystem_vendor == 0x1028 && subsystem_device == 0x0080)) {
1510 /* this workaround will cause lock-up after suspend/resume on Sony PCG-F305 */
1511 /* It will also cause a lock-up on Latitude LS (PP01S Rev A2) whenever sound devices are accessed */
1512 chip->latitude_workaround = 0;
1514 if (subsystem_vendor == 0x1028 && subsystem_device == 0x0080) {
1515 /* this workaround will cause lock-up after suspend/resume on a Dell laptop */
1516 chip->latitude_workaround = 0;
1519 snd_nm256_init_chip(chip);
1521 if ((err = snd_nm256_pcm(chip, 0)) < 0)
1524 if ((err = snd_nm256_mixer(chip)) < 0)
1527 // pci_set_master(pci); /* needed? */
1529 snd_card_set_pm_callback(card, nm256_suspend, nm256_resume, chip);
1531 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1534 snd_card_set_dev(card, &pci->dev);
1540 snd_nm256_free(chip);
1545 struct nm256_quirk {
1546 unsigned short vendor;
1547 unsigned short device;
1551 #define NM_BLACKLISTED 1
1553 static struct nm256_quirk nm256_quirks[] __devinitdata = {
1554 /* HP omnibook 4150 has cs4232 codec internally */
1555 { .vendor = 0x103c, .device = 0x0007, .type = NM_BLACKLISTED },
1556 { } /* terminator */
1560 static int __devinit snd_nm256_probe(struct pci_dev *pci,
1561 const struct pci_device_id *pci_id)
1567 unsigned int xbuffer_top;
1568 struct nm256_quirk *q;
1569 u16 subsystem_vendor, subsystem_device;
1571 if ((err = pci_enable_device(pci)) < 0)
1574 if (dev >= SNDRV_CARDS)
1581 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1582 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1584 for (q = nm256_quirks; q->vendor; q++) {
1585 if (q->vendor == subsystem_vendor && q->device == subsystem_device) {
1586 if (q->type == NM_BLACKLISTED) {
1587 printk(KERN_INFO "nm256: The device is blacklisted. Loading stopped\n");
1593 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1597 switch (pci->device) {
1598 case PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO:
1599 strcpy(card->driver, "NM256AV");
1601 case PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO:
1602 strcpy(card->driver, "NM256ZX");
1604 case PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO:
1605 strcpy(card->driver, "NM256XL+");
1608 snd_printk("invalid device id 0x%x\n", pci->device);
1609 snd_card_free(card);
1614 xbuffer_top = 0x25a800; /* this avoids conflicts with XFree86 server */
1616 xbuffer_top = buffer_top[dev];
1618 if (playback_bufsize[dev] < 4)
1619 playback_bufsize[dev] = 4;
1620 if (playback_bufsize[dev] > 128)
1621 playback_bufsize[dev] = 128;
1622 if (capture_bufsize[dev] < 4)
1623 capture_bufsize[dev] = 4;
1624 if (capture_bufsize[dev] > 128)
1625 capture_bufsize[dev] = 128;
1626 if ((err = snd_nm256_create(card, pci,
1627 playback_bufsize[dev] * 1024, /* in bytes */
1628 capture_bufsize[dev] * 1024, /* in bytes */
1633 snd_card_free(card);
1637 sprintf(card->shortname, "NeoMagic %s", card->driver);
1638 sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %d",
1640 chip->buffer_addr, chip->cport_addr, chip->irq);
1642 if ((err = snd_card_register(card)) < 0) {
1643 snd_card_free(card);
1647 pci_set_drvdata(pci, card);
1652 static void __devexit snd_nm256_remove(struct pci_dev *pci)
1654 snd_card_free(pci_get_drvdata(pci));
1655 pci_set_drvdata(pci, NULL);
1659 static struct pci_driver driver = {
1660 .name = "NeoMagic 256",
1661 .id_table = snd_nm256_ids,
1662 .probe = snd_nm256_probe,
1663 .remove = __devexit_p(snd_nm256_remove),
1664 SND_PCI_PM_CALLBACKS
1668 static int __init alsa_card_nm256_init(void)
1670 return pci_module_init(&driver);
1673 static void __exit alsa_card_nm256_exit(void)
1675 pci_unregister_driver(&driver);
1678 module_init(alsa_card_nm256_init)
1679 module_exit(alsa_card_nm256_exit)