Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
 /*
 **********************************************************************
 *     main.c - Creative EMU10K1 audio driver
 *     Copyright 1999, 2000 Creative Labs, Inc.
 *
 **********************************************************************
 *
 *     Date                 Author          Summary of changes
 *     ----                 ------          ------------------
 *     October 20, 1999     Bertrand Lee    base code release
 *     November 2, 1999     Alan Cox        cleaned up stuff
 *
 **********************************************************************
 *
 *     This program is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of
 *     the License, or (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public
 *     License along with this program; if not, write to the Free
 *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
 *     USA.
 *
 **********************************************************************
 *
 *      Supported devices:
 *      /dev/dsp:        Standard /dev/dsp device, OSS-compatible
 *      /dev/dsp1:       Routes to rear speakers only	 
 *      /dev/mixer:      Standard /dev/mixer device, OSS-compatible
 *      /dev/midi:       Raw MIDI UART device, mostly OSS-compatible
 *	/dev/sequencer:  Sequencer Interface (requires sound.o)
 *
 *      Revision history:
 *      0.1 beta Initial release
 *      0.2 Lowered initial mixer vol. Improved on stuttering wave playback. Added MIDI UART support.
 *      0.3 Fixed mixer routing bug, added APS, joystick support.
 *      0.4 Added rear-channel, SPDIF support.
 *	0.5 Source cleanup, SMP fixes, multiopen support, 64 bit arch fixes,
 *	    moved bh's to tasklets, moved to the new PCI driver initialization style.
 *	0.6 Make use of pci_alloc_consistent, improve compatibility layer for 2.2 kernels,
 *	    code reorganization and cleanup.
 *	0.7 Support for the Emu-APS. Bug fixes for voice cache setup, mmaped sound + poll().
 *          Support for setting external TRAM size.
 *      0.8 Make use of the kernel ac97 interface. Support for a dsp patch manager.
 *      0.9 Re-enables rear speakers volume controls
 *     0.10 Initializes rear speaker volume.
 *	    Dynamic patch storage allocation.
 *	    New private ioctls to change control gpr values.
 *	    Enable volume control interrupts.
 *	    By default enable dsp routes to digital out. 
 *     0.11 Fixed fx / 4 problem.
 *     0.12 Implemented mmaped for recording.
 *	    Fixed bug: not unreserving mmaped buffer pages.
 *	    IRQ handler cleanup.
 *     0.13 Fixed problem with dsp1
 *          Simplified dsp patch writing (inside the driver)
 *	    Fixed several bugs found by the Stanford tools
 *     0.14 New control gpr to oss mixer mapping feature (Chris Purnell)
 *          Added AC3 Passthrough Support (Juha Yrjola)
 *          Added Support for 5.1 cards (digital out and the third analog out)
 *     0.15 Added Sequencer Support (Daniel Mack)
 *          Support for multichannel pcm playback (Eduard Hasenleithner)
 *     0.16 Mixer improvements, added old treble/bass support (Daniel Bertrand)
 *          Small code format cleanup.
 *          Deadlock bug fix for emu10k1_volxxx_irqhandler().
 *     0.17 Fix for mixer SOUND_MIXER_INFO ioctl.
 *	    Fix for HIGHMEM machines (emu10k1 can only do 31 bit bus master) 
 *	    midi poll initial implementation.
 *	    Small mixer fixes/cleanups.
 *	    Improved support for 5.1 cards.
 *     0.18 Fix for possible leak in pci_alloc_consistent()
 *          Cleaned up poll() functions (audio and midi). Don't start input.
 *	    Restrict DMA pages used to 512Mib range.
 *	    New AC97_BOOST mixer ioctl.
 *    0.19a Added Support for Audigy Cards
 *	    Real fix for kernel with highmem support (cast dma_handle to u32).
 *	    Fix recording buffering parameters calculation.
 *	    Use unsigned long for variables in bit ops.
 *    0.20a Fixed recording startup
 *	    Fixed timer rate setting (it's a 16-bit register)
 *	0.21 Converted code to use pci_name() instead of accessing slot_name
 *	    directly (Eugene Teo)
 *********************************************************************/

/* These are only included once per module */
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>

#include "hwaccess.h"
#include "8010.h"
#include "efxmgr.h"
#include "cardwo.h"
#include "cardwi.h"
#include "cardmo.h"
#include "cardmi.h"
#include "recmgr.h"
#include "ecard.h"


#ifdef EMU10K1_SEQUENCER
#define MIDI_SYNTH_NAME "EMU10K1 MIDI"
#define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
 
#include "../sound_config.h"
#include "../midi_synth.h"

/* this should be in dev_table.h */
#define SNDCARD_EMU10K1 46
#endif
 

/* the emu10k1 _seems_ to only supports 29 bit (512MiB) bit bus master */
#define EMU10K1_DMA_MASK                0x1fffffff	/* DMA buffer mask for pci_alloc_consist */

#ifndef PCI_VENDOR_ID_CREATIVE
#define PCI_VENDOR_ID_CREATIVE 0x1102
#endif

#ifndef PCI_DEVICE_ID_CREATIVE_EMU10K1
#define PCI_DEVICE_ID_CREATIVE_EMU10K1 0x0002
#endif
#ifndef PCI_DEVICE_ID_CREATIVE_AUDIGY
#define PCI_DEVICE_ID_CREATIVE_AUDIGY 0x0004
#endif

#define EMU_APS_SUBID	0x40011102
 
enum {
	EMU10K1 = 0,
	AUDIGY,
};

static char *card_names[] __devinitdata = {
	"EMU10K1",
	"Audigy",
};

static struct pci_device_id emu10k1_pci_tbl[] = {
	{PCI_VENDOR_ID_CREATIVE, PCI_DEVICE_ID_CREATIVE_EMU10K1,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, EMU10K1},
	{PCI_VENDOR_ID_CREATIVE, PCI_DEVICE_ID_CREATIVE_AUDIGY,
	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, AUDIGY},
	{0,}
};

MODULE_DEVICE_TABLE(pci, emu10k1_pci_tbl);

/* Global var instantiation */

LIST_HEAD(emu10k1_devs);

extern struct file_operations emu10k1_audio_fops;
extern struct file_operations emu10k1_mixer_fops;
extern struct file_operations emu10k1_midi_fops;

#ifdef EMU10K1_SEQUENCER
static struct midi_operations emu10k1_midi_operations;
#endif

extern irqreturn_t emu10k1_interrupt(int, void *, struct pt_regs *s);

static int __devinit emu10k1_audio_init(struct emu10k1_card *card)
{
	/* Assign default playback voice parameters */
	if (card->is_audigy)
		card->mchannel_fx = 0;
	else
		card->mchannel_fx = 8;


	if (card->is_audigy) {
		/* mono voice */
		card->waveout.send_dcba[SEND_MONO] = 0xffffffff;
		card->waveout.send_hgfe[SEND_MONO] = 0x0000ffff;
	
		/* stereo voice */
		/* left */
		card->waveout.send_dcba[SEND_LEFT] = 0x00ff00ff;
		card->waveout.send_hgfe[SEND_LEFT] = 0x00007f7f;	
		/* right */
		card->waveout.send_dcba[SEND_RIGHT] = 0xff00ff00;
		card->waveout.send_hgfe[SEND_RIGHT] = 0x00007f7f;

		card->waveout.send_routing[ROUTE_PCM] = 0x03020100; // Regular pcm
		card->waveout.send_routing2[ROUTE_PCM] = 0x07060504;

		card->waveout.send_routing[ROUTE_PT] = 0x3f3f3d3c; // Passthrough
		card->waveout.send_routing2[ROUTE_PT] = 0x3f3f3f3f;
		
		card->waveout.send_routing[ROUTE_PCM1] = 0x03020100; // Spare
		card->waveout.send_routing2[ROUTE_PCM1] = 0x07060404;
		
	} else {
		/* mono voice */
		card->waveout.send_dcba[SEND_MONO] = 0x0000ffff;
	
		/* stereo voice */
		/* left */
		card->waveout.send_dcba[SEND_LEFT] = 0x000000ff;
		/* right */
		card->waveout.send_dcba[SEND_RIGHT] = 0x0000ff00;

		card->waveout.send_routing[ROUTE_PCM] = 0x3210; // pcm
		card->waveout.send_routing[ROUTE_PT] = 0x3210; // passthrough
		card->waveout.send_routing[ROUTE_PCM1] = 0x7654; // /dev/dsp1
	}

	/* Assign default recording parameters */
	/* FIXME */
	if (card->is_aps)
		card->wavein.recsrc = WAVERECORD_FX;
	else
		card->wavein.recsrc = WAVERECORD_AC97;

	card->wavein.fxwc = 0x0003;
	return 0;
}

static void emu10k1_audio_cleanup(struct emu10k1_card *card)
{
}

static int __devinit emu10k1_register_devices(struct emu10k1_card *card)
{
	card->audio_dev = register_sound_dsp(&emu10k1_audio_fops, -1);
	if (card->audio_dev < 0) {
		printk(KERN_ERR "emu10k1: cannot register first audio device!\n");
		goto err_dev;
	}

	card->audio_dev1 = register_sound_dsp(&emu10k1_audio_fops, -1);
	if (card->audio_dev1 < 0) {
		printk(KERN_ERR "emu10k1: cannot register second audio device!\n");
		goto err_dev1;
	}

	card->ac97->dev_mixer = register_sound_mixer(&emu10k1_mixer_fops, -1);
	if (card->ac97->dev_mixer < 0) {
		printk(KERN_ERR "emu10k1: cannot register mixer device\n");
		goto err_mixer;
        }

	card->midi_dev = register_sound_midi(&emu10k1_midi_fops, -1);
	if (card->midi_dev < 0) {
                printk(KERN_ERR "emu10k1: cannot register midi device!\n");
		goto err_midi;
        }

#ifdef EMU10K1_SEQUENCER
	card->seq_dev = sound_alloc_mididev();
	if (card->seq_dev == -1)
		printk(KERN_WARNING "emu10k1: unable to register sequencer device!");
	else {
		std_midi_synth.midi_dev = card->seq_dev;
		midi_devs[card->seq_dev] = 
			(struct midi_operations *)
			kmalloc(sizeof(struct midi_operations), GFP_KERNEL);

		if (midi_devs[card->seq_dev] == NULL) {
			printk(KERN_ERR "emu10k1: unable to allocate memory!");
			sound_unload_mididev(card->seq_dev);
			card->seq_dev = -1;
			/* return without error */
		} else {
			memcpy((char *)midi_devs[card->seq_dev], 
				(char *)&emu10k1_midi_operations, 
				sizeof(struct midi_operations));
			midi_devs[card->seq_dev]->devc = card;
			sequencer_init();
			card->seq_mididev = NULL;
		}
	}
#endif
	return 0;

err_midi:
	unregister_sound_mixer(card->ac97->dev_mixer);
err_mixer:
	unregister_sound_dsp(card->audio_dev);
err_dev1:
	unregister_sound_dsp(card->audio_dev);
err_dev:
	return -ENODEV;
}

static void emu10k1_unregister_devices(struct emu10k1_card *card)
{
#ifdef EMU10K1_SEQUENCER
	if (card->seq_dev > -1) {
		kfree(midi_devs[card->seq_dev]);
		midi_devs[card->seq_dev] = NULL;
		sound_unload_mididev(card->seq_dev);
		card->seq_dev = -1;
	}
#endif

	unregister_sound_midi(card->midi_dev);
	unregister_sound_mixer(card->ac97->dev_mixer);
	unregister_sound_dsp(card->audio_dev1);
	unregister_sound_dsp(card->audio_dev);
}

static int emu10k1_info_proc (char *page, char **start, off_t off,
			      int count, int *eof, void *data)
{
	struct emu10k1_card *card = data;
	int len = 0;
	
	if (card == NULL)
		return -ENODEV;

	len += sprintf (page + len, "Driver Version : %s\n", DRIVER_VERSION);
	len += sprintf (page + len, "Card type      : %s\n", card->is_aps ? "Aps" : (card->is_audigy ? "Audigy" : "Emu10k1"));
	len += sprintf (page + len, "Revision       : %d\n", card->chiprev);
	len += sprintf (page + len, "Model          : %#06x\n", card->model);
	len += sprintf (page + len, "IO             : %#06lx-%#06lx\n", card->iobase, card->iobase + card->length - 1);
	len += sprintf (page + len, "IRQ            : %d\n\n", card->irq);
	
	len += sprintf (page + len, "Registered /dev Entries:\n");
	len += sprintf (page + len, "/dev/dsp%d\n", card->audio_dev / 16);
	len += sprintf (page + len, "/dev/dsp%d\n", card->audio_dev1 / 16);
	len += sprintf (page + len, "/dev/mixer%d\n", card->ac97->dev_mixer / 16);
	len += sprintf (page + len, "/dev/midi%d\n", card->midi_dev / 16);

#ifdef EMU10K1_SEQUENCER
	len += sprintf (page + len, "/dev/sequencer\n");
#endif

	return len;
}

static int __devinit emu10k1_proc_init(struct emu10k1_card *card)
{
	char s[48];

	if (!proc_mkdir ("driver/emu10k1", NULL)) {
		printk(KERN_ERR "emu10k1: unable to create proc directory driver/emu10k1\n");
		goto err_out;
	}

	sprintf(s, "driver/emu10k1/%s", pci_name(card->pci_dev));
	if (!proc_mkdir (s, NULL)) {
		printk(KERN_ERR "emu10k1: unable to create proc directory %s\n", s);
		goto err_emu10k1_proc;
	}

	sprintf(s, "driver/emu10k1/%s/info", pci_name(card->pci_dev));
	if (!create_proc_read_entry (s, 0, NULL, emu10k1_info_proc, card)) {
		printk(KERN_ERR "emu10k1: unable to create proc entry %s\n", s);
		goto err_dev_proc;
	}

	if (!card->is_aps) {
		sprintf(s, "driver/emu10k1/%s/ac97", pci_name(card->pci_dev));
		if (!create_proc_read_entry (s, 0, NULL, ac97_read_proc, card->ac97)) {
			printk(KERN_ERR "emu10k1: unable to create proc entry %s\n", s);
			goto err_proc_ac97;
		}
	}

	return 0;

err_proc_ac97:
	sprintf(s, "driver/emu10k1/%s/info", pci_name(card->pci_dev));
	remove_proc_entry(s, NULL);

err_dev_proc:
	sprintf(s, "driver/emu10k1/%s", pci_name(card->pci_dev));
	remove_proc_entry(s, NULL);

err_emu10k1_proc:
	remove_proc_entry("driver/emu10k1", NULL);

err_out:
	return -EIO;
}

static void emu10k1_proc_cleanup(struct emu10k1_card *card)
{
	char s[48];

	if (!card->is_aps) {
		sprintf(s, "driver/emu10k1/%s/ac97", pci_name(card->pci_dev));
		remove_proc_entry(s, NULL);
	}

	sprintf(s, "driver/emu10k1/%s/info", pci_name(card->pci_dev));
	remove_proc_entry(s, NULL);

	sprintf(s, "driver/emu10k1/%s", pci_name(card->pci_dev));
	remove_proc_entry(s, NULL);
		
	remove_proc_entry("driver/emu10k1", NULL);
}

static int __devinit emu10k1_mixer_init(struct emu10k1_card *card)
{
	struct ac97_codec *codec  = ac97_alloc_codec();
	
	if(codec == NULL)
	{
		printk(KERN_ERR "emu10k1: cannot allocate mixer\n");
		return -EIO;
	}
	card->ac97 = codec;
	card->ac97->private_data = card;

	if (!card->is_aps) {
		card->ac97->id = 0;
		card->ac97->codec_read = emu10k1_ac97_read;
        	card->ac97->codec_write = emu10k1_ac97_write;

		if (ac97_probe_codec (card->ac97) == 0) {
			printk(KERN_ERR "emu10k1: unable to probe AC97 codec\n");
			goto err_out;
		}
		/* 5.1: Enable the additional AC97 Slots and unmute extra channels on AC97 codec */
		if (codec->codec_read(codec, AC97_EXTENDED_ID) & 0x0080){
			printk(KERN_INFO "emu10k1: SBLive! 5.1 card detected\n"); 
			sblive_writeptr(card, AC97SLOT, 0, AC97SLOT_CNTR | AC97SLOT_LFE);
			codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0);
		}

		// Force 5bit:		    
		//card->ac97->bit_resolution=5;

		/* these will store the original values and never be modified */
		card->ac97_supported_mixers = card->ac97->supported_mixers;
		card->ac97_stereo_mixers = card->ac97->stereo_mixers;
	}

	return 0;

 err_out:
 	ac97_release_codec(card->ac97);
	return -EIO;
}

static void emu10k1_mixer_cleanup(struct emu10k1_card *card)
{
	ac97_release_codec(card->ac97);
}

static int __devinit emu10k1_midi_init(struct emu10k1_card *card)
{
	int ret;

	card->mpuout = kmalloc(sizeof(struct emu10k1_mpuout), GFP_KERNEL);
	if (card->mpuout == NULL) {
		printk(KERN_WARNING "emu10k1: Unable to allocate emu10k1_mpuout: out of memory\n");
		ret = -ENOMEM;
		goto err_out1;
	}

	memset(card->mpuout, 0, sizeof(struct emu10k1_mpuout));

	card->mpuout->intr = 1;
	card->mpuout->status = FLAGS_AVAILABLE;
	card->mpuout->state = CARDMIDIOUT_STATE_DEFAULT;

	tasklet_init(&card->mpuout->tasklet, emu10k1_mpuout_bh, (unsigned long) card);

	spin_lock_init(&card->mpuout->lock);

	card->mpuin = kmalloc(sizeof(struct emu10k1_mpuin), GFP_KERNEL);
	if (card->mpuin == NULL) {
		printk(KERN_WARNING "emu10k1: Unable to allocate emu10k1_mpuin: out of memory\n");
		ret = -ENOMEM;
                goto err_out2;
	}

	memset(card->mpuin, 0, sizeof(struct emu10k1_mpuin));

	card->mpuin->status = FLAGS_AVAILABLE;

	tasklet_init(&card->mpuin->tasklet, emu10k1_mpuin_bh, (unsigned long) card->mpuin);

	spin_lock_init(&card->mpuin->lock);

	/* Reset the MPU port */
	if (emu10k1_mpu_reset(card) < 0) {
		ERROR();
		ret = -EIO;
		goto err_out3;
	}

	return 0;

err_out3:
	kfree(card->mpuin);
err_out2:
	kfree(card->mpuout);
err_out1:
	return ret;
}

static void emu10k1_midi_cleanup(struct emu10k1_card *card)
{
	tasklet_kill(&card->mpuout->tasklet);
	kfree(card->mpuout);

	tasklet_kill(&card->mpuin->tasklet);
	kfree(card->mpuin);
}

static void __devinit voice_init(struct emu10k1_card *card)
{
	int i;

	for (i = 0; i < NUM_G; i++)
		card->voicetable[i] = VOICE_USAGE_FREE;
}

static void __devinit timer_init(struct emu10k1_card *card)
{
	INIT_LIST_HEAD(&card->timers);
	card->timer_delay = TIMER_STOPPED;
	spin_lock_init(&card->timer_lock);
}

static void __devinit addxmgr_init(struct emu10k1_card *card)
{
	u32 count;

	for (count = 0; count < MAXPAGES; count++)
		card->emupagetable[count] = 0;

	/* Mark first page as used */
	/* This page is reserved by the driver */
	card->emupagetable[0] = 0x8001;
	card->emupagetable[1] = MAXPAGES - 1;
}

static void fx_cleanup(struct patch_manager *mgr)
{
	int i;
	for(i = 0; i < mgr->current_pages; i++)
		free_page((unsigned long) mgr->patch[i]);
}

static int __devinit fx_init(struct emu10k1_card *card)
{
	struct patch_manager *mgr = &card->mgr;
	struct dsp_patch *patch;
	struct dsp_rpatch *rpatch;
	s32 left, right;
	int i;
	u32 pc = 0;
	u32 patch_n=0;
	struct emu_efx_info_t emu_efx_info[2]=
		{{ 20, 10, 0x400, 0x100, 0x20 },
		 { 24, 12, 0x600, 0x400, 0x60 },
		}; 
			

	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
		mgr->ctrl_gpr[i][0] = -1;
		mgr->ctrl_gpr[i][1] = -1;
	}


	if (card->is_audigy)
		mgr->current_pages = (2 + PATCHES_PER_PAGE - 1) / PATCHES_PER_PAGE;
	else
		/* !! The number below must equal the number of patches, currently 11 !! */
		mgr->current_pages = (11 + PATCHES_PER_PAGE - 1) / PATCHES_PER_PAGE;
	
	for (i = 0; i < mgr->current_pages; i++) {
		mgr->patch[i] = (void *)__get_free_page(GFP_KERNEL);
		if (mgr->patch[i] == NULL) {
			mgr->current_pages = i;
			fx_cleanup(mgr);
			return -ENOMEM;
		}
		memset(mgr->patch[i], 0, PAGE_SIZE);
	}

	if (card->is_audigy) {
		for (i = 0; i < 1024; i++)
			OP(0xf, 0x0c0, 0x0c0, 0x0cf, 0x0c0);

		for (i = 0; i < 512 ; i++)
			sblive_writeptr(card, A_GPR_BASE+i,0,0);

		pc=0;

		//Pcm input volume
		OP(0, 0x402, 0x0c0, 0x406, 0x000);
		OP(0, 0x403, 0x0c0, 0x407, 0x001);

		//CD-Digital input Volume
		OP(0, 0x404, 0x0c0, 0x40d, 0x42);
		OP(0, 0x405, 0x0c0, 0x40f, 0x43);

		// CD + PCM 
		OP(6, 0x400, 0x0c0, 0x402, 0x404);
		OP(6, 0x401, 0x0c0, 0x403, 0x405);
		
		// Front Output + Master Volume
		OP(0, 0x68, 0x0c0, 0x408, 0x400);
		OP(0, 0x69, 0x0c0, 0x409, 0x401);

		// Add-in analog inputs for other speakers
		OP(6, 0x400, 0x40, 0x400, 0xc0);
		OP(6, 0x401, 0x41, 0x401, 0xc0);

		// Digital Front + Master Volume
		OP(0, 0x60, 0x0c0, 0x408, 0x400);
		OP(0, 0x61, 0x0c0, 0x409, 0x401);

		// Rear Output + Rear Volume
		OP(0, 0x06e, 0x0c0, 0x419, 0x400);
		OP(0, 0x06f, 0x0c0, 0x41a, 0x401);		

		// Digital Rear Output + Rear Volume
		OP(0, 0x066, 0x0c0, 0x419, 0x400);
		OP(0, 0x067, 0x0c0, 0x41a, 0x401);		

		// Audigy Drive, Headphone out
		OP(6, 0x64, 0x0c0, 0x0c0, 0x400);
		OP(6, 0x65, 0x0c0, 0x0c0, 0x401);

		// ac97 Recording
		OP(6, 0x76, 0x0c0, 0x0c0, 0x40);
		OP(6, 0x77, 0x0c0, 0x0c0, 0x41);
		
		// Center = sub = Left/2 + Right/2
		OP(0xe, 0x400, 0x401, 0xcd, 0x400);

		// center/sub  Volume (master)
		OP(0, 0x06a, 0x0c0, 0x408, 0x400);
		OP(0, 0x06b, 0x0c0, 0x409, 0x400);

		// Digital center/sub  Volume (master)
		OP(0, 0x062, 0x0c0, 0x408, 0x400);
		OP(0, 0x063, 0x0c0, 0x409, 0x400);

		ROUTING_PATCH_START(rpatch, "Routing");
		ROUTING_PATCH_END(rpatch);

		/* delimiter patch */
		patch = PATCH(mgr, patch_n);
		patch->code_size = 0;

	
		sblive_writeptr(card, 0x53, 0, 0);
	} else {
		for (i = 0; i < 512 ; i++)
			OP(6, 0x40, 0x40, 0x40, 0x40);

		for (i = 0; i < 256; i++)
			sblive_writeptr_tag(card, 0,
					    FXGPREGBASE + i, 0,
					    TANKMEMADDRREGBASE + i, 0,
					    TAGLIST_END);

		
		pc = 0;

		//first free GPR = 0x11b
	
		
		/* FX volume correction and Volume control*/
		INPUT_PATCH_START(patch, "Pcm L vol", 0x0, 0);
		GET_OUTPUT_GPR(patch, 0x100, 0x0);
		GET_CONTROL_GPR(patch, 0x106, "Vol", 0, 0x7fffffff);
		GET_DYNAMIC_GPR(patch, 0x112);

		OP(4, 0x112, 0x40, PCM_IN_L, 0x44); //*4	
		OP(0, 0x100, 0x040, 0x112, 0x106);  //*vol	
		INPUT_PATCH_END(patch);


		INPUT_PATCH_START(patch, "Pcm R vol", 0x1, 0);
		GET_OUTPUT_GPR(patch, 0x101, 0x1);
		GET_CONTROL_GPR(patch, 0x107, "Vol", 0, 0x7fffffff);
		GET_DYNAMIC_GPR(patch, 0x112);

		OP(4, 0x112, 0x40, PCM_IN_R, 0x44); 
		OP(0, 0x101, 0x040, 0x112, 0x107);

		INPUT_PATCH_END(patch);


		// CD-Digital In Volume control	
		INPUT_PATCH_START(patch, "CD-Digital Vol L", 0x12, 0);
		GET_OUTPUT_GPR(patch, 0x10c, 0x12);
		GET_CONTROL_GPR(patch, 0x10d, "Vol", 0, 0x7fffffff);

		OP(0, 0x10c, 0x040, SPDIF_CD_L, 0x10d);
		INPUT_PATCH_END(patch);

		INPUT_PATCH_START(patch, "CD-Digital Vol R", 0x13, 0);
		GET_OUTPUT_GPR(patch, 0x10e, 0x13);
		GET_CONTROL_GPR(patch, 0x10f, "Vol", 0, 0x7fffffff);

		OP(0, 0x10e, 0x040, SPDIF_CD_R, 0x10f);
		INPUT_PATCH_END(patch);

		//Volume Correction for Multi-channel Inputs	
		INPUT_PATCH_START(patch, "Multi-Channel Gain", 0x08, 0);
		patch->input=patch->output=0x3F00;

		GET_OUTPUT_GPR(patch, 0x113, MULTI_FRONT_L);
		GET_OUTPUT_GPR(patch, 0x114, MULTI_FRONT_R);
		GET_OUTPUT_GPR(patch, 0x115, MULTI_REAR_L);
		GET_OUTPUT_GPR(patch, 0x116, MULTI_REAR_R);
		GET_OUTPUT_GPR(patch, 0x117, MULTI_CENTER);
		GET_OUTPUT_GPR(patch, 0x118, MULTI_LFE);

		OP(4, 0x113, 0x40, MULTI_FRONT_L, 0x44);
		OP(4, 0x114, 0x40, MULTI_FRONT_R, 0x44);
		OP(4, 0x115, 0x40, MULTI_REAR_L, 0x44);
		OP(4, 0x116, 0x40, MULTI_REAR_R, 0x44);
		OP(4, 0x117, 0x40, MULTI_CENTER, 0x44);
		OP(4, 0x118, 0x40, MULTI_LFE, 0x44);
	
		INPUT_PATCH_END(patch);


		//Routing patch start	
		ROUTING_PATCH_START(rpatch, "Routing");
		GET_INPUT_GPR(rpatch, 0x100, 0x0);
		GET_INPUT_GPR(rpatch, 0x101, 0x1);
		GET_INPUT_GPR(rpatch, 0x10c, 0x12);
		GET_INPUT_GPR(rpatch, 0x10e, 0x13);
		GET_INPUT_GPR(rpatch, 0x113, MULTI_FRONT_L);
		GET_INPUT_GPR(rpatch, 0x114, MULTI_FRONT_R);
		GET_INPUT_GPR(rpatch, 0x115, MULTI_REAR_L);
		GET_INPUT_GPR(rpatch, 0x116, MULTI_REAR_R);
		GET_INPUT_GPR(rpatch, 0x117, MULTI_CENTER);
		GET_INPUT_GPR(rpatch, 0x118, MULTI_LFE);

		GET_DYNAMIC_GPR(rpatch, 0x102);
		GET_DYNAMIC_GPR(rpatch, 0x103);

		GET_OUTPUT_GPR(rpatch, 0x104, 0x8);
		GET_OUTPUT_GPR(rpatch, 0x105, 0x9);
		GET_OUTPUT_GPR(rpatch, 0x10a, 0x2);
		GET_OUTPUT_GPR(rpatch, 0x10b, 0x3);
		
		
		/* input buffer */
		OP(6, 0x102, AC97_IN_L, 0x40, 0x40);
		OP(6, 0x103, AC97_IN_R, 0x40, 0x40);


		/* Digital In + PCM + MULTI_FRONT-> AC97 out (front speakers)*/
		OP(6, AC97_FRONT_L, 0x100, 0x10c, 0x113);

		CONNECT(MULTI_FRONT_L, AC97_FRONT_L);
		CONNECT(PCM_IN_L, AC97_FRONT_L);
		CONNECT(SPDIF_CD_L, AC97_FRONT_L);

		OP(6, AC97_FRONT_R, 0x101, 0x10e, 0x114);

		CONNECT(MULTI_FRONT_R, AC97_FRONT_R);
		CONNECT(PCM_IN_R, AC97_FRONT_R);
		CONNECT(SPDIF_CD_R, AC97_FRONT_R);

		/* Digital In + PCM + AC97 In + PCM1 + MULTI_REAR --> Rear Channel */ 
		OP(6, 0x104, PCM1_IN_L, 0x100, 0x115);
		OP(6, 0x104, 0x104, 0x10c, 0x102);

		CONNECT(MULTI_REAR_L, ANALOG_REAR_L);
		CONNECT(AC97_IN_L, ANALOG_REAR_L);
		CONNECT(PCM_IN_L, ANALOG_REAR_L);
		CONNECT(SPDIF_CD_L, ANALOG_REAR_L);
		CONNECT(PCM1_IN_L, ANALOG_REAR_L);

		OP(6, 0x105, PCM1_IN_R, 0x101, 0x116);
		OP(6, 0x105, 0x105, 0x10e, 0x103);

		CONNECT(MULTI_REAR_R, ANALOG_REAR_R);
		CONNECT(AC97_IN_R, ANALOG_REAR_R);
		CONNECT(PCM_IN_R, ANALOG_REAR_R);
		CONNECT(SPDIF_CD_R, ANALOG_REAR_R);
		CONNECT(PCM1_IN_R, ANALOG_REAR_R);

		/* Digital In + PCM + AC97 In + MULTI_FRONT --> Digital out */
		OP(6, 0x10b, 0x100, 0x102, 0x10c);
		OP(6, 0x10b, 0x10b, 0x113, 0x40);

		CONNECT(MULTI_FRONT_L, DIGITAL_OUT_L);
		CONNECT(PCM_IN_L, DIGITAL_OUT_L);
		CONNECT(AC97_IN_L, DIGITAL_OUT_L);
		CONNECT(SPDIF_CD_L, DIGITAL_OUT_L);

		OP(6, 0x10a, 0x101, 0x103, 0x10e);
		OP(6, 0x10b, 0x10b, 0x114, 0x40);

		CONNECT(MULTI_FRONT_R, DIGITAL_OUT_R);
		CONNECT(PCM_IN_R, DIGITAL_OUT_R);
		CONNECT(AC97_IN_R, DIGITAL_OUT_R);
		CONNECT(SPDIF_CD_R, DIGITAL_OUT_R);

		/* AC97 In --> ADC Recording Buffer */
		OP(6, ADC_REC_L, 0x102, 0x40, 0x40);

		CONNECT(AC97_IN_L, ADC_REC_L);

		OP(6, ADC_REC_R, 0x103, 0x40, 0x40);

		CONNECT(AC97_IN_R, ADC_REC_R);


		/* fx12:Analog-Center */
		OP(6, ANALOG_CENTER, 0x117, 0x40, 0x40);
		CONNECT(MULTI_CENTER, ANALOG_CENTER);

		/* fx11:Analog-LFE */
		OP(6, ANALOG_LFE, 0x118, 0x40, 0x40);
		CONNECT(MULTI_LFE, ANALOG_LFE);

		/* fx12:Digital-Center */
		OP(6, DIGITAL_CENTER, 0x117, 0x40, 0x40);
		CONNECT(MULTI_CENTER, DIGITAL_CENTER);
		
		/* fx11:Analog-LFE */
		OP(6, DIGITAL_LFE, 0x118, 0x40, 0x40);
		CONNECT(MULTI_LFE, DIGITAL_LFE);
	
		ROUTING_PATCH_END(rpatch);


		// Rear volume control	
		OUTPUT_PATCH_START(patch, "Vol Rear L", 0x8, 0);
		GET_INPUT_GPR(patch, 0x104, 0x8);
		GET_CONTROL_GPR(patch, 0x119, "Vol", 0, 0x7fffffff);

		OP(0, ANALOG_REAR_L, 0x040, 0x104, 0x119);
		OUTPUT_PATCH_END(patch);

		OUTPUT_PATCH_START(patch, "Vol Rear R", 0x9, 0);
		GET_INPUT_GPR(patch, 0x105, 0x9);
		GET_CONTROL_GPR(patch, 0x11a, "Vol", 0, 0x7fffffff);

		OP(0, ANALOG_REAR_R, 0x040, 0x105, 0x11a);
		OUTPUT_PATCH_END(patch);


		//Master volume control on front-digital	
		OUTPUT_PATCH_START(patch, "Vol Master L", 0x2, 1);
		GET_INPUT_GPR(patch, 0x10a, 0x2);
		GET_CONTROL_GPR(patch, 0x108, "Vol", 0, 0x7fffffff);

		OP(0, DIGITAL_OUT_L, 0x040, 0x10a, 0x108);
		OUTPUT_PATCH_END(patch);


		OUTPUT_PATCH_START(patch, "Vol Master R", 0x3, 1);
		GET_INPUT_GPR(patch, 0x10b, 0x3);
		GET_CONTROL_GPR(patch, 0x109, "Vol", 0, 0x7fffffff);

		OP(0, DIGITAL_OUT_R, 0x040, 0x10b, 0x109);
		OUTPUT_PATCH_END(patch);


		/* delimiter patch */
		patch = PATCH(mgr, patch_n);
		patch->code_size = 0;

	
		sblive_writeptr(card, DBG, 0, 0);
	}

	spin_lock_init(&mgr->lock);

	// Set up Volume controls, try to keep this the same for both Audigy and Live

	//Master volume
	mgr->ctrl_gpr[SOUND_MIXER_VOLUME][0] = 8;
	mgr->ctrl_gpr[SOUND_MIXER_VOLUME][1] = 9;

	left = card->ac97->mixer_state[SOUND_MIXER_VOLUME] & 0xff;
	right = (card->ac97->mixer_state[SOUND_MIXER_VOLUME] >> 8) & 0xff;

	emu10k1_set_volume_gpr(card, 8, left, 1 << card->ac97->bit_resolution);
	emu10k1_set_volume_gpr(card, 9, right, 1 << card->ac97->bit_resolution);

	//Rear volume
	mgr->ctrl_gpr[ SOUND_MIXER_OGAIN ][0] = 0x19;
	mgr->ctrl_gpr[ SOUND_MIXER_OGAIN ][1] = 0x1a;

	left = right = 67;
	card->ac97->mixer_state[SOUND_MIXER_OGAIN] = (right << 8) | left;

	card->ac97->supported_mixers |= SOUND_MASK_OGAIN;
	card->ac97->stereo_mixers |= SOUND_MASK_OGAIN;

	emu10k1_set_volume_gpr(card, 0x19, left, VOL_5BIT);
	emu10k1_set_volume_gpr(card, 0x1a, right, VOL_5BIT);

	//PCM Volume
	mgr->ctrl_gpr[SOUND_MIXER_PCM][0] = 6;
	mgr->ctrl_gpr[SOUND_MIXER_PCM][1] = 7;

	left = card->ac97->mixer_state[SOUND_MIXER_PCM] & 0xff;
	right = (card->ac97->mixer_state[SOUND_MIXER_PCM] >> 8) & 0xff;

	emu10k1_set_volume_gpr(card, 6, left, VOL_5BIT);
	emu10k1_set_volume_gpr(card, 7, right, VOL_5BIT);

	//CD-Digital Volume
	mgr->ctrl_gpr[SOUND_MIXER_DIGITAL1][0] = 0xd;
	mgr->ctrl_gpr[SOUND_MIXER_DIGITAL1][1] = 0xf;

	left = right = 67;
	card->ac97->mixer_state[SOUND_MIXER_DIGITAL1] = (right << 8) | left; 

	card->ac97->supported_mixers |= SOUND_MASK_DIGITAL1;
	card->ac97->stereo_mixers |= SOUND_MASK_DIGITAL1;

	emu10k1_set_volume_gpr(card, 0xd, left, VOL_5BIT);
	emu10k1_set_volume_gpr(card, 0xf, right, VOL_5BIT);


	//hard wire the ac97's pcm, pcm volume is done above using dsp code.
	if (card->is_audigy)
		//for Audigy, we mute it and use the philips 6 channel DAC instead
		emu10k1_ac97_write(card->ac97, 0x18, 0x8000);
	else
		//For the Live we hardwire it to full volume
		emu10k1_ac97_write(card->ac97, 0x18, 0x0);

	//remove it from the ac97_codec's control
	card->ac97_supported_mixers &= ~SOUND_MASK_PCM;
	card->ac97_stereo_mixers &= ~SOUND_MASK_PCM;

	//set Igain to 0dB by default, maybe consider hardwiring it here.
	emu10k1_ac97_write(card->ac97, AC97_RECORD_GAIN, 0x0000);
	card->ac97->mixer_state[SOUND_MIXER_IGAIN] = 0x101; 

	return 0;
}

static int __devinit hw_init(struct emu10k1_card *card)
{
	int nCh;
	u32 pagecount; /* tmp */
	int ret;

	/* Disable audio and lock cache */
	emu10k1_writefn0(card, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE);

	/* Reset recording buffers */
	sblive_writeptr_tag(card, 0,
			    MICBS, ADCBS_BUFSIZE_NONE,
			    MICBA, 0,
			    FXBS, ADCBS_BUFSIZE_NONE,
			    FXBA, 0,
			    ADCBS, ADCBS_BUFSIZE_NONE,
			    ADCBA, 0,
			    TAGLIST_END);

	/* Disable channel interrupt */
	emu10k1_writefn0(card, INTE, 0);
	sblive_writeptr_tag(card, 0,
			    CLIEL, 0,
			    CLIEH, 0,
			    SOLEL, 0,
			    SOLEH, 0,
			    TAGLIST_END);

	if (card->is_audigy) {
		sblive_writeptr_tag(card,0,
				    0x5e,0xf00,
				    0x5f,0x3,
				    TAGLIST_END);
	}

	/* Init envelope engine */
	for (nCh = 0; nCh < NUM_G; nCh++) {
		sblive_writeptr_tag(card, nCh,
				    DCYSUSV, 0,
				    IP, 0,
				    VTFT, 0xffff,
				    CVCF, 0xffff,
				    PTRX, 0,
				    //CPF, 0,
				    CCR, 0,

				    PSST, 0,
				    DSL, 0x10,
				    CCCA, 0,
				    Z1, 0,
				    Z2, 0,
				    FXRT, 0xd01c0000,

				    ATKHLDM, 0,
				    DCYSUSM, 0,
				    IFATN, 0xffff,
				    PEFE, 0,
				    FMMOD, 0,
				    TREMFRQ, 24,	/* 1 Hz */
				    FM2FRQ2, 24,	/* 1 Hz */
				    TEMPENV, 0,

				    /*** These are last so OFF prevents writing ***/
				    LFOVAL2, 0,
				    LFOVAL1, 0,
				    ATKHLDV, 0,
				    ENVVOL, 0,
				    ENVVAL, 0,
                                    TAGLIST_END);
		sblive_writeptr(card, CPF, nCh, 0);
		/*
		  Audigy FXRT initialization
		  reversed eng'd, may not be accurate.
		 */
		if (card->is_audigy) {
			sblive_writeptr_tag(card,nCh,
					    0x4c,0x0,
					    0x4d,0x0,
					    0x4e,0x0,
					    0x4f,0x0,
					    A_FXRT1, 0x3f3f3f3f,
					    A_FXRT2, 0x3f3f3f3f,
					    A_SENDAMOUNTS, 0,
					    TAGLIST_END);
		}
	}
	

	/*
	 ** Init to 0x02109204 :
	 ** Clock accuracy    = 0     (1000ppm)
	 ** Sample Rate       = 2     (48kHz)
	 ** Audio Channel     = 1     (Left of 2)
	 ** Source Number     = 0     (Unspecified)
	 ** Generation Status = 1     (Original for Cat Code 12)
	 ** Cat Code          = 12    (Digital Signal Mixer)
	 ** Mode              = 0     (Mode 0)
	 ** Emphasis          = 0     (None)
	 ** CP                = 1     (Copyright unasserted)
	 ** AN                = 0     (Digital audio)
	 ** P                 = 0     (Consumer)
	 */

	sblive_writeptr_tag(card, 0,

			    /* SPDIF0 */
			    SPCS0, (SPCS_CLKACCY_1000PPM | 0x002000000 |
				    SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT),

			    /* SPDIF1 */
			    SPCS1, (SPCS_CLKACCY_1000PPM | 0x002000000 |
				    SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT),

			    /* SPDIF2 & SPDIF3 */
			    SPCS2, (SPCS_CLKACCY_1000PPM | 0x002000000 |
				    SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS | 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT),

			    TAGLIST_END);

	if (card->is_audigy && (card->chiprev == 4)) {
		/* Hacks for Alice3 to work independent of haP16V driver */
		u32 tmp;

		//Setup SRCMulti_I2S SamplingRate
		tmp = sblive_readptr(card, A_SPDIF_SAMPLERATE, 0);
		tmp &= 0xfffff1ff;
		tmp |= (0x2<<9);
		sblive_writeptr(card, A_SPDIF_SAMPLERATE, 0, tmp);

		/* Setup SRCSel (Enable Spdif,I2S SRCMulti) */
		emu10k1_writefn0(card, 0x20, 0x600000);
		emu10k1_writefn0(card, 0x24, 0x14);

		/* Setup SRCMulti Input Audio Enable */
		emu10k1_writefn0(card, 0x20, 0x6E0000);
		emu10k1_writefn0(card, 0x24, 0xFF00FF00);
	}

	ret = fx_init(card);		/* initialize effects engine */
	if (ret < 0)
		return ret;

	card->tankmem.size = 0;

	card->virtualpagetable.size = MAXPAGES * sizeof(u32);

	card->virtualpagetable.addr = pci_alloc_consistent(card->pci_dev, card->virtualpagetable.size, &card->virtualpagetable.dma_handle);
	if (card->virtualpagetable.addr == NULL) {
		ERROR();
		ret = -ENOMEM;
		goto err0;
	}

	card->silentpage.size = EMUPAGESIZE;

	card->silentpage.addr = pci_alloc_consistent(card->pci_dev, card->silentpage.size, &card->silentpage.dma_handle);
	if (card->silentpage.addr == NULL) {
		ERROR();
		ret = -ENOMEM;
		goto err1;
	}

	for (pagecount = 0; pagecount < MAXPAGES; pagecount++)
		((u32 *) card->virtualpagetable.addr)[pagecount] = cpu_to_le32(((u32) card->silentpage.dma_handle * 2) | pagecount);

	/* Init page table & tank memory base register */
	sblive_writeptr_tag(card, 0,
			    PTB, (u32) card->virtualpagetable.dma_handle,
			    TCB, 0,
			    TCBS, 0,
			    TAGLIST_END);

	for (nCh = 0; nCh < NUM_G; nCh++) {
		sblive_writeptr_tag(card, nCh,
				    MAPA, MAP_PTI_MASK | ((u32) card->silentpage.dma_handle * 2),
				    MAPB, MAP_PTI_MASK | ((u32) card->silentpage.dma_handle * 2),
				    TAGLIST_END);
	}

	/* Hokay, now enable the AUD bit */
	/* Enable Audio = 1 */
	/* Mute Disable Audio = 0 */
	/* Lock Tank Memory = 1 */
	/* Lock Sound Memory = 0 */
	/* Auto Mute = 1 */
	if (card->is_audigy) {
		if (card->chiprev == 4)
			emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_AC3ENABLE_CDSPDIF | HCFG_AC3ENABLE_GPSPDIF | HCFG_AUTOMUTE | HCFG_JOYENABLE);
		else
			emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_AUTOMUTE | HCFG_JOYENABLE);
	} else {
		if (card->model == 0x20 || card->model == 0xc400 ||
		 (card->model == 0x21 && card->chiprev < 6))
	        	emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE);
		else
			emu10k1_writefn0(card, HCFG, HCFG_AUDIOENABLE  | HCFG_LOCKTANKCACHE_MASK | HCFG_AUTOMUTE | HCFG_JOYENABLE);
	}
	/* Enable Vol_Ctrl irqs */
	emu10k1_irq_enable(card, INTE_VOLINCRENABLE | INTE_VOLDECRENABLE | INTE_MUTEENABLE | INTE_FXDSPENABLE);

	if (card->is_audigy && (card->chiprev == 4)) {
		/* Unmute Analog now.  Set GPO6 to 1 for Apollo.
		 * This has to be done after init ALice3 I2SOut beyond 48KHz.
		 * So, sequence is important. */
		u32 tmp = emu10k1_readfn0(card, A_IOCFG);
		tmp |= 0x0040;
		emu10k1_writefn0(card, A_IOCFG, tmp);
	}
	
	/* FIXME: TOSLink detection */
	card->has_toslink = 0;

	/* Initialize digital passthrough variables */
	card->pt.pos_gpr = card->pt.intr_gpr = card->pt.enable_gpr = -1;
	card->pt.selected = 0;
	card->pt.state = PT_STATE_INACTIVE;
	card->pt.spcs_to_use = 0x01;
	card->pt.patch_name = "AC3pass";
	card->pt.intr_gpr_name = "count";
	card->pt.enable_gpr_name = "enable";
	card->pt.pos_gpr_name = "ptr";
	spin_lock_init(&card->pt.lock);
	init_waitqueue_head(&card->pt.wait);

/*	tmp = sblive_readfn0(card, HCFG);
	if (tmp & (HCFG_GPINPUT0 | HCFG_GPINPUT1)) {
		sblive_writefn0(card, HCFG, tmp | 0x800);

		udelay(512);

		if (tmp != (sblive_readfn0(card, HCFG) & ~0x800)) {
			card->has_toslink = 1;
			sblive_writefn0(card, HCFG, tmp);
		}
	}
*/
	return 0;

  err1:
	pci_free_consistent(card->pci_dev, card->virtualpagetable.size, card->virtualpagetable.addr, card->virtualpagetable.dma_handle);
  err0:
	fx_cleanup(&card->mgr);

	return ret;
}

static int __devinit emu10k1_init(struct emu10k1_card *card)
{
	/* Init Card */
	if (hw_init(card) < 0)
		return -1;

	voice_init(card);
	timer_init(card);
	addxmgr_init(card);

	DPD(2, "  hw control register -> %#x\n", emu10k1_readfn0(card, HCFG));

	return 0;
}

static void emu10k1_cleanup(struct emu10k1_card *card)
{
	int ch;

	emu10k1_writefn0(card, INTE, 0);

	/** Shutdown the chip **/
	for (ch = 0; ch < NUM_G; ch++)
		sblive_writeptr(card, DCYSUSV, ch, 0);

	for (ch = 0; ch < NUM_G; ch++) {
		sblive_writeptr_tag(card, ch,
				    VTFT, 0,
				    CVCF, 0,
				    PTRX, 0,
				    //CPF, 0,
				    TAGLIST_END);
		sblive_writeptr(card, CPF, ch, 0);
	}

	/* Disable audio and lock cache */
	emu10k1_writefn0(card, HCFG, HCFG_LOCKSOUNDCACHE | HCFG_LOCKTANKCACHE_MASK | HCFG_MUTEBUTTONENABLE);

	sblive_writeptr_tag(card, 0,
                            PTB, 0,

			    /* Reset recording buffers */
			    MICBS, ADCBS_BUFSIZE_NONE,
			    MICBA, 0,
			    FXBS, ADCBS_BUFSIZE_NONE,
			    FXBA, 0,
			    FXWC, 0,
			    ADCBS, ADCBS_BUFSIZE_NONE,
			    ADCBA, 0,
			    TCBS, 0,
			    TCB, 0,
			    DBG, 0x8000,

			    /* Disable channel interrupt */
			    CLIEL, 0,
			    CLIEH, 0,
			    SOLEL, 0,
			    SOLEH, 0,
			    TAGLIST_END);

	if (card->is_audigy)
		sblive_writeptr(card, 0, A_DBG,  A_DBG_SINGLE_STEP);

	pci_free_consistent(card->pci_dev, card->virtualpagetable.size, card->virtualpagetable.addr, card->virtualpagetable.dma_handle);
	pci_free_consistent(card->pci_dev, card->silentpage.size, card->silentpage.addr, card->silentpage.dma_handle);
	
	if(card->tankmem.size != 0)
		pci_free_consistent(card->pci_dev, card->tankmem.size, card->tankmem.addr, card->tankmem.dma_handle);

	/* release patch storage memory */
	fx_cleanup(&card->mgr);
}

/* Driver initialization routine */
static int __devinit emu10k1_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
{
	struct emu10k1_card *card;
	u32 subsysvid;
	int ret;

	if (pci_set_dma_mask(pci_dev, EMU10K1_DMA_MASK)) {
		printk(KERN_ERR "emu10k1: architecture does not support 29bit PCI busmaster DMA\n");
		return -ENODEV;
	}

	if (pci_enable_device(pci_dev))
		return -EIO;

	pci_set_master(pci_dev);

	if ((card = kmalloc(sizeof(struct emu10k1_card), GFP_KERNEL)) == NULL) {
                printk(KERN_ERR "emu10k1: out of memory\n");
                return -ENOMEM;
        }
        memset(card, 0, sizeof(struct emu10k1_card));

	card->iobase = pci_resource_start(pci_dev, 0);
	card->length = pci_resource_len(pci_dev, 0); 

	if (request_region(card->iobase, card->length, card_names[pci_id->driver_data]) == NULL) {
		printk(KERN_ERR "emu10k1: IO space in use\n");
		ret = -EBUSY;
		goto err_region;
	}

	pci_set_drvdata(pci_dev, card);

	card->irq = pci_dev->irq;
	card->pci_dev = pci_dev;

	/* Reserve IRQ Line */
	if (request_irq(card->irq, emu10k1_interrupt, SA_SHIRQ, card_names[pci_id->driver_data], card)) {
		printk(KERN_ERR "emu10k1: IRQ in use\n");
		ret = -EBUSY;
		goto err_irq;
	}

	pci_read_config_byte(pci_dev, PCI_REVISION_ID, &card->chiprev);
	pci_read_config_word(pci_dev, PCI_SUBSYSTEM_ID, &card->model);

	printk(KERN_INFO "emu10k1: %s rev %d model %#04x found, IO at %#04lx-%#04lx, IRQ %d\n",
		card_names[pci_id->driver_data], card->chiprev, card->model, card->iobase,
		card->iobase + card->length - 1, card->irq);

	if (pci_id->device == PCI_DEVICE_ID_CREATIVE_AUDIGY)
		card->is_audigy = 1;

	pci_read_config_dword(pci_dev, PCI_SUBSYSTEM_VENDOR_ID, &subsysvid);
	card->is_aps = (subsysvid == EMU_APS_SUBID);

	spin_lock_init(&card->lock);
	init_MUTEX(&card->open_sem);
	card->open_mode = 0;
	init_waitqueue_head(&card->open_wait);

	ret = emu10k1_audio_init(card);
	if (ret < 0) {
                printk(KERN_ERR "emu10k1: cannot initialize audio devices\n");
                goto err_audio;
        }

	ret = emu10k1_mixer_init(card);
	if (ret < 0) {
		printk(KERN_ERR "emu10k1: cannot initialize AC97 codec\n");
                goto err_mixer;
	}

	ret = emu10k1_midi_init(card);
	if (ret < 0) {
		printk(KERN_ERR "emu10k1: cannot register midi device\n");
		goto err_midi;
	}

	ret = emu10k1_init(card);
	if (ret < 0) {
		printk(KERN_ERR "emu10k1: cannot initialize device\n");
		goto err_emu10k1_init;
	}

	if (card->is_aps)
		emu10k1_ecard_init(card);

	ret = emu10k1_register_devices(card);
	if (ret < 0)
		goto err_register;

	/* proc entries must be created after registering devices, as
	 * emu10k1_info_proc prints card->audio_dev &co. */
	ret = emu10k1_proc_init(card);
	if (ret < 0) {
		printk(KERN_ERR "emu10k1: cannot initialize proc directory\n");
                goto err_proc;
	}
	
	list_add(&card->list, &emu10k1_devs);

	return 0;

err_proc:
	emu10k1_unregister_devices(card);

err_register:
	emu10k1_cleanup(card);
	
err_emu10k1_init:
	emu10k1_midi_cleanup(card);

err_midi:
	emu10k1_mixer_cleanup(card);

err_mixer:
	emu10k1_audio_cleanup(card);

err_audio:
	free_irq(card->irq, card);

err_irq:
	release_region(card->iobase, card->length);
	pci_set_drvdata(pci_dev, NULL);

err_region:
	kfree(card);

	return ret;
}

static void __devexit emu10k1_remove(struct pci_dev *pci_dev)
{
	struct emu10k1_card *card = pci_get_drvdata(pci_dev);

	list_del(&card->list);

	emu10k1_unregister_devices(card);
	emu10k1_cleanup(card);
	emu10k1_midi_cleanup(card);
	emu10k1_mixer_cleanup(card);
	emu10k1_proc_cleanup(card);
	emu10k1_audio_cleanup(card);	
	free_irq(card->irq, card);
	release_region(card->iobase, card->length);
	kfree(card);
	pci_set_drvdata(pci_dev, NULL);
}

MODULE_AUTHOR("Bertrand Lee, Cai Ying. (Email to: emu10k1-devel@lists.sourceforge.net)");
MODULE_DESCRIPTION("Creative EMU10K1 PCI Audio Driver v" DRIVER_VERSION "\nCopyright (C) 1999 Creative Technology Ltd.");
MODULE_LICENSE("GPL");

static struct pci_driver emu10k1_pci_driver = {
	.name		= "emu10k1",
	.id_table	= emu10k1_pci_tbl,
	.probe		= emu10k1_probe,
	.remove		= __devexit_p(emu10k1_remove),
};

static int __init emu10k1_init_module(void)
{
	printk(KERN_INFO "Creative EMU10K1 PCI Audio Driver, version " DRIVER_VERSION ", " __TIME__ " " __DATE__ "\n");

	return pci_register_driver(&emu10k1_pci_driver);
}

static void __exit emu10k1_cleanup_module(void)
{
	pci_unregister_driver(&emu10k1_pci_driver);

	return;
}

module_init(emu10k1_init_module);
module_exit(emu10k1_cleanup_module);

#ifdef EMU10K1_SEQUENCER

/* in midi.c */
extern int emu10k1_seq_midi_open(int dev, int mode, 
				void (*input)(int dev, unsigned char midi_byte),
				void (*output)(int dev));
extern void emu10k1_seq_midi_close(int dev);
extern int emu10k1_seq_midi_out(int dev, unsigned char midi_byte);
extern int emu10k1_seq_midi_start_read(int dev);
extern int emu10k1_seq_midi_end_read(int dev);
extern void emu10k1_seq_midi_kick(int dev);
extern int emu10k1_seq_midi_buffer_status(int dev);

static struct midi_operations emu10k1_midi_operations =
{
	THIS_MODULE,
	{"EMU10K1 MIDI", 0, 0, SNDCARD_EMU10K1},
	&std_midi_synth,
	{0},
	emu10k1_seq_midi_open,
	emu10k1_seq_midi_close,
	NULL,
	emu10k1_seq_midi_out,
	emu10k1_seq_midi_start_read,
	emu10k1_seq_midi_end_read,
	emu10k1_seq_midi_kick,
	NULL,
	emu10k1_seq_midi_buffer_status,
	NULL
};

#endif