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"
262 static struct pci_device_id snd_nm256_ids[] = {
263 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
264 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
265 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
269 MODULE_DEVICE_TABLE(pci, snd_nm256_ids);
277 snd_nm256_readb(nm256_t *chip, int offset)
279 return readb(chip->cport + offset);
283 snd_nm256_readw(nm256_t *chip, int offset)
285 return readw(chip->cport + offset);
289 snd_nm256_readl(nm256_t *chip, int offset)
291 return readl(chip->cport + offset);
295 snd_nm256_writeb(nm256_t *chip, int offset, u8 val)
297 writeb(val, chip->cport + offset);
301 snd_nm256_writew(nm256_t *chip, int offset, u16 val)
303 writew(val, chip->cport + offset);
307 snd_nm256_writel(nm256_t *chip, int offset, u32 val)
309 writel(val, chip->cport + offset);
313 snd_nm256_write_buffer(nm256_t *chip, void *src, int offset, int size)
315 offset -= chip->buffer_start;
316 #ifdef SNDRV_CONFIG_DEBUG
317 if (offset < 0 || offset >= chip->buffer_size) {
318 snd_printk("write_buffer invalid offset = %d size = %d\n", offset, size);
322 memcpy_toio(chip->buffer + offset, src, size);
326 * coefficient handlers -- what a magic!
330 snd_nm256_get_start_offset(int which)
334 offset += coefficient_sizes[which];
339 snd_nm256_load_one_coefficient(nm256_t *chip, int stream, u32 port, int which)
341 u32 coeff_buf = chip->coeff_buf[stream];
342 u16 offset = snd_nm256_get_start_offset(which);
343 u16 size = coefficient_sizes[which];
345 snd_nm256_write_buffer(chip, coefficients + offset, coeff_buf, size);
346 snd_nm256_writel(chip, port, coeff_buf);
347 /* ??? Record seems to behave differently than playback. */
348 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
350 snd_nm256_writel(chip, port + 4, coeff_buf + size);
354 snd_nm256_load_coefficient(nm256_t *chip, int stream, int number)
356 /* The enable register for the specified engine. */
357 u32 poffset = (stream == SNDRV_PCM_STREAM_CAPTURE ? NM_RECORD_ENABLE_REG : NM_PLAYBACK_ENABLE_REG);
358 u32 addr = NM_COEFF_START_OFFSET;
360 addr += (stream == SNDRV_PCM_STREAM_CAPTURE ? NM_RECORD_REG_OFFSET : NM_PLAYBACK_REG_OFFSET);
362 if (snd_nm256_readb(chip, poffset) & 1) {
363 snd_printd("NM256: Engine was enabled while loading coefficients!\n");
367 /* The recording engine uses coefficient values 8-15. */
369 if (stream == SNDRV_PCM_STREAM_CAPTURE)
372 if (! chip->use_cache) {
373 snd_nm256_load_one_coefficient(chip, stream, addr, number);
376 if (! chip->coeffs_current) {
377 snd_nm256_write_buffer(chip, coefficients, chip->all_coeff_buf,
378 NM_TOTAL_COEFF_COUNT * 4);
379 chip->coeffs_current = 1;
381 u32 base = chip->all_coeff_buf;
382 u32 offset = snd_nm256_get_start_offset(number);
383 u32 end_offset = offset + coefficient_sizes[number];
384 snd_nm256_writel(chip, addr, base + offset);
385 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
387 snd_nm256_writel(chip, addr + 4, base + end_offset);
392 /* The actual rates supported by the card. */
393 static unsigned int samplerates[8] = {
394 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
396 static snd_pcm_hw_constraint_list_t constraints_rates = {
397 .count = ARRAY_SIZE(samplerates),
403 * return the index of the target rate
406 snd_nm256_fixed_rate(unsigned int rate)
409 for (i = 0; i < ARRAY_SIZE(samplerates); i++) {
410 if (rate == samplerates[i])
418 * set sample rate and format
421 snd_nm256_set_format(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
423 snd_pcm_runtime_t *runtime = substream->runtime;
424 int rate_index = snd_nm256_fixed_rate(runtime->rate);
425 unsigned char ratebits = (rate_index << 4) & NM_RATE_MASK;
428 if (snd_pcm_format_width(runtime->format) == 16) {
429 ratebits |= NM_RATE_BITS_16;
432 if (runtime->channels > 1) {
433 ratebits |= NM_RATE_STEREO;
437 runtime->rate = samplerates[rate_index];
439 switch (substream->stream) {
440 case SNDRV_PCM_STREAM_PLAYBACK:
441 snd_nm256_load_coefficient(chip, 0, rate_index); /* 0 = playback */
442 snd_nm256_writeb(chip,
443 NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET,
446 case SNDRV_PCM_STREAM_CAPTURE:
447 snd_nm256_load_coefficient(chip, 1, rate_index); /* 1 = record */
448 snd_nm256_writeb(chip,
449 NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET,
455 /* acquire interrupt */
456 static int snd_nm256_acquire_irq(nm256_t *chip)
458 down(&chip->irq_mutex);
460 if (request_irq(chip->pci->irq, chip->interrupt, SA_INTERRUPT|SA_SHIRQ,
461 chip->card->driver, (void*)chip)) {
462 snd_printk("unable to grab IRQ %d\n", chip->pci->irq);
463 up(&chip->irq_mutex);
466 chip->irq = chip->pci->irq;
469 up(&chip->irq_mutex);
473 /* release interrupt */
474 static void snd_nm256_release_irq(nm256_t *chip)
476 down(&chip->irq_mutex);
477 if (chip->irq_acks > 0)
479 if (chip->irq_acks == 0 && chip->irq >= 0) {
480 free_irq(chip->irq, (void*)chip);
483 up(&chip->irq_mutex);
490 /* update the watermark (current period) */
491 static void snd_nm256_pcm_mark(nm256_t *chip, nm256_stream_t *s, int reg)
494 s->cur_period %= s->periods;
495 snd_nm256_writel(chip, reg, s->buf + s->cur_period * s->period_size);
498 #define snd_nm256_playback_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_PBUFFER_WMARK)
499 #define snd_nm256_capture_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_RBUFFER_WMARK)
502 snd_nm256_playback_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
504 /* program buffer pointers */
505 snd_nm256_writel(chip, NM_PBUFFER_START, s->buf);
506 snd_nm256_writel(chip, NM_PBUFFER_END, s->buf + s->dma_size - (1 << s->shift));
507 snd_nm256_writel(chip, NM_PBUFFER_CURRP, s->buf);
508 snd_nm256_playback_mark(chip, s);
510 /* Enable playback engine and interrupts. */
511 snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG,
512 NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);
513 /* Enable both channels. */
514 snd_nm256_writew(chip, NM_AUDIO_MUTE_REG, 0x0);
518 snd_nm256_capture_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
520 /* program buffer pointers */
521 snd_nm256_writel(chip, NM_RBUFFER_START, s->buf);
522 snd_nm256_writel(chip, NM_RBUFFER_END, s->buf + s->dma_size);
523 snd_nm256_writel(chip, NM_RBUFFER_CURRP, s->buf);
524 snd_nm256_capture_mark(chip, s);
526 /* Enable playback engine and interrupts. */
527 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG,
528 NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
531 /* Stop the play engine. */
533 snd_nm256_playback_stop(nm256_t *chip)
535 /* Shut off sound from both channels. */
536 snd_nm256_writew(chip, NM_AUDIO_MUTE_REG,
537 NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT);
538 /* Disable play engine. */
539 snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG, 0);
543 snd_nm256_capture_stop(nm256_t *chip)
545 /* Disable recording engine. */
546 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG, 0);
550 snd_nm256_playback_trigger(snd_pcm_substream_t *substream, int cmd)
552 nm256_t *chip = snd_pcm_substream_chip(substream);
553 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
556 snd_assert(s != NULL, return -ENXIO);
558 spin_lock(&chip->reg_lock);
560 case SNDRV_PCM_TRIGGER_RESUME:
563 case SNDRV_PCM_TRIGGER_START:
565 snd_nm256_playback_start(chip, s, substream);
569 case SNDRV_PCM_TRIGGER_SUSPEND:
572 case SNDRV_PCM_TRIGGER_STOP:
574 snd_nm256_playback_stop(chip);
582 spin_unlock(&chip->reg_lock);
587 snd_nm256_capture_trigger(snd_pcm_substream_t *substream, int cmd)
589 nm256_t *chip = snd_pcm_substream_chip(substream);
590 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
593 snd_assert(s != NULL, return -ENXIO);
595 spin_lock(&chip->reg_lock);
597 case SNDRV_PCM_TRIGGER_START:
598 case SNDRV_PCM_TRIGGER_RESUME:
600 snd_nm256_capture_start(chip, s, substream);
604 case SNDRV_PCM_TRIGGER_STOP:
605 case SNDRV_PCM_TRIGGER_SUSPEND:
607 snd_nm256_capture_stop(chip);
615 spin_unlock(&chip->reg_lock);
621 * prepare playback/capture channel
623 static int snd_nm256_pcm_prepare(snd_pcm_substream_t *substream)
625 nm256_t *chip = snd_pcm_substream_chip(substream);
626 snd_pcm_runtime_t *runtime = substream->runtime;
627 nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
629 snd_assert(s, return -ENXIO);
630 s->dma_size = frames_to_bytes(runtime, substream->runtime->buffer_size);
631 s->period_size = frames_to_bytes(runtime, substream->runtime->period_size);
632 s->periods = substream->runtime->periods;
635 spin_lock_irq(&chip->reg_lock);
637 snd_nm256_set_format(chip, s, substream);
638 spin_unlock_irq(&chip->reg_lock);
645 * get the current pointer
647 static snd_pcm_uframes_t
648 snd_nm256_playback_pointer(snd_pcm_substream_t * substream)
650 nm256_t *chip = snd_pcm_substream_chip(substream);
651 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
654 snd_assert(s, return 0);
655 curp = snd_nm256_readl(chip, NM_PBUFFER_CURRP) - (unsigned long)s->buf;
657 return bytes_to_frames(substream->runtime, curp);
660 static snd_pcm_uframes_t
661 snd_nm256_capture_pointer(snd_pcm_substream_t * substream)
663 nm256_t *chip = snd_pcm_substream_chip(substream);
664 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
667 snd_assert(s != NULL, return 0);
668 curp = snd_nm256_readl(chip, NM_RBUFFER_CURRP) - (unsigned long)s->buf;
670 return bytes_to_frames(substream->runtime, curp);
673 /* Remapped I/O space can be accessible as pointer on i386 */
674 /* This might be changed in the future */
677 * silence / copy for playback
680 snd_nm256_playback_silence(snd_pcm_substream_t *substream,
681 int channel, /* not used (interleaved data) */
682 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 memset_io(s->bufptr + pos, 0, count);
694 snd_nm256_playback_copy(snd_pcm_substream_t *substream,
695 int channel, /* not used (interleaved data) */
696 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 if (copy_from_user_toio(s->bufptr + pos, src, count))
713 snd_nm256_capture_copy(snd_pcm_substream_t *substream,
714 int channel, /* not used (interleaved data) */
715 snd_pcm_uframes_t pos,
717 snd_pcm_uframes_t count)
719 snd_pcm_runtime_t *runtime = substream->runtime;
720 nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
721 count = frames_to_bytes(runtime, count);
722 pos = frames_to_bytes(runtime, pos);
723 if (copy_to_user_fromio(dst, s->bufptr + pos, count))
728 #endif /* !__i386__ */
732 * update playback/capture watermarks
737 snd_nm256_playback_update(nm256_t *chip)
741 s = &chip->streams[SNDRV_PCM_STREAM_PLAYBACK];
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_playback_mark(chip, s);
752 snd_nm256_capture_update(nm256_t *chip)
756 s = &chip->streams[SNDRV_PCM_STREAM_CAPTURE];
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_capture_mark(chip, s);
768 static snd_pcm_hardware_t snd_nm256_playback =
770 .info = SNDRV_PCM_INFO_MMAP_IOMEM |SNDRV_PCM_INFO_MMAP_VALID |
771 SNDRV_PCM_INFO_INTERLEAVED |
772 /*SNDRV_PCM_INFO_PAUSE |*/
773 SNDRV_PCM_INFO_RESUME,
774 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
775 .rates = SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000,
782 .buffer_bytes_max = 128 * 1024,
783 .period_bytes_min = 256,
784 .period_bytes_max = 128 * 1024,
787 static snd_pcm_hardware_t snd_nm256_capture =
789 .info = SNDRV_PCM_INFO_MMAP_IOMEM | SNDRV_PCM_INFO_MMAP_VALID |
790 SNDRV_PCM_INFO_INTERLEAVED |
791 /*SNDRV_PCM_INFO_PAUSE |*/
792 SNDRV_PCM_INFO_RESUME,
793 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
794 .rates = SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000,
801 .buffer_bytes_max = 128 * 1024,
802 .period_bytes_min = 256,
803 .period_bytes_max = 128 * 1024,
807 /* set dma transfer size */
808 static int snd_nm256_pcm_hw_params(snd_pcm_substream_t *substream, snd_pcm_hw_params_t *hw_params)
810 /* area and addr are already set and unchanged */
811 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
818 static void snd_nm256_setup_stream(nm256_t *chip, nm256_stream_t *s,
819 snd_pcm_substream_t *substream,
820 snd_pcm_hardware_t *hw_ptr)
822 snd_pcm_runtime_t *runtime = substream->runtime;
825 runtime->hw = *hw_ptr;
826 runtime->hw.buffer_bytes_max = s->bufsize;
827 runtime->hw.period_bytes_max = s->bufsize / 2;
828 runtime->dma_area = (void __force *) s->bufptr;
829 runtime->dma_addr = s->bufptr_addr;
830 runtime->dma_bytes = s->bufsize;
831 runtime->private_data = s;
832 s->substream = substream;
834 snd_pcm_set_sync(substream);
835 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
840 snd_nm256_playback_open(snd_pcm_substream_t *substream)
842 nm256_t *chip = snd_pcm_substream_chip(substream);
844 if (snd_nm256_acquire_irq(chip) < 0)
846 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK],
847 substream, &snd_nm256_playback);
852 snd_nm256_capture_open(snd_pcm_substream_t *substream)
854 nm256_t *chip = snd_pcm_substream_chip(substream);
856 if (snd_nm256_acquire_irq(chip) < 0)
858 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_CAPTURE],
859 substream, &snd_nm256_capture);
864 * close - we don't have to do special..
867 snd_nm256_playback_close(snd_pcm_substream_t *substream)
869 nm256_t *chip = snd_pcm_substream_chip(substream);
871 snd_nm256_release_irq(chip);
877 snd_nm256_capture_close(snd_pcm_substream_t *substream)
879 nm256_t *chip = snd_pcm_substream_chip(substream);
881 snd_nm256_release_irq(chip);
886 * create a pcm instance
888 static snd_pcm_ops_t snd_nm256_playback_ops = {
889 .open = snd_nm256_playback_open,
890 .close = snd_nm256_playback_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_playback_trigger,
895 .pointer = snd_nm256_playback_pointer,
897 .copy = snd_nm256_playback_copy,
898 .silence = snd_nm256_playback_silence,
900 .mmap = snd_pcm_lib_mmap_iomem,
903 static snd_pcm_ops_t snd_nm256_capture_ops = {
904 .open = snd_nm256_capture_open,
905 .close = snd_nm256_capture_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_capture_trigger,
910 .pointer = snd_nm256_capture_pointer,
912 .copy = snd_nm256_capture_copy,
914 .mmap = snd_pcm_lib_mmap_iomem,
918 snd_nm256_pcm(nm256_t *chip, int device)
923 for (i = 0; i < 2; i++) {
924 nm256_stream_t *s = &chip->streams[i];
925 s->bufptr = chip->buffer + (s->buf - chip->buffer_start);
926 s->bufptr_addr = chip->buffer_addr + (s->buf - chip->buffer_start);
929 err = snd_pcm_new(chip->card, chip->card->driver, device,
934 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_nm256_playback_ops);
935 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_nm256_capture_ops);
937 pcm->private_data = chip;
946 * Initialize the hardware.
949 snd_nm256_init_chip(nm256_t *chip)
951 /* Reset everything. */
952 snd_nm256_writeb(chip, 0x0, 0x11);
953 snd_nm256_writew(chip, 0x214, 0);
955 //snd_nm256_playback_stop(chip);
956 //snd_nm256_capture_stop(chip);
961 snd_nm256_intr_check(nm256_t *chip)
963 if (chip->badintrcount++ > 1000) {
965 * I'm not sure if the best thing is to stop the card from
966 * playing or just release the interrupt (after all, we're in
967 * a bad situation, so doing fancy stuff may not be such a good
970 * I worry about the card engine continuing to play noise
971 * over and over, however--that could become a very
972 * obnoxious problem. And we know that when this usually
973 * happens things are fairly safe, it just means the user's
974 * inserted a PCMCIA card and someone's spamming us with IRQ 9s.
976 if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
977 snd_nm256_playback_stop(chip);
978 if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
979 snd_nm256_capture_stop(chip);
980 chip->badintrcount = 0;
987 * Handle a potential interrupt for the device referred to by DEV_ID.
989 * I don't like the cut-n-paste job here either between the two routines,
990 * but there are sufficient differences between the two interrupt handlers
991 * that parameterizing it isn't all that great either. (Could use a macro,
992 * I suppose...yucky bleah.)
996 snd_nm256_interrupt(int irq, void *dev_id, struct pt_regs *dummy)
998 nm256_t *chip = dev_id;
1002 status = snd_nm256_readw(chip, NM_INT_REG);
1006 return snd_nm256_intr_check(chip);
1008 chip->badintrcount = 0;
1010 /* Rather boring; check for individual interrupts and process them. */
1012 spin_lock(&chip->reg_lock);
1013 if (status & NM_PLAYBACK_INT) {
1014 status &= ~NM_PLAYBACK_INT;
1015 NM_ACK_INT(chip, NM_PLAYBACK_INT);
1016 snd_nm256_playback_update(chip);
1019 if (status & NM_RECORD_INT) {
1020 status &= ~NM_RECORD_INT;
1021 NM_ACK_INT(chip, NM_RECORD_INT);
1022 snd_nm256_capture_update(chip);
1025 if (status & NM_MISC_INT_1) {
1026 status &= ~NM_MISC_INT_1;
1027 NM_ACK_INT(chip, NM_MISC_INT_1);
1028 snd_printd("NM256: Got misc interrupt #1\n");
1029 snd_nm256_writew(chip, NM_INT_REG, 0x8000);
1030 cbyte = snd_nm256_readb(chip, 0x400);
1031 snd_nm256_writeb(chip, 0x400, cbyte | 2);
1034 if (status & NM_MISC_INT_2) {
1035 status &= ~NM_MISC_INT_2;
1036 NM_ACK_INT(chip, NM_MISC_INT_2);
1037 snd_printd("NM256: Got misc interrupt #2\n");
1038 cbyte = snd_nm256_readb(chip, 0x400);
1039 snd_nm256_writeb(chip, 0x400, cbyte & ~2);
1042 /* Unknown interrupt. */
1044 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1047 NM_ACK_INT(chip, status);
1050 spin_unlock(&chip->reg_lock);
1055 * Handle a potential interrupt for the device referred to by DEV_ID.
1056 * This handler is for the 256ZX, and is very similar to the non-ZX
1061 snd_nm256_interrupt_zx(int irq, void *dev_id, struct pt_regs *dummy)
1063 nm256_t *chip = dev_id;
1067 status = snd_nm256_readl(chip, NM_INT_REG);
1071 return snd_nm256_intr_check(chip);
1073 chip->badintrcount = 0;
1075 /* Rather boring; check for individual interrupts and process them. */
1077 spin_lock(&chip->reg_lock);
1078 if (status & NM2_PLAYBACK_INT) {
1079 status &= ~NM2_PLAYBACK_INT;
1080 NM2_ACK_INT(chip, NM2_PLAYBACK_INT);
1081 snd_nm256_playback_update(chip);
1084 if (status & NM2_RECORD_INT) {
1085 status &= ~NM2_RECORD_INT;
1086 NM2_ACK_INT(chip, NM2_RECORD_INT);
1087 snd_nm256_capture_update(chip);
1090 if (status & NM2_MISC_INT_1) {
1091 status &= ~NM2_MISC_INT_1;
1092 NM2_ACK_INT(chip, NM2_MISC_INT_1);
1093 snd_printd("NM256: Got misc interrupt #1\n");
1094 cbyte = snd_nm256_readb(chip, 0x400);
1095 snd_nm256_writeb(chip, 0x400, cbyte | 2);
1098 if (status & NM2_MISC_INT_2) {
1099 status &= ~NM2_MISC_INT_2;
1100 NM2_ACK_INT(chip, NM2_MISC_INT_2);
1101 snd_printd("NM256: Got misc interrupt #2\n");
1102 cbyte = snd_nm256_readb(chip, 0x400);
1103 snd_nm256_writeb(chip, 0x400, cbyte & ~2);
1106 /* Unknown interrupt. */
1108 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1111 NM2_ACK_INT(chip, status);
1114 spin_unlock(&chip->reg_lock);
1123 * Waits for the mixer to become ready to be written; returns a zero value
1127 snd_nm256_ac97_ready(nm256_t *chip)
1133 testaddr = chip->mixer_status_offset;
1134 testb = chip->mixer_status_mask;
1137 * Loop around waiting for the mixer to become ready.
1139 while (timeout-- > 0) {
1140 if ((snd_nm256_readw(chip, testaddr) & testb) == 0)
1149 static unsigned short
1150 snd_nm256_ac97_read(ac97_t *ac97, unsigned short reg)
1152 nm256_t *chip = ac97->private_data;
1158 if (! snd_nm256_ac97_ready(chip))
1160 res = snd_nm256_readw(chip, chip->mixer_base + reg);
1161 /* Magic delay. Bleah yucky. */
1169 snd_nm256_ac97_write(ac97_t *ac97,
1170 unsigned short reg, unsigned short val)
1172 nm256_t *chip = ac97->private_data;
1176 base = chip->mixer_base;
1178 snd_nm256_ac97_ready(chip);
1180 /* Wait for the write to take, too. */
1181 while (tries-- > 0) {
1182 snd_nm256_writew(chip, base + reg, val);
1183 msleep(1); /* a little delay here seems better.. */
1184 if (snd_nm256_ac97_ready(chip))
1187 snd_printd("nm256: ac97 codec not ready..\n");
1190 /* initialize the ac97 into a known state */
1192 snd_nm256_ac97_reset(ac97_t *ac97)
1194 nm256_t *chip = ac97->private_data;
1196 /* Reset the mixer. 'Tis magic! */
1197 snd_nm256_writeb(chip, 0x6c0, 1);
1198 if (! chip->reset_workaround) {
1199 /* Dell latitude LS will lock up by this */
1200 snd_nm256_writeb(chip, 0x6cc, 0x87);
1202 snd_nm256_writeb(chip, 0x6cc, 0x80);
1203 snd_nm256_writeb(chip, 0x6cc, 0x0);
1206 /* create an ac97 mixer interface */
1207 static int __devinit
1208 snd_nm256_mixer(nm256_t *chip)
1211 ac97_template_t ac97;
1213 static ac97_bus_ops_t ops = {
1214 .reset = snd_nm256_ac97_reset,
1215 .write = snd_nm256_ac97_write,
1216 .read = snd_nm256_ac97_read,
1218 /* looks like nm256 hangs up when unexpected registers are touched... */
1219 static int mixer_regs[] = {
1220 AC97_MASTER, AC97_HEADPHONE, AC97_MASTER_MONO,
1221 AC97_PC_BEEP, AC97_PHONE, AC97_MIC, AC97_LINE, AC97_CD,
1222 AC97_VIDEO, AC97_AUX, AC97_PCM, AC97_REC_SEL,
1223 AC97_REC_GAIN, AC97_GENERAL_PURPOSE, AC97_3D_CONTROL,
1224 /*AC97_EXTENDED_ID,*/
1225 AC97_VENDOR_ID1, AC97_VENDOR_ID2,
1229 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
1232 memset(&ac97, 0, sizeof(ac97));
1233 ac97.scaps = AC97_SCAP_AUDIO; /* we support audio! */
1234 ac97.limited_regs = 1;
1235 for (i = 0; mixer_regs[i] >= 0; i++)
1236 set_bit(mixer_regs[i], ac97.reg_accessed);
1237 ac97.private_data = chip;
1239 err = snd_ac97_mixer(pbus, &ac97, &chip->ac97);
1242 if (! (chip->ac97->id & (0xf0000000))) {
1243 /* looks like an invalid id */
1244 sprintf(chip->card->mixername, "%s AC97", chip->card->driver);
1250 * See if the signature left by the NM256 BIOS is intact; if so, we use
1251 * the associated address as the end of our audio buffer in the video
1255 static int __devinit
1256 snd_nm256_peek_for_sig(nm256_t *chip)
1258 /* The signature is located 1K below the end of video RAM. */
1260 /* Default buffer end is 5120 bytes below the top of RAM. */
1261 unsigned long pointer_found = chip->buffer_end - 0x1400;
1264 temp = ioremap_nocache(chip->buffer_addr + chip->buffer_end - 0x400, 16);
1266 snd_printk("Unable to scan for card signature in video RAM\n");
1271 if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1272 u32 pointer = readl(temp + 4);
1275 * If it's obviously invalid, don't use it
1277 if (pointer == 0xffffffff ||
1278 pointer < chip->buffer_size ||
1279 pointer > chip->buffer_end) {
1280 snd_printk("invalid signature found: 0x%x\n", pointer);
1284 pointer_found = pointer;
1285 printk(KERN_INFO "nm256: found card signature in video RAM: 0x%x\n", pointer);
1290 chip->buffer_end = pointer_found;
1297 * APM event handler, so the card is properly reinitialized after a power
1300 static int nm256_suspend(snd_card_t *card, pm_message_t state)
1302 nm256_t *chip = card->pm_private_data;
1304 snd_pcm_suspend_all(chip->pcm);
1305 snd_ac97_suspend(chip->ac97);
1306 chip->coeffs_current = 0;
1307 pci_disable_device(chip->pci);
1311 static int nm256_resume(snd_card_t *card)
1313 nm256_t *chip = card->pm_private_data;
1316 /* Perform a full reset on the hardware */
1317 pci_enable_device(chip->pci);
1318 snd_nm256_init_chip(chip);
1321 snd_ac97_resume(chip->ac97);
1323 for (i = 0; i < 2; i++) {
1324 nm256_stream_t *s = &chip->streams[i];
1325 if (s->substream && s->suspended) {
1326 spin_lock_irq(&chip->reg_lock);
1327 snd_nm256_set_format(chip, s, s->substream);
1328 spin_unlock_irq(&chip->reg_lock);
1334 #endif /* CONFIG_PM */
1336 static int snd_nm256_free(nm256_t *chip)
1338 if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
1339 snd_nm256_playback_stop(chip);
1340 if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
1341 snd_nm256_capture_stop(chip);
1344 synchronize_irq(chip->irq);
1347 iounmap(chip->cport);
1349 iounmap(chip->buffer);
1350 if (chip->res_cport) {
1351 release_resource(chip->res_cport);
1352 kfree_nocheck(chip->res_cport);
1354 if (chip->res_buffer) {
1355 release_resource(chip->res_buffer);
1356 kfree_nocheck(chip->res_buffer);
1359 free_irq(chip->irq, (void*)chip);
1361 pci_disable_device(chip->pci);
1366 static int snd_nm256_dev_free(snd_device_t *device)
1368 nm256_t *chip = device->device_data;
1369 return snd_nm256_free(chip);
1372 static int __devinit
1373 snd_nm256_create(snd_card_t *card, struct pci_dev *pci,
1374 int play_bufsize, int capt_bufsize,
1382 static snd_device_ops_t ops = {
1383 .dev_free = snd_nm256_dev_free,
1389 if ((err = pci_enable_device(pci)) < 0)
1392 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1394 pci_disable_device(pci);
1400 chip->use_cache = usecache;
1401 spin_lock_init(&chip->reg_lock);
1403 init_MUTEX(&chip->irq_mutex);
1405 chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize = play_bufsize;
1406 chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize = capt_bufsize;
1409 * The NM256 has two memory ports. The first port is nothing
1410 * more than a chunk of video RAM, which is used as the I/O ring
1411 * buffer. The second port has the actual juicy stuff (like the
1412 * mixer and the playback engine control registers).
1415 chip->buffer_addr = pci_resource_start(pci, 0);
1416 chip->cport_addr = pci_resource_start(pci, 1);
1418 /* Init the memory port info. */
1419 /* remap control port (#2) */
1420 chip->res_cport = request_mem_region(chip->cport_addr, NM_PORT2_SIZE,
1422 if (chip->res_cport == NULL) {
1423 snd_printk("memory region 0x%lx (size 0x%x) busy\n",
1424 chip->cport_addr, NM_PORT2_SIZE);
1428 chip->cport = ioremap_nocache(chip->cport_addr, NM_PORT2_SIZE);
1429 if (chip->cport == NULL) {
1430 snd_printk("unable to map control port %lx\n", chip->cport_addr);
1435 if (!strcmp(card->driver, "NM256AV")) {
1436 /* Ok, try to see if this is a non-AC97 version of the hardware. */
1437 pval = snd_nm256_readw(chip, NM_MIXER_PRESENCE);
1438 if ((pval & NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) {
1440 printk(KERN_ERR "nm256: no ac97 is found!\n");
1441 printk(KERN_ERR " force the driver to load by passing in the module parameter\n");
1442 printk(KERN_ERR " force_ac97=1\n");
1443 printk(KERN_ERR " or try sb16 or cs423x drivers instead.\n");
1448 chip->buffer_end = 2560 * 1024;
1449 chip->interrupt = snd_nm256_interrupt;
1450 chip->mixer_status_offset = NM_MIXER_STATUS_OFFSET;
1451 chip->mixer_status_mask = NM_MIXER_READY_MASK;
1453 /* Not sure if there is any relevant detect for the ZX or not. */
1454 if (snd_nm256_readb(chip, 0xa0b) != 0)
1455 chip->buffer_end = 6144 * 1024;
1457 chip->buffer_end = 4096 * 1024;
1459 chip->interrupt = snd_nm256_interrupt_zx;
1460 chip->mixer_status_offset = NM2_MIXER_STATUS_OFFSET;
1461 chip->mixer_status_mask = NM2_MIXER_READY_MASK;
1464 chip->buffer_size = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize + chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize;
1465 if (chip->use_cache)
1466 chip->buffer_size += NM_TOTAL_COEFF_COUNT * 4;
1468 chip->buffer_size += NM_MAX_PLAYBACK_COEF_SIZE + NM_MAX_RECORD_COEF_SIZE;
1470 if (buffertop >= chip->buffer_size && buffertop < chip->buffer_end)
1471 chip->buffer_end = buffertop;
1473 /* get buffer end pointer from signature */
1474 if ((err = snd_nm256_peek_for_sig(chip)) < 0)
1478 chip->buffer_start = chip->buffer_end - chip->buffer_size;
1479 chip->buffer_addr += chip->buffer_start;
1481 printk(KERN_INFO "nm256: Mapping port 1 from 0x%x - 0x%x\n",
1482 chip->buffer_start, chip->buffer_end);
1484 chip->res_buffer = request_mem_region(chip->buffer_addr,
1487 if (chip->res_buffer == NULL) {
1488 snd_printk("nm256: buffer 0x%lx (size 0x%x) busy\n",
1489 chip->buffer_addr, chip->buffer_size);
1493 chip->buffer = ioremap_nocache(chip->buffer_addr, chip->buffer_size);
1494 if (chip->buffer == NULL) {
1496 snd_printk("unable to map ring buffer at %lx\n", chip->buffer_addr);
1501 addr = chip->buffer_start;
1502 chip->streams[SNDRV_PCM_STREAM_PLAYBACK].buf = addr;
1503 addr += chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize;
1504 chip->streams[SNDRV_PCM_STREAM_CAPTURE].buf = addr;
1505 addr += chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize;
1506 if (chip->use_cache) {
1507 chip->all_coeff_buf = addr;
1509 chip->coeff_buf[SNDRV_PCM_STREAM_PLAYBACK] = addr;
1510 addr += NM_MAX_PLAYBACK_COEF_SIZE;
1511 chip->coeff_buf[SNDRV_PCM_STREAM_CAPTURE] = addr;
1514 /* Fixed setting. */
1515 chip->mixer_base = NM_MIXER_OFFSET;
1517 chip->coeffs_current = 0;
1519 snd_nm256_init_chip(chip);
1521 // pci_set_master(pci); /* needed? */
1523 snd_card_set_pm_callback(card, nm256_suspend, nm256_resume, chip);
1525 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1528 snd_card_set_dev(card, &pci->dev);
1534 snd_nm256_free(chip);
1539 struct nm256_quirk {
1540 unsigned short vendor;
1541 unsigned short device;
1545 enum { NM_BLACKLISTED, NM_RESET_WORKAROUND };
1547 static struct nm256_quirk nm256_quirks[] __devinitdata = {
1548 /* HP omnibook 4150 has cs4232 codec internally */
1549 { .vendor = 0x103c, .device = 0x0007, .type = NM_BLACKLISTED },
1551 { .vendor = 0x104d, .device = 0x8041, .type = NM_RESET_WORKAROUND },
1552 /* Dell Latitude LS */
1553 { .vendor = 0x1028, .device = 0x0080, .type = NM_RESET_WORKAROUND },
1554 { } /* terminator */
1558 static int __devinit snd_nm256_probe(struct pci_dev *pci,
1559 const struct pci_device_id *pci_id)
1565 unsigned int xbuffer_top;
1566 struct nm256_quirk *q;
1567 u16 subsystem_vendor, subsystem_device;
1569 if (dev >= SNDRV_CARDS)
1576 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1577 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1579 for (q = nm256_quirks; q->vendor; q++) {
1580 if (q->vendor == subsystem_vendor && q->device == subsystem_device) {
1582 case NM_BLACKLISTED:
1583 printk(KERN_INFO "nm256: The device is blacklisted. Loading stopped\n");
1585 case NM_RESET_WORKAROUND:
1586 reset_workaround[dev] = 1;
1592 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1596 switch (pci->device) {
1597 case PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO:
1598 strcpy(card->driver, "NM256AV");
1600 case PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO:
1601 strcpy(card->driver, "NM256ZX");
1603 case PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO:
1604 strcpy(card->driver, "NM256XL+");
1607 snd_printk("invalid device id 0x%x\n", pci->device);
1608 snd_card_free(card);
1613 xbuffer_top = 0x25a800; /* this avoids conflicts with XFree86 server */
1615 xbuffer_top = buffer_top[dev];
1617 if (playback_bufsize[dev] < 4)
1618 playback_bufsize[dev] = 4;
1619 if (playback_bufsize[dev] > 128)
1620 playback_bufsize[dev] = 128;
1621 if (capture_bufsize[dev] < 4)
1622 capture_bufsize[dev] = 4;
1623 if (capture_bufsize[dev] > 128)
1624 capture_bufsize[dev] = 128;
1625 if ((err = snd_nm256_create(card, pci,
1626 playback_bufsize[dev] * 1024, /* in bytes */
1627 capture_bufsize[dev] * 1024, /* in bytes */
1632 snd_card_free(card);
1636 if (reset_workaround[dev]) {
1637 snd_printdd(KERN_INFO "nm256: reset_workaround activated\n");
1638 chip->reset_workaround = 1;
1641 if ((err = snd_nm256_pcm(chip, 0)) < 0 ||
1642 (err = snd_nm256_mixer(chip)) < 0) {
1643 snd_card_free(card);
1647 sprintf(card->shortname, "NeoMagic %s", card->driver);
1648 sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %d",
1650 chip->buffer_addr, chip->cport_addr, chip->irq);
1652 if ((err = snd_card_register(card)) < 0) {
1653 snd_card_free(card);
1657 pci_set_drvdata(pci, card);
1662 static void __devexit snd_nm256_remove(struct pci_dev *pci)
1664 snd_card_free(pci_get_drvdata(pci));
1665 pci_set_drvdata(pci, NULL);
1669 static struct pci_driver driver = {
1670 .name = "NeoMagic 256",
1671 .owner = THIS_MODULE,
1672 .id_table = snd_nm256_ids,
1673 .probe = snd_nm256_probe,
1674 .remove = __devexit_p(snd_nm256_remove),
1675 SND_PCI_PM_CALLBACKS
1679 static int __init alsa_card_nm256_init(void)
1681 return pci_register_driver(&driver);
1684 static void __exit alsa_card_nm256_exit(void)
1686 pci_unregister_driver(&driver);
1689 module_init(alsa_card_nm256_init)
1690 module_exit(alsa_card_nm256_exit)