blob: 1ec7ebaff9e9f534f92fbbf0723f30082cce7320 [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 */
32#include <sound/driver.h>
33#include <linux/init.h>
34#include <linux/interrupt.h>
35#include <linux/pci.h>
Tobias Klauser9d2f9282006-03-22 10:53:19 +010036#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/slab.h>
38#include <linux/moduleparam.h>
39#include <sound/core.h>
40#include <sound/initval.h>
41#include <sound/pcm.h>
42#include <sound/ac97_codec.h>
43#include <sound/info.h>
44#include <sound/rawmidi.h>
45
46MODULE_AUTHOR("Francisco Moraes <fmoraes@nc.rr.com>");
47MODULE_DESCRIPTION("EMU10K1X");
48MODULE_LICENSE("GPL");
49MODULE_SUPPORTED_DEVICE("{{Dell Creative Labs,SB Live!}");
50
51// module parameters (see "Module Parameters")
52static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
53static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
54static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
55
56module_param_array(index, int, NULL, 0444);
57MODULE_PARM_DESC(index, "Index value for the EMU10K1X soundcard.");
58module_param_array(id, charp, NULL, 0444);
59MODULE_PARM_DESC(id, "ID string for the EMU10K1X soundcard.");
60module_param_array(enable, bool, NULL, 0444);
61MODULE_PARM_DESC(enable, "Enable the EMU10K1X soundcard.");
62
63
64// some definitions were borrowed from emu10k1 driver as they seem to be the same
65/************************************************************************************************/
66/* PCI function 0 registers, address = <val> + PCIBASE0 */
67/************************************************************************************************/
68
69#define PTR 0x00 /* Indexed register set pointer register */
70 /* NOTE: The CHANNELNUM and ADDRESS words can */
71 /* be modified independently of each other. */
72
73#define DATA 0x04 /* Indexed register set data register */
74
75#define IPR 0x08 /* Global interrupt pending register */
76 /* Clear pending interrupts by writing a 1 to */
77 /* the relevant bits and zero to the other bits */
78#define IPR_MIDITRANSBUFEMPTY 0x00000001 /* MIDI UART transmit buffer empty */
79#define IPR_MIDIRECVBUFEMPTY 0x00000002 /* MIDI UART receive buffer empty */
80#define IPR_CH_0_LOOP 0x00000800 /* Channel 0 loop */
81#define IPR_CH_0_HALF_LOOP 0x00000100 /* Channel 0 half loop */
82#define IPR_CAP_0_LOOP 0x00080000 /* Channel capture loop */
83#define IPR_CAP_0_HALF_LOOP 0x00010000 /* Channel capture half loop */
84
85#define INTE 0x0c /* Interrupt enable register */
86#define INTE_MIDITXENABLE 0x00000001 /* Enable MIDI transmit-buffer-empty interrupts */
87#define INTE_MIDIRXENABLE 0x00000002 /* Enable MIDI receive-buffer-empty interrupts */
88#define INTE_CH_0_LOOP 0x00000800 /* Channel 0 loop */
89#define INTE_CH_0_HALF_LOOP 0x00000100 /* Channel 0 half loop */
90#define INTE_CAP_0_LOOP 0x00080000 /* Channel capture loop */
91#define INTE_CAP_0_HALF_LOOP 0x00010000 /* Channel capture half loop */
92
93#define HCFG 0x14 /* Hardware config register */
94
95#define HCFG_LOCKSOUNDCACHE 0x00000008 /* 1 = Cancel bustmaster accesses to soundcache */
96 /* NOTE: This should generally never be used. */
97#define HCFG_AUDIOENABLE 0x00000001 /* 0 = CODECs transmit zero-valued samples */
98 /* Should be set to 1 when the EMU10K1 is */
99 /* completely initialized. */
100#define GPIO 0x18 /* Defaults: 00001080-Analog, 00001000-SPDIF. */
101
102
103#define AC97DATA 0x1c /* AC97 register set data register (16 bit) */
104
105#define AC97ADDRESS 0x1e /* AC97 register set address register (8 bit) */
106
107/********************************************************************************************************/
108/* Emu10k1x pointer-offset register set, accessed through the PTR and DATA registers */
109/********************************************************************************************************/
110#define PLAYBACK_LIST_ADDR 0x00 /* Base DMA address of a list of pointers to each period/size */
111 /* One list entry: 4 bytes for DMA address,
112 * 4 bytes for period_size << 16.
113 * One list entry is 8 bytes long.
114 * One list entry for each period in the buffer.
115 */
116#define PLAYBACK_LIST_SIZE 0x01 /* Size of list in bytes << 16. E.g. 8 periods -> 0x00380000 */
117#define PLAYBACK_LIST_PTR 0x02 /* Pointer to the current period being played */
118#define PLAYBACK_DMA_ADDR 0x04 /* Playback DMA addresss */
119#define PLAYBACK_PERIOD_SIZE 0x05 /* Playback period size */
120#define PLAYBACK_POINTER 0x06 /* Playback period pointer. Sample currently in DAC */
121#define PLAYBACK_UNKNOWN1 0x07
122#define PLAYBACK_UNKNOWN2 0x08
123
124/* Only one capture channel supported */
125#define CAPTURE_DMA_ADDR 0x10 /* Capture DMA address */
126#define CAPTURE_BUFFER_SIZE 0x11 /* Capture buffer size */
127#define CAPTURE_POINTER 0x12 /* Capture buffer pointer. Sample currently in ADC */
128#define CAPTURE_UNKNOWN 0x13
129
130/* From 0x20 - 0x3f, last samples played on each channel */
131
132#define TRIGGER_CHANNEL 0x40 /* Trigger channel playback */
133#define TRIGGER_CHANNEL_0 0x00000001 /* Trigger channel 0 */
134#define TRIGGER_CHANNEL_1 0x00000002 /* Trigger channel 1 */
135#define TRIGGER_CHANNEL_2 0x00000004 /* Trigger channel 2 */
136#define TRIGGER_CAPTURE 0x00000100 /* Trigger capture channel */
137
138#define ROUTING 0x41 /* Setup sound routing ? */
139#define ROUTING_FRONT_LEFT 0x00000001
140#define ROUTING_FRONT_RIGHT 0x00000002
141#define ROUTING_REAR_LEFT 0x00000004
142#define ROUTING_REAR_RIGHT 0x00000008
143#define ROUTING_CENTER_LFE 0x00010000
144
145#define SPCS0 0x42 /* SPDIF output Channel Status 0 register */
146
147#define SPCS1 0x43 /* SPDIF output Channel Status 1 register */
148
149#define SPCS2 0x44 /* SPDIF output Channel Status 2 register */
150
151#define SPCS_CLKACCYMASK 0x30000000 /* Clock accuracy */
152#define SPCS_CLKACCY_1000PPM 0x00000000 /* 1000 parts per million */
153#define SPCS_CLKACCY_50PPM 0x10000000 /* 50 parts per million */
154#define SPCS_CLKACCY_VARIABLE 0x20000000 /* Variable accuracy */
155#define SPCS_SAMPLERATEMASK 0x0f000000 /* Sample rate */
156#define SPCS_SAMPLERATE_44 0x00000000 /* 44.1kHz sample rate */
157#define SPCS_SAMPLERATE_48 0x02000000 /* 48kHz sample rate */
158#define SPCS_SAMPLERATE_32 0x03000000 /* 32kHz sample rate */
159#define SPCS_CHANNELNUMMASK 0x00f00000 /* Channel number */
160#define SPCS_CHANNELNUM_UNSPEC 0x00000000 /* Unspecified channel number */
161#define SPCS_CHANNELNUM_LEFT 0x00100000 /* Left channel */
162#define SPCS_CHANNELNUM_RIGHT 0x00200000 /* Right channel */
163#define SPCS_SOURCENUMMASK 0x000f0000 /* Source number */
164#define SPCS_SOURCENUM_UNSPEC 0x00000000 /* Unspecified source number */
165#define SPCS_GENERATIONSTATUS 0x00008000 /* Originality flag (see IEC-958 spec) */
166#define SPCS_CATEGORYCODEMASK 0x00007f00 /* Category code (see IEC-958 spec) */
167#define SPCS_MODEMASK 0x000000c0 /* Mode (see IEC-958 spec) */
168#define SPCS_EMPHASISMASK 0x00000038 /* Emphasis */
169#define SPCS_EMPHASIS_NONE 0x00000000 /* No emphasis */
170#define SPCS_EMPHASIS_50_15 0x00000008 /* 50/15 usec 2 channel */
171#define SPCS_COPYRIGHT 0x00000004 /* Copyright asserted flag -- do not modify */
172#define SPCS_NOTAUDIODATA 0x00000002 /* 0 = Digital audio, 1 = not audio */
173#define SPCS_PROFESSIONAL 0x00000001 /* 0 = Consumer (IEC-958), 1 = pro (AES3-1992) */
174
175#define SPDIF_SELECT 0x45 /* Enables SPDIF or Analogue outputs 0-Analogue, 0x700-SPDIF */
176
177/* This is the MPU port on the card */
178#define MUDATA 0x47
179#define MUCMD 0x48
180#define MUSTAT MUCMD
181
182/* From 0x50 - 0x5f, last samples captured */
183
184/**
185 * The hardware has 3 channels for playback and 1 for capture.
186 * - channel 0 is the front channel
187 * - channel 1 is the rear channel
188 * - channel 2 is the center/lfe chanel
189 * Volume is controlled by the AC97 for the front and rear channels by
190 * the PCM Playback Volume, Sigmatel Surround Playback Volume and
191 * Surround Playback Volume. The Sigmatel 4-Speaker Stereo switch affects
192 * the front/rear channel mixing in the REAR OUT jack. When using the
193 * 4-Speaker Stereo, both front and rear channels will be mixed in the
194 * REAR OUT.
195 * The center/lfe channel has no volume control and cannot be muted during
196 * playback.
197 */
198
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100199struct emu10k1x_voice {
200 struct emu10k1x *emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int number;
202 int use;
203
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100204 struct emu10k1x_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100207struct emu10k1x_pcm {
208 struct emu10k1x *emu;
209 struct snd_pcm_substream *substream;
210 struct emu10k1x_voice *voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 unsigned short running;
212};
213
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100214struct emu10k1x_midi {
215 struct emu10k1x *emu;
216 struct snd_rawmidi *rmidi;
217 struct snd_rawmidi_substream *substream_input;
218 struct snd_rawmidi_substream *substream_output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 unsigned int midi_mode;
220 spinlock_t input_lock;
221 spinlock_t output_lock;
222 spinlock_t open_lock;
223 int tx_enable, rx_enable;
224 int port;
225 int ipr_tx, ipr_rx;
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100226 void (*interrupt)(struct emu10k1x *emu, unsigned int status);
227};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229// definition of the chip-specific record
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100230struct emu10k1x {
231 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct pci_dev *pci;
233
234 unsigned long port;
235 struct resource *res_port;
236 int irq;
237
Takashi Iwai01f681d2006-11-16 15:39:07 +0100238 unsigned char revision; /* chip revision */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 unsigned int serial; /* serial number */
240 unsigned short model; /* subsystem id */
241
242 spinlock_t emu_lock;
243 spinlock_t voice_lock;
244
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100245 struct snd_ac97 *ac97;
246 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100248 struct emu10k1x_voice voices[3];
249 struct emu10k1x_voice capture_voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 u32 spdif_bits[3]; // SPDIF out setup
251
252 struct snd_dma_buffer dma_buffer;
253
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100254 struct emu10k1x_midi midi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
257/* hardware definition */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100258static struct snd_pcm_hardware snd_emu10k1x_playback_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .info = (SNDRV_PCM_INFO_MMAP |
260 SNDRV_PCM_INFO_INTERLEAVED |
261 SNDRV_PCM_INFO_BLOCK_TRANSFER |
262 SNDRV_PCM_INFO_MMAP_VALID),
263 .formats = SNDRV_PCM_FMTBIT_S16_LE,
264 .rates = SNDRV_PCM_RATE_48000,
265 .rate_min = 48000,
266 .rate_max = 48000,
267 .channels_min = 2,
268 .channels_max = 2,
269 .buffer_bytes_max = (32*1024),
270 .period_bytes_min = 64,
271 .period_bytes_max = (16*1024),
272 .periods_min = 2,
273 .periods_max = 8,
274 .fifo_size = 0,
275};
276
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100277static struct snd_pcm_hardware snd_emu10k1x_capture_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 .info = (SNDRV_PCM_INFO_MMAP |
279 SNDRV_PCM_INFO_INTERLEAVED |
280 SNDRV_PCM_INFO_BLOCK_TRANSFER |
281 SNDRV_PCM_INFO_MMAP_VALID),
282 .formats = SNDRV_PCM_FMTBIT_S16_LE,
283 .rates = SNDRV_PCM_RATE_48000,
284 .rate_min = 48000,
285 .rate_max = 48000,
286 .channels_min = 2,
287 .channels_max = 2,
288 .buffer_bytes_max = (32*1024),
289 .period_bytes_min = 64,
290 .period_bytes_max = (16*1024),
291 .periods_min = 2,
292 .periods_max = 2,
293 .fifo_size = 0,
294};
295
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100296static unsigned int snd_emu10k1x_ptr_read(struct emu10k1x * emu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unsigned int reg,
298 unsigned int chn)
299{
300 unsigned long flags;
301 unsigned int regptr, val;
302
303 regptr = (reg << 16) | chn;
304
305 spin_lock_irqsave(&emu->emu_lock, flags);
306 outl(regptr, emu->port + PTR);
307 val = inl(emu->port + DATA);
308 spin_unlock_irqrestore(&emu->emu_lock, flags);
309 return val;
310}
311
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100312static void snd_emu10k1x_ptr_write(struct emu10k1x *emu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned int reg,
314 unsigned int chn,
315 unsigned int data)
316{
317 unsigned int regptr;
318 unsigned long flags;
319
320 regptr = (reg << 16) | chn;
321
322 spin_lock_irqsave(&emu->emu_lock, flags);
323 outl(regptr, emu->port + PTR);
324 outl(data, emu->port + DATA);
325 spin_unlock_irqrestore(&emu->emu_lock, flags);
326}
327
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100328static void snd_emu10k1x_intr_enable(struct emu10k1x *emu, unsigned int intrenb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 unsigned long flags;
331 unsigned int enable;
332
333 spin_lock_irqsave(&emu->emu_lock, flags);
334 enable = inl(emu->port + INTE) | intrenb;
335 outl(enable, emu->port + INTE);
336 spin_unlock_irqrestore(&emu->emu_lock, flags);
337}
338
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100339static void snd_emu10k1x_intr_disable(struct emu10k1x *emu, unsigned int intrenb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 unsigned long flags;
342 unsigned int enable;
343
344 spin_lock_irqsave(&emu->emu_lock, flags);
345 enable = inl(emu->port + INTE) & ~intrenb;
346 outl(enable, emu->port + INTE);
347 spin_unlock_irqrestore(&emu->emu_lock, flags);
348}
349
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100350static void snd_emu10k1x_gpio_write(struct emu10k1x *emu, unsigned int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 unsigned long flags;
353
354 spin_lock_irqsave(&emu->emu_lock, flags);
355 outl(value, emu->port + GPIO);
356 spin_unlock_irqrestore(&emu->emu_lock, flags);
357}
358
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100359static void snd_emu10k1x_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Jesper Juhl4d572772005-05-30 17:30:32 +0200361 kfree(runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100364static void snd_emu10k1x_pcm_interrupt(struct emu10k1x *emu, struct emu10k1x_voice *voice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100366 struct emu10k1x_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if ((epcm = voice->epcm) == NULL)
369 return;
370 if (epcm->substream == NULL)
371 return;
372#if 0
373 snd_printk(KERN_INFO "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
374 epcm->substream->ops->pointer(epcm->substream),
375 snd_pcm_lib_period_bytes(epcm->substream),
376 snd_pcm_lib_buffer_bytes(epcm->substream));
377#endif
378 snd_pcm_period_elapsed(epcm->substream);
379}
380
381/* open callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100382static int snd_emu10k1x_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100384 struct emu10k1x *chip = snd_pcm_substream_chip(substream);
385 struct emu10k1x_pcm *epcm;
386 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 int err;
388
389 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
390 return err;
391 }
392 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
393 return err;
394
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200395 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (epcm == NULL)
397 return -ENOMEM;
398 epcm->emu = chip;
399 epcm->substream = substream;
400
401 runtime->private_data = epcm;
402 runtime->private_free = snd_emu10k1x_pcm_free_substream;
403
404 runtime->hw = snd_emu10k1x_playback_hw;
405
406 return 0;
407}
408
409/* close callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100410static int snd_emu10k1x_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 return 0;
413}
414
415/* hw_params callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100416static int snd_emu10k1x_pcm_hw_params(struct snd_pcm_substream *substream,
417 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100419 struct snd_pcm_runtime *runtime = substream->runtime;
420 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 if (! epcm->voice) {
423 epcm->voice = &epcm->emu->voices[substream->pcm->device];
424 epcm->voice->use = 1;
425 epcm->voice->epcm = epcm;
426 }
427
428 return snd_pcm_lib_malloc_pages(substream,
429 params_buffer_bytes(hw_params));
430}
431
432/* hw_free callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100433static int snd_emu10k1x_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100435 struct snd_pcm_runtime *runtime = substream->runtime;
436 struct emu10k1x_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (runtime->private_data == NULL)
439 return 0;
440
441 epcm = runtime->private_data;
442
443 if (epcm->voice) {
444 epcm->voice->use = 0;
445 epcm->voice->epcm = NULL;
446 epcm->voice = NULL;
447 }
448
449 return snd_pcm_lib_free_pages(substream);
450}
451
452/* prepare callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100453static int snd_emu10k1x_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100455 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
456 struct snd_pcm_runtime *runtime = substream->runtime;
457 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int voice = epcm->voice->number;
459 u32 *table_base = (u32 *)(emu->dma_buffer.area+1024*voice);
460 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
461 int i;
462
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +0100463 for(i = 0; i < runtime->periods; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *table_base++=runtime->dma_addr+(i*period_size_bytes);
465 *table_base++=period_size_bytes<<16;
466 }
467
468 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_ADDR, voice, emu->dma_buffer.addr+1024*voice);
469 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_SIZE, voice, (runtime->periods - 1) << 19);
470 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_PTR, voice, 0);
471 snd_emu10k1x_ptr_write(emu, PLAYBACK_POINTER, voice, 0);
472 snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN1, voice, 0);
473 snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN2, voice, 0);
474 snd_emu10k1x_ptr_write(emu, PLAYBACK_DMA_ADDR, voice, runtime->dma_addr);
475
476 snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
477
478 return 0;
479}
480
481/* trigger callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100482static int snd_emu10k1x_pcm_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int cmd)
484{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100485 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
486 struct snd_pcm_runtime *runtime = substream->runtime;
487 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int channel = epcm->voice->number;
489 int result = 0;
490
491// snd_printk(KERN_INFO "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n", (int)emu, cmd, (int)substream->ops->pointer(substream));
492
493 switch (cmd) {
494 case SNDRV_PCM_TRIGGER_START:
495 if(runtime->periods == 2)
496 snd_emu10k1x_intr_enable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
497 else
498 snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
499 epcm->running = 1;
500 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
501 break;
502 case SNDRV_PCM_TRIGGER_STOP:
503 epcm->running = 0;
504 snd_emu10k1x_intr_disable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
505 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CHANNEL_0<<channel));
506 break;
507 default:
508 result = -EINVAL;
509 break;
510 }
511 return result;
512}
513
514/* pointer callback */
515static snd_pcm_uframes_t
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100516snd_emu10k1x_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100518 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
519 struct snd_pcm_runtime *runtime = substream->runtime;
520 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int channel = epcm->voice->number;
522 snd_pcm_uframes_t ptr = 0, ptr1 = 0, ptr2= 0,ptr3 = 0,ptr4 = 0;
523
524 if (!epcm->running)
525 return 0;
526
527 ptr3 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
528 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
529 ptr4 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
530
531 if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
532 return 0;
533
534 if (ptr3 != ptr4)
535 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
536 ptr2 = bytes_to_frames(runtime, ptr1);
537 ptr2 += (ptr4 >> 3) * runtime->period_size;
538 ptr = ptr2;
539
540 if (ptr >= runtime->buffer_size)
541 ptr -= runtime->buffer_size;
542
543 return ptr;
544}
545
546/* operators */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100547static struct snd_pcm_ops snd_emu10k1x_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 .open = snd_emu10k1x_playback_open,
549 .close = snd_emu10k1x_playback_close,
550 .ioctl = snd_pcm_lib_ioctl,
551 .hw_params = snd_emu10k1x_pcm_hw_params,
552 .hw_free = snd_emu10k1x_pcm_hw_free,
553 .prepare = snd_emu10k1x_pcm_prepare,
554 .trigger = snd_emu10k1x_pcm_trigger,
555 .pointer = snd_emu10k1x_pcm_pointer,
556};
557
558/* open_capture callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100559static int snd_emu10k1x_pcm_open_capture(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100561 struct emu10k1x *chip = snd_pcm_substream_chip(substream);
562 struct emu10k1x_pcm *epcm;
563 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int err;
565
566 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
567 return err;
568 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
569 return err;
570
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200571 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (epcm == NULL)
573 return -ENOMEM;
574
575 epcm->emu = chip;
576 epcm->substream = substream;
577
578 runtime->private_data = epcm;
579 runtime->private_free = snd_emu10k1x_pcm_free_substream;
580
581 runtime->hw = snd_emu10k1x_capture_hw;
582
583 return 0;
584}
585
586/* close callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100587static int snd_emu10k1x_pcm_close_capture(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 return 0;
590}
591
592/* hw_params callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100593static int snd_emu10k1x_pcm_hw_params_capture(struct snd_pcm_substream *substream,
594 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100596 struct snd_pcm_runtime *runtime = substream->runtime;
597 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 if (! epcm->voice) {
600 if (epcm->emu->capture_voice.use)
601 return -EBUSY;
602 epcm->voice = &epcm->emu->capture_voice;
603 epcm->voice->epcm = epcm;
604 epcm->voice->use = 1;
605 }
606
607 return snd_pcm_lib_malloc_pages(substream,
608 params_buffer_bytes(hw_params));
609}
610
611/* hw_free callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100612static int snd_emu10k1x_pcm_hw_free_capture(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100614 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100616 struct emu10k1x_pcm *epcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 if (runtime->private_data == NULL)
619 return 0;
620 epcm = runtime->private_data;
621
622 if (epcm->voice) {
623 epcm->voice->use = 0;
624 epcm->voice->epcm = NULL;
625 epcm->voice = NULL;
626 }
627
628 return snd_pcm_lib_free_pages(substream);
629}
630
631/* prepare capture callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100632static int snd_emu10k1x_pcm_prepare_capture(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100634 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
635 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 snd_emu10k1x_ptr_write(emu, CAPTURE_DMA_ADDR, 0, runtime->dma_addr);
638 snd_emu10k1x_ptr_write(emu, CAPTURE_BUFFER_SIZE, 0, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
639 snd_emu10k1x_ptr_write(emu, CAPTURE_POINTER, 0, 0);
640 snd_emu10k1x_ptr_write(emu, CAPTURE_UNKNOWN, 0, 0);
641
642 return 0;
643}
644
645/* trigger_capture callback */
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100646static int snd_emu10k1x_pcm_trigger_capture(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int cmd)
648{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100649 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
650 struct snd_pcm_runtime *runtime = substream->runtime;
651 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 int result = 0;
653
654 switch (cmd) {
655 case SNDRV_PCM_TRIGGER_START:
656 snd_emu10k1x_intr_enable(emu, INTE_CAP_0_LOOP |
657 INTE_CAP_0_HALF_LOOP);
658 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|TRIGGER_CAPTURE);
659 epcm->running = 1;
660 break;
661 case SNDRV_PCM_TRIGGER_STOP:
662 epcm->running = 0;
663 snd_emu10k1x_intr_disable(emu, INTE_CAP_0_LOOP |
664 INTE_CAP_0_HALF_LOOP);
665 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CAPTURE));
666 break;
667 default:
668 result = -EINVAL;
669 break;
670 }
671 return result;
672}
673
674/* pointer_capture callback */
675static snd_pcm_uframes_t
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100676snd_emu10k1x_pcm_pointer_capture(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100678 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
679 struct snd_pcm_runtime *runtime = substream->runtime;
680 struct emu10k1x_pcm *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 snd_pcm_uframes_t ptr;
682
683 if (!epcm->running)
684 return 0;
685
686 ptr = bytes_to_frames(runtime, snd_emu10k1x_ptr_read(emu, CAPTURE_POINTER, 0));
687 if (ptr >= runtime->buffer_size)
688 ptr -= runtime->buffer_size;
689
690 return ptr;
691}
692
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100693static struct snd_pcm_ops snd_emu10k1x_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 .open = snd_emu10k1x_pcm_open_capture,
695 .close = snd_emu10k1x_pcm_close_capture,
696 .ioctl = snd_pcm_lib_ioctl,
697 .hw_params = snd_emu10k1x_pcm_hw_params_capture,
698 .hw_free = snd_emu10k1x_pcm_hw_free_capture,
699 .prepare = snd_emu10k1x_pcm_prepare_capture,
700 .trigger = snd_emu10k1x_pcm_trigger_capture,
701 .pointer = snd_emu10k1x_pcm_pointer_capture,
702};
703
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100704static unsigned short snd_emu10k1x_ac97_read(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 unsigned short reg)
706{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100707 struct emu10k1x *emu = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 unsigned long flags;
709 unsigned short val;
710
711 spin_lock_irqsave(&emu->emu_lock, flags);
712 outb(reg, emu->port + AC97ADDRESS);
713 val = inw(emu->port + AC97DATA);
714 spin_unlock_irqrestore(&emu->emu_lock, flags);
715 return val;
716}
717
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100718static void snd_emu10k1x_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned short reg, unsigned short val)
720{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100721 struct emu10k1x *emu = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unsigned long flags;
723
724 spin_lock_irqsave(&emu->emu_lock, flags);
725 outb(reg, emu->port + AC97ADDRESS);
726 outw(val, emu->port + AC97DATA);
727 spin_unlock_irqrestore(&emu->emu_lock, flags);
728}
729
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100730static int snd_emu10k1x_ac97(struct emu10k1x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100732 struct snd_ac97_bus *pbus;
733 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int err;
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100735 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 .write = snd_emu10k1x_ac97_write,
737 .read = snd_emu10k1x_ac97_read,
738 };
739
740 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
741 return err;
742 pbus->no_vra = 1; /* we don't need VRA */
743
744 memset(&ac97, 0, sizeof(ac97));
745 ac97.private_data = chip;
746 ac97.scaps = AC97_SCAP_NO_SPDIF;
747 return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
748}
749
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100750static int snd_emu10k1x_free(struct emu10k1x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
753 // disable interrupts
754 outl(0, chip->port + INTE);
755 // disable audio
756 outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
757
758 // release the i/o port
Takashi Iwaib1d57762005-10-10 11:56:31 +0200759 release_and_free_resource(chip->res_port);
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 // release the irq
762 if (chip->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +0100763 free_irq(chip->irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 // release the DMA
766 if (chip->dma_buffer.area) {
767 snd_dma_free_pages(&chip->dma_buffer);
768 }
769
770 pci_disable_device(chip->pci);
771
772 // release the data
773 kfree(chip);
774 return 0;
775}
776
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100777static int snd_emu10k1x_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100779 struct emu10k1x *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return snd_emu10k1x_free(chip);
781}
782
David Howells7d12e782006-10-05 14:55:46 +0100783static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 unsigned int status;
786
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100787 struct emu10k1x *chip = dev_id;
788 struct emu10k1x_voice *pvoice = chip->voices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 int i;
790 int mask;
791
792 status = inl(chip->port + IPR);
793
Takashi Iwai89173bd2005-11-17 10:43:53 +0100794 if (! status)
795 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Takashi Iwai89173bd2005-11-17 10:43:53 +0100797 // capture interrupt
798 if (status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100799 struct emu10k1x_voice *pvoice = &chip->capture_voice;
800 if (pvoice->use)
Takashi Iwai89173bd2005-11-17 10:43:53 +0100801 snd_emu10k1x_pcm_interrupt(chip, pvoice);
802 else
803 snd_emu10k1x_intr_disable(chip,
804 INTE_CAP_0_LOOP |
805 INTE_CAP_0_HALF_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Takashi Iwai89173bd2005-11-17 10:43:53 +0100807
808 mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
809 for (i = 0; i < 3; i++) {
810 if (status & mask) {
811 if (pvoice->use)
812 snd_emu10k1x_pcm_interrupt(chip, pvoice);
813 else
814 snd_emu10k1x_intr_disable(chip, mask);
815 }
816 pvoice++;
817 mask <<= 1;
818 }
819
820 if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
821 if (chip->midi.interrupt)
822 chip->midi.interrupt(chip, status);
823 else
824 snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
825 }
826
827 // acknowledge the interrupt if necessary
828 outl(status, chip->port + IPR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Takashi Iwai89173bd2005-11-17 10:43:53 +0100830 // snd_printk(KERN_INFO "interrupt %08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return IRQ_HANDLED;
832}
833
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100834static int __devinit snd_emu10k1x_pcm(struct emu10k1x *emu, int device, struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100836 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int err;
838 int capture = 0;
839
840 if (rpcm)
841 *rpcm = NULL;
842 if (device == 0)
843 capture = 1;
844
845 if ((err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm)) < 0)
846 return err;
847
848 pcm->private_data = emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 switch(device) {
851 case 0:
852 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
853 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
854 break;
855 case 1:
856 case 2:
857 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
858 break;
859 }
860
861 pcm->info_flags = 0;
862 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
863 switch(device) {
864 case 0:
865 strcpy(pcm->name, "EMU10K1X Front");
866 break;
867 case 1:
868 strcpy(pcm->name, "EMU10K1X Rear");
869 break;
870 case 2:
871 strcpy(pcm->name, "EMU10K1X Center/LFE");
872 break;
873 }
874 emu->pcm = pcm;
875
876 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
877 snd_dma_pci_data(emu->pci),
878 32*1024, 32*1024);
879
880 if (rpcm)
881 *rpcm = pcm;
882
883 return 0;
884}
885
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100886static int __devinit snd_emu10k1x_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 struct pci_dev *pci,
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100888 struct emu10k1x **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100890 struct emu10k1x *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 int err;
892 int ch;
Takashi Iwai4b32f1a2005-11-17 14:50:31 +0100893 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .dev_free = snd_emu10k1x_dev_free,
895 };
Tobias Klauser9d2f9282006-03-22 10:53:19 +0100896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 *rchip = NULL;
Tobias Klauser9d2f9282006-03-22 10:53:19 +0100898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if ((err = pci_enable_device(pci)) < 0)
900 return err;
Tobias Klauser9d2f9282006-03-22 10:53:19 +0100901 if (pci_set_dma_mask(pci, DMA_28BIT_MASK) < 0 ||
902 pci_set_consistent_dma_mask(pci, DMA_28BIT_MASK) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 snd_printk(KERN_ERR "error to set 28bit mask DMA\n");
904 pci_disable_device(pci);
905 return -ENXIO;
906 }
Tobias Klauser9d2f9282006-03-22 10:53:19 +0100907
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200908 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (chip == NULL) {
910 pci_disable_device(pci);
911 return -ENOMEM;
912 }
Tobias Klauser9d2f9282006-03-22 10:53:19 +0100913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 chip->card = card;
915 chip->pci = pci;
916 chip->irq = -1;
917
918 spin_lock_init(&chip->emu_lock);
919 spin_lock_init(&chip->voice_lock);
920
921 chip->port = pci_resource_start(pci, 0);
922 if ((chip->res_port = request_region(chip->port, 8,
923 "EMU10K1X")) == NULL) {
924 snd_printk(KERN_ERR "emu10k1x: cannot allocate the port 0x%lx\n", chip->port);
925 snd_emu10k1x_free(chip);
926 return -EBUSY;
927 }
928
929 if (request_irq(pci->irq, snd_emu10k1x_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +0100930 IRQF_SHARED, "EMU10K1X", chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 snd_printk(KERN_ERR "emu10k1x: cannot grab irq %d\n", pci->irq);
932 snd_emu10k1x_free(chip);
933 return -EBUSY;
934 }
935 chip->irq = pci->irq;
936
937 if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
938 4 * 1024, &chip->dma_buffer) < 0) {
939 snd_emu10k1x_free(chip);
940 return -ENOMEM;
941 }
942
943 pci_set_master(pci);
944 /* read revision & serial */
Auke Kok44c10132007-06-08 15:46:36 -0700945 chip->revision = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
947 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
948 snd_printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
949 chip->revision, chip->serial);
950
951 outl(0, chip->port + INTE);
952
953 for(ch = 0; ch < 3; ch++) {
954 chip->voices[ch].emu = chip;
955 chip->voices[ch].number = ch;
956 }
957
958 /*
959 * Init to 0x02109204 :
960 * Clock accuracy = 0 (1000ppm)
961 * Sample Rate = 2 (48kHz)
962 * Audio Channel = 1 (Left of 2)
963 * Source Number = 0 (Unspecified)
964 * Generation Status = 1 (Original for Cat Code 12)
965 * Cat Code = 12 (Digital Signal Mixer)
966 * Mode = 0 (Mode 0)
967 * Emphasis = 0 (None)
968 * CP = 1 (Copyright unasserted)
969 * AN = 0 (Audio data)
970 * P = 0 (Consumer)
971 */
972 snd_emu10k1x_ptr_write(chip, SPCS0, 0,
973 chip->spdif_bits[0] =
974 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
975 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
976 SPCS_GENERATIONSTATUS | 0x00001200 |
977 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
978 snd_emu10k1x_ptr_write(chip, SPCS1, 0,
979 chip->spdif_bits[1] =
980 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
981 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
982 SPCS_GENERATIONSTATUS | 0x00001200 |
983 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
984 snd_emu10k1x_ptr_write(chip, SPCS2, 0,
985 chip->spdif_bits[2] =
986 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
987 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
988 SPCS_GENERATIONSTATUS | 0x00001200 |
989 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
990
991 snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
992 snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
993 snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
994
995 outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
996
997 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
998 chip, &ops)) < 0) {
999 snd_emu10k1x_free(chip);
1000 return err;
1001 }
1002 *rchip = chip;
1003 return 0;
1004}
1005
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001006static void snd_emu10k1x_proc_reg_read(struct snd_info_entry *entry,
1007 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001009 struct emu10k1x *emu = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 unsigned long value,value1,value2;
1011 unsigned long flags;
1012 int i;
1013
1014 snd_iprintf(buffer, "Registers:\n\n");
1015 for(i = 0; i < 0x20; i+=4) {
1016 spin_lock_irqsave(&emu->emu_lock, flags);
1017 value = inl(emu->port + i);
1018 spin_unlock_irqrestore(&emu->emu_lock, flags);
1019 snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
1020 }
1021 snd_iprintf(buffer, "\nRegisters\n\n");
1022 for(i = 0; i <= 0x48; i++) {
1023 value = snd_emu10k1x_ptr_read(emu, i, 0);
1024 if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
1025 value1 = snd_emu10k1x_ptr_read(emu, i, 1);
1026 value2 = snd_emu10k1x_ptr_read(emu, i, 2);
1027 snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
1028 } else {
1029 snd_iprintf(buffer, "%02X: %08lX\n", i, value);
1030 }
1031 }
1032}
1033
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001034static void snd_emu10k1x_proc_reg_write(struct snd_info_entry *entry,
1035 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001037 struct emu10k1x *emu = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 char line[64];
1039 unsigned int reg, channel_id , val;
1040
1041 while (!snd_info_get_line(buffer, line, sizeof(line))) {
1042 if (sscanf(line, "%x %x %x", &reg, &channel_id, &val) != 3)
1043 continue;
1044
James Courtier-Dutton9f4bd5d2006-10-01 10:48:04 +01001045 if ((reg < 0x49) && (reg >= 0) && (val <= 0xffffffff)
1046 && (channel_id >= 0) && (channel_id <= 2) )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
1048 }
1049}
1050
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001051static int __devinit snd_emu10k1x_proc_init(struct emu10k1x * emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001053 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 if(! snd_card_proc_new(emu->card, "emu10k1x_regs", &entry)) {
Takashi Iwaibf850202006-04-28 15:13:41 +02001056 snd_info_set_text_ops(entry, emu, snd_emu10k1x_proc_reg_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 entry->c.text.write = snd_emu10k1x_proc_reg_write;
Takashi Iwaibd7bf042005-04-12 16:27:28 +02001058 entry->mode |= S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 entry->private_data = emu;
1060 }
1061
1062 return 0;
1063}
1064
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001065#define snd_emu10k1x_shared_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001067static int snd_emu10k1x_shared_spdif_get(struct snd_kcontrol *kcontrol,
1068 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001070 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
1073
1074 return 0;
1075}
1076
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001077static int snd_emu10k1x_shared_spdif_put(struct snd_kcontrol *kcontrol,
1078 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001080 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 unsigned int val;
1082 int change = 0;
1083
1084 val = ucontrol->value.integer.value[0] ;
1085
1086 if (val) {
1087 // enable spdif output
1088 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
1089 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
1090 snd_emu10k1x_gpio_write(emu, 0x1000);
1091 } else {
1092 // disable spdif output
1093 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
1094 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
1095 snd_emu10k1x_gpio_write(emu, 0x1080);
1096 }
1097 return change;
1098}
1099
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001100static struct snd_kcontrol_new snd_emu10k1x_shared_spdif __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1103 .name = "Analog/Digital Output Jack",
1104 .info = snd_emu10k1x_shared_spdif_info,
1105 .get = snd_emu10k1x_shared_spdif_get,
1106 .put = snd_emu10k1x_shared_spdif_put
1107};
1108
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001109static int snd_emu10k1x_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1112 uinfo->count = 1;
1113 return 0;
1114}
1115
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001116static int snd_emu10k1x_spdif_get(struct snd_kcontrol *kcontrol,
1117 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001119 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1121
1122 ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
1123 ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
1124 ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
1125 ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
1126 return 0;
1127}
1128
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001129static int snd_emu10k1x_spdif_get_mask(struct snd_kcontrol *kcontrol,
1130 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 ucontrol->value.iec958.status[0] = 0xff;
1133 ucontrol->value.iec958.status[1] = 0xff;
1134 ucontrol->value.iec958.status[2] = 0xff;
1135 ucontrol->value.iec958.status[3] = 0xff;
1136 return 0;
1137}
1138
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001139static int snd_emu10k1x_spdif_put(struct snd_kcontrol *kcontrol,
1140 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001142 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1144 int change;
1145 unsigned int val;
1146
1147 val = (ucontrol->value.iec958.status[0] << 0) |
1148 (ucontrol->value.iec958.status[1] << 8) |
1149 (ucontrol->value.iec958.status[2] << 16) |
1150 (ucontrol->value.iec958.status[3] << 24);
1151 change = val != emu->spdif_bits[idx];
1152 if (change) {
1153 snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
1154 emu->spdif_bits[idx] = val;
1155 }
1156 return change;
1157}
1158
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001159static struct snd_kcontrol_new snd_emu10k1x_spdif_mask_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch5549d542005-08-03 13:50:30 +02001162 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1164 .count = 3,
1165 .info = snd_emu10k1x_spdif_info,
1166 .get = snd_emu10k1x_spdif_get_mask
1167};
1168
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001169static struct snd_kcontrol_new snd_emu10k1x_spdif_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Clemens Ladisch5549d542005-08-03 13:50:30 +02001171 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1173 .count = 3,
1174 .info = snd_emu10k1x_spdif_info,
1175 .get = snd_emu10k1x_spdif_get,
1176 .put = snd_emu10k1x_spdif_put
1177};
1178
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001179static int __devinit snd_emu10k1x_mixer(struct emu10k1x *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 int err;
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001182 struct snd_kcontrol *kctl;
1183 struct snd_card *card = emu->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu)) == NULL)
1186 return -ENOMEM;
1187 if ((err = snd_ctl_add(card, kctl)))
1188 return err;
1189 if ((kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu)) == NULL)
1190 return -ENOMEM;
1191 if ((err = snd_ctl_add(card, kctl)))
1192 return err;
1193 if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu)) == NULL)
1194 return -ENOMEM;
1195 if ((err = snd_ctl_add(card, kctl)))
1196 return err;
1197
1198 return 0;
1199}
1200
1201#define EMU10K1X_MIDI_MODE_INPUT (1<<0)
1202#define EMU10K1X_MIDI_MODE_OUTPUT (1<<1)
1203
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001204static inline unsigned char mpu401_read(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
1207}
1208
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001209static inline void mpu401_write(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int data, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
1212}
1213
1214#define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
1215#define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
1216#define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
1217#define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
1218
1219#define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
1220#define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
1221
1222#define MPU401_RESET 0xff
1223#define MPU401_ENTER_UART 0x3f
1224#define MPU401_ACK 0xfe
1225
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001226static void mpu401_clear_rx(struct emu10k1x *emu, struct emu10k1x_midi *mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 int timeout = 100000;
1229 for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
1230 mpu401_read_data(emu, mpu);
1231#ifdef CONFIG_SND_DEBUG
1232 if (timeout <= 0)
1233 snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
1234#endif
1235}
1236
1237/*
1238
1239 */
1240
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001241static void do_emu10k1x_midi_interrupt(struct emu10k1x *emu,
1242 struct emu10k1x_midi *midi, unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 unsigned char byte;
1245
1246 if (midi->rmidi == NULL) {
1247 snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
1248 return;
1249 }
1250
1251 spin_lock(&midi->input_lock);
1252 if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
1253 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1254 mpu401_clear_rx(emu, midi);
1255 } else {
1256 byte = mpu401_read_data(emu, midi);
1257 if (midi->substream_input)
1258 snd_rawmidi_receive(midi->substream_input, &byte, 1);
1259 }
1260 }
1261 spin_unlock(&midi->input_lock);
1262
1263 spin_lock(&midi->output_lock);
1264 if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
1265 if (midi->substream_output &&
1266 snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
1267 mpu401_write_data(emu, midi, byte);
1268 } else {
1269 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1270 }
1271 }
1272 spin_unlock(&midi->output_lock);
1273}
1274
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001275static void snd_emu10k1x_midi_interrupt(struct emu10k1x *emu, unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
1278}
1279
Randy Dunlapb1308072006-07-04 14:25:26 +02001280static int snd_emu10k1x_midi_cmd(struct emu10k1x * emu,
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001281 struct emu10k1x_midi *midi, unsigned char cmd, int ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 unsigned long flags;
1284 int timeout, ok;
1285
1286 spin_lock_irqsave(&midi->input_lock, flags);
1287 mpu401_write_data(emu, midi, 0x00);
1288 /* mpu401_clear_rx(emu, midi); */
1289
1290 mpu401_write_cmd(emu, midi, cmd);
1291 if (ack) {
1292 ok = 0;
1293 timeout = 10000;
1294 while (!ok && timeout-- > 0) {
1295 if (mpu401_input_avail(emu, midi)) {
1296 if (mpu401_read_data(emu, midi) == MPU401_ACK)
1297 ok = 1;
1298 }
1299 }
1300 if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
1301 ok = 1;
1302 } else {
1303 ok = 1;
1304 }
1305 spin_unlock_irqrestore(&midi->input_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +02001306 if (!ok) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
1308 cmd, emu->port,
1309 mpu401_read_stat(emu, midi),
1310 mpu401_read_data(emu, midi));
Randy Dunlapb1308072006-07-04 14:25:26 +02001311 return 1;
1312 }
1313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001316static int snd_emu10k1x_midi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001318 struct emu10k1x *emu;
1319 struct emu10k1x_midi *midi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 unsigned long flags;
1321
1322 emu = midi->emu;
1323 snd_assert(emu, return -ENXIO);
1324 spin_lock_irqsave(&midi->open_lock, flags);
1325 midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
1326 midi->substream_input = substream;
1327 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1328 spin_unlock_irqrestore(&midi->open_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +02001329 if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
1330 goto error_out;
1331 if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
1332 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 } else {
1334 spin_unlock_irqrestore(&midi->open_lock, flags);
1335 }
1336 return 0;
Randy Dunlapb1308072006-07-04 14:25:26 +02001337
1338error_out:
1339 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001342static int snd_emu10k1x_midi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001344 struct emu10k1x *emu;
1345 struct emu10k1x_midi *midi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 unsigned long flags;
1347
1348 emu = midi->emu;
1349 snd_assert(emu, return -ENXIO);
1350 spin_lock_irqsave(&midi->open_lock, flags);
1351 midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
1352 midi->substream_output = substream;
1353 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1354 spin_unlock_irqrestore(&midi->open_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +02001355 if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
1356 goto error_out;
1357 if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
1358 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 } else {
1360 spin_unlock_irqrestore(&midi->open_lock, flags);
1361 }
1362 return 0;
Randy Dunlapb1308072006-07-04 14:25:26 +02001363
1364error_out:
1365 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001368static int snd_emu10k1x_midi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001370 struct emu10k1x *emu;
1371 struct emu10k1x_midi *midi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 unsigned long flags;
Randy Dunlapb1308072006-07-04 14:25:26 +02001373 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 emu = midi->emu;
1376 snd_assert(emu, return -ENXIO);
1377 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;
1398 snd_assert(emu, return -ENXIO);
1399 spin_lock_irqsave(&midi->open_lock, flags);
1400 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1401 midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
1402 midi->substream_output = NULL;
1403 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1404 spin_unlock_irqrestore(&midi->open_lock, flags);
Randy Dunlapb1308072006-07-04 14:25:26 +02001405 err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 } else {
1407 spin_unlock_irqrestore(&midi->open_lock, flags);
1408 }
Randy Dunlapb1308072006-07-04 14:25:26 +02001409 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001412static void snd_emu10k1x_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001414 struct emu10k1x *emu;
1415 struct emu10k1x_midi *midi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 emu = midi->emu;
1417 snd_assert(emu, return);
1418
1419 if (up)
1420 snd_emu10k1x_intr_enable(emu, midi->rx_enable);
1421 else
1422 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1423}
1424
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001425static void snd_emu10k1x_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001427 struct emu10k1x *emu;
1428 struct emu10k1x_midi *midi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 unsigned long flags;
1430
1431 emu = midi->emu;
1432 snd_assert(emu, return);
1433
1434 if (up) {
1435 int max = 4;
1436 unsigned char byte;
1437
1438 /* try to send some amount of bytes here before interrupts */
1439 spin_lock_irqsave(&midi->output_lock, flags);
1440 while (max > 0) {
1441 if (mpu401_output_ready(emu, midi)) {
1442 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
1443 snd_rawmidi_transmit(substream, &byte, 1) != 1) {
1444 /* no more data */
1445 spin_unlock_irqrestore(&midi->output_lock, flags);
1446 return;
1447 }
1448 mpu401_write_data(emu, midi, byte);
1449 max--;
1450 } else {
1451 break;
1452 }
1453 }
1454 spin_unlock_irqrestore(&midi->output_lock, flags);
1455 snd_emu10k1x_intr_enable(emu, midi->tx_enable);
1456 } else {
1457 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1458 }
1459}
1460
1461/*
1462
1463 */
1464
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001465static struct snd_rawmidi_ops snd_emu10k1x_midi_output =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 .open = snd_emu10k1x_midi_output_open,
1468 .close = snd_emu10k1x_midi_output_close,
1469 .trigger = snd_emu10k1x_midi_output_trigger,
1470};
1471
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001472static struct snd_rawmidi_ops snd_emu10k1x_midi_input =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 .open = snd_emu10k1x_midi_input_open,
1475 .close = snd_emu10k1x_midi_input_close,
1476 .trigger = snd_emu10k1x_midi_input_trigger,
1477};
1478
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001479static void snd_emu10k1x_midi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001481 struct emu10k1x_midi *midi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 midi->interrupt = NULL;
1483 midi->rmidi = NULL;
1484}
1485
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001486static int __devinit emu10k1x_midi_init(struct emu10k1x *emu,
1487 struct emu10k1x_midi *midi, int device, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001489 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 int err;
1491
1492 if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
1493 return err;
1494 midi->emu = emu;
1495 spin_lock_init(&midi->open_lock);
1496 spin_lock_init(&midi->input_lock);
1497 spin_lock_init(&midi->output_lock);
1498 strcpy(rmidi->name, name);
1499 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
1500 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
1501 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1502 SNDRV_RAWMIDI_INFO_INPUT |
1503 SNDRV_RAWMIDI_INFO_DUPLEX;
1504 rmidi->private_data = midi;
1505 rmidi->private_free = snd_emu10k1x_midi_free;
1506 midi->rmidi = rmidi;
1507 return 0;
1508}
1509
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001510static int __devinit snd_emu10k1x_midi(struct emu10k1x *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001512 struct emu10k1x_midi *midi = &emu->midi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 int err;
1514
1515 if ((err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)")) < 0)
1516 return err;
1517
1518 midi->tx_enable = INTE_MIDITXENABLE;
1519 midi->rx_enable = INTE_MIDIRXENABLE;
1520 midi->port = MUDATA;
1521 midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
1522 midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
1523 midi->interrupt = snd_emu10k1x_midi_interrupt;
1524 return 0;
1525}
1526
1527static int __devinit snd_emu10k1x_probe(struct pci_dev *pci,
1528 const struct pci_device_id *pci_id)
1529{
1530 static int dev;
Takashi Iwai4b32f1a2005-11-17 14:50:31 +01001531 struct snd_card *card;
1532 struct emu10k1x *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 int err;
1534
1535 if (dev >= SNDRV_CARDS)
1536 return -ENODEV;
1537 if (!enable[dev]) {
1538 dev++;
1539 return -ENOENT;
1540 }
1541
1542 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1543 if (card == NULL)
1544 return -ENOMEM;
1545
1546 if ((err = snd_emu10k1x_create(card, pci, &chip)) < 0) {
1547 snd_card_free(card);
1548 return err;
1549 }
1550
1551 if ((err = snd_emu10k1x_pcm(chip, 0, NULL)) < 0) {
1552 snd_card_free(card);
1553 return err;
1554 }
1555 if ((err = snd_emu10k1x_pcm(chip, 1, NULL)) < 0) {
1556 snd_card_free(card);
1557 return err;
1558 }
1559 if ((err = snd_emu10k1x_pcm(chip, 2, NULL)) < 0) {
1560 snd_card_free(card);
1561 return err;
1562 }
1563
1564 if ((err = snd_emu10k1x_ac97(chip)) < 0) {
1565 snd_card_free(card);
1566 return err;
1567 }
1568
1569 if ((err = snd_emu10k1x_mixer(chip)) < 0) {
1570 snd_card_free(card);
1571 return err;
1572 }
1573
1574 if ((err = snd_emu10k1x_midi(chip)) < 0) {
1575 snd_card_free(card);
1576 return err;
1577 }
1578
1579 snd_emu10k1x_proc_init(chip);
1580
1581 strcpy(card->driver, "EMU10K1X");
1582 strcpy(card->shortname, "Dell Sound Blaster Live!");
1583 sprintf(card->longname, "%s at 0x%lx irq %i",
1584 card->shortname, chip->port, chip->irq);
1585
1586 if ((err = snd_card_register(card)) < 0) {
1587 snd_card_free(card);
1588 return err;
1589 }
1590
1591 pci_set_drvdata(pci, card);
1592 dev++;
1593 return 0;
1594}
1595
1596static void __devexit snd_emu10k1x_remove(struct pci_dev *pci)
1597{
1598 snd_card_free(pci_get_drvdata(pci));
1599 pci_set_drvdata(pci, NULL);
1600}
1601
1602// PCI IDs
Takashi Iwaif40b6892006-07-05 16:51:05 +02001603static struct pci_device_id snd_emu10k1x_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 { 0x1102, 0x0006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Dell OEM version (EMU10K1) */
1605 { 0, }
1606};
1607MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
1608
1609// pci_driver definition
1610static struct pci_driver driver = {
1611 .name = "EMU10K1X",
1612 .id_table = snd_emu10k1x_ids,
1613 .probe = snd_emu10k1x_probe,
1614 .remove = __devexit_p(snd_emu10k1x_remove),
1615};
1616
1617// initialization of the module
1618static int __init alsa_card_emu10k1x_init(void)
1619{
Alexey Dobriyan80b556f2006-08-16 12:56:53 +02001620 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
1623// clean up the module
1624static void __exit alsa_card_emu10k1x_exit(void)
1625{
1626 pci_unregister_driver(&driver);
1627}
1628
1629module_init(alsa_card_emu10k1x_init)
1630module_exit(alsa_card_emu10k1x_exit)