1  /*
2  **********************************************************************
3  *     main.c - Creative EMU10K1 audio driver
4  *     Copyright 1999, 2000 Creative Labs, Inc.
5  *
6  **********************************************************************
7  *
8  *     Date                 Author          Summary of changes
9  *     ----                 ------          ------------------
10  *     October 20, 1999     Bertrand Lee    base code release
11  *     November 2, 1999     Alan Cox        cleaned up stuff
12  *
13  **********************************************************************
14  *
15  *     This program is free software; you can redistribute it and/or
16  *     modify it under the terms of the GNU General Public License as
17  *     published by the Free Software Foundation; either version 2 of
18  *     the License, or (at your option) any later version.
19  *
20  *     This program is distributed in the hope that it will be useful,
21  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *     GNU General Public License for more details.
24  *
25  *     You should have received a copy of the GNU General Public
26  *     License along with this program; if not, write to the Free
27  *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
28  *     USA.
29  *
30  **********************************************************************
31  *
32  *      Supported devices:
33  *      /dev/dsp:        Standard /dev/dsp device, OSS-compatible
34  *      /dev/dsp1:       Routes to rear speakers only
35  *      /dev/mixer:      Standard /dev/mixer device, OSS-compatible
36  *      /dev/midi:       Raw MIDI UART device, mostly OSS-compatible
37  *	/dev/sequencer:  Sequencer Interface (requires sound.o)
38  *
39  *      Revision history:
40  *      0.1 beta Initial release
41  *      0.2 Lowered initial mixer vol. Improved on stuttering wave playback. Added MIDI UART support.
42  *      0.3 Fixed mixer routing bug, added APS, joystick support.
43  *      0.4 Added rear-channel, SPDIF support.
44  *	0.5 Source cleanup, SMP fixes, multiopen support, 64 bit arch fixes,
45  *	    moved bh's to tasklets, moved to the new PCI driver initialization style.
46  *	0.6 Make use of pci_alloc_consistent, improve compatibility layer for 2.2 kernels,
47  *	    code reorganization and cleanup.
48  *	0.7 Support for the Emu-APS. Bug fixes for voice cache setup, mmaped sound + poll().
49  *          Support for setting external TRAM size.
50  *      0.8 Make use of the kernel ac97 interface. Support for a dsp patch manager.
51  *      0.9 Re-enables rear speakers volume controls
52  *     0.10 Initializes rear speaker volume.
53  *	    Dynamic patch storage allocation.
54  *	    New private ioctls to change control gpr values.
55  *	    Enable volume control interrupts.
56  *	    By default enable dsp routes to digital out.
57  *     0.11 Fixed fx / 4 problem.
58  *     0.12 Implemented mmaped for recording.
59  *	    Fixed bug: not unreserving mmaped buffer pages.
60  *	    IRQ handler cleanup.
61  *     0.13 Fixed problem with dsp1
62  *          Simplified dsp patch writing (inside the driver)
63  *	    Fixed several bugs found by the Stanford tools
64  *     0.14 New control gpr to oss mixer mapping feature (Chris Purnell)
65  *          Added AC3 Passthrough Support (Juha Yrjola)
66  *          Added Support for 5.1 cards (digital out and the third analog out)
67  *     0.15 Added Sequencer Support (Daniel Mack)
68  *          Support for multichannel pcm playback (Eduard Hasenleithner)
69  *     0.16 Mixer improvements, added old treble/bass support (Daniel Bertrand)
70  *          Small code format cleanup.
71  *          Deadlock bug fix for emu10k1_volxxx_irqhandler().
72  *     0.17 Fix for mixer SOUND_MIXER_INFO ioctl.
73  *	    Fix for HIGHMEM machines (emu10k1 can only do 31 bit bus master)
74  *	    midi poll initial implementation.
75  *	    Small mixer fixes/cleanups.
76  *	    Improved support for 5.1 cards.
77  *     0.18 Fix for possible leak in pci_alloc_consistent()
78  *          Cleaned up poll() functions (audio and midi). Don't start input.
79  *	    Restrict DMA pages used to 512Mib range.
80  *	    New AC97_BOOST mixer ioctl.
81  *     0.19 Real fix for kernel with highmem support (cast dma_handle to u32).
82  *	    Fix recording buffering parameters calculation.
83  *	    Use unsigned long for variables in bit ops.
84  *     0.20 Fixed recording startup
85  *	    Fixed timer rate setting (it's a 16-bit register)
86  *********************************************************************/
87 
88 /* These are only included once per module */
89 #include <linux/version.h>
90 #include <linux/module.h>
91 #include <linux/slab.h>
92 #include <linux/init.h>
93 #include <linux/delay.h>
94 #include <linux/proc_fs.h>
95 
96 #include "hwaccess.h"
97 #include "8010.h"
98 #include "efxmgr.h"
99 #include "cardwo.h"
100 #include "cardwi.h"
101 #include "cardmo.h"
102 #include "cardmi.h"
103 #include "recmgr.h"
104 #include "ecard.h"
105 
106 
107 #ifdef EMU10K1_SEQUENCER
108 #define MIDI_SYNTH_NAME "EMU10K1 MIDI"
109 #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
110 
111 #include "../sound_config.h"
112 #include "../midi_synth.h"
113 
114 /* this should be in dev_table.h */
115 #define SNDCARD_EMU10K1 46
116 #endif
117 
118 #define DRIVER_VERSION "0.20"
119 
120 /* the emu10k1 _seems_ to only supports 29 bit (512MiB) bit bus master */
121 #define EMU10K1_DMA_MASK                0x1fffffff	/* DMA buffer mask for pci_alloc_consist */
122 
123 #ifndef PCI_VENDOR_ID_CREATIVE
124 #define PCI_VENDOR_ID_CREATIVE 0x1102
125 #endif
126 
127 #ifndef PCI_DEVICE_ID_CREATIVE_EMU10K1
128 #define PCI_DEVICE_ID_CREATIVE_EMU10K1 0x0002
129 #endif
130 
131 #define EMU_APS_SUBID	0x40011102
132 
133 enum {
134 	EMU10K1 = 0,
135 };
136 
137 static char *card_names[] __devinitdata = {
138 	"EMU10K1",
139 };
140 
141 static struct pci_device_id emu10k1_pci_tbl[] = {
142 	{PCI_VENDOR_ID_CREATIVE, PCI_DEVICE_ID_CREATIVE_EMU10K1,
143 	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, EMU10K1},
144 	{0,}
145 };
146 
147 MODULE_DEVICE_TABLE(pci, emu10k1_pci_tbl);
148 
149 /* Global var instantiation */
150 
151 LIST_HEAD(emu10k1_devs);
152 
153 extern struct file_operations emu10k1_audio_fops;
154 extern struct file_operations emu10k1_mixer_fops;
155 extern struct file_operations emu10k1_midi_fops;
156 
157 #ifdef EMU10K1_SEQUENCER
158 static struct midi_operations emu10k1_midi_operations;
159 #endif
160 
161 extern void emu10k1_interrupt(int, void *, struct pt_regs *s);
162 
emu10k1_audio_init(struct emu10k1_card * card)163 static int __devinit emu10k1_audio_init(struct emu10k1_card *card)
164 {
165 	card->audio_dev = register_sound_dsp(&emu10k1_audio_fops, -1);
166 	if (card->audio_dev < 0) {
167 		printk(KERN_ERR "emu10k1: cannot register first audio device!\n");
168 		goto err_dev;
169 	}
170 
171 	card->audio_dev1 = register_sound_dsp(&emu10k1_audio_fops, -1);
172 	if (card->audio_dev1 < 0) {
173 		printk(KERN_ERR "emu10k1: cannot register second audio device!\n");
174 		goto err_dev1;
175 	}
176 
177 	/* Assign default playback voice parameters */
178 	card->mchannel_fx = 8;
179 	/* mono voice */
180 	card->waveout.send_a[0] = 0xff;
181 	card->waveout.send_b[0] = 0xff;
182 	card->waveout.send_c[0] = 0x00;
183 	card->waveout.send_d[0] = 0x00;
184 	card->waveout.send_routing[0] = 0x3210;
185 
186 	/* stereo voice */
187 	/* left */
188 	card->waveout.send_a[1] = 0xff;
189 	card->waveout.send_b[1] = 0x00;
190 	card->waveout.send_c[1] = 0x00;
191 	card->waveout.send_d[1] = 0x00;
192 	card->waveout.send_routing[1] = 0x3210;
193 
194 	/* right */
195 	card->waveout.send_a[2] = 0x00;
196 	card->waveout.send_b[2] = 0xff;
197 	card->waveout.send_c[2] = 0x00;
198 	card->waveout.send_d[2] = 0x00;
199 	card->waveout.send_routing[2] = 0x3210;
200 
201 	/* Assign default recording parameters */
202 	/* FIXME */
203 	if (card->is_aps)
204 		card->wavein.recsrc = WAVERECORD_FX;
205 	else
206 		card->wavein.recsrc = WAVERECORD_AC97;
207 
208 	card->wavein.fxwc = 0x0003;
209 	return 0;
210 
211 err_dev1:
212 	unregister_sound_dsp(card->audio_dev);
213 err_dev:
214 	return -ENODEV;
215 }
216 
emu10k1_audio_cleanup(struct emu10k1_card * card)217 static void __devinit emu10k1_audio_cleanup(struct emu10k1_card *card)
218 {
219 	unregister_sound_dsp(card->audio_dev1);
220 	unregister_sound_dsp(card->audio_dev);
221 }
222 
emu10k1_mixer_init(struct emu10k1_card * card)223 static int __devinit emu10k1_mixer_init(struct emu10k1_card *card)
224 {
225 	char s[32];
226 
227 	struct ac97_codec *codec  = ac97_alloc_codec();
228 
229 	if(codec == NULL)
230 	{
231 		printk(KERN_ERR "emu10k1: cannot allocate mixer\n");
232 		return -EIO;
233 	}
234 	card->ac97 = codec;
235 
236 #warning "Initialisation order race. Must register after usable"
237 
238 	card->ac97->dev_mixer = register_sound_mixer(&emu10k1_mixer_fops, -1);
239 	if (card->ac97->dev_mixer < 0) {
240 		printk(KERN_ERR "emu10k1: cannot register mixer device\n");
241 		goto err_codec;
242         }
243 
244 	card->ac97->private_data = card;
245 
246 	if (!card->is_aps) {
247 		card->ac97->id = 0;
248 		card->ac97->codec_read = emu10k1_ac97_read;
249         	card->ac97->codec_write = emu10k1_ac97_write;
250 
251 		if (ac97_probe_codec (card->ac97) == 0) {
252 			printk(KERN_ERR "emu10k1: unable to probe AC97 codec\n");
253 			goto err_out;
254 		}
255 		/* 5.1: Enable the additional AC97 Slots and unmute extra channels on AC97 codec */
256 		if (codec->codec_read(codec, AC97_EXTENDED_ID) & 0x0080){
257 			printk(KERN_INFO "emu10k1: SBLive! 5.1 card detected\n");
258 			sblive_writeptr(card, AC97SLOT, 0, AC97SLOT_CNTR | AC97SLOT_LFE);
259 			codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0);
260 		}
261 
262 		// Force 5bit:
263 		//card->ac97->bit_resolution=5;
264 
265 		if (!proc_mkdir ("driver/emu10k1", 0)) {
266 			printk(KERN_ERR "emu10k1: unable to create proc directory driver/emu10k1\n");
267 			goto err_out;
268 		}
269 
270 		sprintf(s, "driver/emu10k1/%s", card->pci_dev->slot_name);
271 		if (!proc_mkdir (s, 0)) {
272 			printk(KERN_ERR "emu10k1: unable to create proc directory %s\n", s);
273 			goto err_emu10k1_proc;
274 		}
275 
276 		sprintf(s, "driver/emu10k1/%s/ac97", card->pci_dev->slot_name);
277 		if (!create_proc_read_entry (s, 0, 0, ac97_read_proc, card->ac97)) {
278 			printk(KERN_ERR "emu10k1: unable to create proc entry %s\n", s);
279 			goto err_ac97_proc;
280 		}
281 
282 		/* these will store the original values and never be modified */
283 		card->ac97_supported_mixers = card->ac97->supported_mixers;
284 		card->ac97_stereo_mixers = card->ac97->stereo_mixers;
285 	}
286 
287 	return 0;
288 
289  err_ac97_proc:
290 	sprintf(s, "driver/emu10k1/%s", card->pci_dev->slot_name);
291 	remove_proc_entry(s, NULL);
292 
293  err_emu10k1_proc:
294 	remove_proc_entry("driver/emu10k1", NULL);
295  err_out:
296 	unregister_sound_mixer (card->ac97->dev_mixer);
297  err_codec:
298  	ac97_release_codec(card->ac97);
299 	return -EIO;
300 }
301 
emu10k1_mixer_cleanup(struct emu10k1_card * card)302 static void __devinit emu10k1_mixer_cleanup(struct emu10k1_card *card)
303 {
304 	char s[32];
305 
306 	if (!card->is_aps) {
307 		sprintf(s, "driver/emu10k1/%s/ac97", card->pci_dev->slot_name);
308 		remove_proc_entry(s, NULL);
309 
310 		sprintf(s, "driver/emu10k1/%s", card->pci_dev->slot_name);
311 		remove_proc_entry(s, NULL);
312 
313 		remove_proc_entry("driver/emu10k1", NULL);
314 	}
315 
316 	unregister_sound_mixer (card->ac97->dev_mixer);
317 	ac97_release_codec(card->ac97);
318 }
319 
emu10k1_midi_init(struct emu10k1_card * card)320 static int __devinit emu10k1_midi_init(struct emu10k1_card *card)
321 {
322 	int ret;
323 
324 	card->midi_dev = register_sound_midi(&emu10k1_midi_fops, -1);
325 	if (card->midi_dev < 0) {
326                 printk(KERN_ERR "emu10k1: cannot register midi device!\n");
327 		return -ENODEV;
328         }
329 
330 
331 	card->mpuout = kmalloc(sizeof(struct emu10k1_mpuout), GFP_KERNEL);
332 	if (card->mpuout == NULL) {
333 		printk(KERN_WARNING "emu10k1: Unable to allocate emu10k1_mpuout: out of memory\n");
334 		ret = -ENOMEM;
335 		goto err_out1;
336 	}
337 
338 	memset(card->mpuout, 0, sizeof(struct emu10k1_mpuout));
339 
340 	card->mpuout->intr = 1;
341 	card->mpuout->status = FLAGS_AVAILABLE;
342 	card->mpuout->state = CARDMIDIOUT_STATE_DEFAULT;
343 
344 	tasklet_init(&card->mpuout->tasklet, emu10k1_mpuout_bh, (unsigned long) card);
345 
346 	spin_lock_init(&card->mpuout->lock);
347 
348 	card->mpuin = kmalloc(sizeof(struct emu10k1_mpuin), GFP_KERNEL);
349 	if (card->mpuin == NULL) {
350 		printk(KERN_WARNING "emu10k1: Unable to allocate emu10k1_mpuin: out of memory\n");
351 		ret = -ENOMEM;
352                 goto err_out2;
353 	}
354 
355 	memset(card->mpuin, 0, sizeof(struct emu10k1_mpuin));
356 
357 	card->mpuin->status = FLAGS_AVAILABLE;
358 
359 	tasklet_init(&card->mpuin->tasklet, emu10k1_mpuin_bh, (unsigned long) card->mpuin);
360 
361 	spin_lock_init(&card->mpuin->lock);
362 
363 	/* Reset the MPU port */
364 	if (emu10k1_mpu_reset(card) < 0) {
365 		ERROR();
366 		ret = -EIO;
367 		goto err_out3;
368 	}
369 
370 #ifdef EMU10K1_SEQUENCER
371 	card->seq_dev = sound_alloc_mididev();
372 	if (card->seq_dev == -1)
373 			printk(KERN_WARNING "emu10k1: unable to register sequencer device!");
374 	else {
375 			std_midi_synth.midi_dev = card->seq_dev;
376 			midi_devs[card->seq_dev] =
377 					(struct midi_operations *)
378 					kmalloc(sizeof(struct midi_operations), GFP_KERNEL);
379 
380 			if (midi_devs[card->seq_dev] == NULL) {
381 				printk(KERN_ERR "emu10k1: unable to allocate memory!");
382 				sound_unload_mididev(card->seq_dev);
383 				card->seq_dev = -1;
384 				return 0;
385 			} else {
386 				memcpy((char *)midi_devs[card->seq_dev],
387 					(char *)&emu10k1_midi_operations,
388 					sizeof(struct midi_operations));
389 				midi_devs[card->seq_dev]->devc = card;
390 				sequencer_init();
391 			}
392 	}
393 	card->seq_mididev = 0;
394 #endif
395 	return 0;
396 
397 err_out3:
398 	kfree(card->mpuin);
399 err_out2:
400 	kfree(card->mpuout);
401 err_out1:
402 	unregister_sound_midi(card->midi_dev);
403 	return ret;
404 }
405 
emu10k1_midi_cleanup(struct emu10k1_card * card)406 static void __devinit emu10k1_midi_cleanup(struct emu10k1_card *card)
407 {
408 	tasklet_kill(&card->mpuout->tasklet);
409 	kfree(card->mpuout);
410 
411 	tasklet_kill(&card->mpuin->tasklet);
412 	kfree(card->mpuin);
413 
414 #ifdef EMU10K1_SEQUENCER
415 	if (card->seq_dev > -1) {
416 		kfree(midi_devs[card->seq_dev]);
417 		midi_devs[card->seq_dev] = NULL;
418 		sound_unload_mididev(card->seq_dev);
419 		card->seq_dev = -1;
420 	}
421 #endif
422 
423 	unregister_sound_midi(card->midi_dev);
424 }
425 
voice_init(struct emu10k1_card * card)426 static void __devinit voice_init(struct emu10k1_card *card)
427 {
428 	int i;
429 
430 	for (i = 0; i < NUM_G; i++)
431 		card->voicetable[i] = VOICE_USAGE_FREE;
432 }
433 
timer_init(struct emu10k1_card * card)434 static void __devinit timer_init(struct emu10k1_card *card)
435 {
436 	INIT_LIST_HEAD(&card->timers);
437 	card->timer_delay = TIMER_STOPPED;
438 	card->timer_lock = SPIN_LOCK_UNLOCKED;
439 }
440 
addxmgr_init(struct emu10k1_card * card)441 static void __devinit addxmgr_init(struct emu10k1_card *card)
442 {
443 	u32 count;
444 
445 	for (count = 0; count < MAXPAGES; count++)
446 		card->emupagetable[count] = 0;
447 
448 	/* Mark first page as used */
449 	/* This page is reserved by the driver */
450 	card->emupagetable[0] = 0x8001;
451 	card->emupagetable[1] = MAXPAGES - 1;
452 }
453 
fx_cleanup(struct patch_manager * mgr)454 static void __devinit fx_cleanup(struct patch_manager *mgr)
455 {
456 	int i;
457 	for(i = 0; i < mgr->current_pages; i++)
458 		free_page((unsigned long) mgr->patch[i]);
459 }
460 
fx_init(struct emu10k1_card * card)461 static int __devinit fx_init(struct emu10k1_card *card)
462 {
463 	struct patch_manager *mgr = &card->mgr;
464 	struct dsp_patch *patch;
465 	struct dsp_rpatch *rpatch;
466 	s32 left, right;
467 	int i;
468 	u32 pc = 0;
469 	u32 patch_n;
470 
471 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
472 		mgr->ctrl_gpr[i][0] = -1;
473 		mgr->ctrl_gpr[i][1] = -1;
474 	}
475 
476 	for (i = 0; i < 512; i++)
477 		OP(6, 0x40, 0x40, 0x40, 0x40);
478 
479 	for (i = 0; i < 256; i++)
480 		sblive_writeptr_tag(card, 0,
481 				    FXGPREGBASE + i, 0,
482 				    TANKMEMADDRREGBASE + i, 0,
483 				    TAGLIST_END);
484 
485 	/* !! The number bellow must equal the number of patches, currently 11 !! */
486 	mgr->current_pages = (11 + PATCHES_PER_PAGE - 1) / PATCHES_PER_PAGE;
487 	for (i = 0; i < mgr->current_pages; i++) {
488 		mgr->patch[i] = (void *)__get_free_page(GFP_KERNEL);
489 		if (mgr->patch[i] == NULL) {
490 			mgr->current_pages = i;
491 			fx_cleanup(mgr);
492 			return -ENOMEM;
493 		}
494 		memset(mgr->patch[i], 0, PAGE_SIZE);
495 	}
496 
497 	pc = 0;
498 	patch_n = 0;
499 	//first free GPR = 0x11b
500 
501 	/* FX volume correction and Volume control*/
502 	INPUT_PATCH_START(patch, "Pcm L vol", 0x0, 0);
503 	GET_OUTPUT_GPR(patch, 0x100, 0x0);
504 	GET_CONTROL_GPR(patch, 0x106, "Vol", 0, 0x7fffffff);
505 	GET_DYNAMIC_GPR(patch, 0x112);
506 
507 	OP(4, 0x112, 0x40, PCM_IN_L, 0x44); //*4
508 	OP(0, 0x100, 0x040, 0x112, 0x106);  //*vol
509 	INPUT_PATCH_END(patch);
510 
511 
512 	INPUT_PATCH_START(patch, "Pcm R vol", 0x1, 0);
513 	GET_OUTPUT_GPR(patch, 0x101, 0x1);
514 	GET_CONTROL_GPR(patch, 0x107, "Vol", 0, 0x7fffffff);
515 	GET_DYNAMIC_GPR(patch, 0x112);
516 
517 	OP(4, 0x112, 0x40, PCM_IN_R, 0x44);
518 	OP(0, 0x101, 0x040, 0x112, 0x107);
519 
520 	INPUT_PATCH_END(patch);
521 
522 
523 	// CD-Digital In Volume control
524 	INPUT_PATCH_START(patch, "CD-Digital Vol L", 0x12, 0);
525 	GET_OUTPUT_GPR(patch, 0x10c, 0x12);
526 	GET_CONTROL_GPR(patch, 0x10d, "Vol", 0, 0x7fffffff);
527 
528 	OP(0, 0x10c, 0x040, SPDIF_CD_L, 0x10d);
529 	INPUT_PATCH_END(patch);
530 
531 	INPUT_PATCH_START(patch, "CD-Digital Vol R", 0x13, 0);
532 	GET_OUTPUT_GPR(patch, 0x10e, 0x13);
533 	GET_CONTROL_GPR(patch, 0x10f, "Vol", 0, 0x7fffffff);
534 
535 	OP(0, 0x10e, 0x040, SPDIF_CD_R, 0x10f);
536 	INPUT_PATCH_END(patch);
537 
538 	//Volume Correction for Multi-channel Inputs
539 	INPUT_PATCH_START(patch, "Multi-Channel Gain", 0x08, 0);
540 	patch->input=patch->output=0x3F00;
541 
542 	GET_OUTPUT_GPR(patch, 0x113, MULTI_FRONT_L);
543 	GET_OUTPUT_GPR(patch, 0x114, MULTI_FRONT_R);
544 	GET_OUTPUT_GPR(patch, 0x115, MULTI_REAR_L);
545 	GET_OUTPUT_GPR(patch, 0x116, MULTI_REAR_R);
546 	GET_OUTPUT_GPR(patch, 0x117, MULTI_CENTER);
547 	GET_OUTPUT_GPR(patch, 0x118, MULTI_LFE);
548 
549 	OP(4, 0x113, 0x40, MULTI_FRONT_L, 0x44);
550 	OP(4, 0x114, 0x40, MULTI_FRONT_R, 0x44);
551 	OP(4, 0x115, 0x40, MULTI_REAR_L, 0x44);
552 	OP(4, 0x116, 0x40, MULTI_REAR_R, 0x44);
553 	OP(4, 0x117, 0x40, MULTI_CENTER, 0x44);
554 	OP(4, 0x118, 0x40, MULTI_LFE, 0x44);
555 
556 	INPUT_PATCH_END(patch);
557 
558 
559 	//Routing patch start
560 	ROUTING_PATCH_START(rpatch, "Routing");
561 	GET_INPUT_GPR(rpatch, 0x100, 0x0);
562 	GET_INPUT_GPR(rpatch, 0x101, 0x1);
563 	GET_INPUT_GPR(rpatch, 0x10c, 0x12);
564 	GET_INPUT_GPR(rpatch, 0x10e, 0x13);
565 	GET_INPUT_GPR(rpatch, 0x113, MULTI_FRONT_L);
566 	GET_INPUT_GPR(rpatch, 0x114, MULTI_FRONT_R);
567 	GET_INPUT_GPR(rpatch, 0x115, MULTI_REAR_L);
568 	GET_INPUT_GPR(rpatch, 0x116, MULTI_REAR_R);
569 	GET_INPUT_GPR(rpatch, 0x117, MULTI_CENTER);
570 	GET_INPUT_GPR(rpatch, 0x118, MULTI_LFE);
571 
572 	GET_DYNAMIC_GPR(rpatch, 0x102);
573 	GET_DYNAMIC_GPR(rpatch, 0x103);
574 
575 	GET_OUTPUT_GPR(rpatch, 0x104, 0x8);
576 	GET_OUTPUT_GPR(rpatch, 0x105, 0x9);
577 	GET_OUTPUT_GPR(rpatch, 0x10a, 0x2);
578 	GET_OUTPUT_GPR(rpatch, 0x10b, 0x3);
579 
580 
581 	/* input buffer */
582 	OP(6, 0x102, AC97_IN_L, 0x40, 0x40);
583 	OP(6, 0x103, AC97_IN_R, 0x40, 0x40);
584 
585 
586 	/* Digital In + PCM + MULTI_FRONT-> AC97 out (front speakers)*/
587 	OP(6, AC97_FRONT_L, 0x100, 0x10c, 0x113);
588 
589 	CONNECT(MULTI_FRONT_L, AC97_FRONT_L);
590 	CONNECT(PCM_IN_L, AC97_FRONT_L);
591 	CONNECT(SPDIF_CD_L, AC97_FRONT_L);
592 
593 	OP(6, AC97_FRONT_R, 0x101, 0x10e, 0x114);
594 
595 	CONNECT(MULTI_FRONT_R, AC97_FRONT_R);
596 	CONNECT(PCM_IN_R, AC97_FRONT_R);
597 	CONNECT(SPDIF_CD_R, AC97_FRONT_R);
598 
599 	/* Digital In + PCM + AC97 In + PCM1 + MULTI_REAR --> Rear Channel */
600 	OP(6, 0x104, PCM1_IN_L, 0x100, 0x115);
601 	OP(6, 0x104, 0x104, 0x10c, 0x102);
602 
603 	CONNECT(MULTI_REAR_L, ANALOG_REAR_L);
604 	CONNECT(AC97_IN_L, ANALOG_REAR_L);
605 	CONNECT(PCM_IN_L, ANALOG_REAR_L);
606 	CONNECT(SPDIF_CD_L, ANALOG_REAR_L);
607 	CONNECT(PCM1_IN_L, ANALOG_REAR_L);
608 
609 	OP(6, 0x105, PCM1_IN_R, 0x101, 0x116);
610 	OP(6, 0x105, 0x105, 0x10e, 0x103);
611 
612 	CONNECT(MULTI_REAR_R, ANALOG_REAR_R);
613 	CONNECT(AC97_IN_R, ANALOG_REAR_R);
614 	CONNECT(PCM_IN_R, ANALOG_REAR_R);
615 	CONNECT(SPDIF_CD_R, ANALOG_REAR_R);
616 	CONNECT(PCM1_IN_R, ANALOG_REAR_R);
617 
618 	/* Digital In + PCM + AC97 In + MULTI_FRONT --> Digital out */
619 	OP(6, 0x10a, 0x100, 0x102, 0x10c);
620 	OP(6, 0x10a, 0x10a, 0x113, 0x40);
621 
622 	CONNECT(MULTI_FRONT_L, DIGITAL_OUT_L);
623 	CONNECT(PCM_IN_L, DIGITAL_OUT_L);
624 	CONNECT(AC97_IN_L, DIGITAL_OUT_L);
625 	CONNECT(SPDIF_CD_L, DIGITAL_OUT_L);
626 
627 	OP(6, 0x10b, 0x101, 0x103, 0x10e);
628 	OP(6, 0x10b, 0x10b, 0x114, 0x40);
629 
630 	CONNECT(MULTI_FRONT_R, DIGITAL_OUT_R);
631 	CONNECT(PCM_IN_R, DIGITAL_OUT_R);
632 	CONNECT(AC97_IN_R, DIGITAL_OUT_R);
633 	CONNECT(SPDIF_CD_R, DIGITAL_OUT_R);
634 
635 	/* AC97 In --> ADC Recording Buffer */
636 	OP(6, ADC_REC_L, 0x102, 0x40, 0x40);
637 
638 	CONNECT(AC97_IN_L, ADC_REC_L);
639 
640 	OP(6, ADC_REC_R, 0x103, 0x40, 0x40);
641 
642 	CONNECT(AC97_IN_R, ADC_REC_R);
643 
644 
645 	/* fx12:Analog-Center */
646 	OP(6, ANALOG_CENTER, 0x117, 0x40, 0x40);
647 	CONNECT(MULTI_CENTER, ANALOG_CENTER);
648 
649 	/* fx11:Analog-LFE */
650 	OP(6, ANALOG_LFE, 0x118, 0x40, 0x40);
651 	CONNECT(MULTI_LFE, ANALOG_LFE);
652 
653 	/* fx12:Digital-Center */
654 	OP(6, DIGITAL_CENTER, 0x117, 0x40, 0x40);
655 	CONNECT(MULTI_CENTER, DIGITAL_CENTER);
656 
657 	/* fx11:Analog-LFE */
658 	OP(6, DIGITAL_LFE, 0x118, 0x40, 0x40);
659 	CONNECT(MULTI_LFE, DIGITAL_LFE);
660 
661 	ROUTING_PATCH_END(rpatch);
662 
663 
664 	// Rear volume control
665 	OUTPUT_PATCH_START(patch, "Vol Rear L", 0x8, 0);
666 	GET_INPUT_GPR(patch, 0x104, 0x8);
667 	GET_CONTROL_GPR(patch, 0x119, "Vol", 0, 0x7fffffff);
668 
669 	OP(0, ANALOG_REAR_L, 0x040, 0x104, 0x119);
670 	OUTPUT_PATCH_END(patch);
671 
672 
673 	OUTPUT_PATCH_START(patch, "Vol Rear R", 0x9, 0);
674 	GET_INPUT_GPR(patch, 0x105, 0x9);
675 	GET_CONTROL_GPR(patch, 0x11a, "Vol", 0, 0x7fffffff);
676 
677 	OP(0, ANALOG_REAR_R, 0x040, 0x105, 0x11a);
678 	OUTPUT_PATCH_END(patch);
679 
680 
681 	//Master volume control on front-digital
682 	OUTPUT_PATCH_START(patch, "Vol Master L", 0x2, 1);
683 	GET_INPUT_GPR(patch, 0x10a, 0x2);
684 	GET_CONTROL_GPR(patch, 0x108, "Vol", 0, 0x7fffffff);
685 
686 	OP(0, DIGITAL_OUT_L, 0x040, 0x10a, 0x108);
687 	OUTPUT_PATCH_END(patch);
688 
689 
690 	OUTPUT_PATCH_START(patch, "Vol Master R", 0x3, 1);
691 	GET_INPUT_GPR(patch, 0x10b, 0x3);
692 	GET_CONTROL_GPR(patch, 0x109, "Vol", 0, 0x7fffffff);
693 
694 	OP(0, DIGITAL_OUT_R, 0x040, 0x10b, 0x109);
695 	OUTPUT_PATCH_END(patch);
696 
697 
698 	/* delimiter patch */
699 	patch = PATCH(mgr, patch_n);
700 	patch->code_size = 0;
701 
702 	sblive_writeptr(card, DBG, 0, 0);
703 
704 	mgr->lock = SPIN_LOCK_UNLOCKED;
705 
706 
707 	//Master volume
708 	mgr->ctrl_gpr[SOUND_MIXER_VOLUME][0] = 8;
709 	mgr->ctrl_gpr[SOUND_MIXER_VOLUME][1] = 9;
710 
711 	left = card->ac97->mixer_state[SOUND_MIXER_VOLUME] & 0xff;
712 	right = (card->ac97->mixer_state[SOUND_MIXER_VOLUME] >> 8) & 0xff;
713 
714 	emu10k1_set_volume_gpr(card, 8, left, 1 << card->ac97->bit_resolution);
715 	emu10k1_set_volume_gpr(card, 9, right, 1 << card->ac97->bit_resolution);
716 
717 	//Rear volume
718 	mgr->ctrl_gpr[ SOUND_MIXER_OGAIN ][0] = 0x19;
719 	mgr->ctrl_gpr[ SOUND_MIXER_OGAIN ][1] = 0x1a;
720 
721 	left = right = 67;
722 	card->ac97->mixer_state[SOUND_MIXER_OGAIN] = (right << 8) | left;
723 
724 	card->ac97->supported_mixers |= SOUND_MASK_OGAIN;
725 	card->ac97->stereo_mixers |= SOUND_MASK_OGAIN;
726 
727 	emu10k1_set_volume_gpr(card, 0x19, left, VOL_5BIT);
728 	emu10k1_set_volume_gpr(card, 0x1a, right, VOL_5BIT);
729 
730 	//PCM Volume
731 	mgr->ctrl_gpr[SOUND_MIXER_PCM][0] = 6;
732 	mgr->ctrl_gpr[SOUND_MIXER_PCM][1] = 7;
733 
734 	left = card->ac97->mixer_state[SOUND_MIXER_PCM] & 0xff;
735 	right = (card->ac97->mixer_state[SOUND_MIXER_PCM] >> 8) & 0xff;
736 
737 	emu10k1_set_volume_gpr(card, 6, left, VOL_5BIT);
738 	emu10k1_set_volume_gpr(card, 7, right, VOL_5BIT);
739 
740 	//CD-Digital Volume
741 	mgr->ctrl_gpr[SOUND_MIXER_DIGITAL1][0] = 0xd;
742 	mgr->ctrl_gpr[SOUND_MIXER_DIGITAL1][1] = 0xf;
743 
744 	left = right = 67;
745 	card->ac97->mixer_state[SOUND_MIXER_DIGITAL1] = (right << 8) | left;
746 
747 	card->ac97->supported_mixers |= SOUND_MASK_DIGITAL1;
748 	card->ac97->stereo_mixers |= SOUND_MASK_DIGITAL1;
749 
750 	emu10k1_set_volume_gpr(card, 0xd, left, VOL_5BIT);
751 	emu10k1_set_volume_gpr(card, 0xf, right, VOL_5BIT);
752 
753 	//hard wire the ac97's pcm, we'll do that in dsp code instead.
754 	emu10k1_ac97_write(card->ac97, 0x18, 0x0);
755 	card->ac97_supported_mixers &= ~SOUND_MASK_PCM;
756 	card->ac97_stereo_mixers &= ~SOUND_MASK_PCM;
757 
758 	//set Igain to 0dB by default, maybe consider hardwiring it here.
759 	emu10k1_ac97_write(card->ac97, AC97_RECORD_GAIN, 0x0000);
760 	card->ac97->mixer_state[SOUND_MIXER_IGAIN] = 0x101;
761 
762 	return 0;
763 }
764 
hw_init(struct emu10k1_card * card)765 static int __devinit hw_init(struct emu10k1_card *card)
766 {
767 	int nCh;
768 	u32 pagecount; /* tmp */
769 	int ret;
770 
771 	/* Disable audio and lock cache */
772 	emu10k1_writefn0(card, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE);
773 
774 	/* Reset recording buffers */
775 	sblive_writeptr_tag(card, 0,
776 			    MICBS, ADCBS_BUFSIZE_NONE,
777 			    MICBA, 0,
778 			    FXBS, ADCBS_BUFSIZE_NONE,
779 			    FXBA, 0,
780 			    ADCBS, ADCBS_BUFSIZE_NONE,
781 			    ADCBA, 0,
782 			    TAGLIST_END);
783 
784 	/* Disable channel interrupt */
785 	emu10k1_writefn0(card, INTE, 0);
786 	sblive_writeptr_tag(card, 0,
787 			    CLIEL, 0,
788 			    CLIEH, 0,
789 			    SOLEL, 0,
790 			    SOLEH, 0,
791 			    TAGLIST_END);
792 
793 	/* Init envelope engine */
794 	for (nCh = 0; nCh < NUM_G; nCh++) {
795 		sblive_writeptr_tag(card, nCh,
796 				    DCYSUSV, 0,
797 				    IP, 0,
798 				    VTFT, 0xffff,
799 				    CVCF, 0xffff,
800 				    PTRX, 0,
801 				    //CPF, 0,
802 				    CCR, 0,
803 
804 				    PSST, 0,
805 				    DSL, 0x10,
806 				    CCCA, 0,
807 				    Z1, 0,
808 				    Z2, 0,
809 				    FXRT, 0xd01c0000,
810 
811 				    ATKHLDM, 0,
812 				    DCYSUSM, 0,
813 				    IFATN, 0xffff,
814 				    PEFE, 0,
815 				    FMMOD, 0,
816 				    TREMFRQ, 24,	/* 1 Hz */
817 				    FM2FRQ2, 24,	/* 1 Hz */
818 				    TEMPENV, 0,
819 
820 				    /*** These are last so OFF prevents writing ***/
821 				    LFOVAL2, 0,
822 				    LFOVAL1, 0,
823 				    ATKHLDV, 0,
824 				    ENVVOL, 0,
825 				    ENVVAL, 0,
826                                     TAGLIST_END);
827 		sblive_writeptr(card, CPF, nCh, 0);
828 	}
829 
830 
831 	/*
832 	 ** Init to 0x02109204 :
833 	 ** Clock accuracy    = 0     (1000ppm)
834 	 ** Sample Rate       = 2     (48kHz)
835 	 ** Audio Channel     = 1     (Left of 2)
836 	 ** Source Number     = 0     (Unspecified)
837 	 ** Generation Status = 1     (Original for Cat Code 12)
838 	 ** Cat Code          = 12    (Digital Signal Mixer)
839 	 ** Mode              = 0     (Mode 0)
840 	 ** Emphasis          = 0     (None)
841 	 ** CP                = 1     (Copyright unasserted)
842 	 ** AN                = 0     (Digital audio)
843 	 ** P                 = 0     (Consumer)
844 	 */
845 
846 	sblive_writeptr_tag(card, 0,
847 
848 			    /* SPDIF0 */
849 			    SPCS0, (SPCS_CLKACCY_1000PPM | 0x002000000 |
850 				    SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT),
851 
852 			    /* SPDIF1 */
853 			    SPCS1, (SPCS_CLKACCY_1000PPM | 0x002000000 |
854 				    SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT),
855 
856 			    /* SPDIF2 & SPDIF3 */
857 			    SPCS2, (SPCS_CLKACCY_1000PPM | 0x002000000 |
858 				    SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT),
859 
860 			    TAGLIST_END);
861 
862 	ret = fx_init(card);		/* initialize effects engine */
863 	if (ret < 0)
864 		return ret;
865 
866 	card->tankmem.size = 0;
867 
868 	card->virtualpagetable.size = MAXPAGES * sizeof(u32);
869 
870 	card->virtualpagetable.addr = pci_alloc_consistent(card->pci_dev, card->virtualpagetable.size, &card->virtualpagetable.dma_handle);
871 	if (card->virtualpagetable.addr == NULL) {
872 		ERROR();
873 		ret = -ENOMEM;
874 		goto err0;
875 	}
876 
877 	card->silentpage.size = EMUPAGESIZE;
878 
879 	card->silentpage.addr = pci_alloc_consistent(card->pci_dev, card->silentpage.size, &card->silentpage.dma_handle);
880 	if (card->silentpage.addr == NULL) {
881 		ERROR();
882 		ret = -ENOMEM;
883 		goto err1;
884 	}
885 
886 	for (pagecount = 0; pagecount < MAXPAGES; pagecount++)
887 		((u32 *) card->virtualpagetable.addr)[pagecount] = cpu_to_le32(((u32) card->silentpage.dma_handle * 2) | pagecount);
888 
889 	/* Init page table & tank memory base register */
890 	sblive_writeptr_tag(card, 0,
891 			    PTB, (u32) card->virtualpagetable.dma_handle,
892 			    TCB, 0,
893 			    TCBS, 0,
894 			    TAGLIST_END);
895 
896 	for (nCh = 0; nCh < NUM_G; nCh++) {
897 		sblive_writeptr_tag(card, nCh,
898 				    MAPA, MAP_PTI_MASK | ((u32) card->silentpage.dma_handle * 2),
899 				    MAPB, MAP_PTI_MASK | ((u32) card->silentpage.dma_handle * 2),
900 				    TAGLIST_END);
901 	}
902 
903 	/* Hokay, now enable the AUD bit */
904 	/* Enable Audio = 1 */
905 	/* Mute Disable Audio = 0 */
906 	/* Lock Tank Memory = 1 */
907 	/* Lock Sound Memory = 0 */
908 	/* Auto Mute = 1 */
909 
910 	if (card->model == 0x20 || card->model == 0xc400 ||
911 	  (card->model == 0x21 && card->chiprev < 6))
912 	        emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE);
913 	else
914 		emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE | HCFG_JOYENABLE);
915 
916 	/* Enable Vol_Ctrl irqs */
917 	emu10k1_irq_enable(card, INTE_VOLINCRENABLE | INTE_VOLDECRENABLE | INTE_MUTEENABLE | INTE_FXDSPENABLE);
918 
919 	/* FIXME: TOSLink detection */
920 	card->has_toslink = 0;
921 
922 	/* Initialize digital passthrough variables */
923 	card->pt.pos_gpr = card->pt.intr_gpr = card->pt.enable_gpr = -1;
924 	card->pt.selected = 0;
925 	card->pt.state = PT_STATE_INACTIVE;
926 	card->pt.spcs_to_use = 0x01;
927 	card->pt.patch_name = "AC3pass";
928 	card->pt.intr_gpr_name = "count";
929 	card->pt.enable_gpr_name = "enable";
930 	card->pt.pos_gpr_name = "ptr";
931 	spin_lock_init(&card->pt.lock);
932 	init_waitqueue_head(&card->pt.wait);
933 
934 /*	tmp = sblive_readfn0(card, HCFG);
935 	if (tmp & (HCFG_GPINPUT0 | HCFG_GPINPUT1)) {
936 		sblive_writefn0(card, HCFG, tmp | 0x800);
937 
938 		udelay(512);
939 
940 		if (tmp != (sblive_readfn0(card, HCFG) & ~0x800)) {
941 			card->has_toslink = 1;
942 			sblive_writefn0(card, HCFG, tmp);
943 		}
944 	}
945 */
946 	return 0;
947 
948   err1:
949 	pci_free_consistent(card->pci_dev, card->virtualpagetable.size, card->virtualpagetable.addr, card->virtualpagetable.dma_handle);
950   err0:
951 	fx_cleanup(&card->mgr);
952 
953 	return ret;
954 }
955 
emu10k1_init(struct emu10k1_card * card)956 static int __devinit emu10k1_init(struct emu10k1_card *card)
957 {
958 	/* Init Card */
959 	if (hw_init(card) < 0)
960 		return -1;
961 
962 	voice_init(card);
963 	timer_init(card);
964 	addxmgr_init(card);
965 
966 	DPD(2, "  hw control register -> %#x\n", emu10k1_readfn0(card, HCFG));
967 
968 	return 0;
969 }
970 
emu10k1_cleanup(struct emu10k1_card * card)971 static void __devinit emu10k1_cleanup(struct emu10k1_card *card)
972 {
973 	int ch;
974 
975 	emu10k1_writefn0(card, INTE, 0);
976 
977 	/** Shutdown the chip **/
978 	for (ch = 0; ch < NUM_G; ch++)
979 		sblive_writeptr(card, DCYSUSV, ch, 0);
980 
981 	for (ch = 0; ch < NUM_G; ch++) {
982 		sblive_writeptr_tag(card, ch,
983 				    VTFT, 0,
984 				    CVCF, 0,
985 				    PTRX, 0,
986 				    //CPF, 0,
987 				    TAGLIST_END);
988 		sblive_writeptr(card, CPF, ch, 0);
989 	}
990 
991 	/* Disable audio and lock cache */
992 	emu10k1_writefn0(card, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE);
993 
994 	sblive_writeptr_tag(card, 0,
995                             PTB, 0,
996 
997 			    /* Reset recording buffers */
998 			    MICBS, ADCBS_BUFSIZE_NONE,
999 			    MICBA, 0,
1000 			    FXBS, ADCBS_BUFSIZE_NONE,
1001 			    FXBA, 0,
1002 			    FXWC, 0,
1003 			    ADCBS, ADCBS_BUFSIZE_NONE,
1004 			    ADCBA, 0,
1005 			    TCBS, 0,
1006 			    TCB, 0,
1007 			    DBG, 0x8000,
1008 
1009 			    /* Disable channel interrupt */
1010 			    CLIEL, 0,
1011 			    CLIEH, 0,
1012 			    SOLEL, 0,
1013 			    SOLEH, 0,
1014 			    TAGLIST_END);
1015 
1016 
1017 	pci_free_consistent(card->pci_dev, card->virtualpagetable.size, card->virtualpagetable.addr, card->virtualpagetable.dma_handle);
1018 	pci_free_consistent(card->pci_dev, card->silentpage.size, card->silentpage.addr, card->silentpage.dma_handle);
1019 
1020 	if(card->tankmem.size != 0)
1021 		pci_free_consistent(card->pci_dev, card->tankmem.size, card->tankmem.addr, card->tankmem.dma_handle);
1022 
1023 	/* release patch storage memory */
1024 	fx_cleanup(&card->mgr);
1025 }
1026 
1027 /* Driver initialization routine */
emu10k1_probe(struct pci_dev * pci_dev,const struct pci_device_id * pci_id)1028 static int __devinit emu10k1_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
1029 {
1030 	struct emu10k1_card *card;
1031 	u32 subsysvid;
1032 	int ret;
1033 
1034 	if (pci_set_dma_mask(pci_dev, EMU10K1_DMA_MASK)) {
1035 		printk(KERN_ERR "emu10k1: architecture does not support 29bit PCI busmaster DMA\n");
1036 		return -ENODEV;
1037 	}
1038 
1039 	if (pci_enable_device(pci_dev))
1040 		return -EIO;
1041 
1042 	pci_set_master(pci_dev);
1043 
1044 	if ((card = kmalloc(sizeof(struct emu10k1_card), GFP_KERNEL)) == NULL) {
1045                 printk(KERN_ERR "emu10k1: out of memory\n");
1046                 return -ENOMEM;
1047         }
1048         memset(card, 0, sizeof(struct emu10k1_card));
1049 
1050 	card->iobase = pci_resource_start(pci_dev, 0);
1051 	card->length = pci_resource_len(pci_dev, 0);
1052 
1053 	if (request_region(card->iobase, card->length, card_names[pci_id->driver_data]) == NULL) {
1054 		printk(KERN_ERR "emu10k1: IO space in use\n");
1055 		ret = -EBUSY;
1056 		goto err_region;
1057 	}
1058 
1059 	pci_set_drvdata(pci_dev, card);
1060 
1061 	card->irq = pci_dev->irq;
1062 	card->pci_dev = pci_dev;
1063 
1064 	/* Reserve IRQ Line */
1065 	if (request_irq(card->irq, emu10k1_interrupt, SA_SHIRQ, card_names[pci_id->driver_data], card)) {
1066 		printk(KERN_ERR "emu10k1: IRQ in use\n");
1067 		ret = -EBUSY;
1068 		goto err_irq;
1069 	}
1070 
1071 	pci_read_config_byte(pci_dev, PCI_REVISION_ID, &card->chiprev);
1072 	pci_read_config_word(pci_dev, PCI_SUBSYSTEM_ID, &card->model);
1073 
1074 	printk(KERN_INFO "emu10k1: %s rev %d model %#04x found, IO at %#04lx-%#04lx, IRQ %d\n",
1075 		card_names[pci_id->driver_data], card->chiprev, card->model, card->iobase,
1076 		card->iobase + card->length - 1, card->irq);
1077 
1078 	pci_read_config_dword(pci_dev, PCI_SUBSYSTEM_VENDOR_ID, &subsysvid);
1079 	card->is_aps = (subsysvid == EMU_APS_SUBID);
1080 
1081 	spin_lock_init(&card->lock);
1082 	init_MUTEX(&card->open_sem);
1083 	card->open_mode = 0;
1084 	init_waitqueue_head(&card->open_wait);
1085 
1086 	ret = emu10k1_audio_init(card);
1087 	if(ret < 0) {
1088                 printk(KERN_ERR "emu10k1: cannot initialize audio devices\n");
1089                 goto err_audio;
1090         }
1091 
1092 	ret = emu10k1_mixer_init(card);
1093 	if(ret < 0) {
1094 		printk(KERN_ERR "emu10k1: cannot initialize AC97 codec\n");
1095                 goto err_mixer;
1096 	}
1097 
1098 	ret = emu10k1_midi_init(card);
1099 	if (ret < 0) {
1100 		printk(KERN_ERR "emu10k1: cannot register midi device\n");
1101 		goto err_midi;
1102 	}
1103 
1104 	ret = emu10k1_init(card);
1105 	if (ret < 0) {
1106 		printk(KERN_ERR "emu10k1: cannot initialize device\n");
1107 		goto err_emu10k1_init;
1108 	}
1109 
1110 	if (card->is_aps)
1111 		emu10k1_ecard_init(card);
1112 
1113 	list_add(&card->list, &emu10k1_devs);
1114 
1115 	return 0;
1116 
1117 err_emu10k1_init:
1118 	emu10k1_midi_cleanup(card);
1119 
1120 err_midi:
1121 	emu10k1_mixer_cleanup(card);
1122 
1123 err_mixer:
1124 	emu10k1_audio_cleanup(card);
1125 
1126 err_audio:
1127 	free_irq(card->irq, card);
1128 
1129 err_irq:
1130 	release_region(card->iobase, card->length);
1131 	pci_set_drvdata(pci_dev, NULL);
1132 
1133 err_region:
1134 	kfree(card);
1135 
1136 	return ret;
1137 }
1138 
emu10k1_remove(struct pci_dev * pci_dev)1139 static void __devexit emu10k1_remove(struct pci_dev *pci_dev)
1140 {
1141 	struct emu10k1_card *card = pci_get_drvdata(pci_dev);
1142 
1143 	list_del(&card->list);
1144 
1145 	emu10k1_cleanup(card);
1146 	emu10k1_midi_cleanup(card);
1147 	emu10k1_mixer_cleanup(card);
1148 	emu10k1_audio_cleanup(card);
1149 	free_irq(card->irq, card);
1150 	release_region(card->iobase, card->length);
1151 	kfree(card);
1152 	pci_set_drvdata(pci_dev, NULL);
1153 }
1154 
1155 MODULE_AUTHOR("Bertrand Lee, Cai Ying. (Email to: emu10k1-devel@lists.sourceforge.net)");
1156 MODULE_DESCRIPTION("Creative EMU10K1 PCI Audio Driver v" DRIVER_VERSION "\nCopyright (C) 1999 Creative Technology Ltd.");
1157 MODULE_LICENSE("GPL");
1158 
1159 static struct pci_driver emu10k1_pci_driver = {
1160 	name:		"emu10k1",
1161 	id_table:	emu10k1_pci_tbl,
1162 	probe:		emu10k1_probe,
1163 	remove:		__devexit_p(emu10k1_remove),
1164 };
1165 
emu10k1_init_module(void)1166 static int __init emu10k1_init_module(void)
1167 {
1168 	printk(KERN_INFO "Creative EMU10K1 PCI Audio Driver, version " DRIVER_VERSION ", " __TIME__ " " __DATE__ "\n");
1169 
1170 	return pci_module_init(&emu10k1_pci_driver);
1171 }
1172 
emu10k1_cleanup_module(void)1173 static void __exit emu10k1_cleanup_module(void)
1174 {
1175 	pci_unregister_driver(&emu10k1_pci_driver);
1176 
1177 	return;
1178 }
1179 
1180 module_init(emu10k1_init_module);
1181 module_exit(emu10k1_cleanup_module);
1182 
1183 #ifdef EMU10K1_SEQUENCER
1184 
1185 /* in midi.c */
1186 extern int emu10k1_seq_midi_open(int dev, int mode,
1187 				void (*input)(int dev, unsigned char midi_byte),
1188 				void (*output)(int dev));
1189 extern void emu10k1_seq_midi_close(int dev);
1190 extern int emu10k1_seq_midi_out(int dev, unsigned char midi_byte);
1191 extern int emu10k1_seq_midi_start_read(int dev);
1192 extern int emu10k1_seq_midi_end_read(int dev);
1193 extern void emu10k1_seq_midi_kick(int dev);
1194 extern int emu10k1_seq_midi_buffer_status(int dev);
1195 
1196 static struct midi_operations emu10k1_midi_operations =
1197 {
1198 	THIS_MODULE,
1199 	{"EMU10K1 MIDI", 0, 0, SNDCARD_EMU10K1},
1200 	&std_midi_synth,
1201 	{0},
1202 	emu10k1_seq_midi_open,
1203 	emu10k1_seq_midi_close,
1204 	NULL,
1205 	emu10k1_seq_midi_out,
1206 	emu10k1_seq_midi_start_read,
1207 	emu10k1_seq_midi_end_read,
1208 	emu10k1_seq_midi_kick,
1209 	NULL,
1210 	emu10k1_seq_midi_buffer_status,
1211 	NULL
1212 };
1213 
1214 #endif
1215