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 <sound/core.h>
35 #include <sound/info.h>
36 #include <sound/control.h>
37 #include <sound/pcm.h>
38 #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 */
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);
98 /* The BIOS signature. */
99 #define NM_SIGNATURE 0x4e4d0000
100 /* Signature mask. */
101 #define NM_SIG_MASK 0xffff0000
103 /* Size of the second memory area. */
104 #define NM_PORT2_SIZE 4096
106 /* The base offset of the mixer in the second memory area. */
107 #define NM_MIXER_OFFSET 0x600
109 /* The maximum size of a coefficient entry. */
110 #define NM_MAX_PLAYBACK_COEF_SIZE 0x5000
111 #define NM_MAX_RECORD_COEF_SIZE 0x1260
113 /* The interrupt register. */
114 #define NM_INT_REG 0xa04
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)
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
130 * For the ZX. It uses the same interrupt register, but it holds 32
131 * bits instead of 16.
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))
139 /* The ZX's "mixer ready" status bit and location. */
140 #define NM2_MIXER_STATUS_OFFSET 0xa06
141 #define NM2_MIXER_READY_MASK 0x0800
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
148 /* The rate register is located 2 bytes from the start of the register area. */
149 #define NM_RATE_REG_OFFSET 2
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
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
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
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
172 /* coefficient buffer pointer */
173 #define NM_COEFF_START_OFFSET 0x1c
174 #define NM_COEFF_END_OFFSET 0x20
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)
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)
191 typedef struct snd_nm256 nm256_t;
192 typedef struct snd_nm256_stream nm256_stream_t;
193 #define chip_t nm256_t
195 struct snd_nm256_stream {
198 snd_pcm_substream_t *substream;
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 */
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 # */
218 unsigned long cport; /* control port */
219 struct resource *res_cport; /* its resource */
220 unsigned long cport_addr; /* physical address */
222 unsigned long buffer; /* buffer */
223 struct resource *res_buffer; /* its resource */
224 unsigned long buffer_addr; /* buffer phyiscal address */
226 u32 buffer_start; /* start offset from pci resource 0 */
227 u32 buffer_end; /* end offset */
228 u32 buffer_size; /* total buffer size */
230 u32 all_coeff_buf; /* coefficient buffer */
231 u32 coeff_buf[2]; /* coefficient buffer for each stream */
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 */
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 */
242 irqreturn_t (*interrupt)(int, void *, struct pt_regs *);
243 int badintrcount; /* counter to check bogus interrupts */
245 nm256_stream_t streams[2];
259 * include coefficient table
261 #include "nm256_coef.c"
268 #ifndef PCI_VENDOR_ID_NEOMAGIC
269 #define PCI_VENDOR_ID_NEOMEGIC 0x10c8
271 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO
272 #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
274 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO
275 #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
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},
285 MODULE_DEVICE_TABLE(pci, snd_nm256_ids);
293 snd_nm256_readb(nm256_t *chip, int offset)
295 return readb(chip->cport + offset);
299 snd_nm256_readw(nm256_t *chip, int offset)
301 return readw(chip->cport + offset);
305 snd_nm256_readl(nm256_t *chip, int offset)
307 return readl(chip->cport + offset);
311 snd_nm256_writeb(nm256_t *chip, int offset, u8 val)
313 writeb(val, chip->cport + offset);
317 snd_nm256_writew(nm256_t *chip, int offset, u16 val)
319 writew(val, chip->cport + offset);
323 snd_nm256_writel(nm256_t *chip, int offset, u32 val)
325 writel(val, chip->cport + offset);
329 snd_nm256_write_buffer(nm256_t *chip, void *src, int offset, int size)
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);
338 memcpy_toio((void *)chip->buffer + offset, src, size);
342 * coefficient handlers -- what a magic!
346 snd_nm256_get_start_offset(int which)
350 offset += coefficient_sizes[which];
355 snd_nm256_load_one_coefficient(nm256_t *chip, int stream, u32 port, int which)
357 u32 coeff_buf = chip->coeff_buf[stream];
358 u16 offset = snd_nm256_get_start_offset(which);
359 u16 size = coefficient_sizes[which];
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)
366 snd_nm256_writel(chip, port + 4, coeff_buf + size);
370 snd_nm256_load_coefficient(nm256_t *chip, int stream, int number)
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;
376 addr += (stream == SNDRV_PCM_STREAM_CAPTURE ? NM_RECORD_REG_OFFSET : NM_PLAYBACK_REG_OFFSET);
378 if (snd_nm256_readb(chip, poffset) & 1) {
379 snd_printd("NM256: Engine was enabled while loading coefficients!\n");
383 /* The recording engine uses coefficient values 8-15. */
385 if (stream == SNDRV_PCM_STREAM_CAPTURE)
388 if (! chip->use_cache) {
389 snd_nm256_load_one_coefficient(chip, stream, addr, number);
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;
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)
403 snd_nm256_writel(chip, addr + 4, base + end_offset);
408 /* The actual rates supported by the card. */
409 static unsigned int samplerates[8] = {
410 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
412 #define NUM_SAMPLERATES (sizeof(samplerates) / sizeof(samplerates[0]))
413 static snd_pcm_hw_constraint_list_t constraints_rates = {
414 .count = NUM_SAMPLERATES,
420 * return the index of the target rate
423 snd_nm256_fixed_rate(unsigned int rate)
426 for (i = 0; i < NUM_SAMPLERATES; i++) {
427 if (rate == samplerates[i])
435 * set sample rate and format
438 snd_nm256_set_format(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
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;
445 if (snd_pcm_format_width(runtime->format) == 16) {
446 ratebits |= NM_RATE_BITS_16;
449 if (runtime->channels > 1) {
450 ratebits |= NM_RATE_STEREO;
454 runtime->rate = samplerates[rate_index];
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,
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,
476 /* update the watermark (current period) */
477 static void snd_nm256_pcm_mark(nm256_t *chip, nm256_stream_t *s, int reg)
480 s->cur_period %= s->periods;
481 snd_nm256_writel(chip, reg, s->buf + s->cur_period * s->period_size);
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)
488 snd_nm256_playback_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
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);
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);
504 snd_nm256_capture_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
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);
512 /* Enable playback engine and interrupts. */
513 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG,
514 NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
517 /* Stop the play engine. */
519 snd_nm256_playback_stop(nm256_t *chip)
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);
529 snd_nm256_capture_stop(nm256_t *chip)
531 /* Disable recording engine. */
532 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG, 0);
536 snd_nm256_playback_trigger(snd_pcm_substream_t *substream, int cmd)
538 nm256_t *chip = snd_pcm_substream_chip(substream);
539 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
543 snd_assert(s != NULL, return -ENXIO);
545 spin_lock_irqsave(&chip->reg_lock, flags);
547 case SNDRV_PCM_TRIGGER_START:
548 case SNDRV_PCM_TRIGGER_RESUME:
550 snd_nm256_playback_start(chip, s, substream);
554 case SNDRV_PCM_TRIGGER_STOP:
555 case SNDRV_PCM_TRIGGER_SUSPEND:
557 snd_nm256_playback_stop(chip);
565 spin_unlock_irqrestore(&chip->reg_lock, flags);
570 snd_nm256_capture_trigger(snd_pcm_substream_t *substream, int cmd)
572 nm256_t *chip = snd_pcm_substream_chip(substream);
573 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
577 snd_assert(s != NULL, return -ENXIO);
579 spin_lock_irqsave(&chip->reg_lock, flags);
581 case SNDRV_PCM_TRIGGER_START:
582 case SNDRV_PCM_TRIGGER_RESUME:
584 snd_nm256_capture_start(chip, s, substream);
588 case SNDRV_PCM_TRIGGER_STOP:
589 case SNDRV_PCM_TRIGGER_SUSPEND:
591 snd_nm256_capture_stop(chip);
599 spin_unlock_irqrestore(&chip->reg_lock, flags);
605 * prepare playback/capture channel
607 static int snd_nm256_pcm_prepare(snd_pcm_substream_t *substream)
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;
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;
620 spin_lock_irqsave(&chip->reg_lock, flags);
622 snd_nm256_set_format(chip, s, substream);
623 spin_unlock_irqrestore(&chip->reg_lock, flags);
630 * get the current pointer
632 static snd_pcm_uframes_t
633 snd_nm256_playback_pointer(snd_pcm_substream_t * substream)
635 nm256_t *chip = snd_pcm_substream_chip(substream);
636 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
639 snd_assert(s, return 0);
640 curp = snd_nm256_readl(chip, NM_PBUFFER_CURRP) - (unsigned long)s->buf;
642 return bytes_to_frames(substream->runtime, curp);
645 static snd_pcm_uframes_t
646 snd_nm256_capture_pointer(snd_pcm_substream_t * substream)
648 nm256_t *chip = snd_pcm_substream_chip(substream);
649 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
652 snd_assert(s != NULL, return 0);
653 curp = snd_nm256_readl(chip, NM_RBUFFER_CURRP) - (unsigned long)s->buf;
655 return bytes_to_frames(substream->runtime, curp);
659 /* FIXME: I/O space is not accessible via pointers on all architectures */
662 * silence / copy for playback
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)
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);
679 snd_nm256_playback_copy(snd_pcm_substream_t *substream,
680 int channel, /* not used (interleaved data) */
681 snd_pcm_uframes_t pos,
683 snd_pcm_uframes_t count)
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))
698 snd_nm256_capture_copy(snd_pcm_substream_t *substream,
699 int channel, /* not used (interleaved data) */
700 snd_pcm_uframes_t pos,
702 snd_pcm_uframes_t count)
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))
713 #endif /* !__i386__ */
717 * update playback/capture watermarks
722 snd_nm256_playback_update(nm256_t *chip)
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);
737 snd_nm256_capture_update(nm256_t *chip)
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);
753 static snd_pcm_hardware_t snd_nm256_playback =
757 SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID|
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,
770 .buffer_bytes_max = 128 * 1024,
771 .period_bytes_min = 256,
772 .period_bytes_max = 128 * 1024,
775 static snd_pcm_hardware_t snd_nm256_capture =
779 SNDRV_PCM_INFO_MMAP|SNDRV_PCM_INFO_MMAP_VALID|
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,
792 .buffer_bytes_max = 128 * 1024,
793 .period_bytes_min = 256,
794 .period_bytes_max = 128 * 1024,
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)
801 /* area and addr are already set and unchanged */
802 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
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)
813 snd_pcm_runtime_t *runtime = substream->runtime;
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;
825 snd_pcm_set_sync(substream);
826 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
831 snd_nm256_playback_open(snd_pcm_substream_t *substream)
833 nm256_t *chip = snd_pcm_substream_chip(substream);
835 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK],
836 substream, &snd_nm256_playback);
841 snd_nm256_capture_open(snd_pcm_substream_t *substream)
843 nm256_t *chip = snd_pcm_substream_chip(substream);
845 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_CAPTURE],
846 substream, &snd_nm256_capture);
851 * close - we don't have to do special..
854 snd_nm256_playback_close(snd_pcm_substream_t *substream)
861 snd_nm256_capture_close(snd_pcm_substream_t *substream)
867 * create a pcm instance
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,
878 .copy = snd_nm256_playback_copy,
879 .silence = snd_nm256_playback_silence,
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,
892 .copy = snd_nm256_capture_copy,
897 snd_nm256_pcm(nm256_t *chip, int device)
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;
908 err = snd_pcm_new(chip->card, chip->card->driver, device,
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);
916 pcm->private_data = chip;
925 * Initialize the hardware.
928 snd_nm256_init_chip(nm256_t *chip)
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);
937 //snd_nm256_playback_stop(chip);
938 //snd_nm256_capture_stop(chip);
939 spin_unlock_irqrestore(&chip->reg_lock, flags);
944 snd_nm256_intr_check(nm256_t *chip)
946 if (chip->badintrcount++ > 1000) {
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
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.
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;
968 * Handle a potential interrupt for the device referred to by DEV_ID.
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.)
977 snd_nm256_interrupt(int irq, void *dev_id, struct pt_regs *dummy)
979 nm256_t *chip = snd_magic_cast(nm256_t, dev_id, return IRQ_NONE);
983 status = snd_nm256_readw(chip, NM_INT_REG);
987 snd_nm256_intr_check(chip);
991 chip->badintrcount = 0;
993 /* Rather boring; check for individual interrupts and process them. */
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);
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);
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);
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);
1025 /* Unknown interrupt. */
1027 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1030 NM_ACK_INT(chip, status);
1033 spin_unlock(&chip->reg_lock);
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
1044 snd_nm256_interrupt_zx(int irq, void *dev_id, struct pt_regs *dummy)
1046 nm256_t *chip = snd_magic_cast(nm256_t, dev_id, return IRQ_NONE);
1050 status = snd_nm256_readl(chip, NM_INT_REG);
1054 snd_nm256_intr_check(chip);
1058 chip->badintrcount = 0;
1060 /* Rather boring; check for individual interrupts and process them. */
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);
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);
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);
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);
1091 /* Unknown interrupt. */
1093 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1096 NM2_ACK_INT(chip, status);
1099 spin_unlock(&chip->reg_lock);
1108 * Waits for the mixer to become ready to be written; returns a zero value
1112 snd_nm256_ac97_ready(nm256_t *chip)
1118 testaddr = chip->mixer_status_offset;
1119 testb = chip->mixer_status_mask;
1122 * Loop around waiting for the mixer to become ready.
1124 while (timeout-- > 0) {
1125 if ((snd_nm256_readw(chip, testaddr) & testb) == 0)
1134 static unsigned short
1135 snd_nm256_ac97_read(ac97_t *ac97, unsigned short reg)
1137 nm256_t *chip = snd_magic_cast(nm256_t, ac97->private_data, return -ENXIO);
1143 if (! snd_nm256_ac97_ready(chip))
1145 res = snd_nm256_readw(chip, chip->mixer_base + reg);
1146 /* Magic delay. Bleah yucky. */
1154 snd_nm256_ac97_write(ac97_t *ac97,
1155 unsigned short reg, unsigned short val)
1157 nm256_t *chip = snd_magic_cast(nm256_t, ac97->private_data, return);
1161 base = chip->mixer_base;
1163 snd_nm256_ac97_ready(chip);
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))
1172 snd_printd("nm256: ac97 codec not ready..\n");
1175 /* initialize the ac97 into a known state */
1177 snd_nm256_ac97_reset(ac97_t *ac97)
1179 nm256_t *chip = snd_magic_cast(nm256_t, ac97->private_data, return);
1180 unsigned long flags;
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);
1189 snd_nm256_writeb(chip, 0x6cc, 0x80);
1190 snd_nm256_writeb(chip, 0x6cc, 0x0);
1191 spin_unlock_irqrestore(&chip->reg_lock, flags);
1194 /* create an ac97 mixer interface */
1195 static int __devinit
1196 snd_nm256_mixer(nm256_t *chip)
1198 ac97_bus_t bus, *pbus;
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,
1208 AC97_VENDOR_ID1, AC97_VENDOR_ID2,
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)
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);
1228 if (! (chip->ac97->id & (0xf0000000))) {
1229 /* looks like an invalid id */
1230 sprintf(chip->card->mixername, "%s AC97", chip->card->driver);
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
1241 static int __devinit
1242 snd_nm256_peek_for_sig(nm256_t *chip)
1244 /* The signature is located 1K below the end of video RAM. */
1246 /* Default buffer end is 5120 bytes below the top of RAM. */
1247 unsigned long pointer_found = chip->buffer_end - 0x1400;
1250 temp = (unsigned long) ioremap_nocache(chip->buffer_addr + chip->buffer_end - 0x400, 16);
1252 snd_printk("Unable to scan for card signature in video RAM\n");
1257 if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1258 u32 pointer = readl(temp + 4);
1261 * If it's obviously invalid, don't use it
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);
1270 pointer_found = pointer;
1271 printk(KERN_INFO "nm256: found card signature in video RAM: 0x%x\n", pointer);
1275 iounmap((void *)temp);
1276 chip->buffer_end = pointer_found;
1283 * APM event handler, so the card is properly reinitialized after a power
1286 static void nm256_suspend(nm256_t *chip)
1288 snd_card_t *card = chip->card;
1290 if (card->power_state == SNDRV_CTL_POWER_D3hot)
1293 snd_pcm_suspend_all(chip->pcm);
1294 chip->coeffs_current = 0;
1295 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1298 static void nm256_resume(nm256_t *chip)
1300 snd_card_t *card = chip->card;
1302 if (card->power_state == SNDRV_CTL_POWER_D0)
1305 /* Perform a full reset on the hardware */
1306 pci_enable_device(chip->pci);
1307 snd_nm256_init_chip(chip);
1310 snd_ac97_resume(chip->ac97);
1312 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1315 static int snd_nm256_suspend(struct pci_dev *dev, u32 state)
1317 nm256_t *chip = snd_magic_cast(nm256_t, pci_get_drvdata(dev), return -ENXIO);
1318 nm256_suspend(chip);
1321 static int snd_nm256_resume(struct pci_dev *dev)
1323 nm256_t *chip = snd_magic_cast(nm256_t, pci_get_drvdata(dev), return -ENXIO);
1329 static int snd_nm256_set_power_state(snd_card_t *card, unsigned int power_state)
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:
1338 case SNDRV_CTL_POWER_D3hot:
1339 case SNDRV_CTL_POWER_D3cold:
1340 nm256_suspend(chip);
1348 #endif /* CONFIG_PM */
1350 static int snd_nm256_free(nm256_t *chip)
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);
1358 synchronize_irq(chip->irq);
1361 iounmap((void *) chip->cport);
1363 iounmap((void *) chip->buffer);
1364 if (chip->res_cport) {
1365 release_resource(chip->res_cport);
1366 kfree_nocheck(chip->res_cport);
1368 if (chip->res_buffer) {
1369 release_resource(chip->res_buffer);
1370 kfree_nocheck(chip->res_buffer);
1373 free_irq(chip->irq, (void*)chip);
1375 snd_magic_kfree(chip);
1379 static int snd_nm256_dev_free(snd_device_t *device)
1381 nm256_t *chip = snd_magic_cast(nm256_t, device->device_data, return -ENXIO);
1382 return snd_nm256_free(chip);
1385 static int __devinit
1386 snd_nm256_create(snd_card_t *card, struct pci_dev *pci,
1387 int play_bufsize, int capt_bufsize,
1395 static snd_device_ops_t ops = {
1396 .dev_free = snd_nm256_dev_free,
1399 u16 subsystem_vendor, subsystem_device;
1403 chip = snd_magic_kcalloc(nm256_t, 0, GFP_KERNEL);
1409 chip->use_cache = usecache;
1410 spin_lock_init(&chip->reg_lock);
1413 chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize = play_bufsize;
1414 chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize = capt_bufsize;
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).
1423 chip->buffer_addr = pci_resource_start(pci, 0);
1424 chip->cport_addr = pci_resource_start(pci, 1);
1426 /* Init the memory port info. */
1427 /* remap control port (#2) */
1428 chip->res_cport = request_mem_region(chip->cport_addr, NM_PORT2_SIZE,
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);
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);
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) {
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");
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;
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;
1465 chip->buffer_end = 4096 * 1024;
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;
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;
1476 chip->buffer_size += NM_MAX_PLAYBACK_COEF_SIZE + NM_MAX_RECORD_COEF_SIZE;
1478 if (buffertop >= chip->buffer_size && buffertop < chip->buffer_end)
1479 chip->buffer_end = buffertop;
1481 /* get buffer end pointer from signature */
1482 if ((err = snd_nm256_peek_for_sig(chip)) < 0)
1486 chip->buffer_start = chip->buffer_end - chip->buffer_size;
1487 chip->buffer_addr += chip->buffer_start;
1489 printk(KERN_INFO "nm256: Mapping port 1 from 0x%x - 0x%x\n",
1490 chip->buffer_start, chip->buffer_end);
1492 chip->res_buffer = request_mem_region(chip->buffer_addr,
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);
1501 chip->buffer = (unsigned long) ioremap_nocache(chip->buffer_addr, chip->buffer_size);
1502 if (chip->buffer == 0) {
1504 snd_printk("unable to map ring buffer at %lx\n", chip->buffer_addr);
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;
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;
1522 /* acquire interrupt */
1523 if (request_irq(pci->irq, chip->interrupt, SA_INTERRUPT|SA_SHIRQ,
1524 card->driver, (void*)chip)) {
1526 snd_printk("unable to grab IRQ %d\n", pci->irq);
1529 chip->irq = pci->irq;
1531 /* Fixed setting. */
1532 chip->mixer_base = NM_MIXER_OFFSET;
1534 chip->coeffs_current = 0;
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;
1545 snd_nm256_init_chip(chip);
1547 if ((err = snd_nm256_pcm(chip, 0)) < 0)
1550 if ((err = snd_nm256_mixer(chip)) < 0)
1553 // pci_set_master(pci); /* needed? */
1556 card->set_power_state = snd_nm256_set_power_state;
1557 card->power_state_private_data = chip;
1560 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1563 snd_card_set_dev(card, &pci->dev);
1569 snd_nm256_free(chip);
1574 static int __devinit snd_nm256_probe(struct pci_dev *pci,
1575 const struct pci_device_id *pci_id)
1581 unsigned int xbuffer_top;
1583 if ((err = pci_enable_device(pci)) < 0)
1586 if (dev >= SNDRV_CARDS)
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");
1605 snd_printk("invalid device id 0x%x\n", pci->device);
1606 snd_card_free(card);
1611 xbuffer_top = 0x25a800; /* this avoids conflicts with XFree86 server */
1613 xbuffer_top = buffer_top[dev];
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 */
1630 snd_card_free(card);
1634 sprintf(card->shortname, "NeoMagic %s", card->driver);
1635 sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %d",
1637 chip->buffer_addr, chip->cport_addr, chip->irq);
1639 if ((err = snd_card_register(card)) < 0) {
1640 snd_card_free(card);
1644 pci_set_drvdata(pci, chip);
1649 static void __devexit snd_nm256_remove(struct pci_dev *pci)
1651 nm256_t *chip = snd_magic_cast(nm256_t, pci_get_drvdata(pci), return);
1653 snd_card_free(chip->card);
1654 pci_set_drvdata(pci, NULL);
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),
1664 .suspend = snd_nm256_suspend,
1665 .resume = snd_nm256_resume,
1670 static int __init alsa_card_nm256_init(void)
1673 if ((err = pci_module_init(&driver)) < 0) {
1675 printk(KERN_ERR "NeoMagic 256 audio soundchip not found or device busy\n");
1682 static void __exit alsa_card_nm256_exit(void)
1684 pci_unregister_driver(&driver);
1687 module_init(alsa_card_nm256_init)
1688 module_exit(alsa_card_nm256_exit)
1692 /* format is: snd-nm256=enable,index,id,
1693 playback_bufsize,capture_bufsize,
1694 force_ac97,buffer_top,use_cache */
1696 static int __init alsa_card_nm256_setup(char *str)
1698 static unsigned __initdata nr_dev = 0;
1700 if (nr_dev >= SNDRV_CARDS)
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);
1714 __setup("snd-nm256=", alsa_card_nm256_setup);
1716 #endif /* ifndef MODULE */