blob: 0c701e4ec8a58223d71d985bd0fd593d0acb2c5e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
3 * Driver EMU10K1X chips
4 *
5 * Parts of this code were adapted from audigyls.c driver which is
6 * Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
7 *
8 * BUGS:
9 * --
10 *
11 * TODO:
12 *
13 * Chips (SB0200 model):
14 * - EMU10K1X-DBQ
15 * - STAC 9708T
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 *
31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/init.h>
33#include <linux/interrupt.h>
34#include <linux/pci.h>
Tobias Klauser9d2f9282006-03-22 10:53:19 +010035#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/slab.h>
37#include <linux/moduleparam.h>
38#include <sound/core.h>
39#include <sound/initval.h>
40#include <sound/pcm.h>
41#include <sound/ac97_codec.h>
42#include <sound/info.h>
43#include <sound/rawmidi.h>
44
45MODULE_AUTHOR("Francisco Moraes <fmoraes@nc.rr.com>");
46MODULE_DESCRIPTION("EMU10K1X");
47MODULE_LICENSE("GPL");
48MODULE_SUPPORTED_DEVICE("{{Dell Creative Labs,SB Live!}");
49
50// module parameters (see "Module Parameters")
51static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
52static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
53static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
54
55module_param_array(index, int, NULL, 0444);
56MODULE_PARM_DESC(index, "Index value for the EMU10K1X soundcard.");
57module_param_array(id, charp, NULL, 0444);
58MODULE_PARM_DESC(id, "ID string for the EMU10K1X soundcard.");
59module_param_array(enable, bool, NULL, 0444);
60MODULE_PARM_DESC(enable, "Enable the EMU10K1X soundcard.");
61
62
63// some definitions were borrowed from emu10k1 driver as they seem to be the same
64/************************************************************************************************/
65/* PCI function 0 registers, address = <val> + PCIBASE0 */
66/************************************************************************************************/
67
68#define PTR 0x00 /* Indexed register set pointer register */
69 /* NOTE: The CHANNELNUM and ADDRESS words can */
70 /* be modified independently of each other. */
71
72#define DATA 0x04 /* Indexed register set data register */
73
74#define IPR 0x08 /* Global interrupt pending register */
75 /* Clear pending interrupts by writing a 1 to */
76 /* the relevant bits and zero to the other bits */
77#define IPR_MIDITRANSBUFEMPTY 0x00000001 /* MIDI UART transmit buffer empty */
78#define IPR_MIDIRECVBUFEMPTY 0x00000002 /* MIDI UART receive buffer empty */
79#define IPR_CH_0_LOOP 0x00000800 /* Channel 0 loop */
80#define IPR_CH_0_HALF_LOOP 0x00000100 /* Channel 0 half loop */
81#define IPR_CAP_0_LOOP 0x00080000 /* Channel capture loop */
82#define IPR_CAP_0_HALF_LOOP 0x00010000 /* Channel capture half loop */
83
84#define INTE 0x0c /* Interrupt enable register */
85#define INTE_MIDITXENABLE 0x00000001 /* Enable MIDI transmit-buffer-empty interrupts */
86#define INTE_MIDIRXENABLE 0x00000002 /* Enable MIDI receive-buffer-empty interrupts */
87#define INTE_CH_0_LOOP 0x00000800 /* Channel 0 loop */
88#define INTE_CH_0_HALF_LOOP 0x00000100 /* Channel 0 half loop */
89#define INTE_CAP_0_LOOP 0x00080000 /* Channel capture loop */
90#define INTE_CAP_0_HALF_LOOP 0x00010000 /* Channel capture half loop */
91
92#define HCFG 0x14 /* Hardware config register */
93
94#define HCFG_LOCKSOUNDCACHE 0x00000008 /* 1 = Cancel bustmaster accesses to soundcache */
95 /* NOTE: This should generally never be used. */
96#define HCFG_AUDIOENABLE 0x00000001 /* 0 = CODECs transmit zero-valued samples */
97 /* Should be set to 1 when the EMU10K1 is */
98 /* completely initialized. */
99#define GPIO 0x18 /* Defaults: 00001080-Analog, 00001000-SPDIF. */
100
101
102#define AC97DATA 0x1c /* AC97 register set data register (16 bit) */
103
104#define AC97ADDRESS 0x1e /* AC97 register set address register (8 bit) */
105
106/********************************************************************************************************/
107/* Emu10k1x pointer-offset register set, accessed through the PTR and DATA registers */
108/********************************************************************************************************/
109#define PLAYBACK_LIST_ADDR 0x00 /* Base DMA address of a list of pointers to each period/size */
110 /* One list entry: 4 bytes for DMA address,
111 * 4 bytes for period_size << 16.
112 * One list entry is 8 bytes long.
113 * One list entry for each period in the buffer.
114 */
115#define PLAYBACK_LIST_SIZE 0x01 /* Size of list in bytes << 16. E.g. 8 periods -> 0x00380000 */
116#define PLAYBACK_LIST_PTR 0x02 /* Pointer to the current period being played */
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400117#define PLAYBACK_DMA_ADDR 0x04 /* Playback DMA address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define PLAYBACK_PERIOD_SIZE 0x05 /* Playback period size */
119#define PLAYBACK_POINTER 0x06 /* Playback period pointer. Sample currently in DAC */
120#define PLAYBACK_UNKNOWN1 0x07
121#define PLAYBACK_UNKNOWN2 0x08
122
123/* Only one capture channel supported */
124#define CAPTURE_DMA_ADDR 0x10 /* Capture DMA address */
125#define CAPTURE_BUFFER_SIZE 0x11 /* Capture buffer size */
126#define CAPTURE_POINTER 0x12 /* Capture buffer pointer. Sample currently in ADC */
127#define CAPTURE_UNKNOWN 0x13
128
129/* From 0x20 - 0x3f, last samples played on each channel */
130
131#define TRIGGER_CHANNEL 0x40 /* Trigger channel playback */
132#define TRIGGER_CHANNEL_0 0x00000001 /* Trigger channel 0 */
133#define TRIGGER_CHANNEL_1 0x00000002 /* Trigger channel 1 */
134#define TRIGGER_CHANNEL_2 0x00000004 /* Trigger channel 2 */
135#define TRIGGER_CAPTURE 0x00000100 /* Trigger capture channel */
136
137#define ROUTING 0x41 /* Setup sound routing ? */
138#define ROUTING_FRONT_LEFT 0x00000001
139#define ROUTING_FRONT_RIGHT 0x00000002
140#define ROUTING_REAR_LEFT 0x00000004
141#define ROUTING_REAR_RIGHT 0x00000008
142#define ROUTING_CENTER_LFE 0x00010000
143
144#define SPCS0 0x42 /* SPDIF output Channel Status 0 register */
145
146#define SPCS1 0x43 /* SPDIF output Channel Status 1 register */
147
148#define SPCS2 0x44 /* SPDIF output Channel Status 2 register */
149
150#define SPCS_CLKACCYMASK 0x30000000 /* Clock accuracy */
151#define SPCS_CLKACCY_1000PPM 0x00000000 /* 1000 parts per million */
152#define SPCS_CLKACCY_50PPM 0x10000000 /* 50 parts per million */
153#define SPCS_CLKACCY_VARIABLE 0x20000000 /* Variable accuracy */
154#define SPCS_SAMPLERATEMASK 0x0f000000 /* Sample rate */
155#define SPCS_SAMPLERATE_44 0x00000000 /* 44.1kHz sample rate */
156#define SPCS_SAMPLERATE_48 0x02000000 /* 48kHz sample rate */
157#define SPCS_SAMPLERATE_32 0x03000000 /* 32kHz sample rate */
158#define SPCS_CHANNELNUMMASK 0x00f00000 /* Channel number */
159#define SPCS_CHANNELNUM_UNSPEC 0x00000000 /* Unspecified channel number */
160#define SPCS_CHANNELNUM_LEFT 0x00100000 /* Left channel */
161#define SPCS_CHANNELNUM_RIGHT 0x00200000 /* Right channel */
162#define SPCS_SOURCENUMMASK 0x000f0000 /* Source number */
163#define SPCS_SOURCENUM_UNSPEC 0x00000000 /* Unspecified source number */
164#define SPCS_GENERATIONSTATUS 0x00008000 /* Originality flag (see IEC-958 spec) */
165#define SPCS_CATEGORYCODEMASK 0x00007f00 /* Category code (see IEC-958 spec) */
166#define SPCS_MODEMASK 0x000000c0 /* Mode (see IEC-958 spec) */
167#define SPCS_EMPHASISMASK 0x00000038 /* Emphasis */
168#define SPCS_EMPHASIS_NONE 0x00000000 /* No emphasis */
169#define SPCS_EMPHASIS_50_15 0x00000008 /* 50/15 usec 2 channel */
170#define SPCS_COPYRIGHT 0x00000004 /* Copyright asserted flag -- do not modify */
171#define SPCS_NOTAUDIODATA 0x00000002 /* 0 = Digital audio, 1 = not audio */
172#define SPCS_PROFESSIONAL 0x00000001 /* 0 = Consumer (IEC-958), 1 = pro (AES3-1992) */
173
174#define SPDIF_SELECT 0x45 /* Enables SPDIF or Analogue outputs 0-Analogue, 0x700-SPDIF */
175
176/* This is the MPU port on the card */
177#define MUDATA 0x47
178#define MUCMD 0x48
179#define MUSTAT MUCMD
180
181/* From 0x50 - 0x5f, last samples captured */
182
183/**
184 * The hardware has 3 channels for playback and 1 for capture.
185 * - channel 0 is the front channel
186 * - channel 1 is the rear channel
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200187 * - channel 2 is the center/lfe channel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * Volume is controlled by the AC97 for the front and rear channels by
189 * the PCM Playback Volume, Sigmatel Surround Playback Volume and
190 * Surround Playback Volume. The Sigmatel 4-Speaker Stereo switch affects
191 * the front/rear channel mixing in the REAR OUT jack. When using the
192 * 4-Speaker Stereo, both front and rear channels will be mixed in the
193 * REAR OUT.
194 * The center/lfe channel has no volume control and cannot be muted during
195 * playback.
196 */
197
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100198struct emu10k1x_voice {
199 struct emu10k1x *emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int number;
201 int use;
202
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100203 struct emu10k1x_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100206struct emu10k1x_pcm {
207 struct emu10k1x *emu;
208 struct snd_pcm_substream *substream;
209 struct emu10k1x_voice *voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned short running;
211};
212
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100213struct emu10k1x_midi {
214 struct emu10k1x *emu;
215 struct snd_rawmidi *rmidi;
216 struct snd_rawmidi_substream *substream_input;
217 struct snd_rawmidi_substream *substream_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 unsigned int midi_mode;
219 spinlock_t input_lock;
220 spinlock_t output_lock;
221 spinlock_t open_lock;
222 int tx_enable, rx_enable;
223 int port;
224 int ipr_tx, ipr_rx;
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100225 void (*interrupt)(struct emu10k1x *emu, unsigned int status);
226};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228// definition of the chip-specific record
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100229struct emu10k1x {
230 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct pci_dev *pci;
232
233 unsigned long port;
234 struct resource *res_port;
235 int irq;
236
Takashi Iwai01f681d2006-11-16 15:39:07 +0100237 unsigned char revision; /* chip revision */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 unsigned int serial; /* serial number */
239 unsigned short model; /* subsystem id */
240
241 spinlock_t emu_lock;
242 spinlock_t voice_lock;
243
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100244 struct snd_ac97 *ac97;
245 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100247 struct emu10k1x_voice voices[3];
248 struct emu10k1x_voice capture_voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 u32 spdif_bits[3]; // SPDIF out setup
250
251 struct snd_dma_buffer dma_buffer;
252
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100253 struct emu10k1x_midi midi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
256/* hardware definition */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100257static struct snd_pcm_hardware snd_emu10k1x_playback_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .info = (SNDRV_PCM_INFO_MMAP |
259 SNDRV_PCM_INFO_INTERLEAVED |
260 SNDRV_PCM_INFO_BLOCK_TRANSFER |
261 SNDRV_PCM_INFO_MMAP_VALID),
262 .formats = SNDRV_PCM_FMTBIT_S16_LE,
263 .rates = SNDRV_PCM_RATE_48000,
264 .rate_min = 48000,
265 .rate_max = 48000,
266 .channels_min = 2,
267 .channels_max = 2,
268 .buffer_bytes_max = (32*1024),
269 .period_bytes_min = 64,
270 .period_bytes_max = (16*1024),
271 .periods_min = 2,
272 .periods_max = 8,
273 .fifo_size = 0,
274};
275
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100276static struct snd_pcm_hardware snd_emu10k1x_capture_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 .info = (SNDRV_PCM_INFO_MMAP |
278 SNDRV_PCM_INFO_INTERLEAVED |
279 SNDRV_PCM_INFO_BLOCK_TRANSFER |
280 SNDRV_PCM_INFO_MMAP_VALID),
281 .formats = SNDRV_PCM_FMTBIT_S16_LE,
282 .rates = SNDRV_PCM_RATE_48000,
283 .rate_min = 48000,
284 .rate_max = 48000,
285 .channels_min = 2,
286 .channels_max = 2,
287 .buffer_bytes_max = (32*1024),
288 .period_bytes_min = 64,
289 .period_bytes_max = (16*1024),
290 .periods_min = 2,
291 .periods_max = 2,
292 .fifo_size = 0,
293};
294
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100295static unsigned int snd_emu10k1x_ptr_read(struct emu10k1x * emu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 unsigned int reg,
297 unsigned int chn)
298{
299 unsigned long flags;
300 unsigned int regptr, val;
301
302 regptr = (reg << 16) | chn;
303
304 spin_lock_irqsave(&emu->emu_lock, flags);
305 outl(regptr, emu->port + PTR);
306 val = inl(emu->port + DATA);
307 spin_unlock_irqrestore(&emu->emu_lock, flags);
308 return val;
309}
310
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100311static void snd_emu10k1x_ptr_write(struct emu10k1x *emu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 unsigned int reg,
313 unsigned int chn,
314 unsigned int data)
315{
316 unsigned int regptr;
317 unsigned long flags;
318
319 regptr = (reg << 16) | chn;
320
321 spin_lock_irqsave(&emu->emu_lock, flags);
322 outl(regptr, emu->port + PTR);
323 outl(data, emu->port + DATA);
324 spin_unlock_irqrestore(&emu->emu_lock, flags);
325}
326
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100327static void snd_emu10k1x_intr_enable(struct emu10k1x *emu, unsigned int intrenb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 unsigned long flags;
Harvey Harrisonf2948fc2008-02-29 11:44:57 +0100330 unsigned int intr_enable;
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 spin_lock_irqsave(&emu->emu_lock, flags);
Harvey Harrisonf2948fc2008-02-29 11:44:57 +0100333 intr_enable = inl(emu->port + INTE) | intrenb;
334 outl(intr_enable, emu->port + INTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 spin_unlock_irqrestore(&emu->emu_lock, flags);
336}
337
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100338static void snd_emu10k1x_intr_disable(struct emu10k1x *emu, unsigned int intrenb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 unsigned long flags;
Harvey Harrisonf2948fc2008-02-29 11:44:57 +0100341 unsigned int intr_enable;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 spin_lock_irqsave(&emu->emu_lock, flags);
Harvey Harrisonf2948fc2008-02-29 11:44:57 +0100344 intr_enable = inl(emu->port + INTE) & ~intrenb;
345 outl(intr_enable, emu->port + INTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 spin_unlock_irqrestore(&emu->emu_lock, flags);
347}
348
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100349static void snd_emu10k1x_gpio_write(struct emu10k1x *emu, unsigned int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 unsigned long flags;
352
353 spin_lock_irqsave(&emu->emu_lock, flags);
354 outl(value, emu->port + GPIO);
355 spin_unlock_irqrestore(&emu->emu_lock, flags);
356}
357
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100358static void snd_emu10k1x_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Jesper Juhl4d572772005-05-30 17:30:32 +0200360 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100363static void snd_emu10k1x_pcm_interrupt(struct emu10k1x *emu, struct emu10k1x_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100365 struct emu10k1x_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if ((epcm = voice->epcm) == NULL)
368 return;
369 if (epcm->substream == NULL)
370 return;
371#if 0
372 snd_printk(KERN_INFO "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
373 epcm->substream->ops->pointer(epcm->substream),
374 snd_pcm_lib_period_bytes(epcm->substream),
375 snd_pcm_lib_buffer_bytes(epcm->substream));
376#endif
377 snd_pcm_period_elapsed(epcm->substream);
378}
379
380/* open callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100381static int snd_emu10k1x_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100383 struct emu10k1x *chip = snd_pcm_substream_chip(substream);
384 struct emu10k1x_pcm *epcm;
385 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int err;
387
388 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
389 return err;
390 }
391 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
392 return err;
393
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200394 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (epcm == NULL)
396 return -ENOMEM;
397 epcm->emu = chip;
398 epcm->substream = substream;
399
400 runtime->private_data = epcm;
401 runtime->private_free = snd_emu10k1x_pcm_free_substream;
402
403 runtime->hw = snd_emu10k1x_playback_hw;
404
405 return 0;
406}
407
408/* close callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100409static int snd_emu10k1x_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 return 0;
412}
413
414/* hw_params callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100415static int snd_emu10k1x_pcm_hw_params(struct snd_pcm_substream *substream,
416 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100418 struct snd_pcm_runtime *runtime = substream->runtime;
419 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (! epcm->voice) {
422 epcm->voice = &epcm->emu->voices[substream->pcm->device];
423 epcm->voice->use = 1;
424 epcm->voice->epcm = epcm;
425 }
426
427 return snd_pcm_lib_malloc_pages(substream,
428 params_buffer_bytes(hw_params));
429}
430
431/* hw_free callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100432static int snd_emu10k1x_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100434 struct snd_pcm_runtime *runtime = substream->runtime;
435 struct emu10k1x_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (runtime->private_data == NULL)
438 return 0;
439
440 epcm = runtime->private_data;
441
442 if (epcm->voice) {
443 epcm->voice->use = 0;
444 epcm->voice->epcm = NULL;
445 epcm->voice = NULL;
446 }
447
448 return snd_pcm_lib_free_pages(substream);
449}
450
451/* prepare callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100452static int snd_emu10k1x_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100454 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
455 struct snd_pcm_runtime *runtime = substream->runtime;
456 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int voice = epcm->voice->number;
458 u32 *table_base = (u32 *)(emu->dma_buffer.area+1024*voice);
459 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
460 int i;
461
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100462 for(i = 0; i < runtime->periods; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *table_base++=runtime->dma_addr+(i*period_size_bytes);
464 *table_base++=period_size_bytes<<16;
465 }
466
467 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_ADDR, voice, emu->dma_buffer.addr+1024*voice);
468 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_SIZE, voice, (runtime->periods - 1) << 19);
469 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_PTR, voice, 0);
470 snd_emu10k1x_ptr_write(emu, PLAYBACK_POINTER, voice, 0);
471 snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN1, voice, 0);
472 snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN2, voice, 0);
473 snd_emu10k1x_ptr_write(emu, PLAYBACK_DMA_ADDR, voice, runtime->dma_addr);
474
475 snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
476
477 return 0;
478}
479
480/* trigger callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100481static int snd_emu10k1x_pcm_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int cmd)
483{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100484 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
485 struct snd_pcm_runtime *runtime = substream->runtime;
486 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 int channel = epcm->voice->number;
488 int result = 0;
489
490// snd_printk(KERN_INFO "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n", (int)emu, cmd, (int)substream->ops->pointer(substream));
491
492 switch (cmd) {
493 case SNDRV_PCM_TRIGGER_START:
494 if(runtime->periods == 2)
495 snd_emu10k1x_intr_enable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
496 else
497 snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
498 epcm->running = 1;
499 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
500 break;
501 case SNDRV_PCM_TRIGGER_STOP:
502 epcm->running = 0;
503 snd_emu10k1x_intr_disable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
504 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CHANNEL_0<<channel));
505 break;
506 default:
507 result = -EINVAL;
508 break;
509 }
510 return result;
511}
512
513/* pointer callback */
514static snd_pcm_uframes_t
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100515snd_emu10k1x_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100517 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
518 struct snd_pcm_runtime *runtime = substream->runtime;
519 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int channel = epcm->voice->number;
521 snd_pcm_uframes_t ptr = 0, ptr1 = 0, ptr2= 0,ptr3 = 0,ptr4 = 0;
522
523 if (!epcm->running)
524 return 0;
525
526 ptr3 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
527 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
528 ptr4 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
529
530 if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
531 return 0;
532
533 if (ptr3 != ptr4)
534 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
535 ptr2 = bytes_to_frames(runtime, ptr1);
536 ptr2 += (ptr4 >> 3) * runtime->period_size;
537 ptr = ptr2;
538
539 if (ptr >= runtime->buffer_size)
540 ptr -= runtime->buffer_size;
541
542 return ptr;
543}
544
545/* operators */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100546static struct snd_pcm_ops snd_emu10k1x_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 .open = snd_emu10k1x_playback_open,
548 .close = snd_emu10k1x_playback_close,
549 .ioctl = snd_pcm_lib_ioctl,
550 .hw_params = snd_emu10k1x_pcm_hw_params,
551 .hw_free = snd_emu10k1x_pcm_hw_free,
552 .prepare = snd_emu10k1x_pcm_prepare,
553 .trigger = snd_emu10k1x_pcm_trigger,
554 .pointer = snd_emu10k1x_pcm_pointer,
555};
556
557/* open_capture callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100558static int snd_emu10k1x_pcm_open_capture(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100560 struct emu10k1x *chip = snd_pcm_substream_chip(substream);
561 struct emu10k1x_pcm *epcm;
562 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 int err;
564
565 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
566 return err;
567 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
568 return err;
569
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200570 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (epcm == NULL)
572 return -ENOMEM;
573
574 epcm->emu = chip;
575 epcm->substream = substream;
576
577 runtime->private_data = epcm;
578 runtime->private_free = snd_emu10k1x_pcm_free_substream;
579
580 runtime->hw = snd_emu10k1x_capture_hw;
581
582 return 0;
583}
584
585/* close callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100586static int snd_emu10k1x_pcm_close_capture(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 return 0;
589}
590
591/* hw_params callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100592static int snd_emu10k1x_pcm_hw_params_capture(struct snd_pcm_substream *substream,
593 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100595 struct snd_pcm_runtime *runtime = substream->runtime;
596 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (! epcm->voice) {
599 if (epcm->emu->capture_voice.use)
600 return -EBUSY;
601 epcm->voice = &epcm->emu->capture_voice;
602 epcm->voice->epcm = epcm;
603 epcm->voice->use = 1;
604 }
605
606 return snd_pcm_lib_malloc_pages(substream,
607 params_buffer_bytes(hw_params));
608}
609
610/* hw_free callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100611static int snd_emu10k1x_pcm_hw_free_capture(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100613 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100615 struct emu10k1x_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (runtime->private_data == NULL)
618 return 0;
619 epcm = runtime->private_data;
620
621 if (epcm->voice) {
622 epcm->voice->use = 0;
623 epcm->voice->epcm = NULL;
624 epcm->voice = NULL;
625 }
626
627 return snd_pcm_lib_free_pages(substream);
628}
629
630/* prepare capture callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100631static int snd_emu10k1x_pcm_prepare_capture(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100633 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
634 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 snd_emu10k1x_ptr_write(emu, CAPTURE_DMA_ADDR, 0, runtime->dma_addr);
637 snd_emu10k1x_ptr_write(emu, CAPTURE_BUFFER_SIZE, 0, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
638 snd_emu10k1x_ptr_write(emu, CAPTURE_POINTER, 0, 0);
639 snd_emu10k1x_ptr_write(emu, CAPTURE_UNKNOWN, 0, 0);
640
641 return 0;
642}
643
644/* trigger_capture callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100645static int snd_emu10k1x_pcm_trigger_capture(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int cmd)
647{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100648 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
649 struct snd_pcm_runtime *runtime = substream->runtime;
650 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int result = 0;
652
653 switch (cmd) {
654 case SNDRV_PCM_TRIGGER_START:
655 snd_emu10k1x_intr_enable(emu, INTE_CAP_0_LOOP |
656 INTE_CAP_0_HALF_LOOP);
657 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|TRIGGER_CAPTURE);
658 epcm->running = 1;
659 break;
660 case SNDRV_PCM_TRIGGER_STOP:
661 epcm->running = 0;
662 snd_emu10k1x_intr_disable(emu, INTE_CAP_0_LOOP |
663 INTE_CAP_0_HALF_LOOP);
664 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CAPTURE));
665 break;
666 default:
667 result = -EINVAL;
668 break;
669 }
670 return result;
671}
672
673/* pointer_capture callback */
674static snd_pcm_uframes_t
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100675snd_emu10k1x_pcm_pointer_capture(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100677 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
678 struct snd_pcm_runtime *runtime = substream->runtime;
679 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 snd_pcm_uframes_t ptr;
681
682 if (!epcm->running)
683 return 0;
684
685 ptr = bytes_to_frames(runtime, snd_emu10k1x_ptr_read(emu, CAPTURE_POINTER, 0));
686 if (ptr >= runtime->buffer_size)
687 ptr -= runtime->buffer_size;
688
689 return ptr;
690}
691
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100692static struct snd_pcm_ops snd_emu10k1x_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 .open = snd_emu10k1x_pcm_open_capture,
694 .close = snd_emu10k1x_pcm_close_capture,
695 .ioctl = snd_pcm_lib_ioctl,
696 .hw_params = snd_emu10k1x_pcm_hw_params_capture,
697 .hw_free = snd_emu10k1x_pcm_hw_free_capture,
698 .prepare = snd_emu10k1x_pcm_prepare_capture,
699 .trigger = snd_emu10k1x_pcm_trigger_capture,
700 .pointer = snd_emu10k1x_pcm_pointer_capture,
701};
702
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100703static unsigned short snd_emu10k1x_ac97_read(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 unsigned short reg)
705{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100706 struct emu10k1x *emu = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 unsigned long flags;
708 unsigned short val;
709
710 spin_lock_irqsave(&emu->emu_lock, flags);
711 outb(reg, emu->port + AC97ADDRESS);
712 val = inw(emu->port + AC97DATA);
713 spin_unlock_irqrestore(&emu->emu_lock, flags);
714 return val;
715}
716
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100717static void snd_emu10k1x_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 unsigned short reg, unsigned short val)
719{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100720 struct emu10k1x *emu = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 unsigned long flags;
722
723 spin_lock_irqsave(&emu->emu_lock, flags);
724 outb(reg, emu->port + AC97ADDRESS);
725 outw(val, emu->port + AC97DATA);
726 spin_unlock_irqrestore(&emu->emu_lock, flags);
727}
728
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100729static int snd_emu10k1x_ac97(struct emu10k1x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100731 struct snd_ac97_bus *pbus;
732 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int err;
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100734 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 .write = snd_emu10k1x_ac97_write,
736 .read = snd_emu10k1x_ac97_read,
737 };
738
739 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
740 return err;
741 pbus->no_vra = 1; /* we don't need VRA */
742
743 memset(&ac97, 0, sizeof(ac97));
744 ac97.private_data = chip;
745 ac97.scaps = AC97_SCAP_NO_SPDIF;
746 return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
747}
748
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100749static int snd_emu10k1x_free(struct emu10k1x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
752 // disable interrupts
753 outl(0, chip->port + INTE);
754 // disable audio
755 outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
756
Takashi Iwaiebf029d2008-04-22 17:28:11 +0200757 /* release the irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (chip->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +0100759 free_irq(chip->irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Takashi Iwaiebf029d2008-04-22 17:28:11 +0200761 // release the i/o port
762 release_and_free_resource(chip->res_port);
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 // release the DMA
765 if (chip->dma_buffer.area) {
766 snd_dma_free_pages(&chip->dma_buffer);
767 }
768
769 pci_disable_device(chip->pci);
770
771 // release the data
772 kfree(chip);
773 return 0;
774}
775
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100776static int snd_emu10k1x_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100778 struct emu10k1x *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return snd_emu10k1x_free(chip);
780}
781
David Howells7d12e782006-10-05 14:55:46 +0100782static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 unsigned int status;
785
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100786 struct emu10k1x *chip = dev_id;
787 struct emu10k1x_voice *pvoice = chip->voices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int i;
789 int mask;
790
791 status = inl(chip->port + IPR);
792
Takashi Iwai89173bd2005-11-17 10:43:53 +0100793 if (! status)
794 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Takashi Iwai89173bd2005-11-17 10:43:53 +0100796 // capture interrupt
797 if (status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
Harvey Harrisonf2948fc2008-02-29 11:44:57 +0100798 struct emu10k1x_voice *cap_voice = &chip->capture_voice;
799 if (cap_voice->use)
800 snd_emu10k1x_pcm_interrupt(chip, cap_voice);
Takashi Iwai89173bd2005-11-17 10:43:53 +0100801 else
802 snd_emu10k1x_intr_disable(chip,
803 INTE_CAP_0_LOOP |
804 INTE_CAP_0_HALF_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Takashi Iwai89173bd2005-11-17 10:43:53 +0100806
807 mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
808 for (i = 0; i < 3; i++) {
809 if (status & mask) {
810 if (pvoice->use)
811 snd_emu10k1x_pcm_interrupt(chip, pvoice);
812 else
813 snd_emu10k1x_intr_disable(chip, mask);
814 }
815 pvoice++;
816 mask <<= 1;
817 }
818
819 if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
820 if (chip->midi.interrupt)
821 chip->midi.interrupt(chip, status);
822 else
823 snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
824 }
825
826 // acknowledge the interrupt if necessary
827 outl(status, chip->port + IPR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Takashi Iwai89173bd2005-11-17 10:43:53 +0100829 // snd_printk(KERN_INFO "interrupt %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return IRQ_HANDLED;
831}
832
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100833static int __devinit snd_emu10k1x_pcm(struct emu10k1x *emu, int device, struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100835 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int err;
837 int capture = 0;
838
839 if (rpcm)
840 *rpcm = NULL;
841 if (device == 0)
842 capture = 1;
843
844 if ((err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm)) < 0)
845 return err;
846
847 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 switch(device) {
850 case 0:
851 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
852 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
853 break;
854 case 1:
855 case 2:
856 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
857 break;
858 }
859
860 pcm->info_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 switch(device) {
862 case 0:
863 strcpy(pcm->name, "EMU10K1X Front");
864 break;
865 case 1:
866 strcpy(pcm->name, "EMU10K1X Rear");
867 break;
868 case 2:
869 strcpy(pcm->name, "EMU10K1X Center/LFE");
870 break;
871 }
872 emu->pcm = pcm;
873
874 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
875 snd_dma_pci_data(emu->pci),
876 32*1024, 32*1024);
877
878 if (rpcm)
879 *rpcm = pcm;
880
881 return 0;
882}
883
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100884static int __devinit snd_emu10k1x_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct pci_dev *pci,
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100886 struct emu10k1x **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100888 struct emu10k1x *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 int err;
890 int ch;
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100891 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 .dev_free = snd_emu10k1x_dev_free,
893 };
Tobias Klauser9d2f9282006-03-22 10:53:19 +0100894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 *rchip = NULL;
Tobias Klauser9d2f9282006-03-22 10:53:19 +0100896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if ((err = pci_enable_device(pci)) < 0)
898 return err;
Yang Hongyangce0b6202009-04-06 19:01:17 -0700899 if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) < 0 ||
900 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 snd_printk(KERN_ERR "error to set 28bit mask DMA\n");
902 pci_disable_device(pci);
903 return -ENXIO;
904 }
Tobias Klauser9d2f9282006-03-22 10:53:19 +0100905
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200906 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (chip == NULL) {
908 pci_disable_device(pci);
909 return -ENOMEM;
910 }
Tobias Klauser9d2f9282006-03-22 10:53:19 +0100911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 chip->card = card;
913 chip->pci = pci;
914 chip->irq = -1;
915
916 spin_lock_init(&chip->emu_lock);
917 spin_lock_init(&chip->voice_lock);
918
919 chip->port = pci_resource_start(pci, 0);
920 if ((chip->res_port = request_region(chip->port, 8,
921 "EMU10K1X")) == NULL) {
922 snd_printk(KERN_ERR "emu10k1x: cannot allocate the port 0x%lx\n", chip->port);
923 snd_emu10k1x_free(chip);
924 return -EBUSY;
925 }
926
927 if (request_irq(pci->irq, snd_emu10k1x_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +0100928 IRQF_SHARED, "EMU10K1X", chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 snd_printk(KERN_ERR "emu10k1x: cannot grab irq %d\n", pci->irq);
930 snd_emu10k1x_free(chip);
931 return -EBUSY;
932 }
933 chip->irq = pci->irq;
934
935 if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
936 4 * 1024, &chip->dma_buffer) < 0) {
937 snd_emu10k1x_free(chip);
938 return -ENOMEM;
939 }
940
941 pci_set_master(pci);
942 /* read revision & serial */
Auke Kok44c10132007-06-08 15:46:36 -0700943 chip->revision = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
945 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
946 snd_printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
947 chip->revision, chip->serial);
948
949 outl(0, chip->port + INTE);
950
951 for(ch = 0; ch < 3; ch++) {
952 chip->voices[ch].emu = chip;
953 chip->voices[ch].number = ch;
954 }
955
956 /*
957 * Init to 0x02109204 :
958 * Clock accuracy = 0 (1000ppm)
959 * Sample Rate = 2 (48kHz)
960 * Audio Channel = 1 (Left of 2)
961 * Source Number = 0 (Unspecified)
962 * Generation Status = 1 (Original for Cat Code 12)
963 * Cat Code = 12 (Digital Signal Mixer)
964 * Mode = 0 (Mode 0)
965 * Emphasis = 0 (None)
966 * CP = 1 (Copyright unasserted)
967 * AN = 0 (Audio data)
968 * P = 0 (Consumer)
969 */
970 snd_emu10k1x_ptr_write(chip, SPCS0, 0,
971 chip->spdif_bits[0] =
972 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
973 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
974 SPCS_GENERATIONSTATUS | 0x00001200 |
975 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
976 snd_emu10k1x_ptr_write(chip, SPCS1, 0,
977 chip->spdif_bits[1] =
978 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
979 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
980 SPCS_GENERATIONSTATUS | 0x00001200 |
981 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
982 snd_emu10k1x_ptr_write(chip, SPCS2, 0,
983 chip->spdif_bits[2] =
984 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
985 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
986 SPCS_GENERATIONSTATUS | 0x00001200 |
987 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
988
989 snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
990 snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
991 snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
992
993 outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
994
995 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
996 chip, &ops)) < 0) {
997 snd_emu10k1x_free(chip);
998 return err;
999 }
1000 *rchip = chip;
1001 return 0;
1002}
1003
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001004static void snd_emu10k1x_proc_reg_read(struct snd_info_entry *entry,
1005 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001007 struct emu10k1x *emu = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 unsigned long value,value1,value2;
1009 unsigned long flags;
1010 int i;
1011
1012 snd_iprintf(buffer, "Registers:\n\n");
1013 for(i = 0; i < 0x20; i+=4) {
1014 spin_lock_irqsave(&emu->emu_lock, flags);
1015 value = inl(emu->port + i);
1016 spin_unlock_irqrestore(&emu->emu_lock, flags);
1017 snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
1018 }
1019 snd_iprintf(buffer, "\nRegisters\n\n");
1020 for(i = 0; i <= 0x48; i++) {
1021 value = snd_emu10k1x_ptr_read(emu, i, 0);
1022 if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
1023 value1 = snd_emu10k1x_ptr_read(emu, i, 1);
1024 value2 = snd_emu10k1x_ptr_read(emu, i, 2);
1025 snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
1026 } else {
1027 snd_iprintf(buffer, "%02X: %08lX\n", i, value);
1028 }
1029 }
1030}
1031
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001032static void snd_emu10k1x_proc_reg_write(struct snd_info_entry *entry,
1033 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001035 struct emu10k1x *emu = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 char line[64];
1037 unsigned int reg, channel_id , val;
1038
1039 while (!snd_info_get_line(buffer, line, sizeof(line))) {
1040 if (sscanf(line, "%x %x %x", &reg, &channel_id, &val) != 3)
1041 continue;
1042
Roel Kluin84ed1a12009-10-23 16:03:08 +02001043 if (reg < 0x49 && val <= 0xffffffff && channel_id <= 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
1045 }
1046}
1047
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001048static int __devinit snd_emu10k1x_proc_init(struct emu10k1x * emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001050 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 if(! snd_card_proc_new(emu->card, "emu10k1x_regs", &entry)) {
Takashi Iwaibf850202006-04-28 15:13:41 +02001053 snd_info_set_text_ops(entry, emu, snd_emu10k1x_proc_reg_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 entry->c.text.write = snd_emu10k1x_proc_reg_write;
Takashi Iwaibd7bf042005-04-12 16:27:28 +02001055 entry->mode |= S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 entry->private_data = emu;
1057 }
1058
1059 return 0;
1060}
1061
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001062#define snd_emu10k1x_shared_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001064static int snd_emu10k1x_shared_spdif_get(struct snd_kcontrol *kcontrol,
1065 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001067 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
1070
1071 return 0;
1072}
1073
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001074static int snd_emu10k1x_shared_spdif_put(struct snd_kcontrol *kcontrol,
1075 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001077 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 unsigned int val;
1079 int change = 0;
1080
1081 val = ucontrol->value.integer.value[0] ;
1082
1083 if (val) {
1084 // enable spdif output
1085 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
1086 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
1087 snd_emu10k1x_gpio_write(emu, 0x1000);
1088 } else {
1089 // disable spdif output
1090 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
1091 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
1092 snd_emu10k1x_gpio_write(emu, 0x1080);
1093 }
1094 return change;
1095}
1096
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001097static struct snd_kcontrol_new snd_emu10k1x_shared_spdif __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1100 .name = "Analog/Digital Output Jack",
1101 .info = snd_emu10k1x_shared_spdif_info,
1102 .get = snd_emu10k1x_shared_spdif_get,
1103 .put = snd_emu10k1x_shared_spdif_put
1104};
1105
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001106static int snd_emu10k1x_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1109 uinfo->count = 1;
1110 return 0;
1111}
1112
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001113static int snd_emu10k1x_spdif_get(struct snd_kcontrol *kcontrol,
1114 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001116 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1118
1119 ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
1120 ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
1121 ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
1122 ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
1123 return 0;
1124}
1125
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001126static int snd_emu10k1x_spdif_get_mask(struct snd_kcontrol *kcontrol,
1127 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 ucontrol->value.iec958.status[0] = 0xff;
1130 ucontrol->value.iec958.status[1] = 0xff;
1131 ucontrol->value.iec958.status[2] = 0xff;
1132 ucontrol->value.iec958.status[3] = 0xff;
1133 return 0;
1134}
1135
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001136static int snd_emu10k1x_spdif_put(struct snd_kcontrol *kcontrol,
1137 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001139 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1141 int change;
1142 unsigned int val;
1143
1144 val = (ucontrol->value.iec958.status[0] << 0) |
1145 (ucontrol->value.iec958.status[1] << 8) |
1146 (ucontrol->value.iec958.status[2] << 16) |
1147 (ucontrol->value.iec958.status[3] << 24);
1148 change = val != emu->spdif_bits[idx];
1149 if (change) {
1150 snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
1151 emu->spdif_bits[idx] = val;
1152 }
1153 return change;
1154}
1155
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001156static struct snd_kcontrol_new snd_emu10k1x_spdif_mask_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02001159 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1161 .count = 3,
1162 .info = snd_emu10k1x_spdif_info,
1163 .get = snd_emu10k1x_spdif_get_mask
1164};
1165
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001166static struct snd_kcontrol_new snd_emu10k1x_spdif_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Clemens Ladisch5549d542005-08-03 13:50:30 +02001168 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1170 .count = 3,
1171 .info = snd_emu10k1x_spdif_info,
1172 .get = snd_emu10k1x_spdif_get,
1173 .put = snd_emu10k1x_spdif_put
1174};
1175
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001176static int __devinit snd_emu10k1x_mixer(struct emu10k1x *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 int err;
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001179 struct snd_kcontrol *kctl;
1180 struct snd_card *card = emu->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu)) == NULL)
1183 return -ENOMEM;
1184 if ((err = snd_ctl_add(card, kctl)))
1185 return err;
1186 if ((kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu)) == NULL)
1187 return -ENOMEM;
1188 if ((err = snd_ctl_add(card, kctl)))
1189 return err;
1190 if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu)) == NULL)
1191 return -ENOMEM;
1192 if ((err = snd_ctl_add(card, kctl)))
1193 return err;
1194
1195 return 0;
1196}
1197
1198#define EMU10K1X_MIDI_MODE_INPUT (1<<0)
1199#define EMU10K1X_MIDI_MODE_OUTPUT (1<<1)
1200
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001201static inline unsigned char mpu401_read(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
1204}
1205
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001206static inline void mpu401_write(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int data, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
1209}
1210
1211#define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
1212#define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
1213#define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
1214#define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
1215
1216#define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
1217#define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
1218
1219#define MPU401_RESET 0xff
1220#define MPU401_ENTER_UART 0x3f
1221#define MPU401_ACK 0xfe
1222
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001223static void mpu401_clear_rx(struct emu10k1x *emu, struct emu10k1x_midi *mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 int timeout = 100000;
1226 for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
1227 mpu401_read_data(emu, mpu);
1228#ifdef CONFIG_SND_DEBUG
1229 if (timeout <= 0)
1230 snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
1231#endif
1232}
1233
1234/*
1235
1236 */
1237
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001238static void do_emu10k1x_midi_interrupt(struct emu10k1x *emu,
1239 struct emu10k1x_midi *midi, unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 unsigned char byte;
1242
1243 if (midi->rmidi == NULL) {
1244 snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
1245 return;
1246 }
1247
1248 spin_lock(&midi->input_lock);
1249 if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
1250 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1251 mpu401_clear_rx(emu, midi);
1252 } else {
1253 byte = mpu401_read_data(emu, midi);
1254 if (midi->substream_input)
1255 snd_rawmidi_receive(midi->substream_input, &byte, 1);
1256 }
1257 }
1258 spin_unlock(&midi->input_lock);
1259
1260 spin_lock(&midi->output_lock);
1261 if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
1262 if (midi->substream_output &&
1263 snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
1264 mpu401_write_data(emu, midi, byte);
1265 } else {
1266 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1267 }
1268 }
1269 spin_unlock(&midi->output_lock);
1270}
1271
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001272static void snd_emu10k1x_midi_interrupt(struct emu10k1x *emu, unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
1275}
1276
Randy Dunlapb1308072006-07-04 14:25:26 +02001277static int snd_emu10k1x_midi_cmd(struct emu10k1x * emu,
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001278 struct emu10k1x_midi *midi, unsigned char cmd, int ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 unsigned long flags;
1281 int timeout, ok;
1282
1283 spin_lock_irqsave(&midi->input_lock, flags);
1284 mpu401_write_data(emu, midi, 0x00);
1285 /* mpu401_clear_rx(emu, midi); */
1286
1287 mpu401_write_cmd(emu, midi, cmd);
1288 if (ack) {
1289 ok = 0;
1290 timeout = 10000;
1291 while (!ok && timeout-- > 0) {
1292 if (mpu401_input_avail(emu, midi)) {
1293 if (mpu401_read_data(emu, midi) == MPU401_ACK)
1294 ok = 1;
1295 }
1296 }
1297 if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
1298 ok = 1;
1299 } else {
1300 ok = 1;
1301 }
1302 spin_unlock_irqrestore(&midi->input_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +02001303 if (!ok) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
1305 cmd, emu->port,
1306 mpu401_read_stat(emu, midi),
1307 mpu401_read_data(emu, midi));
Randy Dunlapb1308072006-07-04 14:25:26 +02001308 return 1;
1309 }
1310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001313static int snd_emu10k1x_midi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001315 struct emu10k1x *emu;
1316 struct emu10k1x_midi *midi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 unsigned long flags;
1318
1319 emu = midi->emu;
Takashi Iwaida3cec32008-08-08 17:12:14 +02001320 if (snd_BUG_ON(!emu))
1321 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 spin_lock_irqsave(&midi->open_lock, flags);
1323 midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
1324 midi->substream_input = substream;
1325 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1326 spin_unlock_irqrestore(&midi->open_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +02001327 if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
1328 goto error_out;
1329 if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
1330 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 } else {
1332 spin_unlock_irqrestore(&midi->open_lock, flags);
1333 }
1334 return 0;
Randy Dunlapb1308072006-07-04 14:25:26 +02001335
1336error_out:
1337 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001340static int snd_emu10k1x_midi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001342 struct emu10k1x *emu;
1343 struct emu10k1x_midi *midi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 unsigned long flags;
1345
1346 emu = midi->emu;
Takashi Iwaida3cec32008-08-08 17:12:14 +02001347 if (snd_BUG_ON(!emu))
1348 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 spin_lock_irqsave(&midi->open_lock, flags);
1350 midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
1351 midi->substream_output = substream;
1352 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1353 spin_unlock_irqrestore(&midi->open_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +02001354 if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
1355 goto error_out;
1356 if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
1357 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 } else {
1359 spin_unlock_irqrestore(&midi->open_lock, flags);
1360 }
1361 return 0;
Randy Dunlapb1308072006-07-04 14:25:26 +02001362
1363error_out:
1364 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001367static int snd_emu10k1x_midi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001369 struct emu10k1x *emu;
1370 struct emu10k1x_midi *midi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 unsigned long flags;
Randy Dunlapb1308072006-07-04 14:25:26 +02001372 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 emu = midi->emu;
Takashi Iwaida3cec32008-08-08 17:12:14 +02001375 if (snd_BUG_ON(!emu))
1376 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 spin_lock_irqsave(&midi->open_lock, flags);
1378 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1379 midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
1380 midi->substream_input = NULL;
1381 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1382 spin_unlock_irqrestore(&midi->open_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +02001383 err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 } else {
1385 spin_unlock_irqrestore(&midi->open_lock, flags);
1386 }
Randy Dunlapb1308072006-07-04 14:25:26 +02001387 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001390static int snd_emu10k1x_midi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001392 struct emu10k1x *emu;
1393 struct emu10k1x_midi *midi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 unsigned long flags;
Randy Dunlapb1308072006-07-04 14:25:26 +02001395 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 emu = midi->emu;
Takashi Iwaida3cec32008-08-08 17:12:14 +02001398 if (snd_BUG_ON(!emu))
1399 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 spin_lock_irqsave(&midi->open_lock, flags);
1401 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1402 midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
1403 midi->substream_output = NULL;
1404 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1405 spin_unlock_irqrestore(&midi->open_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +02001406 err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 } else {
1408 spin_unlock_irqrestore(&midi->open_lock, flags);
1409 }
Randy Dunlapb1308072006-07-04 14:25:26 +02001410 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001413static void snd_emu10k1x_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001415 struct emu10k1x *emu;
1416 struct emu10k1x_midi *midi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 emu = midi->emu;
Takashi Iwaida3cec32008-08-08 17:12:14 +02001418 if (snd_BUG_ON(!emu))
1419 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 if (up)
1422 snd_emu10k1x_intr_enable(emu, midi->rx_enable);
1423 else
1424 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1425}
1426
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001427static void snd_emu10k1x_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001429 struct emu10k1x *emu;
1430 struct emu10k1x_midi *midi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 unsigned long flags;
1432
1433 emu = midi->emu;
Takashi Iwaida3cec32008-08-08 17:12:14 +02001434 if (snd_BUG_ON(!emu))
1435 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 if (up) {
1438 int max = 4;
1439 unsigned char byte;
1440
1441 /* try to send some amount of bytes here before interrupts */
1442 spin_lock_irqsave(&midi->output_lock, flags);
1443 while (max > 0) {
1444 if (mpu401_output_ready(emu, midi)) {
1445 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
1446 snd_rawmidi_transmit(substream, &byte, 1) != 1) {
1447 /* no more data */
1448 spin_unlock_irqrestore(&midi->output_lock, flags);
1449 return;
1450 }
1451 mpu401_write_data(emu, midi, byte);
1452 max--;
1453 } else {
1454 break;
1455 }
1456 }
1457 spin_unlock_irqrestore(&midi->output_lock, flags);
1458 snd_emu10k1x_intr_enable(emu, midi->tx_enable);
1459 } else {
1460 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1461 }
1462}
1463
1464/*
1465
1466 */
1467
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001468static struct snd_rawmidi_ops snd_emu10k1x_midi_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 .open = snd_emu10k1x_midi_output_open,
1471 .close = snd_emu10k1x_midi_output_close,
1472 .trigger = snd_emu10k1x_midi_output_trigger,
1473};
1474
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001475static struct snd_rawmidi_ops snd_emu10k1x_midi_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 .open = snd_emu10k1x_midi_input_open,
1478 .close = snd_emu10k1x_midi_input_close,
1479 .trigger = snd_emu10k1x_midi_input_trigger,
1480};
1481
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001482static void snd_emu10k1x_midi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001484 struct emu10k1x_midi *midi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 midi->interrupt = NULL;
1486 midi->rmidi = NULL;
1487}
1488
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001489static int __devinit emu10k1x_midi_init(struct emu10k1x *emu,
1490 struct emu10k1x_midi *midi, int device, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001492 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 int err;
1494
1495 if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
1496 return err;
1497 midi->emu = emu;
1498 spin_lock_init(&midi->open_lock);
1499 spin_lock_init(&midi->input_lock);
1500 spin_lock_init(&midi->output_lock);
1501 strcpy(rmidi->name, name);
1502 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
1503 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
1504 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1505 SNDRV_RAWMIDI_INFO_INPUT |
1506 SNDRV_RAWMIDI_INFO_DUPLEX;
1507 rmidi->private_data = midi;
1508 rmidi->private_free = snd_emu10k1x_midi_free;
1509 midi->rmidi = rmidi;
1510 return 0;
1511}
1512
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001513static int __devinit snd_emu10k1x_midi(struct emu10k1x *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001515 struct emu10k1x_midi *midi = &emu->midi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 int err;
1517
1518 if ((err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)")) < 0)
1519 return err;
1520
1521 midi->tx_enable = INTE_MIDITXENABLE;
1522 midi->rx_enable = INTE_MIDIRXENABLE;
1523 midi->port = MUDATA;
1524 midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
1525 midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
1526 midi->interrupt = snd_emu10k1x_midi_interrupt;
1527 return 0;
1528}
1529
1530static int __devinit snd_emu10k1x_probe(struct pci_dev *pci,
1531 const struct pci_device_id *pci_id)
1532{
1533 static int dev;
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001534 struct snd_card *card;
1535 struct emu10k1x *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 int err;
1537
1538 if (dev >= SNDRV_CARDS)
1539 return -ENODEV;
1540 if (!enable[dev]) {
1541 dev++;
1542 return -ENOENT;
1543 }
1544
Takashi Iwaie58de7b2008-12-28 16:44:30 +01001545 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
1546 if (err < 0)
1547 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 if ((err = snd_emu10k1x_create(card, pci, &chip)) < 0) {
1550 snd_card_free(card);
1551 return err;
1552 }
1553
1554 if ((err = snd_emu10k1x_pcm(chip, 0, NULL)) < 0) {
1555 snd_card_free(card);
1556 return err;
1557 }
1558 if ((err = snd_emu10k1x_pcm(chip, 1, NULL)) < 0) {
1559 snd_card_free(card);
1560 return err;
1561 }
1562 if ((err = snd_emu10k1x_pcm(chip, 2, NULL)) < 0) {
1563 snd_card_free(card);
1564 return err;
1565 }
1566
1567 if ((err = snd_emu10k1x_ac97(chip)) < 0) {
1568 snd_card_free(card);
1569 return err;
1570 }
1571
1572 if ((err = snd_emu10k1x_mixer(chip)) < 0) {
1573 snd_card_free(card);
1574 return err;
1575 }
1576
1577 if ((err = snd_emu10k1x_midi(chip)) < 0) {
1578 snd_card_free(card);
1579 return err;
1580 }
1581
1582 snd_emu10k1x_proc_init(chip);
1583
1584 strcpy(card->driver, "EMU10K1X");
1585 strcpy(card->shortname, "Dell Sound Blaster Live!");
1586 sprintf(card->longname, "%s at 0x%lx irq %i",
1587 card->shortname, chip->port, chip->irq);
1588
Takashi Iwai682fc0a2007-11-26 08:45:43 +01001589 snd_card_set_dev(card, &pci->dev);
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if ((err = snd_card_register(card)) < 0) {
1592 snd_card_free(card);
1593 return err;
1594 }
1595
1596 pci_set_drvdata(pci, card);
1597 dev++;
1598 return 0;
1599}
1600
1601static void __devexit snd_emu10k1x_remove(struct pci_dev *pci)
1602{
1603 snd_card_free(pci_get_drvdata(pci));
1604 pci_set_drvdata(pci, NULL);
1605}
1606
1607// PCI IDs
Alexey Dobriyancebe41d2010-02-06 00:21:03 +02001608static DEFINE_PCI_DEVICE_TABLE(snd_emu10k1x_ids) = {
Joe Perches0d7392e2009-06-24 23:18:02 -07001609 { PCI_VDEVICE(CREATIVE, 0x0006), 0 }, /* Dell OEM version (EMU10K1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 { 0, }
1611};
1612MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
1613
1614// pci_driver definition
1615static struct pci_driver driver = {
1616 .name = "EMU10K1X",
1617 .id_table = snd_emu10k1x_ids,
1618 .probe = snd_emu10k1x_probe,
1619 .remove = __devexit_p(snd_emu10k1x_remove),
1620};
1621
1622// initialization of the module
1623static int __init alsa_card_emu10k1x_init(void)
1624{
Alexey Dobriyan80b556f2006-08-16 12:56:53 +02001625 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628// clean up the module
1629static void __exit alsa_card_emu10k1x_exit(void)
1630{
1631 pci_unregister_driver(&driver);
1632}
1633
1634module_init(alsa_card_emu10k1x_init)
1635module_exit(alsa_card_emu10k1x_exit)