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