[ALSA] ac97 - Remove ac9_enum definition from public header
[deliverable/linux.git] / sound / pci / emu10k1 / emu10k1x.c
CommitLineData
1da177e4
LT
1/*
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>
36#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 */
117#define PLAYBACK_DMA_ADDR 0x04 /* Playback DMA addresss */
118#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
187 * - channel 2 is the center/lfe chanel
188 * 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
198typedef struct snd_emu10k1x_voice emu10k1x_voice_t;
199typedef struct snd_emu10k1x emu10k1x_t;
200typedef struct snd_emu10k1x_pcm emu10k1x_pcm_t;
201
202struct snd_emu10k1x_voice {
203 emu10k1x_t *emu;
204 int number;
205 int use;
206
207 emu10k1x_pcm_t *epcm;
208};
209
210struct snd_emu10k1x_pcm {
211 emu10k1x_t *emu;
212 snd_pcm_substream_t *substream;
213 emu10k1x_voice_t *voice;
214 unsigned short running;
215};
216
217typedef struct {
218 struct snd_emu10k1x *emu;
219 snd_rawmidi_t *rmidi;
220 snd_rawmidi_substream_t *substream_input;
221 snd_rawmidi_substream_t *substream_output;
222 unsigned int midi_mode;
223 spinlock_t input_lock;
224 spinlock_t output_lock;
225 spinlock_t open_lock;
226 int tx_enable, rx_enable;
227 int port;
228 int ipr_tx, ipr_rx;
229 void (*interrupt)(emu10k1x_t *emu, unsigned int status);
230} emu10k1x_midi_t;
231
232// definition of the chip-specific record
233struct snd_emu10k1x {
234 snd_card_t *card;
235 struct pci_dev *pci;
236
237 unsigned long port;
238 struct resource *res_port;
239 int irq;
240
241 unsigned int revision; /* chip revision */
242 unsigned int serial; /* serial number */
243 unsigned short model; /* subsystem id */
244
245 spinlock_t emu_lock;
246 spinlock_t voice_lock;
247
248 ac97_t *ac97;
249 snd_pcm_t *pcm;
250
251 emu10k1x_voice_t voices[3];
252 emu10k1x_voice_t capture_voice;
253 u32 spdif_bits[3]; // SPDIF out setup
254
255 struct snd_dma_buffer dma_buffer;
256
257 emu10k1x_midi_t midi;
258};
259
260/* hardware definition */
261static snd_pcm_hardware_t snd_emu10k1x_playback_hw = {
262 .info = (SNDRV_PCM_INFO_MMAP |
263 SNDRV_PCM_INFO_INTERLEAVED |
264 SNDRV_PCM_INFO_BLOCK_TRANSFER |
265 SNDRV_PCM_INFO_MMAP_VALID),
266 .formats = SNDRV_PCM_FMTBIT_S16_LE,
267 .rates = SNDRV_PCM_RATE_48000,
268 .rate_min = 48000,
269 .rate_max = 48000,
270 .channels_min = 2,
271 .channels_max = 2,
272 .buffer_bytes_max = (32*1024),
273 .period_bytes_min = 64,
274 .period_bytes_max = (16*1024),
275 .periods_min = 2,
276 .periods_max = 8,
277 .fifo_size = 0,
278};
279
280static snd_pcm_hardware_t snd_emu10k1x_capture_hw = {
281 .info = (SNDRV_PCM_INFO_MMAP |
282 SNDRV_PCM_INFO_INTERLEAVED |
283 SNDRV_PCM_INFO_BLOCK_TRANSFER |
284 SNDRV_PCM_INFO_MMAP_VALID),
285 .formats = SNDRV_PCM_FMTBIT_S16_LE,
286 .rates = SNDRV_PCM_RATE_48000,
287 .rate_min = 48000,
288 .rate_max = 48000,
289 .channels_min = 2,
290 .channels_max = 2,
291 .buffer_bytes_max = (32*1024),
292 .period_bytes_min = 64,
293 .period_bytes_max = (16*1024),
294 .periods_min = 2,
295 .periods_max = 2,
296 .fifo_size = 0,
297};
298
299static unsigned int snd_emu10k1x_ptr_read(emu10k1x_t * emu,
300 unsigned int reg,
301 unsigned int chn)
302{
303 unsigned long flags;
304 unsigned int regptr, val;
305
306 regptr = (reg << 16) | chn;
307
308 spin_lock_irqsave(&emu->emu_lock, flags);
309 outl(regptr, emu->port + PTR);
310 val = inl(emu->port + DATA);
311 spin_unlock_irqrestore(&emu->emu_lock, flags);
312 return val;
313}
314
315static void snd_emu10k1x_ptr_write(emu10k1x_t *emu,
316 unsigned int reg,
317 unsigned int chn,
318 unsigned int data)
319{
320 unsigned int regptr;
321 unsigned long flags;
322
323 regptr = (reg << 16) | chn;
324
325 spin_lock_irqsave(&emu->emu_lock, flags);
326 outl(regptr, emu->port + PTR);
327 outl(data, emu->port + DATA);
328 spin_unlock_irqrestore(&emu->emu_lock, flags);
329}
330
331static void snd_emu10k1x_intr_enable(emu10k1x_t *emu, unsigned int intrenb)
332{
333 unsigned long flags;
334 unsigned int enable;
335
336 spin_lock_irqsave(&emu->emu_lock, flags);
337 enable = inl(emu->port + INTE) | intrenb;
338 outl(enable, emu->port + INTE);
339 spin_unlock_irqrestore(&emu->emu_lock, flags);
340}
341
342static void snd_emu10k1x_intr_disable(emu10k1x_t *emu, unsigned int intrenb)
343{
344 unsigned long flags;
345 unsigned int enable;
346
347 spin_lock_irqsave(&emu->emu_lock, flags);
348 enable = inl(emu->port + INTE) & ~intrenb;
349 outl(enable, emu->port + INTE);
350 spin_unlock_irqrestore(&emu->emu_lock, flags);
351}
352
353static void snd_emu10k1x_gpio_write(emu10k1x_t *emu, unsigned int value)
354{
355 unsigned long flags;
356
357 spin_lock_irqsave(&emu->emu_lock, flags);
358 outl(value, emu->port + GPIO);
359 spin_unlock_irqrestore(&emu->emu_lock, flags);
360}
361
362static void snd_emu10k1x_pcm_free_substream(snd_pcm_runtime_t *runtime)
363{
4d572776 364 kfree(runtime->private_data);
1da177e4
LT
365}
366
367static void snd_emu10k1x_pcm_interrupt(emu10k1x_t *emu, emu10k1x_voice_t *voice)
368{
369 emu10k1x_pcm_t *epcm;
370
371 if ((epcm = voice->epcm) == NULL)
372 return;
373 if (epcm->substream == NULL)
374 return;
375#if 0
376 snd_printk(KERN_INFO "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
377 epcm->substream->ops->pointer(epcm->substream),
378 snd_pcm_lib_period_bytes(epcm->substream),
379 snd_pcm_lib_buffer_bytes(epcm->substream));
380#endif
381 snd_pcm_period_elapsed(epcm->substream);
382}
383
384/* open callback */
385static int snd_emu10k1x_playback_open(snd_pcm_substream_t *substream)
386{
387 emu10k1x_t *chip = snd_pcm_substream_chip(substream);
388 emu10k1x_pcm_t *epcm;
389 snd_pcm_runtime_t *runtime = substream->runtime;
390 int err;
391
392 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
393 return err;
394 }
395 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
396 return err;
397
e560d8d8 398 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1da177e4
LT
399 if (epcm == NULL)
400 return -ENOMEM;
401 epcm->emu = chip;
402 epcm->substream = substream;
403
404 runtime->private_data = epcm;
405 runtime->private_free = snd_emu10k1x_pcm_free_substream;
406
407 runtime->hw = snd_emu10k1x_playback_hw;
408
409 return 0;
410}
411
412/* close callback */
413static int snd_emu10k1x_playback_close(snd_pcm_substream_t *substream)
414{
415 return 0;
416}
417
418/* hw_params callback */
419static int snd_emu10k1x_pcm_hw_params(snd_pcm_substream_t *substream,
420 snd_pcm_hw_params_t * hw_params)
421{
422 snd_pcm_runtime_t *runtime = substream->runtime;
423 emu10k1x_pcm_t *epcm = runtime->private_data;
424
425 if (! epcm->voice) {
426 epcm->voice = &epcm->emu->voices[substream->pcm->device];
427 epcm->voice->use = 1;
428 epcm->voice->epcm = epcm;
429 }
430
431 return snd_pcm_lib_malloc_pages(substream,
432 params_buffer_bytes(hw_params));
433}
434
435/* hw_free callback */
436static int snd_emu10k1x_pcm_hw_free(snd_pcm_substream_t *substream)
437{
438 snd_pcm_runtime_t *runtime = substream->runtime;
439 emu10k1x_pcm_t *epcm;
440
441 if (runtime->private_data == NULL)
442 return 0;
443
444 epcm = runtime->private_data;
445
446 if (epcm->voice) {
447 epcm->voice->use = 0;
448 epcm->voice->epcm = NULL;
449 epcm->voice = NULL;
450 }
451
452 return snd_pcm_lib_free_pages(substream);
453}
454
455/* prepare callback */
456static int snd_emu10k1x_pcm_prepare(snd_pcm_substream_t *substream)
457{
458 emu10k1x_t *emu = snd_pcm_substream_chip(substream);
459 snd_pcm_runtime_t *runtime = substream->runtime;
460 emu10k1x_pcm_t *epcm = runtime->private_data;
461 int voice = epcm->voice->number;
462 u32 *table_base = (u32 *)(emu->dma_buffer.area+1024*voice);
463 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
464 int i;
465
466 for(i=0; i < runtime->periods; i++) {
467 *table_base++=runtime->dma_addr+(i*period_size_bytes);
468 *table_base++=period_size_bytes<<16;
469 }
470
471 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_ADDR, voice, emu->dma_buffer.addr+1024*voice);
472 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_SIZE, voice, (runtime->periods - 1) << 19);
473 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_PTR, voice, 0);
474 snd_emu10k1x_ptr_write(emu, PLAYBACK_POINTER, voice, 0);
475 snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN1, voice, 0);
476 snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN2, voice, 0);
477 snd_emu10k1x_ptr_write(emu, PLAYBACK_DMA_ADDR, voice, runtime->dma_addr);
478
479 snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
480
481 return 0;
482}
483
484/* trigger callback */
485static int snd_emu10k1x_pcm_trigger(snd_pcm_substream_t *substream,
486 int cmd)
487{
488 emu10k1x_t *emu = snd_pcm_substream_chip(substream);
489 snd_pcm_runtime_t *runtime = substream->runtime;
490 emu10k1x_pcm_t *epcm = runtime->private_data;
491 int channel = epcm->voice->number;
492 int result = 0;
493
494// snd_printk(KERN_INFO "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n", (int)emu, cmd, (int)substream->ops->pointer(substream));
495
496 switch (cmd) {
497 case SNDRV_PCM_TRIGGER_START:
498 if(runtime->periods == 2)
499 snd_emu10k1x_intr_enable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
500 else
501 snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
502 epcm->running = 1;
503 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
504 break;
505 case SNDRV_PCM_TRIGGER_STOP:
506 epcm->running = 0;
507 snd_emu10k1x_intr_disable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
508 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CHANNEL_0<<channel));
509 break;
510 default:
511 result = -EINVAL;
512 break;
513 }
514 return result;
515}
516
517/* pointer callback */
518static snd_pcm_uframes_t
519snd_emu10k1x_pcm_pointer(snd_pcm_substream_t *substream)
520{
521 emu10k1x_t *emu = snd_pcm_substream_chip(substream);
522 snd_pcm_runtime_t *runtime = substream->runtime;
523 emu10k1x_pcm_t *epcm = runtime->private_data;
524 int channel = epcm->voice->number;
525 snd_pcm_uframes_t ptr = 0, ptr1 = 0, ptr2= 0,ptr3 = 0,ptr4 = 0;
526
527 if (!epcm->running)
528 return 0;
529
530 ptr3 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
531 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
532 ptr4 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
533
534 if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
535 return 0;
536
537 if (ptr3 != ptr4)
538 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
539 ptr2 = bytes_to_frames(runtime, ptr1);
540 ptr2 += (ptr4 >> 3) * runtime->period_size;
541 ptr = ptr2;
542
543 if (ptr >= runtime->buffer_size)
544 ptr -= runtime->buffer_size;
545
546 return ptr;
547}
548
549/* operators */
550static snd_pcm_ops_t snd_emu10k1x_playback_ops = {
551 .open = snd_emu10k1x_playback_open,
552 .close = snd_emu10k1x_playback_close,
553 .ioctl = snd_pcm_lib_ioctl,
554 .hw_params = snd_emu10k1x_pcm_hw_params,
555 .hw_free = snd_emu10k1x_pcm_hw_free,
556 .prepare = snd_emu10k1x_pcm_prepare,
557 .trigger = snd_emu10k1x_pcm_trigger,
558 .pointer = snd_emu10k1x_pcm_pointer,
559};
560
561/* open_capture callback */
562static int snd_emu10k1x_pcm_open_capture(snd_pcm_substream_t *substream)
563{
564 emu10k1x_t *chip = snd_pcm_substream_chip(substream);
565 emu10k1x_pcm_t *epcm;
566 snd_pcm_runtime_t *runtime = substream->runtime;
567 int err;
568
569 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
570 return err;
571 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
572 return err;
573
e560d8d8 574 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1da177e4
LT
575 if (epcm == NULL)
576 return -ENOMEM;
577
578 epcm->emu = chip;
579 epcm->substream = substream;
580
581 runtime->private_data = epcm;
582 runtime->private_free = snd_emu10k1x_pcm_free_substream;
583
584 runtime->hw = snd_emu10k1x_capture_hw;
585
586 return 0;
587}
588
589/* close callback */
590static int snd_emu10k1x_pcm_close_capture(snd_pcm_substream_t *substream)
591{
592 return 0;
593}
594
595/* hw_params callback */
596static int snd_emu10k1x_pcm_hw_params_capture(snd_pcm_substream_t *substream,
597 snd_pcm_hw_params_t * hw_params)
598{
599 snd_pcm_runtime_t *runtime = substream->runtime;
600 emu10k1x_pcm_t *epcm = runtime->private_data;
601
602 if (! epcm->voice) {
603 if (epcm->emu->capture_voice.use)
604 return -EBUSY;
605 epcm->voice = &epcm->emu->capture_voice;
606 epcm->voice->epcm = epcm;
607 epcm->voice->use = 1;
608 }
609
610 return snd_pcm_lib_malloc_pages(substream,
611 params_buffer_bytes(hw_params));
612}
613
614/* hw_free callback */
615static int snd_emu10k1x_pcm_hw_free_capture(snd_pcm_substream_t *substream)
616{
617 snd_pcm_runtime_t *runtime = substream->runtime;
618
619 emu10k1x_pcm_t *epcm;
620
621 if (runtime->private_data == NULL)
622 return 0;
623 epcm = runtime->private_data;
624
625 if (epcm->voice) {
626 epcm->voice->use = 0;
627 epcm->voice->epcm = NULL;
628 epcm->voice = NULL;
629 }
630
631 return snd_pcm_lib_free_pages(substream);
632}
633
634/* prepare capture callback */
635static int snd_emu10k1x_pcm_prepare_capture(snd_pcm_substream_t *substream)
636{
637 emu10k1x_t *emu = snd_pcm_substream_chip(substream);
638 snd_pcm_runtime_t *runtime = substream->runtime;
639
640 snd_emu10k1x_ptr_write(emu, CAPTURE_DMA_ADDR, 0, runtime->dma_addr);
641 snd_emu10k1x_ptr_write(emu, CAPTURE_BUFFER_SIZE, 0, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
642 snd_emu10k1x_ptr_write(emu, CAPTURE_POINTER, 0, 0);
643 snd_emu10k1x_ptr_write(emu, CAPTURE_UNKNOWN, 0, 0);
644
645 return 0;
646}
647
648/* trigger_capture callback */
649static int snd_emu10k1x_pcm_trigger_capture(snd_pcm_substream_t *substream,
650 int cmd)
651{
652 emu10k1x_t *emu = snd_pcm_substream_chip(substream);
653 snd_pcm_runtime_t *runtime = substream->runtime;
654 emu10k1x_pcm_t *epcm = runtime->private_data;
655 int result = 0;
656
657 switch (cmd) {
658 case SNDRV_PCM_TRIGGER_START:
659 snd_emu10k1x_intr_enable(emu, INTE_CAP_0_LOOP |
660 INTE_CAP_0_HALF_LOOP);
661 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|TRIGGER_CAPTURE);
662 epcm->running = 1;
663 break;
664 case SNDRV_PCM_TRIGGER_STOP:
665 epcm->running = 0;
666 snd_emu10k1x_intr_disable(emu, INTE_CAP_0_LOOP |
667 INTE_CAP_0_HALF_LOOP);
668 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CAPTURE));
669 break;
670 default:
671 result = -EINVAL;
672 break;
673 }
674 return result;
675}
676
677/* pointer_capture callback */
678static snd_pcm_uframes_t
679snd_emu10k1x_pcm_pointer_capture(snd_pcm_substream_t *substream)
680{
681 emu10k1x_t *emu = snd_pcm_substream_chip(substream);
682 snd_pcm_runtime_t *runtime = substream->runtime;
683 emu10k1x_pcm_t *epcm = runtime->private_data;
684 snd_pcm_uframes_t ptr;
685
686 if (!epcm->running)
687 return 0;
688
689 ptr = bytes_to_frames(runtime, snd_emu10k1x_ptr_read(emu, CAPTURE_POINTER, 0));
690 if (ptr >= runtime->buffer_size)
691 ptr -= runtime->buffer_size;
692
693 return ptr;
694}
695
696static snd_pcm_ops_t snd_emu10k1x_capture_ops = {
697 .open = snd_emu10k1x_pcm_open_capture,
698 .close = snd_emu10k1x_pcm_close_capture,
699 .ioctl = snd_pcm_lib_ioctl,
700 .hw_params = snd_emu10k1x_pcm_hw_params_capture,
701 .hw_free = snd_emu10k1x_pcm_hw_free_capture,
702 .prepare = snd_emu10k1x_pcm_prepare_capture,
703 .trigger = snd_emu10k1x_pcm_trigger_capture,
704 .pointer = snd_emu10k1x_pcm_pointer_capture,
705};
706
707static unsigned short snd_emu10k1x_ac97_read(ac97_t *ac97,
708 unsigned short reg)
709{
710 emu10k1x_t *emu = ac97->private_data;
711 unsigned long flags;
712 unsigned short val;
713
714 spin_lock_irqsave(&emu->emu_lock, flags);
715 outb(reg, emu->port + AC97ADDRESS);
716 val = inw(emu->port + AC97DATA);
717 spin_unlock_irqrestore(&emu->emu_lock, flags);
718 return val;
719}
720
721static void snd_emu10k1x_ac97_write(ac97_t *ac97,
722 unsigned short reg, unsigned short val)
723{
724 emu10k1x_t *emu = ac97->private_data;
725 unsigned long flags;
726
727 spin_lock_irqsave(&emu->emu_lock, flags);
728 outb(reg, emu->port + AC97ADDRESS);
729 outw(val, emu->port + AC97DATA);
730 spin_unlock_irqrestore(&emu->emu_lock, flags);
731}
732
733static int snd_emu10k1x_ac97(emu10k1x_t *chip)
734{
735 ac97_bus_t *pbus;
736 ac97_template_t ac97;
737 int err;
738 static ac97_bus_ops_t ops = {
739 .write = snd_emu10k1x_ac97_write,
740 .read = snd_emu10k1x_ac97_read,
741 };
742
743 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
744 return err;
745 pbus->no_vra = 1; /* we don't need VRA */
746
747 memset(&ac97, 0, sizeof(ac97));
748 ac97.private_data = chip;
749 ac97.scaps = AC97_SCAP_NO_SPDIF;
750 return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
751}
752
753static int snd_emu10k1x_free(emu10k1x_t *chip)
754{
755 snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
756 // disable interrupts
757 outl(0, chip->port + INTE);
758 // disable audio
759 outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
760
761 // release the i/o port
b1d5776d
TI
762 release_and_free_resource(chip->res_port);
763
1da177e4
LT
764 // release the irq
765 if (chip->irq >= 0)
766 free_irq(chip->irq, (void *)chip);
767
768 // release the DMA
769 if (chip->dma_buffer.area) {
770 snd_dma_free_pages(&chip->dma_buffer);
771 }
772
773 pci_disable_device(chip->pci);
774
775 // release the data
776 kfree(chip);
777 return 0;
778}
779
780static int snd_emu10k1x_dev_free(snd_device_t *device)
781{
782 emu10k1x_t *chip = device->device_data;
783 return snd_emu10k1x_free(chip);
784}
785
786static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id,
787 struct pt_regs *regs)
788{
789 unsigned int status;
790
791 emu10k1x_t *chip = dev_id;
792 emu10k1x_voice_t *pvoice = chip->voices;
793 int i;
794 int mask;
795
796 status = inl(chip->port + IPR);
797
798 if(status) {
799 // capture interrupt
800 if(status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
801 emu10k1x_voice_t *pvoice = &chip->capture_voice;
802 if(pvoice->use)
803 snd_emu10k1x_pcm_interrupt(chip, pvoice);
804 else
805 snd_emu10k1x_intr_disable(chip,
806 INTE_CAP_0_LOOP |
807 INTE_CAP_0_HALF_LOOP);
808 }
809
810 mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
811 for(i = 0; i < 3; i++) {
812 if(status & mask) {
813 if(pvoice->use)
814 snd_emu10k1x_pcm_interrupt(chip, pvoice);
815 else
816 snd_emu10k1x_intr_disable(chip, mask);
817 }
818 pvoice++;
819 mask <<= 1;
820 }
821
822 if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
823 if (chip->midi.interrupt)
824 chip->midi.interrupt(chip, status);
825 else
826 snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
827 }
828
829 // acknowledge the interrupt if necessary
830 if(status)
831 outl(status, chip->port+IPR);
832
833// snd_printk(KERN_INFO "interrupt %08x\n", status);
834 }
835
836 return IRQ_HANDLED;
837}
838
1da177e4
LT
839static int __devinit snd_emu10k1x_pcm(emu10k1x_t *emu, int device, snd_pcm_t **rpcm)
840{
841 snd_pcm_t *pcm;
842 int err;
843 int capture = 0;
844
845 if (rpcm)
846 *rpcm = NULL;
847 if (device == 0)
848 capture = 1;
849
850 if ((err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm)) < 0)
851 return err;
852
853 pcm->private_data = emu;
1da177e4
LT
854
855 switch(device) {
856 case 0:
857 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
858 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
859 break;
860 case 1:
861 case 2:
862 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
863 break;
864 }
865
866 pcm->info_flags = 0;
867 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
868 switch(device) {
869 case 0:
870 strcpy(pcm->name, "EMU10K1X Front");
871 break;
872 case 1:
873 strcpy(pcm->name, "EMU10K1X Rear");
874 break;
875 case 2:
876 strcpy(pcm->name, "EMU10K1X Center/LFE");
877 break;
878 }
879 emu->pcm = pcm;
880
881 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
882 snd_dma_pci_data(emu->pci),
883 32*1024, 32*1024);
884
885 if (rpcm)
886 *rpcm = pcm;
887
888 return 0;
889}
890
891static int __devinit snd_emu10k1x_create(snd_card_t *card,
892 struct pci_dev *pci,
893 emu10k1x_t **rchip)
894{
895 emu10k1x_t *chip;
896 int err;
897 int ch;
898 static snd_device_ops_t ops = {
899 .dev_free = snd_emu10k1x_dev_free,
900 };
901
902 *rchip = NULL;
903
904 if ((err = pci_enable_device(pci)) < 0)
905 return err;
906 if (pci_set_dma_mask(pci, 0x0fffffff) < 0 ||
907 pci_set_consistent_dma_mask(pci, 0x0fffffff) < 0) {
908 snd_printk(KERN_ERR "error to set 28bit mask DMA\n");
909 pci_disable_device(pci);
910 return -ENXIO;
911 }
912
e560d8d8 913 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1da177e4
LT
914 if (chip == NULL) {
915 pci_disable_device(pci);
916 return -ENOMEM;
917 }
918
919 chip->card = card;
920 chip->pci = pci;
921 chip->irq = -1;
922
923 spin_lock_init(&chip->emu_lock);
924 spin_lock_init(&chip->voice_lock);
925
926 chip->port = pci_resource_start(pci, 0);
927 if ((chip->res_port = request_region(chip->port, 8,
928 "EMU10K1X")) == NULL) {
929 snd_printk(KERN_ERR "emu10k1x: cannot allocate the port 0x%lx\n", chip->port);
930 snd_emu10k1x_free(chip);
931 return -EBUSY;
932 }
933
934 if (request_irq(pci->irq, snd_emu10k1x_interrupt,
935 SA_INTERRUPT|SA_SHIRQ, "EMU10K1X",
936 (void *)chip)) {
937 snd_printk(KERN_ERR "emu10k1x: cannot grab irq %d\n", pci->irq);
938 snd_emu10k1x_free(chip);
939 return -EBUSY;
940 }
941 chip->irq = pci->irq;
942
943 if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
944 4 * 1024, &chip->dma_buffer) < 0) {
945 snd_emu10k1x_free(chip);
946 return -ENOMEM;
947 }
948
949 pci_set_master(pci);
950 /* read revision & serial */
951 pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
952 pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
953 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
954 snd_printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
955 chip->revision, chip->serial);
956
957 outl(0, chip->port + INTE);
958
959 for(ch = 0; ch < 3; ch++) {
960 chip->voices[ch].emu = chip;
961 chip->voices[ch].number = ch;
962 }
963
964 /*
965 * Init to 0x02109204 :
966 * Clock accuracy = 0 (1000ppm)
967 * Sample Rate = 2 (48kHz)
968 * Audio Channel = 1 (Left of 2)
969 * Source Number = 0 (Unspecified)
970 * Generation Status = 1 (Original for Cat Code 12)
971 * Cat Code = 12 (Digital Signal Mixer)
972 * Mode = 0 (Mode 0)
973 * Emphasis = 0 (None)
974 * CP = 1 (Copyright unasserted)
975 * AN = 0 (Audio data)
976 * P = 0 (Consumer)
977 */
978 snd_emu10k1x_ptr_write(chip, SPCS0, 0,
979 chip->spdif_bits[0] =
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, SPCS1, 0,
985 chip->spdif_bits[1] =
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 snd_emu10k1x_ptr_write(chip, SPCS2, 0,
991 chip->spdif_bits[2] =
992 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
993 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
994 SPCS_GENERATIONSTATUS | 0x00001200 |
995 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
996
997 snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
998 snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
999 snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
1000
1001 outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
1002
1003 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1004 chip, &ops)) < 0) {
1005 snd_emu10k1x_free(chip);
1006 return err;
1007 }
1008 *rchip = chip;
1009 return 0;
1010}
1011
1012static void snd_emu10k1x_proc_reg_read(snd_info_entry_t *entry,
1013 snd_info_buffer_t * buffer)
1014{
1015 emu10k1x_t *emu = entry->private_data;
1016 unsigned long value,value1,value2;
1017 unsigned long flags;
1018 int i;
1019
1020 snd_iprintf(buffer, "Registers:\n\n");
1021 for(i = 0; i < 0x20; i+=4) {
1022 spin_lock_irqsave(&emu->emu_lock, flags);
1023 value = inl(emu->port + i);
1024 spin_unlock_irqrestore(&emu->emu_lock, flags);
1025 snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
1026 }
1027 snd_iprintf(buffer, "\nRegisters\n\n");
1028 for(i = 0; i <= 0x48; i++) {
1029 value = snd_emu10k1x_ptr_read(emu, i, 0);
1030 if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
1031 value1 = snd_emu10k1x_ptr_read(emu, i, 1);
1032 value2 = snd_emu10k1x_ptr_read(emu, i, 2);
1033 snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
1034 } else {
1035 snd_iprintf(buffer, "%02X: %08lX\n", i, value);
1036 }
1037 }
1038}
1039
1040static void snd_emu10k1x_proc_reg_write(snd_info_entry_t *entry,
1041 snd_info_buffer_t *buffer)
1042{
1043 emu10k1x_t *emu = entry->private_data;
1044 char line[64];
1045 unsigned int reg, channel_id , val;
1046
1047 while (!snd_info_get_line(buffer, line, sizeof(line))) {
1048 if (sscanf(line, "%x %x %x", &reg, &channel_id, &val) != 3)
1049 continue;
1050
1051 if ((reg < 0x49) && (reg >=0) && (val <= 0xffffffff)
1052 && (channel_id >=0) && (channel_id <= 2) )
1053 snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
1054 }
1055}
1056
1057static int __devinit snd_emu10k1x_proc_init(emu10k1x_t * emu)
1058{
1059 snd_info_entry_t *entry;
1060
1061 if(! snd_card_proc_new(emu->card, "emu10k1x_regs", &entry)) {
1062 snd_info_set_text_ops(entry, emu, 1024, snd_emu10k1x_proc_reg_read);
1063 entry->c.text.write_size = 64;
1064 entry->c.text.write = snd_emu10k1x_proc_reg_write;
bd7bf042 1065 entry->mode |= S_IWUSR;
1da177e4
LT
1066 entry->private_data = emu;
1067 }
1068
1069 return 0;
1070}
1071
1072static int snd_emu10k1x_shared_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1073{
1074 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1075 uinfo->count = 1;
1076 uinfo->value.integer.min = 0;
1077 uinfo->value.integer.max = 1;
1078 return 0;
1079}
1080
1081static int snd_emu10k1x_shared_spdif_get(snd_kcontrol_t * kcontrol,
1082 snd_ctl_elem_value_t * ucontrol)
1083{
1084 emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
1085
1086 ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
1087
1088 return 0;
1089}
1090
1091static int snd_emu10k1x_shared_spdif_put(snd_kcontrol_t * kcontrol,
1092 snd_ctl_elem_value_t * ucontrol)
1093{
1094 emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
1095 unsigned int val;
1096 int change = 0;
1097
1098 val = ucontrol->value.integer.value[0] ;
1099
1100 if (val) {
1101 // enable spdif output
1102 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
1103 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
1104 snd_emu10k1x_gpio_write(emu, 0x1000);
1105 } else {
1106 // disable spdif output
1107 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
1108 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
1109 snd_emu10k1x_gpio_write(emu, 0x1080);
1110 }
1111 return change;
1112}
1113
1114static snd_kcontrol_new_t snd_emu10k1x_shared_spdif __devinitdata =
1115{
1116 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1117 .name = "Analog/Digital Output Jack",
1118 .info = snd_emu10k1x_shared_spdif_info,
1119 .get = snd_emu10k1x_shared_spdif_get,
1120 .put = snd_emu10k1x_shared_spdif_put
1121};
1122
1123static int snd_emu10k1x_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1124{
1125 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1126 uinfo->count = 1;
1127 return 0;
1128}
1129
1130static int snd_emu10k1x_spdif_get(snd_kcontrol_t * kcontrol,
1131 snd_ctl_elem_value_t * ucontrol)
1132{
1133 emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
1134 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1135
1136 ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
1137 ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
1138 ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
1139 ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
1140 return 0;
1141}
1142
1143static int snd_emu10k1x_spdif_get_mask(snd_kcontrol_t * kcontrol,
1144 snd_ctl_elem_value_t * ucontrol)
1145{
1146 ucontrol->value.iec958.status[0] = 0xff;
1147 ucontrol->value.iec958.status[1] = 0xff;
1148 ucontrol->value.iec958.status[2] = 0xff;
1149 ucontrol->value.iec958.status[3] = 0xff;
1150 return 0;
1151}
1152
1153static int snd_emu10k1x_spdif_put(snd_kcontrol_t * kcontrol,
1154 snd_ctl_elem_value_t * ucontrol)
1155{
1156 emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
1157 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1158 int change;
1159 unsigned int val;
1160
1161 val = (ucontrol->value.iec958.status[0] << 0) |
1162 (ucontrol->value.iec958.status[1] << 8) |
1163 (ucontrol->value.iec958.status[2] << 16) |
1164 (ucontrol->value.iec958.status[3] << 24);
1165 change = val != emu->spdif_bits[idx];
1166 if (change) {
1167 snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
1168 emu->spdif_bits[idx] = val;
1169 }
1170 return change;
1171}
1172
1173static snd_kcontrol_new_t snd_emu10k1x_spdif_mask_control =
1174{
1175 .access = SNDRV_CTL_ELEM_ACCESS_READ,
5549d549 1176 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1da177e4
LT
1177 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1178 .count = 3,
1179 .info = snd_emu10k1x_spdif_info,
1180 .get = snd_emu10k1x_spdif_get_mask
1181};
1182
1183static snd_kcontrol_new_t snd_emu10k1x_spdif_control =
1184{
5549d549 1185 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1da177e4
LT
1186 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1187 .count = 3,
1188 .info = snd_emu10k1x_spdif_info,
1189 .get = snd_emu10k1x_spdif_get,
1190 .put = snd_emu10k1x_spdif_put
1191};
1192
1193static int __devinit snd_emu10k1x_mixer(emu10k1x_t *emu)
1194{
1195 int err;
1196 snd_kcontrol_t *kctl;
1197 snd_card_t *card = emu->card;
1198
1199 if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu)) == NULL)
1200 return -ENOMEM;
1201 if ((err = snd_ctl_add(card, kctl)))
1202 return err;
1203 if ((kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu)) == NULL)
1204 return -ENOMEM;
1205 if ((err = snd_ctl_add(card, kctl)))
1206 return err;
1207 if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu)) == NULL)
1208 return -ENOMEM;
1209 if ((err = snd_ctl_add(card, kctl)))
1210 return err;
1211
1212 return 0;
1213}
1214
1215#define EMU10K1X_MIDI_MODE_INPUT (1<<0)
1216#define EMU10K1X_MIDI_MODE_OUTPUT (1<<1)
1217
1218static inline unsigned char mpu401_read(emu10k1x_t *emu, emu10k1x_midi_t *mpu, int idx)
1219{
1220 return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
1221}
1222
1223static inline void mpu401_write(emu10k1x_t *emu, emu10k1x_midi_t *mpu, int data, int idx)
1224{
1225 snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
1226}
1227
1228#define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
1229#define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
1230#define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
1231#define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
1232
1233#define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
1234#define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
1235
1236#define MPU401_RESET 0xff
1237#define MPU401_ENTER_UART 0x3f
1238#define MPU401_ACK 0xfe
1239
1240static void mpu401_clear_rx(emu10k1x_t *emu, emu10k1x_midi_t *mpu)
1241{
1242 int timeout = 100000;
1243 for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
1244 mpu401_read_data(emu, mpu);
1245#ifdef CONFIG_SND_DEBUG
1246 if (timeout <= 0)
1247 snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
1248#endif
1249}
1250
1251/*
1252
1253 */
1254
1255static void do_emu10k1x_midi_interrupt(emu10k1x_t *emu, emu10k1x_midi_t *midi, unsigned int status)
1256{
1257 unsigned char byte;
1258
1259 if (midi->rmidi == NULL) {
1260 snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
1261 return;
1262 }
1263
1264 spin_lock(&midi->input_lock);
1265 if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
1266 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1267 mpu401_clear_rx(emu, midi);
1268 } else {
1269 byte = mpu401_read_data(emu, midi);
1270 if (midi->substream_input)
1271 snd_rawmidi_receive(midi->substream_input, &byte, 1);
1272 }
1273 }
1274 spin_unlock(&midi->input_lock);
1275
1276 spin_lock(&midi->output_lock);
1277 if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
1278 if (midi->substream_output &&
1279 snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
1280 mpu401_write_data(emu, midi, byte);
1281 } else {
1282 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1283 }
1284 }
1285 spin_unlock(&midi->output_lock);
1286}
1287
1288static void snd_emu10k1x_midi_interrupt(emu10k1x_t *emu, unsigned int status)
1289{
1290 do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
1291}
1292
1293static void snd_emu10k1x_midi_cmd(emu10k1x_t * emu, emu10k1x_midi_t *midi, unsigned char cmd, int ack)
1294{
1295 unsigned long flags;
1296 int timeout, ok;
1297
1298 spin_lock_irqsave(&midi->input_lock, flags);
1299 mpu401_write_data(emu, midi, 0x00);
1300 /* mpu401_clear_rx(emu, midi); */
1301
1302 mpu401_write_cmd(emu, midi, cmd);
1303 if (ack) {
1304 ok = 0;
1305 timeout = 10000;
1306 while (!ok && timeout-- > 0) {
1307 if (mpu401_input_avail(emu, midi)) {
1308 if (mpu401_read_data(emu, midi) == MPU401_ACK)
1309 ok = 1;
1310 }
1311 }
1312 if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
1313 ok = 1;
1314 } else {
1315 ok = 1;
1316 }
1317 spin_unlock_irqrestore(&midi->input_lock, flags);
1318 if (!ok)
1319 snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
1320 cmd, emu->port,
1321 mpu401_read_stat(emu, midi),
1322 mpu401_read_data(emu, midi));
1323}
1324
1325static int snd_emu10k1x_midi_input_open(snd_rawmidi_substream_t * substream)
1326{
1327 emu10k1x_t *emu;
1328 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1329 unsigned long flags;
1330
1331 emu = midi->emu;
1332 snd_assert(emu, return -ENXIO);
1333 spin_lock_irqsave(&midi->open_lock, flags);
1334 midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
1335 midi->substream_input = substream;
1336 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1337 spin_unlock_irqrestore(&midi->open_lock, flags);
1338 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
1339 snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
1340 } else {
1341 spin_unlock_irqrestore(&midi->open_lock, flags);
1342 }
1343 return 0;
1344}
1345
1346static int snd_emu10k1x_midi_output_open(snd_rawmidi_substream_t * substream)
1347{
1348 emu10k1x_t *emu;
1349 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1350 unsigned long flags;
1351
1352 emu = midi->emu;
1353 snd_assert(emu, return -ENXIO);
1354 spin_lock_irqsave(&midi->open_lock, flags);
1355 midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
1356 midi->substream_output = substream;
1357 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1358 spin_unlock_irqrestore(&midi->open_lock, flags);
1359 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
1360 snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
1361 } else {
1362 spin_unlock_irqrestore(&midi->open_lock, flags);
1363 }
1364 return 0;
1365}
1366
1367static int snd_emu10k1x_midi_input_close(snd_rawmidi_substream_t * substream)
1368{
1369 emu10k1x_t *emu;
1370 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1371 unsigned long flags;
1372
1373 emu = midi->emu;
1374 snd_assert(emu, return -ENXIO);
1375 spin_lock_irqsave(&midi->open_lock, flags);
1376 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1377 midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
1378 midi->substream_input = NULL;
1379 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1380 spin_unlock_irqrestore(&midi->open_lock, flags);
1381 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1382 } else {
1383 spin_unlock_irqrestore(&midi->open_lock, flags);
1384 }
1385 return 0;
1386}
1387
1388static int snd_emu10k1x_midi_output_close(snd_rawmidi_substream_t * substream)
1389{
1390 emu10k1x_t *emu;
1391 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1392 unsigned long flags;
1393
1394 emu = midi->emu;
1395 snd_assert(emu, return -ENXIO);
1396 spin_lock_irqsave(&midi->open_lock, flags);
1397 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1398 midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
1399 midi->substream_output = NULL;
1400 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1401 spin_unlock_irqrestore(&midi->open_lock, flags);
1402 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1403 } else {
1404 spin_unlock_irqrestore(&midi->open_lock, flags);
1405 }
1406 return 0;
1407}
1408
1409static void snd_emu10k1x_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
1410{
1411 emu10k1x_t *emu;
1412 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1413 emu = midi->emu;
1414 snd_assert(emu, return);
1415
1416 if (up)
1417 snd_emu10k1x_intr_enable(emu, midi->rx_enable);
1418 else
1419 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1420}
1421
1422static void snd_emu10k1x_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
1423{
1424 emu10k1x_t *emu;
1425 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1426 unsigned long flags;
1427
1428 emu = midi->emu;
1429 snd_assert(emu, return);
1430
1431 if (up) {
1432 int max = 4;
1433 unsigned char byte;
1434
1435 /* try to send some amount of bytes here before interrupts */
1436 spin_lock_irqsave(&midi->output_lock, flags);
1437 while (max > 0) {
1438 if (mpu401_output_ready(emu, midi)) {
1439 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
1440 snd_rawmidi_transmit(substream, &byte, 1) != 1) {
1441 /* no more data */
1442 spin_unlock_irqrestore(&midi->output_lock, flags);
1443 return;
1444 }
1445 mpu401_write_data(emu, midi, byte);
1446 max--;
1447 } else {
1448 break;
1449 }
1450 }
1451 spin_unlock_irqrestore(&midi->output_lock, flags);
1452 snd_emu10k1x_intr_enable(emu, midi->tx_enable);
1453 } else {
1454 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1455 }
1456}
1457
1458/*
1459
1460 */
1461
1462static snd_rawmidi_ops_t snd_emu10k1x_midi_output =
1463{
1464 .open = snd_emu10k1x_midi_output_open,
1465 .close = snd_emu10k1x_midi_output_close,
1466 .trigger = snd_emu10k1x_midi_output_trigger,
1467};
1468
1469static snd_rawmidi_ops_t snd_emu10k1x_midi_input =
1470{
1471 .open = snd_emu10k1x_midi_input_open,
1472 .close = snd_emu10k1x_midi_input_close,
1473 .trigger = snd_emu10k1x_midi_input_trigger,
1474};
1475
1476static void snd_emu10k1x_midi_free(snd_rawmidi_t *rmidi)
1477{
1478 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)rmidi->private_data;
1479 midi->interrupt = NULL;
1480 midi->rmidi = NULL;
1481}
1482
1483static int __devinit emu10k1x_midi_init(emu10k1x_t *emu, emu10k1x_midi_t *midi, int device, char *name)
1484{
1485 snd_rawmidi_t *rmidi;
1486 int err;
1487
1488 if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
1489 return err;
1490 midi->emu = emu;
1491 spin_lock_init(&midi->open_lock);
1492 spin_lock_init(&midi->input_lock);
1493 spin_lock_init(&midi->output_lock);
1494 strcpy(rmidi->name, name);
1495 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
1496 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
1497 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1498 SNDRV_RAWMIDI_INFO_INPUT |
1499 SNDRV_RAWMIDI_INFO_DUPLEX;
1500 rmidi->private_data = midi;
1501 rmidi->private_free = snd_emu10k1x_midi_free;
1502 midi->rmidi = rmidi;
1503 return 0;
1504}
1505
1506static int __devinit snd_emu10k1x_midi(emu10k1x_t *emu)
1507{
1508 emu10k1x_midi_t *midi = &emu->midi;
1509 int err;
1510
1511 if ((err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)")) < 0)
1512 return err;
1513
1514 midi->tx_enable = INTE_MIDITXENABLE;
1515 midi->rx_enable = INTE_MIDIRXENABLE;
1516 midi->port = MUDATA;
1517 midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
1518 midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
1519 midi->interrupt = snd_emu10k1x_midi_interrupt;
1520 return 0;
1521}
1522
1523static int __devinit snd_emu10k1x_probe(struct pci_dev *pci,
1524 const struct pci_device_id *pci_id)
1525{
1526 static int dev;
1527 snd_card_t *card;
1528 emu10k1x_t *chip;
1529 int err;
1530
1531 if (dev >= SNDRV_CARDS)
1532 return -ENODEV;
1533 if (!enable[dev]) {
1534 dev++;
1535 return -ENOENT;
1536 }
1537
1538 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1539 if (card == NULL)
1540 return -ENOMEM;
1541
1542 if ((err = snd_emu10k1x_create(card, pci, &chip)) < 0) {
1543 snd_card_free(card);
1544 return err;
1545 }
1546
1547 if ((err = snd_emu10k1x_pcm(chip, 0, NULL)) < 0) {
1548 snd_card_free(card);
1549 return err;
1550 }
1551 if ((err = snd_emu10k1x_pcm(chip, 1, NULL)) < 0) {
1552 snd_card_free(card);
1553 return err;
1554 }
1555 if ((err = snd_emu10k1x_pcm(chip, 2, NULL)) < 0) {
1556 snd_card_free(card);
1557 return err;
1558 }
1559
1560 if ((err = snd_emu10k1x_ac97(chip)) < 0) {
1561 snd_card_free(card);
1562 return err;
1563 }
1564
1565 if ((err = snd_emu10k1x_mixer(chip)) < 0) {
1566 snd_card_free(card);
1567 return err;
1568 }
1569
1570 if ((err = snd_emu10k1x_midi(chip)) < 0) {
1571 snd_card_free(card);
1572 return err;
1573 }
1574
1575 snd_emu10k1x_proc_init(chip);
1576
1577 strcpy(card->driver, "EMU10K1X");
1578 strcpy(card->shortname, "Dell Sound Blaster Live!");
1579 sprintf(card->longname, "%s at 0x%lx irq %i",
1580 card->shortname, chip->port, chip->irq);
1581
1582 if ((err = snd_card_register(card)) < 0) {
1583 snd_card_free(card);
1584 return err;
1585 }
1586
1587 pci_set_drvdata(pci, card);
1588 dev++;
1589 return 0;
1590}
1591
1592static void __devexit snd_emu10k1x_remove(struct pci_dev *pci)
1593{
1594 snd_card_free(pci_get_drvdata(pci));
1595 pci_set_drvdata(pci, NULL);
1596}
1597
1598// PCI IDs
1599static struct pci_device_id snd_emu10k1x_ids[] = {
1600 { 0x1102, 0x0006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Dell OEM version (EMU10K1) */
1601 { 0, }
1602};
1603MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
1604
1605// pci_driver definition
1606static struct pci_driver driver = {
1607 .name = "EMU10K1X",
1608 .id_table = snd_emu10k1x_ids,
1609 .probe = snd_emu10k1x_probe,
1610 .remove = __devexit_p(snd_emu10k1x_remove),
1611};
1612
1613// initialization of the module
1614static int __init alsa_card_emu10k1x_init(void)
1615{
1616 int err;
1617
01d25d46 1618 if ((err = pci_register_driver(&driver)) > 0)
1da177e4
LT
1619 return err;
1620
1621 return 0;
1622}
1623
1624// clean up the module
1625static void __exit alsa_card_emu10k1x_exit(void)
1626{
1627 pci_unregister_driver(&driver);
1628}
1629
1630module_init(alsa_card_emu10k1x_init)
1631module_exit(alsa_card_emu10k1x_exit)
This page took 0.19959 seconds and 5 git commands to generate.