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_SUPPORTED_DEVICE("{{NeoMagic,NM256AV},"
49 "{NeoMagic,NM256ZX}}");
52 * some compile conditions.
55 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
56 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
57 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
58 static int playback_bufsize[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 16};
59 static int capture_bufsize[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 16};
60 static int force_ac97[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; /* disabled as default */
61 static int buffer_top[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; /* not specified */
62 static int use_cache[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; /* disabled */
63 static int vaio_hack[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; /* disabled */
64 static int reset_workaround[SNDRV_CARDS];
66 module_param_array(index, int, NULL, 0444);
67 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
68 module_param_array(id, charp, NULL, 0444);
69 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
70 module_param_array(enable, bool, NULL, 0444);
71 MODULE_PARM_DESC(enable, "Enable this soundcard.");
72 module_param_array(playback_bufsize, int, NULL, 0444);
73 MODULE_PARM_DESC(playback_bufsize, "DAC frame size in kB for " CARD_NAME " soundcard.");
74 module_param_array(capture_bufsize, int, NULL, 0444);
75 MODULE_PARM_DESC(capture_bufsize, "ADC frame size in kB for " CARD_NAME " soundcard.");
76 module_param_array(force_ac97, bool, NULL, 0444);
77 MODULE_PARM_DESC(force_ac97, "Force to use AC97 codec for " CARD_NAME " soundcard.");
78 module_param_array(buffer_top, int, NULL, 0444);
79 MODULE_PARM_DESC(buffer_top, "Set the top address of audio buffer for " CARD_NAME " soundcard.");
80 module_param_array(use_cache, bool, NULL, 0444);
81 MODULE_PARM_DESC(use_cache, "Enable the cache for coefficient table access.");
82 module_param_array(vaio_hack, bool, NULL, 0444);
83 MODULE_PARM_DESC(vaio_hack, "Enable workaround for Sony VAIO notebooks.");
84 module_param_array(reset_workaround, bool, NULL, 0444);
85 MODULE_PARM_DESC(reset_workaround, "Enable AC97 RESET workaround for some laptops.");
91 /* The BIOS signature. */
92 #define NM_SIGNATURE 0x4e4d0000
94 #define NM_SIG_MASK 0xffff0000
96 /* Size of the second memory area. */
97 #define NM_PORT2_SIZE 4096
99 /* The base offset of the mixer in the second memory area. */
100 #define NM_MIXER_OFFSET 0x600
102 /* The maximum size of a coefficient entry. */
103 #define NM_MAX_PLAYBACK_COEF_SIZE 0x5000
104 #define NM_MAX_RECORD_COEF_SIZE 0x1260
106 /* The interrupt register. */
107 #define NM_INT_REG 0xa04
109 #define NM_PLAYBACK_INT 0x40
110 #define NM_RECORD_INT 0x100
111 #define NM_MISC_INT_1 0x4000
112 #define NM_MISC_INT_2 0x1
113 #define NM_ACK_INT(chip, X) snd_nm256_writew(chip, NM_INT_REG, (X) << 1)
115 /* The AV's "mixer ready" status bit and location. */
116 #define NM_MIXER_STATUS_OFFSET 0xa04
117 #define NM_MIXER_READY_MASK 0x0800
118 #define NM_MIXER_PRESENCE 0xa06
119 #define NM_PRESENCE_MASK 0x0050
120 #define NM_PRESENCE_VALUE 0x0040
123 * For the ZX. It uses the same interrupt register, but it holds 32
124 * bits instead of 16.
126 #define NM2_PLAYBACK_INT 0x10000
127 #define NM2_RECORD_INT 0x80000
128 #define NM2_MISC_INT_1 0x8
129 #define NM2_MISC_INT_2 0x2
130 #define NM2_ACK_INT(chip, X) snd_nm256_writel(chip, NM_INT_REG, (X))
132 /* The ZX's "mixer ready" status bit and location. */
133 #define NM2_MIXER_STATUS_OFFSET 0xa06
134 #define NM2_MIXER_READY_MASK 0x0800
136 /* The playback registers start from here. */
137 #define NM_PLAYBACK_REG_OFFSET 0x0
138 /* The record registers start from here. */
139 #define NM_RECORD_REG_OFFSET 0x200
141 /* The rate register is located 2 bytes from the start of the register area. */
142 #define NM_RATE_REG_OFFSET 2
144 /* Mono/stereo flag, number of bits on playback, and rate mask. */
145 #define NM_RATE_STEREO 1
146 #define NM_RATE_BITS_16 2
147 #define NM_RATE_MASK 0xf0
149 /* Playback enable register. */
150 #define NM_PLAYBACK_ENABLE_REG (NM_PLAYBACK_REG_OFFSET + 0x1)
151 #define NM_PLAYBACK_ENABLE_FLAG 1
152 #define NM_PLAYBACK_ONESHOT 2
153 #define NM_PLAYBACK_FREERUN 4
155 /* Mutes the audio output. */
156 #define NM_AUDIO_MUTE_REG (NM_PLAYBACK_REG_OFFSET + 0x18)
157 #define NM_AUDIO_MUTE_LEFT 0x8000
158 #define NM_AUDIO_MUTE_RIGHT 0x0080
160 /* Recording enable register. */
161 #define NM_RECORD_ENABLE_REG (NM_RECORD_REG_OFFSET + 0)
162 #define NM_RECORD_ENABLE_FLAG 1
163 #define NM_RECORD_FREERUN 2
165 /* coefficient buffer pointer */
166 #define NM_COEFF_START_OFFSET 0x1c
167 #define NM_COEFF_END_OFFSET 0x20
169 /* DMA buffer offsets */
170 #define NM_RBUFFER_START (NM_RECORD_REG_OFFSET + 0x4)
171 #define NM_RBUFFER_END (NM_RECORD_REG_OFFSET + 0x10)
172 #define NM_RBUFFER_WMARK (NM_RECORD_REG_OFFSET + 0xc)
173 #define NM_RBUFFER_CURRP (NM_RECORD_REG_OFFSET + 0x8)
175 #define NM_PBUFFER_START (NM_PLAYBACK_REG_OFFSET + 0x4)
176 #define NM_PBUFFER_END (NM_PLAYBACK_REG_OFFSET + 0x14)
177 #define NM_PBUFFER_WMARK (NM_PLAYBACK_REG_OFFSET + 0xc)
178 #define NM_PBUFFER_CURRP (NM_PLAYBACK_REG_OFFSET + 0x8)
184 typedef struct snd_nm256 nm256_t;
185 typedef struct snd_nm256_stream nm256_stream_t;
187 struct snd_nm256_stream {
190 snd_pcm_substream_t *substream;
194 u32 buf; /* offset from chip->buffer */
195 int bufsize; /* buffer size in bytes */
196 void __iomem *bufptr; /* mapped pointer */
197 unsigned long bufptr_addr; /* physical address of the mapped pointer */
199 int dma_size; /* buffer size of the substream in bytes */
200 int period_size; /* period size in bytes */
201 int periods; /* # of periods */
202 int shift; /* bit shifts */
203 int cur_period; /* current period # */
211 void __iomem *cport; /* control port */
212 struct resource *res_cport; /* its resource */
213 unsigned long cport_addr; /* physical address */
215 void __iomem *buffer; /* buffer */
216 struct resource *res_buffer; /* its resource */
217 unsigned long buffer_addr; /* buffer phyiscal address */
219 u32 buffer_start; /* start offset from pci resource 0 */
220 u32 buffer_end; /* end offset */
221 u32 buffer_size; /* total buffer size */
223 u32 all_coeff_buf; /* coefficient buffer */
224 u32 coeff_buf[2]; /* coefficient buffer for each stream */
226 unsigned int coeffs_current: 1; /* coeff. table is loaded? */
227 unsigned int use_cache: 1; /* use one big coef. table */
228 unsigned int reset_workaround: 1; /* Workaround for some laptops to avoid freeze */
230 int mixer_base; /* register offset of ac97 mixer */
231 int mixer_status_offset; /* offset of mixer status reg. */
232 int mixer_status_mask; /* bit mask to test the mixer status */
236 irqreturn_t (*interrupt)(int, void *, struct pt_regs *);
237 int badintrcount; /* counter to check bogus interrupts */
238 struct semaphore irq_mutex;
240 nm256_stream_t streams[2];
254 * include coefficient table
256 #include "nm256_coef.c"
263 #ifndef PCI_VENDOR_ID_NEOMAGIC
264 #define PCI_VENDOR_ID_NEOMEGIC 0x10c8
266 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO
267 #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
269 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO
270 #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
272 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO
273 #define PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO 0x8016
277 static struct pci_device_id snd_nm256_ids[] = {
278 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
279 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
280 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
284 MODULE_DEVICE_TABLE(pci, snd_nm256_ids);
292 snd_nm256_readb(nm256_t *chip, int offset)
294 return readb(chip->cport + offset);
298 snd_nm256_readw(nm256_t *chip, int offset)
300 return readw(chip->cport + offset);
304 snd_nm256_readl(nm256_t *chip, int offset)
306 return readl(chip->cport + offset);
310 snd_nm256_writeb(nm256_t *chip, int offset, u8 val)
312 writeb(val, chip->cport + offset);
316 snd_nm256_writew(nm256_t *chip, int offset, u16 val)
318 writew(val, chip->cport + offset);
322 snd_nm256_writel(nm256_t *chip, int offset, u32 val)
324 writel(val, chip->cport + offset);
328 snd_nm256_write_buffer(nm256_t *chip, void *src, int offset, int size)
330 offset -= chip->buffer_start;
331 #ifdef SNDRV_CONFIG_DEBUG
332 if (offset < 0 || offset >= chip->buffer_size) {
333 snd_printk("write_buffer invalid offset = %d size = %d\n", offset, size);
337 memcpy_toio(chip->buffer + offset, src, size);
341 * coefficient handlers -- what a magic!
345 snd_nm256_get_start_offset(int which)
349 offset += coefficient_sizes[which];
354 snd_nm256_load_one_coefficient(nm256_t *chip, int stream, u32 port, int which)
356 u32 coeff_buf = chip->coeff_buf[stream];
357 u16 offset = snd_nm256_get_start_offset(which);
358 u16 size = coefficient_sizes[which];
360 snd_nm256_write_buffer(chip, coefficients + offset, coeff_buf, size);
361 snd_nm256_writel(chip, port, coeff_buf);
362 /* ??? Record seems to behave differently than playback. */
363 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
365 snd_nm256_writel(chip, port + 4, coeff_buf + size);
369 snd_nm256_load_coefficient(nm256_t *chip, int stream, int number)
371 /* The enable register for the specified engine. */
372 u32 poffset = (stream == SNDRV_PCM_STREAM_CAPTURE ? NM_RECORD_ENABLE_REG : NM_PLAYBACK_ENABLE_REG);
373 u32 addr = NM_COEFF_START_OFFSET;
375 addr += (stream == SNDRV_PCM_STREAM_CAPTURE ? NM_RECORD_REG_OFFSET : NM_PLAYBACK_REG_OFFSET);
377 if (snd_nm256_readb(chip, poffset) & 1) {
378 snd_printd("NM256: Engine was enabled while loading coefficients!\n");
382 /* The recording engine uses coefficient values 8-15. */
384 if (stream == SNDRV_PCM_STREAM_CAPTURE)
387 if (! chip->use_cache) {
388 snd_nm256_load_one_coefficient(chip, stream, addr, number);
391 if (! chip->coeffs_current) {
392 snd_nm256_write_buffer(chip, coefficients, chip->all_coeff_buf,
393 NM_TOTAL_COEFF_COUNT * 4);
394 chip->coeffs_current = 1;
396 u32 base = chip->all_coeff_buf;
397 u32 offset = snd_nm256_get_start_offset(number);
398 u32 end_offset = offset + coefficient_sizes[number];
399 snd_nm256_writel(chip, addr, base + offset);
400 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
402 snd_nm256_writel(chip, addr + 4, base + end_offset);
407 /* The actual rates supported by the card. */
408 static unsigned int samplerates[8] = {
409 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
411 static snd_pcm_hw_constraint_list_t constraints_rates = {
412 .count = ARRAY_SIZE(samplerates),
418 * return the index of the target rate
421 snd_nm256_fixed_rate(unsigned int rate)
424 for (i = 0; i < ARRAY_SIZE(samplerates); i++) {
425 if (rate == samplerates[i])
433 * set sample rate and format
436 snd_nm256_set_format(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
438 snd_pcm_runtime_t *runtime = substream->runtime;
439 int rate_index = snd_nm256_fixed_rate(runtime->rate);
440 unsigned char ratebits = (rate_index << 4) & NM_RATE_MASK;
443 if (snd_pcm_format_width(runtime->format) == 16) {
444 ratebits |= NM_RATE_BITS_16;
447 if (runtime->channels > 1) {
448 ratebits |= NM_RATE_STEREO;
452 runtime->rate = samplerates[rate_index];
454 switch (substream->stream) {
455 case SNDRV_PCM_STREAM_PLAYBACK:
456 snd_nm256_load_coefficient(chip, 0, rate_index); /* 0 = playback */
457 snd_nm256_writeb(chip,
458 NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET,
461 case SNDRV_PCM_STREAM_CAPTURE:
462 snd_nm256_load_coefficient(chip, 1, rate_index); /* 1 = record */
463 snd_nm256_writeb(chip,
464 NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET,
470 /* acquire interrupt */
471 static int snd_nm256_acquire_irq(nm256_t *chip)
473 down(&chip->irq_mutex);
475 if (request_irq(chip->pci->irq, chip->interrupt, SA_INTERRUPT|SA_SHIRQ,
476 chip->card->driver, (void*)chip)) {
477 snd_printk("unable to grab IRQ %d\n", chip->pci->irq);
478 up(&chip->irq_mutex);
481 chip->irq = chip->pci->irq;
484 up(&chip->irq_mutex);
488 /* release interrupt */
489 static void snd_nm256_release_irq(nm256_t *chip)
491 down(&chip->irq_mutex);
492 if (chip->irq_acks > 0)
494 if (chip->irq_acks == 0 && chip->irq >= 0) {
495 free_irq(chip->irq, (void*)chip);
498 up(&chip->irq_mutex);
505 /* update the watermark (current period) */
506 static void snd_nm256_pcm_mark(nm256_t *chip, nm256_stream_t *s, int reg)
509 s->cur_period %= s->periods;
510 snd_nm256_writel(chip, reg, s->buf + s->cur_period * s->period_size);
513 #define snd_nm256_playback_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_PBUFFER_WMARK)
514 #define snd_nm256_capture_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_RBUFFER_WMARK)
517 snd_nm256_playback_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
519 /* program buffer pointers */
520 snd_nm256_writel(chip, NM_PBUFFER_START, s->buf);
521 snd_nm256_writel(chip, NM_PBUFFER_END, s->buf + s->dma_size - (1 << s->shift));
522 snd_nm256_writel(chip, NM_PBUFFER_CURRP, s->buf);
523 snd_nm256_playback_mark(chip, s);
525 /* Enable playback engine and interrupts. */
526 snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG,
527 NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);
528 /* Enable both channels. */
529 snd_nm256_writew(chip, NM_AUDIO_MUTE_REG, 0x0);
533 snd_nm256_capture_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
535 /* program buffer pointers */
536 snd_nm256_writel(chip, NM_RBUFFER_START, s->buf);
537 snd_nm256_writel(chip, NM_RBUFFER_END, s->buf + s->dma_size);
538 snd_nm256_writel(chip, NM_RBUFFER_CURRP, s->buf);
539 snd_nm256_capture_mark(chip, s);
541 /* Enable playback engine and interrupts. */
542 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG,
543 NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
546 /* Stop the play engine. */
548 snd_nm256_playback_stop(nm256_t *chip)
550 /* Shut off sound from both channels. */
551 snd_nm256_writew(chip, NM_AUDIO_MUTE_REG,
552 NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT);
553 /* Disable play engine. */
554 snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG, 0);
558 snd_nm256_capture_stop(nm256_t *chip)
560 /* Disable recording engine. */
561 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG, 0);
565 snd_nm256_playback_trigger(snd_pcm_substream_t *substream, int cmd)
567 nm256_t *chip = snd_pcm_substream_chip(substream);
568 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
571 snd_assert(s != NULL, return -ENXIO);
573 spin_lock(&chip->reg_lock);
575 case SNDRV_PCM_TRIGGER_RESUME:
578 case SNDRV_PCM_TRIGGER_START:
580 snd_nm256_playback_start(chip, s, substream);
584 case SNDRV_PCM_TRIGGER_SUSPEND:
587 case SNDRV_PCM_TRIGGER_STOP:
589 snd_nm256_playback_stop(chip);
597 spin_unlock(&chip->reg_lock);
602 snd_nm256_capture_trigger(snd_pcm_substream_t *substream, int cmd)
604 nm256_t *chip = snd_pcm_substream_chip(substream);
605 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
608 snd_assert(s != NULL, return -ENXIO);
610 spin_lock(&chip->reg_lock);
612 case SNDRV_PCM_TRIGGER_START:
613 case SNDRV_PCM_TRIGGER_RESUME:
615 snd_nm256_capture_start(chip, s, substream);
619 case SNDRV_PCM_TRIGGER_STOP:
620 case SNDRV_PCM_TRIGGER_SUSPEND:
622 snd_nm256_capture_stop(chip);
630 spin_unlock(&chip->reg_lock);
636 * prepare playback/capture channel
638 static int snd_nm256_pcm_prepare(snd_pcm_substream_t *substream)
640 nm256_t *chip = snd_pcm_substream_chip(substream);
641 snd_pcm_runtime_t *runtime = substream->runtime;
642 nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
644 snd_assert(s, return -ENXIO);
645 s->dma_size = frames_to_bytes(runtime, substream->runtime->buffer_size);
646 s->period_size = frames_to_bytes(runtime, substream->runtime->period_size);
647 s->periods = substream->runtime->periods;
650 spin_lock_irq(&chip->reg_lock);
652 snd_nm256_set_format(chip, s, substream);
653 spin_unlock_irq(&chip->reg_lock);
660 * get the current pointer
662 static snd_pcm_uframes_t
663 snd_nm256_playback_pointer(snd_pcm_substream_t * substream)
665 nm256_t *chip = snd_pcm_substream_chip(substream);
666 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
669 snd_assert(s, return 0);
670 curp = snd_nm256_readl(chip, NM_PBUFFER_CURRP) - (unsigned long)s->buf;
672 return bytes_to_frames(substream->runtime, curp);
675 static snd_pcm_uframes_t
676 snd_nm256_capture_pointer(snd_pcm_substream_t * substream)
678 nm256_t *chip = snd_pcm_substream_chip(substream);
679 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
682 snd_assert(s != NULL, return 0);
683 curp = snd_nm256_readl(chip, NM_RBUFFER_CURRP) - (unsigned long)s->buf;
685 return bytes_to_frames(substream->runtime, curp);
688 /* Remapped I/O space can be accessible as pointer on i386 */
689 /* This might be changed in the future */
692 * silence / copy for playback
695 snd_nm256_playback_silence(snd_pcm_substream_t *substream,
696 int channel, /* not used (interleaved data) */
697 snd_pcm_uframes_t pos,
698 snd_pcm_uframes_t count)
700 snd_pcm_runtime_t *runtime = substream->runtime;
701 nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
702 count = frames_to_bytes(runtime, count);
703 pos = frames_to_bytes(runtime, pos);
704 memset_io(s->bufptr + pos, 0, count);
709 snd_nm256_playback_copy(snd_pcm_substream_t *substream,
710 int channel, /* not used (interleaved data) */
711 snd_pcm_uframes_t pos,
713 snd_pcm_uframes_t count)
715 snd_pcm_runtime_t *runtime = substream->runtime;
716 nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
717 count = frames_to_bytes(runtime, count);
718 pos = frames_to_bytes(runtime, pos);
719 if (copy_from_user_toio(s->bufptr + pos, src, count))
728 snd_nm256_capture_copy(snd_pcm_substream_t *substream,
729 int channel, /* not used (interleaved data) */
730 snd_pcm_uframes_t pos,
732 snd_pcm_uframes_t count)
734 snd_pcm_runtime_t *runtime = substream->runtime;
735 nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
736 count = frames_to_bytes(runtime, count);
737 pos = frames_to_bytes(runtime, pos);
738 if (copy_to_user_fromio(dst, s->bufptr + pos, count))
743 #endif /* !__i386__ */
747 * update playback/capture watermarks
752 snd_nm256_playback_update(nm256_t *chip)
756 s = &chip->streams[SNDRV_PCM_STREAM_PLAYBACK];
757 if (s->running && s->substream) {
758 spin_unlock(&chip->reg_lock);
759 snd_pcm_period_elapsed(s->substream);
760 spin_lock(&chip->reg_lock);
761 snd_nm256_playback_mark(chip, s);
767 snd_nm256_capture_update(nm256_t *chip)
771 s = &chip->streams[SNDRV_PCM_STREAM_CAPTURE];
772 if (s->running && s->substream) {
773 spin_unlock(&chip->reg_lock);
774 snd_pcm_period_elapsed(s->substream);
775 spin_lock(&chip->reg_lock);
776 snd_nm256_capture_mark(chip, s);
783 static snd_pcm_hardware_t snd_nm256_playback =
785 .info = SNDRV_PCM_INFO_MMAP_IOMEM |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,
802 static snd_pcm_hardware_t snd_nm256_capture =
804 .info = SNDRV_PCM_INFO_MMAP_IOMEM | SNDRV_PCM_INFO_MMAP_VALID |
805 SNDRV_PCM_INFO_INTERLEAVED |
806 /*SNDRV_PCM_INFO_PAUSE |*/
807 SNDRV_PCM_INFO_RESUME,
808 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
809 .rates = SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000,
816 .buffer_bytes_max = 128 * 1024,
817 .period_bytes_min = 256,
818 .period_bytes_max = 128 * 1024,
822 /* set dma transfer size */
823 static int snd_nm256_pcm_hw_params(snd_pcm_substream_t *substream, snd_pcm_hw_params_t *hw_params)
825 /* area and addr are already set and unchanged */
826 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
833 static void snd_nm256_setup_stream(nm256_t *chip, nm256_stream_t *s,
834 snd_pcm_substream_t *substream,
835 snd_pcm_hardware_t *hw_ptr)
837 snd_pcm_runtime_t *runtime = substream->runtime;
840 runtime->hw = *hw_ptr;
841 runtime->hw.buffer_bytes_max = s->bufsize;
842 runtime->hw.period_bytes_max = s->bufsize / 2;
843 runtime->dma_area = (void*) s->bufptr;
844 runtime->dma_addr = s->bufptr_addr;
845 runtime->dma_bytes = s->bufsize;
846 runtime->private_data = s;
847 s->substream = substream;
849 snd_pcm_set_sync(substream);
850 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
855 snd_nm256_playback_open(snd_pcm_substream_t *substream)
857 nm256_t *chip = snd_pcm_substream_chip(substream);
859 if (snd_nm256_acquire_irq(chip) < 0)
861 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK],
862 substream, &snd_nm256_playback);
867 snd_nm256_capture_open(snd_pcm_substream_t *substream)
869 nm256_t *chip = snd_pcm_substream_chip(substream);
871 if (snd_nm256_acquire_irq(chip) < 0)
873 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_CAPTURE],
874 substream, &snd_nm256_capture);
879 * close - we don't have to do special..
882 snd_nm256_playback_close(snd_pcm_substream_t *substream)
884 nm256_t *chip = snd_pcm_substream_chip(substream);
886 snd_nm256_release_irq(chip);
892 snd_nm256_capture_close(snd_pcm_substream_t *substream)
894 nm256_t *chip = snd_pcm_substream_chip(substream);
896 snd_nm256_release_irq(chip);
901 * create a pcm instance
903 static snd_pcm_ops_t snd_nm256_playback_ops = {
904 .open = snd_nm256_playback_open,
905 .close = snd_nm256_playback_close,
906 .ioctl = snd_pcm_lib_ioctl,
907 .hw_params = snd_nm256_pcm_hw_params,
908 .prepare = snd_nm256_pcm_prepare,
909 .trigger = snd_nm256_playback_trigger,
910 .pointer = snd_nm256_playback_pointer,
912 .copy = snd_nm256_playback_copy,
913 .silence = snd_nm256_playback_silence,
915 .mmap = snd_pcm_lib_mmap_iomem,
918 static snd_pcm_ops_t snd_nm256_capture_ops = {
919 .open = snd_nm256_capture_open,
920 .close = snd_nm256_capture_close,
921 .ioctl = snd_pcm_lib_ioctl,
922 .hw_params = snd_nm256_pcm_hw_params,
923 .prepare = snd_nm256_pcm_prepare,
924 .trigger = snd_nm256_capture_trigger,
925 .pointer = snd_nm256_capture_pointer,
927 .copy = snd_nm256_capture_copy,
929 .mmap = snd_pcm_lib_mmap_iomem,
933 snd_nm256_pcm(nm256_t *chip, int device)
938 for (i = 0; i < 2; i++) {
939 nm256_stream_t *s = &chip->streams[i];
940 s->bufptr = chip->buffer + (s->buf - chip->buffer_start);
941 s->bufptr_addr = chip->buffer_addr + (s->buf - chip->buffer_start);
944 err = snd_pcm_new(chip->card, chip->card->driver, device,
949 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_nm256_playback_ops);
950 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_nm256_capture_ops);
952 pcm->private_data = chip;
961 * Initialize the hardware.
964 snd_nm256_init_chip(nm256_t *chip)
966 /* Reset everything. */
967 snd_nm256_writeb(chip, 0x0, 0x11);
968 snd_nm256_writew(chip, 0x214, 0);
970 //snd_nm256_playback_stop(chip);
971 //snd_nm256_capture_stop(chip);
976 snd_nm256_intr_check(nm256_t *chip)
978 if (chip->badintrcount++ > 1000) {
980 * I'm not sure if the best thing is to stop the card from
981 * playing or just release the interrupt (after all, we're in
982 * a bad situation, so doing fancy stuff may not be such a good
985 * I worry about the card engine continuing to play noise
986 * over and over, however--that could become a very
987 * obnoxious problem. And we know that when this usually
988 * happens things are fairly safe, it just means the user's
989 * inserted a PCMCIA card and someone's spamming us with IRQ 9s.
991 if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
992 snd_nm256_playback_stop(chip);
993 if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
994 snd_nm256_capture_stop(chip);
995 chip->badintrcount = 0;
1002 * Handle a potential interrupt for the device referred to by DEV_ID.
1004 * I don't like the cut-n-paste job here either between the two routines,
1005 * but there are sufficient differences between the two interrupt handlers
1006 * that parameterizing it isn't all that great either. (Could use a macro,
1007 * I suppose...yucky bleah.)
1011 snd_nm256_interrupt(int irq, void *dev_id, struct pt_regs *dummy)
1013 nm256_t *chip = dev_id;
1017 status = snd_nm256_readw(chip, NM_INT_REG);
1021 return snd_nm256_intr_check(chip);
1023 chip->badintrcount = 0;
1025 /* Rather boring; check for individual interrupts and process them. */
1027 spin_lock(&chip->reg_lock);
1028 if (status & NM_PLAYBACK_INT) {
1029 status &= ~NM_PLAYBACK_INT;
1030 NM_ACK_INT(chip, NM_PLAYBACK_INT);
1031 snd_nm256_playback_update(chip);
1034 if (status & NM_RECORD_INT) {
1035 status &= ~NM_RECORD_INT;
1036 NM_ACK_INT(chip, NM_RECORD_INT);
1037 snd_nm256_capture_update(chip);
1040 if (status & NM_MISC_INT_1) {
1041 status &= ~NM_MISC_INT_1;
1042 NM_ACK_INT(chip, NM_MISC_INT_1);
1043 snd_printd("NM256: Got misc interrupt #1\n");
1044 snd_nm256_writew(chip, NM_INT_REG, 0x8000);
1045 cbyte = snd_nm256_readb(chip, 0x400);
1046 snd_nm256_writeb(chip, 0x400, cbyte | 2);
1049 if (status & NM_MISC_INT_2) {
1050 status &= ~NM_MISC_INT_2;
1051 NM_ACK_INT(chip, NM_MISC_INT_2);
1052 snd_printd("NM256: Got misc interrupt #2\n");
1053 cbyte = snd_nm256_readb(chip, 0x400);
1054 snd_nm256_writeb(chip, 0x400, cbyte & ~2);
1057 /* Unknown interrupt. */
1059 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1062 NM_ACK_INT(chip, status);
1065 spin_unlock(&chip->reg_lock);
1070 * Handle a potential interrupt for the device referred to by DEV_ID.
1071 * This handler is for the 256ZX, and is very similar to the non-ZX
1076 snd_nm256_interrupt_zx(int irq, void *dev_id, struct pt_regs *dummy)
1078 nm256_t *chip = dev_id;
1082 status = snd_nm256_readl(chip, NM_INT_REG);
1086 return snd_nm256_intr_check(chip);
1088 chip->badintrcount = 0;
1090 /* Rather boring; check for individual interrupts and process them. */
1092 spin_lock(&chip->reg_lock);
1093 if (status & NM2_PLAYBACK_INT) {
1094 status &= ~NM2_PLAYBACK_INT;
1095 NM2_ACK_INT(chip, NM2_PLAYBACK_INT);
1096 snd_nm256_playback_update(chip);
1099 if (status & NM2_RECORD_INT) {
1100 status &= ~NM2_RECORD_INT;
1101 NM2_ACK_INT(chip, NM2_RECORD_INT);
1102 snd_nm256_capture_update(chip);
1105 if (status & NM2_MISC_INT_1) {
1106 status &= ~NM2_MISC_INT_1;
1107 NM2_ACK_INT(chip, NM2_MISC_INT_1);
1108 snd_printd("NM256: Got misc interrupt #1\n");
1109 cbyte = snd_nm256_readb(chip, 0x400);
1110 snd_nm256_writeb(chip, 0x400, cbyte | 2);
1113 if (status & NM2_MISC_INT_2) {
1114 status &= ~NM2_MISC_INT_2;
1115 NM2_ACK_INT(chip, NM2_MISC_INT_2);
1116 snd_printd("NM256: Got misc interrupt #2\n");
1117 cbyte = snd_nm256_readb(chip, 0x400);
1118 snd_nm256_writeb(chip, 0x400, cbyte & ~2);
1121 /* Unknown interrupt. */
1123 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1126 NM2_ACK_INT(chip, status);
1129 spin_unlock(&chip->reg_lock);
1138 * Waits for the mixer to become ready to be written; returns a zero value
1142 snd_nm256_ac97_ready(nm256_t *chip)
1148 testaddr = chip->mixer_status_offset;
1149 testb = chip->mixer_status_mask;
1152 * Loop around waiting for the mixer to become ready.
1154 while (timeout-- > 0) {
1155 if ((snd_nm256_readw(chip, testaddr) & testb) == 0)
1164 static unsigned short
1165 snd_nm256_ac97_read(ac97_t *ac97, unsigned short reg)
1167 nm256_t *chip = ac97->private_data;
1173 if (! snd_nm256_ac97_ready(chip))
1175 res = snd_nm256_readw(chip, chip->mixer_base + reg);
1176 /* Magic delay. Bleah yucky. */
1184 snd_nm256_ac97_write(ac97_t *ac97,
1185 unsigned short reg, unsigned short val)
1187 nm256_t *chip = ac97->private_data;
1191 base = chip->mixer_base;
1193 snd_nm256_ac97_ready(chip);
1195 /* Wait for the write to take, too. */
1196 while (tries-- > 0) {
1197 snd_nm256_writew(chip, base + reg, val);
1198 msleep(1); /* a little delay here seems better.. */
1199 if (snd_nm256_ac97_ready(chip))
1202 snd_printd("nm256: ac97 codec not ready..\n");
1205 /* initialize the ac97 into a known state */
1207 snd_nm256_ac97_reset(ac97_t *ac97)
1209 nm256_t *chip = ac97->private_data;
1211 /* Reset the mixer. 'Tis magic! */
1212 snd_nm256_writeb(chip, 0x6c0, 1);
1213 if (! chip->reset_workaround) {
1214 /* Dell latitude LS will lock up by this */
1215 snd_nm256_writeb(chip, 0x6cc, 0x87);
1217 snd_nm256_writeb(chip, 0x6cc, 0x80);
1218 snd_nm256_writeb(chip, 0x6cc, 0x0);
1221 /* create an ac97 mixer interface */
1222 static int __devinit
1223 snd_nm256_mixer(nm256_t *chip)
1226 ac97_template_t ac97;
1228 static ac97_bus_ops_t ops = {
1229 .reset = snd_nm256_ac97_reset,
1230 .write = snd_nm256_ac97_write,
1231 .read = snd_nm256_ac97_read,
1233 /* looks like nm256 hangs up when unexpected registers are touched... */
1234 static int mixer_regs[] = {
1235 AC97_MASTER, AC97_HEADPHONE, AC97_MASTER_MONO,
1236 AC97_PC_BEEP, AC97_PHONE, AC97_MIC, AC97_LINE, AC97_CD,
1237 AC97_VIDEO, AC97_AUX, AC97_PCM, AC97_REC_SEL,
1238 AC97_REC_GAIN, AC97_GENERAL_PURPOSE, AC97_3D_CONTROL,
1239 /*AC97_EXTENDED_ID,*/
1240 AC97_VENDOR_ID1, AC97_VENDOR_ID2,
1244 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
1247 memset(&ac97, 0, sizeof(ac97));
1248 ac97.scaps = AC97_SCAP_AUDIO; /* we support audio! */
1249 ac97.limited_regs = 1;
1250 for (i = 0; mixer_regs[i] >= 0; i++)
1251 set_bit(mixer_regs[i], ac97.reg_accessed);
1252 ac97.private_data = chip;
1254 err = snd_ac97_mixer(pbus, &ac97, &chip->ac97);
1257 if (! (chip->ac97->id & (0xf0000000))) {
1258 /* looks like an invalid id */
1259 sprintf(chip->card->mixername, "%s AC97", chip->card->driver);
1265 * See if the signature left by the NM256 BIOS is intact; if so, we use
1266 * the associated address as the end of our audio buffer in the video
1270 static int __devinit
1271 snd_nm256_peek_for_sig(nm256_t *chip)
1273 /* The signature is located 1K below the end of video RAM. */
1275 /* Default buffer end is 5120 bytes below the top of RAM. */
1276 unsigned long pointer_found = chip->buffer_end - 0x1400;
1279 temp = ioremap_nocache(chip->buffer_addr + chip->buffer_end - 0x400, 16);
1281 snd_printk("Unable to scan for card signature in video RAM\n");
1286 if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1287 u32 pointer = readl(temp + 4);
1290 * If it's obviously invalid, don't use it
1292 if (pointer == 0xffffffff ||
1293 pointer < chip->buffer_size ||
1294 pointer > chip->buffer_end) {
1295 snd_printk("invalid signature found: 0x%x\n", pointer);
1299 pointer_found = pointer;
1300 printk(KERN_INFO "nm256: found card signature in video RAM: 0x%x\n", pointer);
1305 chip->buffer_end = pointer_found;
1312 * APM event handler, so the card is properly reinitialized after a power
1315 static int nm256_suspend(snd_card_t *card, pm_message_t state)
1317 nm256_t *chip = card->pm_private_data;
1319 snd_pcm_suspend_all(chip->pcm);
1320 snd_ac97_suspend(chip->ac97);
1321 chip->coeffs_current = 0;
1322 pci_disable_device(chip->pci);
1326 static int nm256_resume(snd_card_t *card)
1328 nm256_t *chip = card->pm_private_data;
1331 /* Perform a full reset on the hardware */
1332 pci_enable_device(chip->pci);
1333 snd_nm256_init_chip(chip);
1336 snd_ac97_resume(chip->ac97);
1338 for (i = 0; i < 2; i++) {
1339 nm256_stream_t *s = &chip->streams[i];
1340 if (s->substream && s->suspended) {
1341 spin_lock_irq(&chip->reg_lock);
1342 snd_nm256_set_format(chip, s, s->substream);
1343 spin_unlock_irq(&chip->reg_lock);
1349 #endif /* CONFIG_PM */
1351 static int snd_nm256_free(nm256_t *chip)
1353 if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
1354 snd_nm256_playback_stop(chip);
1355 if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
1356 snd_nm256_capture_stop(chip);
1359 synchronize_irq(chip->irq);
1362 iounmap(chip->cport);
1364 iounmap(chip->buffer);
1365 if (chip->res_cport) {
1366 release_resource(chip->res_cport);
1367 kfree_nocheck(chip->res_cport);
1369 if (chip->res_buffer) {
1370 release_resource(chip->res_buffer);
1371 kfree_nocheck(chip->res_buffer);
1374 free_irq(chip->irq, (void*)chip);
1376 pci_disable_device(chip->pci);
1381 static int snd_nm256_dev_free(snd_device_t *device)
1383 nm256_t *chip = device->device_data;
1384 return snd_nm256_free(chip);
1387 static int __devinit
1388 snd_nm256_create(snd_card_t *card, struct pci_dev *pci,
1389 int play_bufsize, int capt_bufsize,
1397 static snd_device_ops_t ops = {
1398 .dev_free = snd_nm256_dev_free,
1404 if ((err = pci_enable_device(pci)) < 0)
1407 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
1409 pci_disable_device(pci);
1415 chip->use_cache = usecache;
1416 spin_lock_init(&chip->reg_lock);
1418 init_MUTEX(&chip->irq_mutex);
1420 chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize = play_bufsize;
1421 chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize = capt_bufsize;
1424 * The NM256 has two memory ports. The first port is nothing
1425 * more than a chunk of video RAM, which is used as the I/O ring
1426 * buffer. The second port has the actual juicy stuff (like the
1427 * mixer and the playback engine control registers).
1430 chip->buffer_addr = pci_resource_start(pci, 0);
1431 chip->cport_addr = pci_resource_start(pci, 1);
1433 /* Init the memory port info. */
1434 /* remap control port (#2) */
1435 chip->res_cport = request_mem_region(chip->cport_addr, NM_PORT2_SIZE,
1437 if (chip->res_cport == NULL) {
1438 snd_printk("memory region 0x%lx (size 0x%x) busy\n",
1439 chip->cport_addr, NM_PORT2_SIZE);
1443 chip->cport = ioremap_nocache(chip->cport_addr, NM_PORT2_SIZE);
1444 if (chip->cport == NULL) {
1445 snd_printk("unable to map control port %lx\n", chip->cport_addr);
1450 if (!strcmp(card->driver, "NM256AV")) {
1451 /* Ok, try to see if this is a non-AC97 version of the hardware. */
1452 pval = snd_nm256_readw(chip, NM_MIXER_PRESENCE);
1453 if ((pval & NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) {
1455 printk(KERN_ERR "nm256: no ac97 is found!\n");
1456 printk(KERN_ERR " force the driver to load by passing in the module parameter\n");
1457 printk(KERN_ERR " force_ac97=1\n");
1458 printk(KERN_ERR " or try sb16 or cs423x drivers instead.\n");
1463 chip->buffer_end = 2560 * 1024;
1464 chip->interrupt = snd_nm256_interrupt;
1465 chip->mixer_status_offset = NM_MIXER_STATUS_OFFSET;
1466 chip->mixer_status_mask = NM_MIXER_READY_MASK;
1468 /* Not sure if there is any relevant detect for the ZX or not. */
1469 if (snd_nm256_readb(chip, 0xa0b) != 0)
1470 chip->buffer_end = 6144 * 1024;
1472 chip->buffer_end = 4096 * 1024;
1474 chip->interrupt = snd_nm256_interrupt_zx;
1475 chip->mixer_status_offset = NM2_MIXER_STATUS_OFFSET;
1476 chip->mixer_status_mask = NM2_MIXER_READY_MASK;
1479 chip->buffer_size = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize + chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize;
1480 if (chip->use_cache)
1481 chip->buffer_size += NM_TOTAL_COEFF_COUNT * 4;
1483 chip->buffer_size += NM_MAX_PLAYBACK_COEF_SIZE + NM_MAX_RECORD_COEF_SIZE;
1485 if (buffertop >= chip->buffer_size && buffertop < chip->buffer_end)
1486 chip->buffer_end = buffertop;
1488 /* get buffer end pointer from signature */
1489 if ((err = snd_nm256_peek_for_sig(chip)) < 0)
1493 chip->buffer_start = chip->buffer_end - chip->buffer_size;
1494 chip->buffer_addr += chip->buffer_start;
1496 printk(KERN_INFO "nm256: Mapping port 1 from 0x%x - 0x%x\n",
1497 chip->buffer_start, chip->buffer_end);
1499 chip->res_buffer = request_mem_region(chip->buffer_addr,
1502 if (chip->res_buffer == NULL) {
1503 snd_printk("nm256: buffer 0x%lx (size 0x%x) busy\n",
1504 chip->buffer_addr, chip->buffer_size);
1508 chip->buffer = ioremap_nocache(chip->buffer_addr, chip->buffer_size);
1509 if (chip->buffer == NULL) {
1511 snd_printk("unable to map ring buffer at %lx\n", chip->buffer_addr);
1516 addr = chip->buffer_start;
1517 chip->streams[SNDRV_PCM_STREAM_PLAYBACK].buf = addr;
1518 addr += chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize;
1519 chip->streams[SNDRV_PCM_STREAM_CAPTURE].buf = addr;
1520 addr += chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize;
1521 if (chip->use_cache) {
1522 chip->all_coeff_buf = addr;
1524 chip->coeff_buf[SNDRV_PCM_STREAM_PLAYBACK] = addr;
1525 addr += NM_MAX_PLAYBACK_COEF_SIZE;
1526 chip->coeff_buf[SNDRV_PCM_STREAM_CAPTURE] = addr;
1529 /* Fixed setting. */
1530 chip->mixer_base = NM_MIXER_OFFSET;
1532 chip->coeffs_current = 0;
1534 snd_nm256_init_chip(chip);
1536 // pci_set_master(pci); /* needed? */
1538 snd_card_set_pm_callback(card, nm256_suspend, nm256_resume, chip);
1540 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1543 snd_card_set_dev(card, &pci->dev);
1549 snd_nm256_free(chip);
1554 struct nm256_quirk {
1555 unsigned short vendor;
1556 unsigned short device;
1560 enum { NM_BLACKLISTED, NM_RESET_WORKAROUND };
1562 static struct nm256_quirk nm256_quirks[] __devinitdata = {
1563 /* HP omnibook 4150 has cs4232 codec internally */
1564 { .vendor = 0x103c, .device = 0x0007, .type = NM_BLACKLISTED },
1566 { .vendor = 0x104d, .device = 0x8041, .type = NM_RESET_WORKAROUND },
1567 /* Dell Latitude LS */
1568 { .vendor = 0x1028, .device = 0x0080, .type = NM_RESET_WORKAROUND },
1569 { } /* terminator */
1573 static int __devinit snd_nm256_probe(struct pci_dev *pci,
1574 const struct pci_device_id *pci_id)
1580 unsigned int xbuffer_top;
1581 struct nm256_quirk *q;
1582 u16 subsystem_vendor, subsystem_device;
1584 if (dev >= SNDRV_CARDS)
1591 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1592 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1594 for (q = nm256_quirks; q->vendor; q++) {
1595 if (q->vendor == subsystem_vendor && q->device == subsystem_device) {
1597 case NM_BLACKLISTED:
1598 printk(KERN_INFO "nm256: The device is blacklisted. Loading stopped\n");
1600 case NM_RESET_WORKAROUND:
1601 reset_workaround[dev] = 1;
1607 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1611 switch (pci->device) {
1612 case PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO:
1613 strcpy(card->driver, "NM256AV");
1615 case PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO:
1616 strcpy(card->driver, "NM256ZX");
1618 case PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO:
1619 strcpy(card->driver, "NM256XL+");
1622 snd_printk("invalid device id 0x%x\n", pci->device);
1623 snd_card_free(card);
1628 xbuffer_top = 0x25a800; /* this avoids conflicts with XFree86 server */
1630 xbuffer_top = buffer_top[dev];
1632 if (playback_bufsize[dev] < 4)
1633 playback_bufsize[dev] = 4;
1634 if (playback_bufsize[dev] > 128)
1635 playback_bufsize[dev] = 128;
1636 if (capture_bufsize[dev] < 4)
1637 capture_bufsize[dev] = 4;
1638 if (capture_bufsize[dev] > 128)
1639 capture_bufsize[dev] = 128;
1640 if ((err = snd_nm256_create(card, pci,
1641 playback_bufsize[dev] * 1024, /* in bytes */
1642 capture_bufsize[dev] * 1024, /* in bytes */
1647 snd_card_free(card);
1651 if (reset_workaround[dev]) {
1652 snd_printdd(KERN_INFO "nm256: reset_workaround activated\n");
1653 chip->reset_workaround = 1;
1656 if ((err = snd_nm256_pcm(chip, 0)) < 0 ||
1657 (err = snd_nm256_mixer(chip)) < 0) {
1658 snd_card_free(card);
1662 sprintf(card->shortname, "NeoMagic %s", card->driver);
1663 sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %d",
1665 chip->buffer_addr, chip->cport_addr, chip->irq);
1667 if ((err = snd_card_register(card)) < 0) {
1668 snd_card_free(card);
1672 pci_set_drvdata(pci, card);
1677 static void __devexit snd_nm256_remove(struct pci_dev *pci)
1679 snd_card_free(pci_get_drvdata(pci));
1680 pci_set_drvdata(pci, NULL);
1684 static struct pci_driver driver = {
1685 .name = "NeoMagic 256",
1686 .id_table = snd_nm256_ids,
1687 .probe = snd_nm256_probe,
1688 .remove = __devexit_p(snd_nm256_remove),
1689 SND_PCI_PM_CALLBACKS
1693 static int __init alsa_card_nm256_init(void)
1695 return pci_register_driver(&driver);
1698 static void __exit alsa_card_nm256_exit(void)
1700 pci_unregister_driver(&driver);
1703 module_init(alsa_card_nm256_init)
1704 module_exit(alsa_card_nm256_exit)