aboutsummaryrefslogblamecommitdiffstats
path: root/sound/oss/ite8172.c
blob: 68aab3605d74572cf27f59c66b37a28c9acdb5a7 (plain) (tree)
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
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805








































































                                                                            

                        








































































































































































































































                                                                               
                                























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                
                                   

                                                 
                                                     



                                                        
                                             




                                                        
                                           

































                                                                            
                                     







                                                                           
                                



                                                         
                                   








                                                                     
                                     


























































































                                                                           
                                        
                                                                     
                                          



























                                                                           
                                   


















                                                                 
                                                                



























































































































































































                                                                              
                                                   

















































                                                                           
/*
 *      ite8172.c  --  ITE IT8172G Sound Driver.
 *
 * Copyright 2001 MontaVista Software Inc.
 * Author: MontaVista Software, Inc.
 *         	stevel@mvista.com or source@mvista.com
 *
 *  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  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
 *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
 *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
 *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  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.
 *
 *
 * Module command line parameters:
 *
 *  Supported devices:
 *  /dev/dsp    standard OSS /dev/dsp device
 *  /dev/mixer  standard OSS /dev/mixer device
 *
 * Notes:
 *
 *  1. Much of the OSS buffer allocation, ioctl's, and mmap'ing are
 *     taken, slightly modified or not at all, from the ES1371 driver,
 *     so refer to the credits in es1371.c for those. The rest of the
 *     code (probe, open, read, write, the ISR, etc.) is new.
 *  2. The following support is untested:
 *      * Memory mapping the audio buffers, and the ioctl controls that go
 *        with it.
 *      * S/PDIF output.
 *      * I2S support.
 *  3. The following is not supported:
 *      * legacy audio mode.
 *  4. Support for volume button interrupts is implemented but doesn't
 *     work yet.
 *
 *  Revision history
 *    02.08.2001  Initial release
 *    06.22.2001  Added I2S support
 *    07.30.2003  Removed initialisation to zero for static variables
 *		   (spdif[NR_DEVICE], i2s_fmt[NR_DEVICE], and devindex)
 */
#include <linux/module.h>
#include <linux/string.h>
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/sound.h>
#include <linux/slab.h>
#include <linux/soundcard.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/bitops.h>
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <linux/smp_lock.h>
#include <linux/ac97_codec.h>
#include <linux/interrupt.h>
#include <linux/mutex.h>

#include <asm/io.h>
#include <asm/dma.h>
#include <asm/uaccess.h>
#include <asm/it8172/it8172.h>

/* --------------------------------------------------------------------- */

#undef OSS_DOCUMENTED_MIXER_SEMANTICS
#define IT8172_DEBUG
#undef IT8172_VERBOSE_DEBUG
#define DBG(x) {}

#define IT8172_MODULE_NAME "IT8172 audio"
#define PFX IT8172_MODULE_NAME

#ifdef IT8172_DEBUG
#define dbg(format, arg...) printk(KERN_DEBUG PFX ": " format "\n" , ## arg)
#else
#define dbg(format, arg...) do {} while (0)
#endif
#define err(format, arg...) printk(KERN_ERR PFX ": " format "\n" , ## arg)
#define info(format, arg...) printk(KERN_INFO PFX ": " format "\n" , ## arg)
#define warn(format, arg...) printk(KERN_WARNING PFX ": " format "\n" , ## arg)


#define IT8172_MODULE_NAME "IT8172 audio"
#define PFX IT8172_MODULE_NAME

#ifdef IT8172_DEBUG
#define dbg(format, arg...) printk(KERN_DEBUG PFX ": " format "\n" , ## arg)
#else
#define dbg(format, arg...) do {} while (0)
#endif
#define err(format, arg...) printk(KERN_ERR PFX ": " format "\n" , ## arg)
#define info(format, arg...) printk(KERN_INFO PFX ": " format "\n" , ## arg)
#define warn(format, arg...) printk(KERN_WARNING PFX ": " format "\n" , ## arg)


static const unsigned sample_shift[] = { 0, 1, 1, 2 };


/*
 * Audio Controller register bit definitions follow. See
 * include/asm/it8172/it8172.h for register offsets.
 */

/* PCM Out Volume Reg */
#define PCMOV_PCMOM	(1<<15)	/* PCM Out Mute default 1: mute */
#define	PCMOV_PCMRCG_BIT 8	/* PCM Right channel Gain */
#define	PCMOV_PCMRCG_MASK (0x1f<<PCMOV_PCMRCG_BIT)
#define PCMOV_PCMLCG_BIT 0	/* PCM Left channel gain  */
#define PCMOV_PCMLCG_MASK 0x1f

/* FM Out Volume Reg */
#define FMOV_FMOM       (1<<15)	/* FM Out Mute default 1: mute */
#define	FMOV_FMRCG_BIT	8	/* FM Right channel Gain */
#define	FMOV_FMRCG_MASK (0x1f<<FMOV_FMRCG_BIT)
#define FMOV_FMLCG_BIT	0	/* FM Left channel gain  */
#define FMOV_FMLCG_MASK 0x1f

/* I2S Out Volume Reg */
#define I2SV_I2SOM	 (1<<15) /* I2S Out Mute default 1: mute */
#define	I2SV_I2SRCG_BIT	 8	 /* I2S Right channel Gain */
#define	I2SV_I2SRCG_MASK (0x1f<<I2SV_I2SRCG_BIT)
#define I2SV_I2SLCG_BIT	 0	 /* I2S Left channel gain  */
#define I2SV_I2SLCG_MASK 0x1f

/* Digital Recording Source Select Reg */
#define	DRSS_BIT   0
#define	DRSS_MASK  0x07
#define   DRSS_AC97_PRIM 0
#define   DRSS_FM        1
#define   DRSS_I2S       2
#define   DRSS_PCM       3
#define   DRSS_AC97_SEC  4

/* Playback/Capture Channel Control Registers */
#define	CC_SM	        (1<<15)	/* Stereo, Mone 0: mono 1: stereo */
#define	CC_DF	        (1<<14)	/* Data Format 0: 8 bit 1: 16 bit */
#define CC_FMT_BIT      14
#define CC_FMT_MASK     (0x03<<CC_FMT_BIT)
#define CC_CF_BIT       12      /* Channel format (Playback only) */
#define CC_CF_MASK      (0x03<<CC_CF_BIT)
#define	  CC_CF_2	0
#define   CC_CF_4	(1<<CC_CF_BIT)
#define   CC_CF_6	(2<<CC_CF_BIT)
#define CC_SR_BIT       8       /* sample Rate */
#define CC_SR_MASK      (0x0f<<CC_SR_BIT)
#define	  CC_SR_5500	0
#define	  CC_SR_8000	(1<<CC_SR_BIT)
#define	  CC_SR_9600	(2<<CC_SR_BIT)
#define	  CC_SR_11025	(3<<CC_SR_BIT)
#define	  CC_SR_16000	(4<<CC_SR_BIT)
#define	  CC_SR_19200	(5<<CC_SR_BIT)
#define	  CC_SR_22050	(6<<CC_SR_BIT)
#define	  CC_SR_32000	(7<<CC_SR_BIT)
#define	  CC_SR_38400	(8<<CC_SR_BIT)
#define	  CC_SR_44100	(9<<CC_SR_BIT)
#define	  CC_SR_48000	(10<<CC_SR_BIT)
#define	CC_CSP	        (1<<7)	/* Channel stop 
				 * 0: End of Current buffer
				 * 1: Immediately stop when rec stop */
#define CC_CP	        (1<<6)	/* Channel pause 0: normal, 1: pause */
#define	CC_CA	        (1<<5)	/* Channel Action 0: Stop , 1: start */
#define	CC_CB2L         (1<<2)	/* Cur. buf. 2 xfr is last 0: No, 1: Yes */
#define CC_CB1L         (1<<1)	/* Cur. buf. 1 xfr is last 0: No, 1: Yes */
#define CC_DE	        1	/* DFC/DFIFO Data Empty 1: empty, 0: not empty
				 * (Playback only)
				 */

/* Codec Control Reg */
#define CODECC_GME	(1<<9)	/* AC97 GPIO Mode enable */
#define	CODECC_ATM	(1<<8)	/* AC97 ATE test mode 0: test 1: normal */
#define	CODECC_WR	(1<<6)	/* AC97 Warn reset 1: warm reset , 0: Normal */
#define	CODECC_CR	(1<<5)	/* AC97 Cold reset 1: Cold reset , 0: Normal */


/* I2S Control Reg	*/
#define	I2SMC_SR_BIT	 6	/* I2S Sampling rate 
				 * 00: 48KHz, 01: 44.1 KHz, 10: 32 32 KHz */
#define	I2SMC_SR_MASK    (0x03<<I2SMC_SR_BIT)
#define	  I2SMC_SR_48000 0
#define	  I2SMC_SR_44100 (1<<I2SMC_SR_BIT)
#define	  I2SMC_SR_32000 (2<<I2SMC_SR_BIT)
#define	I2SMC_SRSS	 (1<<5)	/* Sample Rate Source Select 1:S/W, 0: H/W */
#define I2SMC_I2SF_BIT	 0	/* I2S Format */
#define I2SMC_I2SF_MASK  0x03
#define   I2SMC_I2SF_DAC 0
#define   I2SMC_I2SF_ADC 2
#define   I2SMC_I2SF_I2S 3


/* Volume up, Down, Mute */
#define	VS_VMP	(1<<2)	/* Volume mute 1: pushed, 0: not */
#define	VS_VDP	(1<<1)	/* Volume Down 1: pushed, 0: not */
#define VS_VUP	1	/* Volime Up 1: pushed, 0: not */

/* SRC, Mixer test control/DFC status reg */
#define SRCS_DPUSC      (1<<5)	/* DFC Playback underrun Status/clear */
#define	SRCS_DCOSC	(1<<4)	/* DFC Capture Overrun Status/clear */
#define SRCS_SIS	(1<<3)	/* SRC input select 1: Mixer, 0: Codec I/F */
#define SRCS_CDIS_BIT	0	/* Codec Data Input Select */
#define SRCS_CDIS_MASK  0x07
#define   SRCS_CDIS_MIXER 0
#define   SRCS_CDIS_PCM   1
#define   SRCS_CDIS_I2S   2
#define   SRCS_CDIS_FM    3
#define   SRCS_CDIS_DFC   4


/* Codec Index Reg command Port */
#define CIRCP_CID_BIT   10
#define CIRCP_CID_MASK  (0x03<<CIRCP_CID_BIT)
#define CIRCP_CPS	(1<<9)	/* Command Port Status 0: ready, 1: busy */
#define	CIRCP_DPVF	(1<<8)	/* Data Port Valid Flag 0: invalis, 1: valid */
#define CIRCP_RWC	(1<<7)	/* Read/write command */
#define CIRCP_CIA_BIT   0
#define CIRCP_CIA_MASK  0x007F	/* Codec Index Address */

/* Test Mode Control/Test group Select Control */

/* General Control Reg */
#define GC_VDC_BIT	6	/* Volume Division Control */
#define GC_VDC_MASK     (0x03<<GC_VDC_BIT)
#define   GC_VDC_NONE   0
#define   GC_VDC_DIV2   (1<<GC_VDC_BIT)
#define   GC_VDC_DIV4   (2<<GC_VDC_BIT)
#define	GC_SOE	        (1<<2)	/* S/PDIF Output enable */
#define	GC_SWR	        1	/* Software warn reset */

/* Interrupt mask Control Reg */
#define	IMC_VCIM	(1<<6)	/* Volume CNTL interrupt mask */
#define	IMC_CCIM	(1<<1)	/* Capture Chan. iterrupt mask */
#define	IMC_PCIM	1	/* Playback Chan. interrupt mask */

/* Interrupt status/clear reg */
#define	ISC_VCI	        (1<<6)	/* Volume CNTL interrupt 1: clears */
#define	ISC_CCI	        (1<<1)	/* Capture Chan. interrupt 1: clears  */
#define	ISC_PCI	        1	/* Playback Chan. interrupt 1: clears */

/* misc stuff */
#define POLL_COUNT   0x5000


/* --------------------------------------------------------------------- */

/*
 * Define DIGITAL1 as the I2S channel, since it is not listed in
 * soundcard.h.
 */
#define SOUND_MIXER_I2S        SOUND_MIXER_DIGITAL1
#define SOUND_MASK_I2S         SOUND_MASK_DIGITAL1
#define SOUND_MIXER_READ_I2S   MIXER_READ(SOUND_MIXER_I2S)
#define SOUND_MIXER_WRITE_I2S  MIXER_WRITE(SOUND_MIXER_I2S)

/* --------------------------------------------------------------------- */

struct it8172_state {
	/* list of it8172 devices */
	struct list_head devs;

	/* the corresponding pci_dev structure */
	struct pci_dev *dev;

	/* soundcore stuff */
	int dev_audio;

	/* hardware resources */
	unsigned long io;
	unsigned int irq;

	/* PCI ID's */
	u16 vendor;
	u16 device;
	u8 rev; /* the chip revision */

	/* options */
	int spdif_volume; /* S/PDIF output is enabled if != -1 */
	int i2s_volume;   /* current I2S out volume, in OSS format */
	int i2s_recording;/* 1 = recording from I2S, 0 = not */
    
#ifdef IT8172_DEBUG
	/* debug /proc entry */
	struct proc_dir_entry *ps;
	struct proc_dir_entry *ac97_ps;
#endif /* IT8172_DEBUG */

	struct ac97_codec codec;

	unsigned short pcc, capcc;
	unsigned dacrate, adcrate;

	spinlock_t lock;
	struct mutex open_mutex;
	mode_t open_mode;
	wait_queue_head_t open_wait;

	struct dmabuf {
		void *rawbuf;
		dma_addr_t dmaaddr;
		unsigned buforder;
		unsigned numfrag;
		unsigned fragshift;
		void* nextIn;
		void* nextOut;
		int count;
		int curBufPtr;
		unsigned total_bytes;
		unsigned error; /* over/underrun */
		wait_queue_head_t wait;
		/* redundant, but makes calculations easier */
		unsigned fragsize;
		unsigned dmasize;
		unsigned fragsamples;
		/* OSS stuff */
		unsigned mapped:1;
		unsigned ready:1;
		unsigned stopped:1;
		unsigned ossfragshift;
		int ossmaxfrags;
		unsigned subdivision;
	} dma_dac, dma_adc;
};

/* --------------------------------------------------------------------- */

static LIST_HEAD(devs);

/* --------------------------------------------------------------------- */

static inline unsigned ld2(unsigned int x)
{
	unsigned r = 0;
	
	if (x >= 0x10000) {
		x >>= 16;
		r += 16;
	}
	if (x >= 0x100) {
		x >>= 8;
		r += 8;
	}
	if (x >= 0x10) {
		x >>= 4;
		r += 4;
	}
	if (x >= 4) {
		x >>= 2;
		r += 2;
	}
	if (x >= 2)
		r++;
	return r;
}

/* --------------------------------------------------------------------- */

static void it8172_delay(int msec)
{
	unsigned long tmo;
	signed long tmo2;

	if (in_interrupt())
		return;
    
	tmo = jiffies + (msec*HZ)/1000;
	for (;;) {
		tmo2 = tmo - jiffies;
		if (tmo2 <= 0)
			break;
		schedule_timeout(tmo2);
	}
}


static unsigned short
get_compat_rate(unsigned* rate)
{
	unsigned rate_out = *rate;
	unsigned short sr;
    
	if (rate_out >= 46050) {
		sr = CC_SR_48000; rate_out = 48000;
	} else if (rate_out >= 41250) {
		sr = CC_SR_44100; rate_out = 44100;
	} else if (rate_out >= 35200) {
		sr = CC_SR_38400; rate_out = 38400;
	} else if (rate_out >= 27025) {
		sr = CC_SR_32000; rate_out = 32000;
	} else if (rate_out >= 20625) {
		sr = CC_SR_22050; rate_out = 22050;
	} else if (rate_out >= 17600) {
		sr = CC_SR_19200; rate_out = 19200;
	} else if (rate_out >= 13513) {
		sr = CC_SR_16000; rate_out = 16000;
	} else if (rate_out >= 10313) {
		sr = CC_SR_11025; rate_out = 11025;
	} else if (rate_out >= 8800) {
		sr = CC_SR_9600; rate_out = 9600;
	} else if (rate_out >= 6750) {
		sr = CC_SR_8000; rate_out = 8000;
	} else {
		sr = CC_SR_5500; rate_out = 5500;
	}

	*rate = rate_out;
	return sr;
}

static void set_adc_rate(struct it8172_state *s, unsigned rate)
{
	unsigned long flags;
	unsigned short sr;
    
	sr = get_compat_rate(&rate);

	spin_lock_irqsave(&s->lock, flags);
	s->capcc &= ~CC_SR_MASK;
	s->capcc |= sr;
	outw(s->capcc, s->io+IT_AC_CAPCC);
	spin_unlock_irqrestore(&s->lock, flags);

	s->adcrate = rate;
}


static void set_dac_rate(struct it8172_state *s, unsigned rate)
{
	unsigned long flags;
	unsigned short sr;
    
	sr = get_compat_rate(&rate);

	spin_lock_irqsave(&s->lock, flags);
	s->pcc &= ~CC_SR_MASK;
	s->pcc |= sr;
	outw(s->pcc, s->io+IT_AC_PCC);
	spin_unlock_irqrestore(&s->lock, flags);

	s->dacrate = rate;
}


/* --------------------------------------------------------------------- */

static u16 rdcodec(struct ac97_codec *codec, u8 addr)
{
	struct it8172_state *s = (struct it8172_state *)codec->private_data;
	unsigned long flags;
	unsigned short circp, data;
	int i;
    
	spin_lock_irqsave(&s->lock, flags);

	for (i = 0; i < POLL_COUNT; i++)
		if (!(inw(s->io+IT_AC_CIRCP) & CIRCP_CPS))
			break;
	if (i == POLL_COUNT)
		err("rdcodec: codec ready poll expired!");

	circp = addr & CIRCP_CIA_MASK;
	circp |= (codec->id << CIRCP_CID_BIT);
	circp |= CIRCP_RWC; // read command
	outw(circp, s->io+IT_AC_CIRCP);

	/* now wait for the data */
	for (i = 0; i < POLL_COUNT; i++)
		if (inw(s->io+IT_AC_CIRCP) & CIRCP_DPVF)
			break;
	if (i == POLL_COUNT)
		err("rdcodec: read poll expired!");

	data = inw(s->io+IT_AC_CIRDP);
	spin_unlock_irqrestore(&s->lock, flags);

	return data;
}


static void wrcodec(struct ac97_codec *codec, u8 addr, u16 data)
{
	struct it8172_state *s = (struct it8172_state *)codec->private_data;
	unsigned long flags;
	unsigned short circp;
	int i;
    
	spin_lock_irqsave(&s->lock, flags);

	for (i = 0; i < POLL_COUNT; i++)
		if (!(inw(s->io+IT_AC_CIRCP) & CIRCP_CPS))
			break;
	if (i == POLL_COUNT)
		err("wrcodec: codec ready poll expired!");

	circp = addr & CIRCP_CIA_MASK;
	circp |= (codec->id << CIRCP_CID_BIT);
	circp &= ~CIRCP_RWC; // write command

	outw(data,  s->io+IT_AC_CIRDP);  // send data first
	outw(circp, s->io+IT_AC_CIRCP);

	spin_unlock_irqrestore(&s->lock, flags);
}


static void waitcodec(struct ac97_codec *codec)
{
	unsigned short temp;

	/* codec_wait is used to wait for a ready state after
	   an AC97_RESET. */
	it8172_delay(10);

	temp = rdcodec(codec, 0x26);

	// If power down, power up
	if (temp & 0x3f00) {
		// Power on
		wrcodec(codec, 0x26, 0);
		it8172_delay(100);
		// Reread
		temp = rdcodec(codec, 0x26);
	}
    
	// Check if Codec REF,ANL,DAC,ADC ready***/
	if ((temp & 0x3f0f) != 0x000f) {
		err("codec reg 26 status (0x%x) not ready!!", temp);
		return;
	}
}


/* --------------------------------------------------------------------- */

static inline void stop_adc(struct it8172_state *s)
{
	struct dmabuf* db = &s->dma_adc;
	unsigned long flags;
	unsigned char imc;
    
	if (db->stopped)
		return;

	spin_lock_irqsave(&s->lock, flags);

	s->capcc &= ~(CC_CA | CC_CP | CC_CB2L | CC_CB1L);
	s->capcc |= CC_CSP;
	outw(s->capcc, s->io+IT_AC_CAPCC);
    
	// disable capture interrupt
	imc = inb(s->io+IT_AC_IMC);
	outb(imc | IMC_CCIM, s->io+IT_AC_IMC);

	db->stopped = 1;

	spin_unlock_irqrestore(&s->lock, flags);
}	

static inline void stop_dac(struct it8172_state *s)
{
	struct dmabuf* db = &s->dma_dac;
	unsigned long flags;
	unsigned char imc;
    
	if (db->stopped)
		return;

	spin_lock_irqsave(&s->lock, flags);

	s->pcc &= ~(CC_CA | CC_CP | CC_CB2L | CC_CB1L);
	s->pcc |= CC_CSP;
	outw(s->pcc, s->io+IT_AC_PCC);
    
	// disable playback interrupt
	imc = inb(s->io+IT_AC_IMC);
	outb(imc | IMC_PCIM, s->io+IT_AC_IMC);

	db->stopped = 1;
    
	spin_unlock_irqrestore(&s->lock, flags);
}	

static void start_dac(struct it8172_state *s)
{
	struct dmabuf* db = &s->dma_dac;
	unsigned long flags;
	unsigned char imc;
	unsigned long buf1, buf2;
    
	if (!db->stopped)
		return;
    
	spin_lock_irqsave(&s->lock, flags);

	// reset Buffer 1 and 2 pointers to nextOut and nextOut+fragsize
	buf1 = virt_to_bus(db->nextOut);
	buf2 = buf1 + db->fragsize;
	if (buf2 >= db->dmaaddr + db->dmasize)
		buf2 -= db->dmasize;
    
	outl(buf1, s->io+IT_AC_PCB1STA);
	outl(buf2, s->io+IT_AC_PCB2STA);
	db->curBufPtr = IT_AC_PCB1STA;
    
	// enable playback interrupt
	imc = inb(s->io+IT_AC_IMC);
	outb(imc & ~IMC_PCIM, s->io+IT_AC_IMC);

	s->pcc &= ~(CC_CSP | CC_CP | CC_CB2L | CC_CB1L);
	s->pcc |= CC_CA;
	outw(s->pcc, s->io+IT_AC_PCC);
    
	db->stopped = 0;

	spin_unlock_irqrestore(&s->lock, flags);
}	

static void start_adc(struct it8172_state *s)
{
	struct dmabuf* db = &s->dma_adc;
	unsigned long flags;
	unsigned char imc;
	unsigned long buf1, buf2;
    
	if (!db->stopped)
		return;

	spin_lock_irqsave(&s->lock, flags);

	// reset Buffer 1 and 2 pointers to nextIn and nextIn+fragsize
	buf1 = virt_to_bus(db->nextIn);
	buf2 = buf1 + db->fragsize;
	if (buf2 >= db->dmaaddr + db->dmasize)
		buf2 -= db->dmasize;
    
	outl(buf1, s->io+IT_AC_CAPB1STA);
	outl(buf2, s->io+IT_AC_CAPB2STA);
	db->curBufPtr = IT_AC_CAPB1STA;

	// enable capture interrupt
	imc = inb(s->io+IT_AC_IMC);
	outb(imc & ~IMC_CCIM, s->io+IT_AC_IMC);

	s->capcc &= ~(CC_CSP | CC_CP | CC_CB2L | CC_CB1L);
	s->capcc |= CC_CA;
	outw(s->capcc, s->io+IT_AC_CAPCC);
    
	db->stopped = 0;

	spin_unlock_irqrestore(&s->lock, flags);
}	

/* --------------------------------------------------------------------- */

#define DMABUF_DEFAULTORDER (17-PAGE_SHIFT)
#define DMABUF_MINORDER 1

static inline void dealloc_dmabuf(struct it8172_state *s, struct dmabuf *db)
{
	struct page *page, *pend;

	if (db->rawbuf) {
		/* undo marking the pages as reserved */
		pend = virt_to_page(db->rawbuf +
				    (PAGE_SIZE << db->buforder) - 1);
		for (page = virt_to_page(db->rawbuf); page <= pend; page++)
			ClearPageReserved(page);
		pci_free_consistent(s->dev, PAGE_SIZE << db->buforder,
				    db->rawbuf, db->dmaaddr);
	}
	db->rawbuf = db->nextIn = db->nextOut = NULL;
	db->mapped = db->ready = 0;
}

static int prog_dmabuf(struct it8172_state *s, struct dmabuf *db,
		       unsigned rate, unsigned fmt, unsigned reg)
{
	int order;
	unsigned bytepersec;
	unsigned bufs;
	struct page *page, *pend;

	if (!db->rawbuf) {
		db->ready = db->mapped = 0;
		for (order = DMABUF_DEFAULTORDER;
		     order >= DMABUF_MINORDER; order--)
			if ((db->rawbuf =
			     pci_alloc_consistent(s->dev,
						  PAGE_SIZE << order,
						  &db->dmaaddr)))
				break;
		if (!db->rawbuf)
			return -ENOMEM;
		db->buforder = order;
		/* now mark the pages as reserved;
		   otherwise remap_pfn_range doesn't do what we want */
		pend = virt_to_page(db->rawbuf +
				    (PAGE_SIZE << db->buforder) - 1);
		for (page = virt_to_page(db->rawbuf); page <= pend; page++)
			SetPageReserved(page);
	}

	db->count = 0;
	db->nextIn = db->nextOut = db->rawbuf;
    
	bytepersec = rate << sample_shift[fmt];
	bufs = PAGE_SIZE << db->buforder;
	if (db->ossfragshift) {
		if ((1000 << db->ossfragshift) < bytepersec)
			db->fragshift = ld2(bytepersec/1000);
		else
			db->fragshift = db->ossfragshift;
	} else {
		db->fragshift = ld2(bytepersec/100/(db->subdivision ?
						    db->subdivision : 1));
		if (db->fragshift < 3)
			db->fragshift = 3;
	}
	db->numfrag = bufs >> db->fragshift;
	while (db->numfrag < 4 && db->fragshift > 3) {
		db->fragshift--;
		db->numfrag = bufs >> db->fragshift;
	}
	db->fragsize = 1 << db->fragshift;
	if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
		db->numfrag = db->ossmaxfrags;
	db->fragsamples = db->fragsize >> sample_shift[fmt];
	db->dmasize = db->numfrag << db->fragshift;
	memset(db->rawbuf, (fmt & (CC_DF>>CC_FMT_BIT)) ? 0 : 0x80, bufs);
    
#ifdef IT8172_VERBOSE_DEBUG
	dbg("rate=%d, fragsize=%d, numfrag=%d, dmasize=%d",
	    rate, db->fragsize, db->numfrag, db->dmasize);
#endif

	// set data length register
	outw(db->fragsize, s->io+reg+2);
	db->ready = 1;

	return 0;
}

static inline int prog_dmabuf_adc(struct it8172_state *s)
{
	stop_adc(s);
	return prog_dmabuf(s, &s->dma_adc, s->adcrate,
			   (s->capcc & CC_FMT_MASK) >> CC_FMT_BIT,
			   IT_AC_CAPCC);
}

static inline int prog_dmabuf_dac(struct it8172_state *s)
{
	stop_dac(s);
	return prog_dmabuf(s, &s->dma_dac, s->dacrate,
			   (s->pcc & CC_FMT_MASK) >> CC_FMT_BIT,
			   IT_AC_PCC);
}


/* hold spinlock for the following! */

static irqreturn_t it8172_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct it8172_state *s = (struct it8172_state *)dev_id;
	struct dmabuf* dac = &s->dma_dac;
	struct dmabuf* adc = &s->dma_adc;
	unsigned char isc, vs;
	unsigned short vol, mute;
	unsigned long newptr;
    
	spin_lock(&s->lock);

	isc = inb(s->io+IT_AC_ISC);

	/* fastpath out, to ease interrupt sharing */
	if (!(isc & (ISC_VCI | ISC_CCI | ISC_PCI))) {
		spin_unlock(&s->lock);
		return IRQ_NONE;
	}
    
	/* clear audio interrupts first */
	outb(isc | ISC_VCI | ISC_CCI | ISC_PCI, s->io+IT_AC_ISC);
    
	/* handle volume button events (ignore if S/PDIF enabled) */
	if ((isc & ISC_VCI) && s->spdif_volume == -1) {
		vs = inb(s->io+IT_AC_VS);
		outb(0, s->io+IT_AC_VS);
		vol = inw(s->io+IT_AC_PCMOV);
		mute = vol & PCMOV_PCMOM;
		vol &= PCMOV_PCMLCG_MASK;
		if ((vs & VS_VUP) && vol > 0)
			vol--;
		if ((vs & VS_VDP) && vol < 0x1f)
			vol++;
		vol |= (vol << PCMOV_PCMRCG_BIT);
		if (vs & VS_VMP)
			vol |= (mute ^ PCMOV_PCMOM);
		outw(vol, s->io+IT_AC_PCMOV);
	}
    
	/* update capture pointers */
	if (isc & ISC_CCI) {
		if (adc->count > adc->dmasize - adc->fragsize) {
			// Overrun. Stop ADC and log the error
			stop_adc(s);
			adc->error++;
			dbg("adc overrun");
		} else {
			newptr = virt_to_bus(adc->nextIn) + 2*adc->fragsize;
			if (newptr >= adc->dmaaddr + adc->dmasize)
				newptr -= adc->dmasize;
	    
			outl(newptr, s->io+adc->curBufPtr);
			adc->curBufPtr = (adc->curBufPtr == IT_AC_CAPB1STA) ?
				IT_AC_CAPB2STA : IT_AC_CAPB1STA;
	    
			adc->nextIn += adc->fragsize;
			if (adc->nextIn >= adc->rawbuf + adc->dmasize)
				adc->nextIn -= adc->dmasize;
	    
			adc->count += adc->fragsize;
			adc->total_bytes += adc->fragsize;

			/* wake up anybody listening */
			if (waitqueue_active(&adc->wait))
				wake_up_interruptible(&adc->wait);
		}
	}
    
	/* update playback pointers */
	if (isc & ISC_PCI) {
		newptr = virt_to_bus(dac->nextOut) + 2*dac->fragsize;
		if (newptr >= dac->dmaaddr + dac->dmasize)
			newptr -= dac->dmasize;
	
		outl(newptr, s->io+dac->curBufPtr);
		dac->curBufPtr = (dac->curBufPtr == IT_AC_PCB1STA) ?
			IT_AC_PCB2STA : IT_AC_PCB1STA;
	
		dac->nextOut += dac->fragsize;
		if (dac->nextOut >= dac->rawbuf + dac->dmasize)
			dac->nextOut -= dac->dmasize;
	
		dac->count -= dac->fragsize;
		dac->total_bytes += dac->fragsize;

		/* wake up anybody listening */
		if (waitqueue_active(&dac->wait))
			wake_up_interruptible(&dac->wait);
	
		if (dac->count <= 0)
			stop_dac(s);
	}
    
	spin_unlock(&s->lock);
	return IRQ_HANDLED;
}

/* --------------------------------------------------------------------- */

static int it8172_open_mixdev(struct inode *inode, struct file *file)
{
	int minor = iminor(inode);
	struct list_head *list;
	struct it8172_state *s;

	for (list = devs.next; ; list = list->next) {
		if (list == &devs)
			return -ENODEV;
		s = list_entry(list, struct it8172_state, devs);
		if (s->codec.dev_mixer == minor)
			break;
	}
	file->private_data = s;
	return nonseekable_open(inode, file);
}

static int it8172_release_mixdev(struct inode *inode, struct file *file)
{
	return 0;
}


static u16
cvt_ossvol(unsigned int gain)
{
	u16 ret;
    
	if (gain == 0)
		return 0;
    
	if (gain > 100)
		gain = 100;
    
	ret = (100 - gain + 32) / 4;
	ret = ret > 31 ? 31 : ret;
	return ret;
}


static int mixdev_ioctl(struct ac97_codec *codec, unsigned int cmd,
			unsigned long arg)
{
	struct it8172_state *s = (struct it8172_state *)codec->private_data;
	unsigned int left, right;
	unsigned long flags;
	int val;
	u16 vol;
    
	/*
	 * When we are in S/PDIF mode, we want to disable any analog output so
	 * we filter the master/PCM channel volume ioctls.
	 *
	 * Also filter I2S channel, which AC'97 knows nothing about.
	 */

	switch (cmd) {
	case SOUND_MIXER_WRITE_VOLUME:
		// if not in S/PDIF mode, pass to AC'97
		if (s->spdif_volume == -1)
			break;
		return 0;
	case SOUND_MIXER_WRITE_PCM:
		// if not in S/PDIF mode, pass to AC'97
		if (s->spdif_volume == -1)
			break;
		if (get_user(val, (int *)arg))
			return -EFAULT;
		right = ((val >> 8)  & 0xff);
		left = (val  & 0xff);
		if (right > 100)
			right = 100;
		if (left > 100)
			left = 100;
		s->spdif_volume = (right << 8) | left;
		vol = cvt_ossvol(left);
		vol |= (cvt_ossvol(right) << PCMOV_PCMRCG_BIT);
		if (vol == 0)
			vol = PCMOV_PCMOM; // mute
		spin_lock_irqsave(&s->lock, flags);
		outw(vol, s->io+IT_AC_PCMOV);
		spin_unlock_irqrestore(&s->lock, flags);
		return put_user(s->spdif_volume, (int *)arg);
	case SOUND_MIXER_READ_PCM:
		// if not in S/PDIF mode, pass to AC'97
		if (s->spdif_volume == -1)
			break;
		return put_user(s->spdif_volume, (int *)arg);
	case SOUND_MIXER_WRITE_I2S:
		if (get_user(val, (int *)arg))
			return -EFAULT;
		right = ((val >> 8)  & 0xff);
		left = (val  & 0xff);
		if (right > 100)
			right = 100;
		if (left > 100)
			left = 100;
		s->i2s_volume = (right << 8) | left;
		vol = cvt_ossvol(left);
		vol |= (cvt_ossvol(right) << I2SV_I2SRCG_BIT);
		if (vol == 0)
			vol = I2SV_I2SOM; // mute
		outw(vol, s->io+IT_AC_I2SV);
		return put_user(s->i2s_volume, (int *)arg);
	case SOUND_MIXER_READ_I2S:
		return put_user(s->i2s_volume, (int *)arg);
	case SOUND_MIXER_WRITE_RECSRC:
		if (get_user(val, (int *)arg))
			return -EFAULT;
		if (val & SOUND_MASK_I2S) {
			s->i2s_recording = 1;
			outb(DRSS_I2S, s->io+IT_AC_DRSS);
			return 0;
		} else {
			s->i2s_recording = 0;
			outb(DRSS_AC97_PRIM, s->io+IT_AC_DRSS);
			// now let AC'97 select record source
			break;
		}
	case SOUND_MIXER_READ_RECSRC:
		if (s->i2s_recording)
			return put_user(SOUND_MASK_I2S, (int *)arg);
		else
			// let AC'97 report recording source
			break;
	}

	return codec->mixer_ioctl(codec, cmd, arg);
}

static int it8172_ioctl_mixdev(struct inode *inode, struct file *file,
			       unsigned int cmd, unsigned long arg)
{
	struct it8172_state *s = (struct it8172_state *)file->private_data;
	struct ac97_codec *codec = &s->codec;

	return mixdev_ioctl(codec, cmd, arg);
}

static /*const*/ struct file_operations it8172_mixer_fops = {
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.ioctl		= it8172_ioctl_mixdev,
	.open		= it8172_open_mixdev,
	.release	= it8172_release_mixdev,
};

/* --------------------------------------------------------------------- */

static int drain_dac(struct it8172_state *s, int nonblock)
{
	unsigned long flags;
	int count, tmo;
	
	if (s->dma_dac.mapped || !s->dma_dac.ready || s->dma_dac.stopped)
		return 0;

	for (;;) {
		spin_lock_irqsave(&s->lock, flags);
		count = s->dma_dac.count;
		spin_unlock_irqrestore(&s->lock, flags);
		if (count <= 0)
			break;
		if (signal_pending(current))
			break;
		//if (nonblock)
		//return -EBUSY;
		tmo = 1000 * count / s->dacrate;
		tmo >>= sample_shift[(s->pcc & CC_FMT_MASK) >> CC_FMT_BIT];
		it8172_delay(tmo);
	}
	if (signal_pending(current))
		return -ERESTARTSYS;
	return 0;
}

/* --------------------------------------------------------------------- */


/*
 * Copy audio data to/from user buffer from/to dma buffer, taking care
 * that we wrap when reading/writing the dma buffer. Returns actual byte
 * count written to or read from the dma buffer.
 */
static int copy_dmabuf_user(struct dmabuf *db, char* userbuf,
			    int count, int to_user)
{
	char* bufptr = to_user ? db->nextOut : db->nextIn;
	char* bufend = db->rawbuf + db->dmasize;
	
	if (bufptr + count > bufend) {
		int partial = (int)(bufend - bufptr);
		if (to_user) {
			if (copy_to_user(userbuf, bufptr, partial))
				return -EFAULT;
			if (copy_to_user(userbuf + partial, db->rawbuf,
					 count - partial))
				return -EFAULT;
		} else {
			if (copy_from_user(bufptr, userbuf, partial))
				return -EFAULT;
			if (copy_from_user(db->rawbuf,
					   userbuf + partial,
					   count - partial))
				return -EFAULT;
		}
	} else {
		if (to_user) {
			if (copy_to_user(userbuf, bufptr, count))
				return -EFAULT;
		} else {
			if (copy_from_user(bufptr, userbuf, count))
				return -EFAULT;
		}
	}
	
	return count;
}


static ssize_t it8172_read(struct file *file, char *buffer,
			   size_t count, loff_t *ppos)
{
	struct it8172_state *s = (struct it8172_state *)file->private_data;
	struct dmabuf *db = &s->dma_adc;
	ssize_t ret;
	unsigned long flags;
	int cnt, remainder, avail;

	if (db->mapped)
		return -ENXIO;
	if (!access_ok(VERIFY_WRITE, buffer, count))
		return -EFAULT;
	ret = 0;

	while (count > 0) {
		// wait for samples in capture buffer
		do {
			spin_lock_irqsave(&s->lock, flags);
			if (db->stopped)
				start_adc(s);
			avail = db->count;
			spin_unlock_irqrestore(&s->lock, flags);
			if (avail <= 0) {
				if (file->f_flags & O_NONBLOCK) {
					if (!ret)
						ret = -EAGAIN;
					return ret;
				}
				interruptible_sleep_on(&db->wait);
				if (signal_pending(current)) {
					if (!ret)
						ret = -ERESTARTSYS;
					return ret;
				}
			}
		} while (avail <= 0);

		// copy from nextOut to user
		if ((cnt = copy_dmabuf_user(db, buffer, count > avail ?
					    avail : count, 1)) < 0) {
			if (!ret)
				ret = -EFAULT;
			return ret;
		}

		spin_lock_irqsave(&s->lock, flags);
		db->count -= cnt;
		spin_unlock_irqrestore(&s->lock, flags);

		db->nextOut += cnt;
		if (db->nextOut >= db->rawbuf + db->dmasize)
			db->nextOut -= db->dmasize;	

		count -= cnt;
		buffer += cnt;
		ret += cnt;
	} // while (count > 0)

	/*
	 * See if the dma buffer count after this read call is
	 * aligned on a fragsize boundary. If not, read from
	 * buffer until we reach a boundary, and let's hope this
	 * is just the last remainder of an audio record. If not
	 * it means the user is not reading in fragsize chunks, in
	 * which case it's his/her fault that there are audio gaps
	 * in their record.
	 */
	spin_lock_irqsave(&s->lock, flags);
	remainder = db->count % db->fragsize;
	if (remainder) {
		db->nextOut += remainder;
		if (db->nextOut >= db->rawbuf + db->dmasize)
			db->nextOut -= db->dmasize;
		db->count -= remainder;
	}
	spin_unlock_irqrestore(&s->lock, flags);

	return ret;
}

static ssize_t it8172_write(struct file *file, const char *buffer,
			    size_t count, loff_t *ppos)
{
	struct it8172_state *s = (struct it8172_state *)file->private_data;
	struct dmabuf *db = &s->dma_dac;
	ssize_t ret;
	unsigned long flags;
	int cnt, remainder, avail;

	if (db->mapped)
		return -ENXIO;
	if (!access_ok(VERIFY_READ, buffer, count))
		return -EFAULT;
	ret = 0;
    
	while (count > 0) {
		// wait for space in playback buffer
		do {
			spin_lock_irqsave(&s->lock, flags);
			avail = db->dmasize - db->count;
			spin_unlock_irqrestore(&s->lock, flags);
			if (avail <= 0) {
				if (file->f_flags & O_NONBLOCK) {
					if (!ret)
						ret = -EAGAIN;
					return ret;
				}
				interruptible_sleep_on(&db->wait);
				if (signal_pending(current)) {
					if (!ret)
						ret = -ERESTARTSYS;
					return ret;
				}
			}
		} while (avail <= 0);
	
		// copy to nextIn
		if ((cnt = copy_dmabuf_user(db, (char*)buffer,
					    count > avail ?
					    avail : count, 0)) < 0) {
			if (!ret)
				ret = -EFAULT;
			return ret;
		}

		spin_lock_irqsave(&s->lock, flags);
		db->count += cnt;
		if (db->stopped)
			start_dac(s);
		spin_unlock_irqrestore(&s->lock, flags);
	
		db->nextIn += cnt;
		if (db->nextIn >= db->rawbuf + db->dmasize)
			db->nextIn -= db->dmasize;
	
		count -= cnt;
		buffer += cnt;
		ret += cnt;
	} // while (count > 0)
	
	/*
	 * See if the dma buffer count after this write call is
	 * aligned on a fragsize boundary. If not, fill buffer
	 * with silence to the next boundary, and let's hope this
	 * is just the last remainder of an audio playback. If not
	 * it means the user is not sending us fragsize chunks, in
	 * which case it's his/her fault that there are audio gaps
	 * in their playback.
	 */
	spin_lock_irqsave(&s->lock, flags);
	remainder = db->count % db->fragsize;
	if (remainder) {
		int fill_cnt = db->fragsize - remainder;
		memset(db->nextIn, 0, fill_cnt);
		db->nextIn += fill_cnt;
		if (db->nextIn >= db->rawbuf + db->dmasize)
			db->nextIn -= db->dmasize;
		db->count += fill_cnt;
	}
	spin_unlock_irqrestore(&s->lock, flags);

	return ret;
}

/* No kernel lock - we have our own spinlock */
static unsigned int it8172_poll(struct file *file,
				struct poll_table_struct *wait)
{
	struct it8172_state *s = (struct it8172_state *)file->private_data;
	unsigned long flags;
	unsigned int mask = 0;

	if (file->f_mode & FMODE_WRITE) {
		if (!s->dma_dac.ready)
			return 0;
		poll_wait(file, &s->dma_dac.wait, wait);
	}
	if (file->f_mode & FMODE_READ) {
		if (!s->dma_adc.ready)
			return 0;
		poll_wait(file, &s->dma_adc.wait, wait);
	}
	
	spin_lock_irqsave(&s->lock, flags);
	if (file->f_mode & FMODE_READ) {
		if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
			mask |= POLLIN | POLLRDNORM;
	}
	if (file->f_mode & FMODE_WRITE) {
		if (s->dma_dac.mapped) {
			if (s->dma_dac.count >= (signed)s->dma_dac.fragsize) 
				mask |= POLLOUT | POLLWRNORM;
		} else {
			if ((signed)s->dma_dac.dmasize >=
			    s->dma_dac.count + (signed)s->dma_dac.fragsize)
				mask |= POLLOUT | POLLWRNORM;
		}
	}
	spin_unlock_irqrestore(&s->lock, flags);
	return mask;
}

static int it8172_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct it8172_state *s = (struct it8172_state *)file->private_data;
	struct dmabuf *db;
	unsigned long size;

	lock_kernel();
	if (vma->vm_flags & VM_WRITE)
		db = &s->dma_dac;
	else if (vma->vm_flags & VM_READ)
		db = &s->dma_adc;
	else {
		unlock_kernel();
		return -EINVAL;
	}
	if (vma->vm_pgoff != 0) {
		unlock_kernel();
		return -EINVAL;
	}
	size = vma->vm_end - vma->vm_start;
	if (size > (PAGE_SIZE << db->buforder)) {
		unlock_kernel();
		return -EINVAL;
	}
	if (remap_pfn_range(vma, vma->vm_start,
			     virt_to_phys(db->rawbuf) >> PAGE_SHIFT,
			     size, vma->vm_page_prot)) {
		unlock_kernel();
		return -EAGAIN;
	}
	db->mapped = 1;
	unlock_kernel();
	return 0;
}


#ifdef IT8172_VERBOSE_DEBUG
static struct ioctl_str_t {
	unsigned int cmd;
	const char* str;
} ioctl_str[] = {
	{SNDCTL_DSP_RESET, "SNDCTL_DSP_RESET"},
	{SNDCTL_DSP_SYNC, "SNDCTL_DSP_SYNC"},
	{SNDCTL_DSP_SPEED, "SNDCTL_DSP_SPEED"},
	{SNDCTL_DSP_STEREO, "SNDCTL_DSP_STEREO"},
	{SNDCTL_DSP_GETBLKSIZE, "SNDCTL_DSP_GETBLKSIZE"},
	{SNDCTL_DSP_SAMPLESIZE, "SNDCTL_DSP_SAMPLESIZE"},
	{SNDCTL_DSP_CHANNELS, "SNDCTL_DSP_CHANNELS"},
	{SOUND_PCM_WRITE_CHANNELS, "SOUND_PCM_WRITE_CHANNELS"},
	{SOUND_PCM_WRITE_FILTER, "SOUND_PCM_WRITE_FILTER"},
	{SNDCTL_DSP_POST, "SNDCTL_DSP_POST"},
	{SNDCTL_DSP_SUBDIVIDE, "SNDCTL_DSP_SUBDIVIDE"},
	{SNDCTL_DSP_SETFRAGMENT, "SNDCTL_DSP_SETFRAGMENT"},
	{SNDCTL_DSP_GETFMTS, "SNDCTL_DSP_GETFMTS"},
	{SNDCTL_DSP_SETFMT, "SNDCTL_DSP_SETFMT"},
	{SNDCTL_DSP_GETOSPACE, "SNDCTL_DSP_GETOSPACE"},
	{SNDCTL_DSP_GETISPACE, "SNDCTL_DSP_GETISPACE"},
	{SNDCTL_DSP_NONBLOCK, "SNDCTL_DSP_NONBLOCK"},
	{SNDCTL_DSP_GETCAPS, "SNDCTL_DSP_GETCAPS"},
	{SNDCTL_DSP_GETTRIGGER, "SNDCTL_DSP_GETTRIGGER"},
	{SNDCTL_DSP_SETTRIGGER, "SNDCTL_DSP_SETTRIGGER"},
	{SNDCTL_DSP_GETIPTR, "SNDCTL_DSP_GETIPTR"},
	{SNDCTL_DSP_GETOPTR, "SNDCTL_DSP_GETOPTR"},
	{SNDCTL_DSP_MAPINBUF, "SNDCTL_DSP_MAPINBUF"},
	{SNDCTL_DSP_MAPOUTBUF, "SNDCTL_DSP_MAPOUTBUF"},
	{SNDCTL_DSP_SETSYNCRO, "SNDCTL_DSP_SETSYNCRO"},
	{SNDCTL_DSP_SETDUPLEX, "SNDCTL_DSP_SETDUPLEX"},
	{SNDCTL_DSP_GETODELAY, "SNDCTL_DSP_GETODELAY"},
	{SNDCTL_DSP_GETCHANNELMASK, "SNDCTL_DSP_GETCHANNELMASK"},
	{SNDCTL_DSP_BIND_CHANNEL, "SNDCTL_DSP_BIND_CHANNEL"},
	{OSS_GETVERSION, "OSS_GETVERSION"},
	{SOUND_PCM_READ_RATE, "SOUND_PCM_READ_RATE"},
	{SOUND_PCM_READ_CHANNELS, "SOUND_PCM_READ_CHANNELS"},
	{SOUND_PCM_READ_BITS, "SOUND_PCM_READ_BITS"},
	{SOUND_PCM_READ_FILTER, "SOUND_PCM_READ_FILTER"}
};
#endif    

static int it8172_ioctl(struct inode *inode, struct file *file,
			unsigned int cmd, unsigned long arg)
{
	struct it8172_state *s = (struct it8172_state *)file->private_data;
	unsigned long flags;
	audio_buf_info abinfo;
	count_info cinfo;
	int count;
	int val, mapped, ret, diff;

	mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
		((file->f_mode & FMODE_READ) && s->dma_adc.mapped);

#ifdef IT8172_VERBOSE_DEBUG
	for (count=0; count<sizeof(ioctl_str)/sizeof(ioctl_str[0]); count++) {
		if (ioctl_str[count].cmd == cmd)
			break;
	}
	if (count < sizeof(ioctl_str)/sizeof(ioctl_str[0]))
		dbg("ioctl %s, arg=0x%08x",
		    ioctl_str[count].str, (unsigned int)arg);
	else
		dbg("ioctl unknown, 0x%x", cmd);
#endif
    
	switch (cmd) {
	case OSS_GETVERSION:
		return put_user(SOUND_VERSION, (int *)arg);

	case SNDCTL_DSP_SYNC:
		if (file->f_mode & FMODE_WRITE)
			return drain_dac(s, file->f_flags & O_NONBLOCK);
		return 0;
		
	case SNDCTL_DSP_SETDUPLEX:
		return 0;

	case SNDCTL_DSP_GETCAPS:
		return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME |
				DSP_CAP_TRIGGER | DSP_CAP_MMAP, (int *)arg);
		
	case SNDCTL_DSP_RESET:
		if (file->f_mode & FMODE_WRITE) {
			stop_dac(s);
			synchronize_irq(s->irq);
			s->dma_dac.count = s->dma_dac.total_bytes = 0;
			s->dma_dac.nextIn = s->dma_dac.nextOut =
				s->dma_dac.rawbuf;
		}
		if (file->f_mode & FMODE_READ) {
			stop_adc(s);
			synchronize_irq(s->irq);
			s->dma_adc.count = s->dma_adc.total_bytes = 0;
			s->dma_adc.nextIn = s->dma_adc.nextOut =
				s->dma_adc.rawbuf;
		}
		return 0;

	case SNDCTL_DSP_SPEED:
		if (get_user(val, (int *)arg))
			return -EFAULT;
		if (val >= 0) {
			if (file->f_mode & FMODE_READ) {
				stop_adc(s);
				set_adc_rate(s, val);
				if ((ret = prog_dmabuf_adc(s)))
					return ret;
			}
			if (file->f_mode & FMODE_WRITE) {
				stop_dac(s);
				set_dac_rate(s, val);
				if ((ret = prog_dmabuf_dac(s)))
					return ret;
			}
		}
		return put_user((file->f_mode & FMODE_READ) ?
				s->adcrate : s->dacrate, (int *)arg);

	case SNDCTL_DSP_STEREO:
		if (get_user(val, (int *)arg))
			return -EFAULT;
		if (file->f_mode & FMODE_READ) {
			stop_adc(s);
			if (val)
				s->capcc |= CC_SM;
			else
				s->capcc &= ~CC_SM;
			outw(s->capcc, s->io+IT_AC_CAPCC);
			if ((ret = prog_dmabuf_adc(s)))
				return ret;
		}
		if (file->f_mode & FMODE_WRITE) {
			stop_dac(s);
			if (val)
				s->pcc |= CC_SM;
			else
				s->pcc &= ~CC_SM;
			outw(s->pcc, s->io+IT_AC_PCC);
			if ((ret = prog_dmabuf_dac(s)))
				return ret;
		}
		return 0;

	case SNDCTL_DSP_CHANNELS:
		if (get_user(val, (int *)arg))
			return -EFAULT;
		if (val != 0) {
			if (file->f_mode & FMODE_READ) {
				stop_adc(s);
				if (val >= 2) {
					val = 2;
					s->capcc |= CC_SM;
				}
				else
					s->capcc &= ~CC_SM;
				outw(s->capcc, s->io+IT_AC_CAPCC);
				if ((ret = prog_dmabuf_adc(s)))
					return ret;
			}
			if (file->f_mode & FMODE_WRITE) {
				stop_dac(s);
				switch (val) {
				case 1:
					s->pcc &= ~CC_SM;
					break;
				case 2:
					s->pcc |= CC_SM;
					break;
				default:
					// FIX! support multichannel???
					val = 2;
					s->pcc |= CC_SM;
					break;
				}
				outw(s->pcc, s->io+IT_AC_PCC);
				if ((ret = prog_dmabuf_dac(s)))
					return ret;
			}
		}
		return put_user(val, (int *)arg);
		
	case SNDCTL_DSP_GETFMTS: /* Returns a mask */
		return put_user(AFMT_S16_LE|AFMT_U8, (int *)arg);
		
	case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
		if (get_user(val, (int *)arg))
			return -EFAULT;
		if (val != AFMT_QUERY) {
			if (file->f_mode & FMODE_READ) {
				stop_adc(s);
				if (val == AFMT_S16_LE)
					s->capcc |= CC_DF;
				else {
					val = AFMT_U8;
					s->capcc &= ~CC_DF;
				}
				outw(s->capcc, s->io+IT_AC_CAPCC);
				if ((ret = prog_dmabuf_adc(s)))
					return ret;
			}
			if (file->f_mode & FMODE_WRITE) {
				stop_dac(s);
				if (val == AFMT_S16_LE)
					s->pcc |= CC_DF;
				else {
					val = AFMT_U8;
					s->pcc &= ~CC_DF;
				}
				outw(s->pcc, s->io+IT_AC_PCC);
				if ((ret = prog_dmabuf_dac(s)))
					return ret;
			}
		} else {
			if (file->f_mode & FMODE_READ)
				val = (s->capcc & CC_DF) ?
					AFMT_S16_LE : AFMT_U8;
			else
				val = (s->pcc & CC_DF) ?
					AFMT_S16_LE : AFMT_U8;
		}
		return put_user(val, (int *)arg);
		
	case SNDCTL_DSP_POST:
		return 0;

	case SNDCTL_DSP_GETTRIGGER:
		val = 0;
		spin_lock_irqsave(&s->lock, flags);
		if (file->f_mode & FMODE_READ && !s->dma_adc.stopped)
			val |= PCM_ENABLE_INPUT;
		if (file->f_mode & FMODE_WRITE && !s->dma_dac.stopped)
			val |= PCM_ENABLE_OUTPUT;
		spin_unlock_irqrestore(&s->lock, flags);
		return put_user(val, (int *)arg);
		
	case SNDCTL_DSP_SETTRIGGER:
		if (get_user(val, (int *)arg))
			return -EFAULT;
		if (file->f_mode & FMODE_READ) {
			if (val & PCM_ENABLE_INPUT)
				start_adc(s);
			else
				stop_adc(s);
		}
		if (file->f_mode & FMODE_WRITE) {
			if (val & PCM_ENABLE_OUTPUT)
				start_dac(s);
			else
				stop_dac(s);
		}
		return 0;

	case SNDCTL_DSP_GETOSPACE:
		if (!(file->f_mode & FMODE_WRITE))
			return -EINVAL;
		abinfo.fragsize = s->dma_dac.fragsize;
		spin_lock_irqsave(&s->lock, flags);
		count = s->dma_dac.count;
		if (!s->dma_dac.stopped)
			count -= (s->dma_dac.fragsize -
				  inw(s->io+IT_AC_PCDL));
		spin_unlock_irqrestore(&s->lock, flags);
		if (count < 0)
			count = 0;
		abinfo.bytes = s->dma_dac.dmasize - count;
		abinfo.fragstotal = s->dma_dac.numfrag;
		abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;      
		return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ?
			-EFAULT : 0;

	case SNDCTL_DSP_GETISPACE:
		if (!(file->f_mode & FMODE_READ))
			return -EINVAL;
		abinfo.fragsize = s->dma_adc.fragsize;
		spin_lock_irqsave(&s->lock, flags);
		count = s->dma_adc.count;
		if (!s->dma_adc.stopped)
			count += (s->dma_adc.fragsize -
				  inw(s->io+IT_AC_CAPCDL));
		spin_unlock_irqrestore(&s->lock, flags);
		if (count < 0)
			count = 0;
		abinfo.bytes = count;
		abinfo.fragstotal = s->dma_adc.numfrag;
		abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;      
		return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ?
			-EFAULT : 0;
		
	case SNDCTL_DSP_NONBLOCK:
		file->f_flags |= O_NONBLOCK;
		return 0;

	case SNDCTL_DSP_GETODELAY:
		if (!(file->f_mode & FMODE_WRITE))
			return -EINVAL;
		spin_lock_irqsave(&s->lock, flags);
		count = s->dma_dac.count;
		if (!s->dma_dac.stopped)
			count -= (s->dma_dac.fragsize -
				  inw(s->io+IT_AC_PCDL));
		spin_unlock_irqrestore(&s->lock, flags);
		if (count < 0)
			count = 0;
		return put_user(count, (int *)arg);

	case SNDCTL_DSP_GETIPTR:
		if (!(file->f_mode & FMODE_READ))
			return -EINVAL;
		spin_lock_irqsave(&s->lock, flags);
		cinfo.bytes = s->dma_adc.total_bytes;
		count = s->dma_adc.count;
		if (!s->dma_adc.stopped) {
			diff = s->dma_adc.fragsize - inw(s->io+IT_AC_CAPCDL);
			count += diff;
			cinfo.bytes += diff;
			cinfo.ptr = inl(s->io+s->dma_adc.curBufPtr) -
				s->dma_adc.dmaaddr;
		} else
			cinfo.ptr = virt_to_bus(s->dma_adc.nextIn) -
				s->dma_adc.dmaaddr;
		if (s->dma_adc.mapped)
			s->dma_adc.count &= s->dma_adc.fragsize-1;
		spin_unlock_irqrestore(&s->lock, flags);
		if (count < 0)
			count = 0;
		cinfo.blocks = count >> s->dma_adc.fragshift;
		if (copy_to_user((void *)arg, &cinfo, sizeof(cinfo)))
			return -EFAULT;
		return 0;

	case SNDCTL_DSP_GETOPTR:
		if (!(file->f_mode & FMODE_READ))
			return -EINVAL;
		spin_lock_irqsave(&s->lock, flags);
		cinfo.bytes = s->dma_dac.total_bytes;
		count = s->dma_dac.count;
		if (!s->dma_dac.stopped) {
			diff = s->dma_dac.fragsize - inw(s->io+IT_AC_CAPCDL);
			count -= diff;
			cinfo.bytes += diff;
			cinfo.ptr = inl(s->io+s->dma_dac.curBufPtr) -
				s->dma_dac.dmaaddr;
		} else
			cinfo.ptr = virt_to_bus(s->dma_dac.nextOut) -
				s->dma_dac.dmaaddr;
		if (s->dma_dac.mapped)
			s->dma_dac.count &= s->dma_dac.fragsize-1;
		spin_unlock_irqrestore(&s->lock, flags);
		if (count < 0)
			count = 0;
		cinfo.blocks = count >> s->dma_dac.fragshift;
		if (copy_to_user((void *)arg, &cinfo, sizeof(cinfo)))
			return -EFAULT;
		return 0;

	case SNDCTL_DSP_GETBLKSIZE:
		if (file->f_mode & FMODE_WRITE)
			return put_user(s->dma_dac.fragsize, (int *)arg);
		else
			return put_user(s->dma_adc.fragsize, (int *)arg);

	case SNDCTL_DSP_SETFRAGMENT:
		if (get_user(val, (int *)arg))
			return -EFAULT;
		if (file->f_mode & FMODE_READ) {
			stop_adc(s);
			s->dma_adc.ossfragshift = val & 0xffff;
			s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
			if (s->dma_adc.ossfragshift < 4)
				s->dma_adc.ossfragshift = 4;
			if (s->dma_adc.ossfragshift > 15)
				s->dma_adc.ossfragshift = 15;
			if (s->dma_adc.ossmaxfrags < 4)
				s->dma_adc.ossmaxfrags = 4;
			if ((ret = prog_dmabuf_adc(s)))
				return ret;
		}
		if (file->f_mode & FMODE_WRITE) {
			stop_dac(s);
			s->dma_dac.ossfragshift = val & 0xffff;
			s->dma_dac.ossmaxfrags = (val >> 16) & 0xffff;
			if (s->dma_dac.ossfragshift < 4)
				s->dma_dac.ossfragshift = 4;
			if (s->dma_dac.ossfragshift > 15)
				s->dma_dac.ossfragshift = 15;
			if (s->dma_dac.ossmaxfrags < 4)
				s->dma_dac.ossmaxfrags = 4;
			if ((ret = prog_dmabuf_dac(s)))
				return ret;
		}
		return 0;

	case SNDCTL_DSP_SUBDIVIDE:
		if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
		    (file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
			return -EINVAL;
		if (get_user(val, (int *)arg))
			return -EFAULT;
		if (val != 1 && val != 2 && val != 4)
			return -EINVAL;
		if (file->f_mode & FMODE_READ) {
			stop_adc(s);
			s->dma_adc.subdivision = val;
			if ((ret = prog_dmabuf_adc(s)))
				return ret;
		}
		if (file->f_mode & FMODE_WRITE) {
			stop_dac(s);
			s->dma_dac.subdivision = val;
			if ((ret = prog_dmabuf_dac(s)))
				return ret;
		}
		return 0;

	case SOUND_PCM_READ_RATE:
		return put_user((file->f_mode & FMODE_READ) ?
				s->adcrate : s->dacrate, (int *)arg);

	case SOUND_PCM_READ_CHANNELS:
		if (file->f_mode & FMODE_READ)
			return put_user((s->capcc & CC_SM) ? 2 : 1,
					(int *)arg);
		else
			return put_user((s->pcc & CC_SM) ? 2 : 1,
					(int *)arg);
	    
	case SOUND_PCM_READ_BITS:
		if (file->f_mode & FMODE_READ)
			return put_user((s->capcc & CC_DF) ? 16 : 8,
					(int *)arg);
		else
			return put_user((s->pcc & CC_DF) ? 16 : 8,
					(int *)arg);

	case SOUND_PCM_WRITE_FILTER:
	case SNDCTL_DSP_SETSYNCRO:
	case SOUND_PCM_READ_FILTER:
		return -EINVAL;
	}

	return mixdev_ioctl(&s->codec, cmd, arg);
}


static int it8172_open(struct inode *inode, struct file *file)
{
	int minor = iminor(inode);
	DECLARE_WAITQUEUE(wait, current);
	unsigned long flags;
	struct list_head *list;
	struct it8172_state *s;
	int ret;
    
#ifdef IT8172_VERBOSE_DEBUG
	if (file->f_flags & O_NONBLOCK)
		dbg("%s: non-blocking", __FUNCTION__);
	else
		dbg("%s: blocking", __FUNCTION__);
#endif
	
	for (list = devs.next; ; list = list->next) {
		if (list == &devs)
			return -ENODEV;
		s = list_entry(list, struct it8172_state, devs);
		if (!((s->dev_audio ^ minor) & ~0xf))
			break;
	}
	file->private_data = s;
	/* wait for device to become free */
	mutex_lock(&s->open_mutex);
	while (s->open_mode & file->f_mode) {
		if (file->f_flags & O_NONBLOCK) {
			mutex_unlock(&s->open_mutex);
			return -EBUSY;
		}
		add_wait_queue(&s->open_wait, &wait);
		__set_current_state(TASK_INTERRUPTIBLE);
		mutex_unlock(&s->open_mutex);
		schedule();
		remove_wait_queue(&s->open_wait, &wait);
		set_current_state(TASK_RUNNING);
		if (signal_pending(current))
			return -ERESTARTSYS;
		mutex_lock(&s->open_mutex);
	}

	spin_lock_irqsave(&s->lock, flags);

	if (file->f_mode & FMODE_READ) {
		s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags =
			s->dma_adc.subdivision = s->dma_adc.total_bytes = 0;
		s->capcc &= ~(CC_SM | CC_DF);
		set_adc_rate(s, 8000);
		if ((minor & 0xf) == SND_DEV_DSP16)
			s->capcc |= CC_DF;
		outw(s->capcc, s->io+IT_AC_CAPCC);
		if ((ret = prog_dmabuf_adc(s))) {
			spin_unlock_irqrestore(&s->lock, flags);
			return ret;
		}
	}
	if (file->f_mode & FMODE_WRITE) {
		s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags =
			s->dma_dac.subdivision = s->dma_dac.total_bytes = 0;
		s->pcc &= ~(CC_SM | CC_DF);
		set_dac_rate(s, 8000);
		if ((minor & 0xf) == SND_DEV_DSP16)
			s->pcc |= CC_DF;
		outw(s->pcc, s->io+IT_AC_PCC);
		if ((ret = prog_dmabuf_dac(s))) {
			spin_unlock_irqrestore(&s->lock, flags);
			return ret;
		}
	}
    
	spin_unlock_irqrestore(&s->lock, flags);

	s->open_mode |= (file->f_mode & (FMODE_READ | FMODE_WRITE));
	mutex_unlock(&s->open_mutex);
	return nonseekable_open(inode, file);
}

static int it8172_release(struct inode *inode, struct file *file)
{
	struct it8172_state *s = (struct it8172_state *)file->private_data;

#ifdef IT8172_VERBOSE_DEBUG
	dbg("%s", __FUNCTION__);
#endif
	lock_kernel();
	if (file->f_mode & FMODE_WRITE)
		drain_dac(s, file->f_flags & O_NONBLOCK);
	mutex_lock(&s->open_mutex);
	if (file->f_mode & FMODE_WRITE) {
		stop_dac(s);
		dealloc_dmabuf(s, &s->dma_dac);
	}
	if (file->f_mode & FMODE_READ) {
		stop_adc(s);
		dealloc_dmabuf(s, &s->dma_adc);
	}
	s->open_mode &= ((~file->f_mode) & (FMODE_READ|FMODE_WRITE));
	mutex_unlock(&s->open_mutex);
	wake_up(&s->open_wait);
	unlock_kernel();
	return 0;
}

static /*const*/ struct file_operations it8172_audio_fops = {
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.read		= it8172_read,
	.write		= it8172_write,
	.poll		= it8172_poll,
	.ioctl		= it8172_ioctl,
	.mmap		= it8172_mmap,
	.open		= it8172_open,
	.release	= it8172_release,
};


/* --------------------------------------------------------------------- */


/* --------------------------------------------------------------------- */

/*
 * for debugging purposes, we'll create a proc device that dumps the
 * CODEC chipstate
 */

#ifdef IT8172_DEBUG
static int proc_it8172_dump (char *buf, char **start, off_t fpos,
			     int length, int *eof, void *data)
{
	struct it8172_state *s;
	int cnt, len = 0;

	if (list_empty(&devs))
		return 0;
	s = list_entry(devs.next, struct it8172_state, devs);

	/* print out header */
	len += sprintf(buf + len, "\n\t\tIT8172 Audio Debug\n\n");

	// print out digital controller state
	len += sprintf (buf + len, "IT8172 Audio Controller registers\n");
	len += sprintf (buf + len, "---------------------------------\n");
	cnt=0;
	while (cnt < 0x72) {
		if (cnt == IT_AC_PCB1STA || cnt == IT_AC_PCB2STA ||
		    cnt == IT_AC_CAPB1STA || cnt == IT_AC_CAPB2STA ||
		    cnt == IT_AC_PFDP) {
			len+= sprintf (buf + len, "reg %02x = %08x\n",
				       cnt, inl(s->io+cnt));
			cnt += 4;
		} else {
			len+= sprintf (buf + len, "reg %02x = %04x\n",
				       cnt, inw(s->io+cnt));
			cnt += 2;
		}
	}
    
	/* print out CODEC state */
	len += sprintf (buf + len, "\nAC97 CODEC registers\n");
	len += sprintf (buf + len, "----------------------\n");
	for (cnt=0; cnt <= 0x7e; cnt = cnt +2)
		len+= sprintf (buf + len, "reg %02x = %04x\n",
			       cnt, rdcodec(&s->codec, cnt));

	if (fpos >=len){
		*start = buf;
		*eof =1;
		return 0;
	}
	*start = buf + fpos;
	if ((len -= fpos) > length)
		return length;
	*eof =1;
	return len;

}
#endif /* IT8172_DEBUG */

/* --------------------------------------------------------------------- */

/* maximum number of devices; only used for command line params */
#define NR_DEVICE 5

static int spdif[NR_DEVICE];
static int i2s_fmt[NR_DEVICE];

static unsigned int devindex;

module_param_array(spdif, int, NULL, 0);
MODULE_PARM_DESC(spdif, "if 1 the S/PDIF digital output is enabled");
module_param_array(i2s_fmt, int, NULL, 0);
MODULE_PARM_DESC(i2s_fmt, "the format of I2S");

MODULE_AUTHOR("Monta Vista Software, stevel@mvista.com");
MODULE_DESCRIPTION("IT8172 Audio Driver");

/* --------------------------------------------------------------------- */

static int __devinit it8172_probe(struct pci_dev *pcidev,
				  const struct pci_device_id *pciid)
{
	struct it8172_state *s;
	int i, val;
	unsigned short pcisr, vol;
	unsigned char legacy, imc;
	char proc_str[80];
    
	if (pcidev->irq == 0) 
		return -1;

	if (!(s = kmalloc(sizeof(struct it8172_state), GFP_KERNEL))) {
		err("alloc of device struct failed");
		return -1;
	}
	
	memset(s, 0, sizeof(struct it8172_state));
	init_waitqueue_head(&s->dma_adc.wait);
	init_waitqueue_head(&s->dma_dac.wait);
	init_waitqueue_head(&s->open_wait);
	mutex_init(&s->open_mutex);
	spin_lock_init(&s->lock);
	s->dev = pcidev;
	s->io = pci_resource_start(pcidev, 0);
	s->irq = pcidev->irq;
	s->vendor = pcidev->vendor;
	s->device = pcidev->device;
	pci_read_config_byte(pcidev, PCI_REVISION_ID, &s->rev);
	s->codec.private_data = s;
	s->codec.id = 0;
	s->codec.codec_read = rdcodec;
	s->codec.codec_write = wrcodec;
	s->codec.codec_wait = waitcodec;

	if (!request_region(s->io, pci_resource_len(pcidev,0),
			    IT8172_MODULE_NAME)) {
		err("io ports %#lx->%#lx in use",
		    s->io, s->io + pci_resource_len(pcidev,0)-1);
		goto err_region;
	}
	if (request_irq(s->irq, it8172_interrupt, IRQF_DISABLED,
			IT8172_MODULE_NAME, s)) {
		err("irq %u in use", s->irq);
		goto err_irq;
	}

	info("IO at %#lx, IRQ %d", s->io, s->irq);

	/* register devices */
	if ((s->dev_audio = register_sound_dsp(&it8172_audio_fops, -1)) < 0)
		goto err_dev1;
	if ((s->codec.dev_mixer =
	     register_sound_mixer(&it8172_mixer_fops, -1)) < 0)
		goto err_dev2;

#ifdef IT8172_DEBUG
	/* initialize the debug proc device */
	s->ps = create_proc_read_entry(IT8172_MODULE_NAME, 0, NULL,
				       proc_it8172_dump, NULL);
#endif /* IT8172_DEBUG */
	
	/*
	 * Reset the Audio device using the IT8172 PCI Reset register. This
	 * creates an audible double click on a speaker connected to Line-out.
	 */
	IT_IO_READ16(IT_PM_PCISR, pcisr);
	pcisr |= IT_PM_PCISR_ACSR;
	IT_IO_WRITE16(IT_PM_PCISR, pcisr);
	/* wait up to 100msec for reset to complete */
	for (i=0; pcisr & IT_PM_PCISR_ACSR; i++) {
		it8172_delay(10);
		if (i == 10)
			break;
		IT_IO_READ16(IT_PM_PCISR, pcisr);
	}
	if (i == 10) {
		err("chip reset timeout!");
		goto err_dev3;
	}
    
	/* enable pci io and bus mastering */
	if (pci_enable_device(pcidev))
		goto err_dev3;
	pci_set_master(pcidev);

	/* get out of legacy mode */
	pci_read_config_byte (pcidev, 0x40, &legacy);
	pci_write_config_byte (pcidev, 0x40, legacy & ~1);
    
	s->spdif_volume = -1;
	/* check to see if s/pdif mode is being requested */
	if (spdif[devindex]) {
		info("enabling S/PDIF output");
		s->spdif_volume = 0;
		outb(GC_SOE, s->io+IT_AC_GC);
	} else {
		info("disabling S/PDIF output");
		outb(0, s->io+IT_AC_GC);
	}
    
	/* check to see if I2S format requested */
	if (i2s_fmt[devindex]) {
		info("setting I2S format to 0x%02x", i2s_fmt[devindex]);
		outb(i2s_fmt[devindex], s->io+IT_AC_I2SMC);
	} else {
		outb(I2SMC_I2SF_I2S, s->io+IT_AC_I2SMC);
	}

	/* cold reset the AC97 */
	outw(CODECC_CR, s->io+IT_AC_CODECC);
	udelay(1000);
	outw(0, s->io+IT_AC_CODECC);
	/* need to delay around 500msec(bleech) to give
	   some CODECs enough time to wakeup */
	it8172_delay(500);
    
	/* AC97 warm reset to start the bitclk */
	outw(CODECC_WR, s->io+IT_AC_CODECC);
	udelay(1000);
	outw(0, s->io+IT_AC_CODECC);
    
	/* codec init */
	if (!ac97_probe_codec(&s->codec))
		goto err_dev3;

	/* add I2S as allowable recording source */
	s->codec.record_sources |= SOUND_MASK_I2S;
	
	/* Enable Volume button interrupts */
	imc = inb(s->io+IT_AC_IMC);
	outb(imc & ~IMC_VCIM, s->io+IT_AC_IMC);

	/* Un-mute PCM and FM out on the controller */
	vol = inw(s->io+IT_AC_PCMOV);
	outw(vol & ~PCMOV_PCMOM, s->io+IT_AC_PCMOV);
	vol = inw(s->io+IT_AC_FMOV);
	outw(vol & ~FMOV_FMOM, s->io+IT_AC_FMOV);
    
	/* set channel defaults to 8-bit, mono, 8 Khz */
	s->pcc = 0;
	s->capcc = 0;
	set_dac_rate(s, 8000);
	set_adc_rate(s, 8000);

	/* set mic to be the recording source */
	val = SOUND_MASK_MIC;
	mixdev_ioctl(&s->codec, SOUND_MIXER_WRITE_RECSRC,
		     (unsigned long)&val);

	/* mute AC'97 master and PCM when in S/PDIF mode */
	if (s->spdif_volume != -1) {
		val = 0x0000;
		s->codec.mixer_ioctl(&s->codec, SOUND_MIXER_WRITE_VOLUME,
				     (unsigned long)&val);
		s->codec.mixer_ioctl(&s->codec, SOUND_MIXER_WRITE_PCM,
				     (unsigned long)&val);
	}
    
#ifdef IT8172_DEBUG
	sprintf(proc_str, "driver/%s/%d/ac97", IT8172_MODULE_NAME,
		s->codec.id);
	s->ac97_ps = create_proc_read_entry (proc_str, 0, NULL,
					     ac97_read_proc, &s->codec);
#endif
    
	/* store it in the driver field */
	pci_set_drvdata(pcidev, s);
	pcidev->dma_mask = 0xffffffff;
	/* put it into driver list */
	list_add_tail(&s->devs, &devs);
	/* increment devindex */
	if (devindex < NR_DEVICE-1)
		devindex++;
	return 0;

 err_dev3:
	unregister_sound_mixer(s->codec.dev_mixer);
 err_dev2:
	unregister_sound_dsp(s->dev_audio);
 err_dev1:
	err("cannot register misc device");
	free_irq(s->irq, s);
 err_irq:
	release_region(s->io, pci_resource_len(pcidev,0));
 err_region:
	kfree(s);
	return -1;
}

static void __devexit it8172_remove(struct pci_dev *dev)
{
	struct it8172_state *s = pci_get_drvdata(dev);

	if (!s)
		return;
	list_del(&s->devs);
#ifdef IT8172_DEBUG
	if (s->ps)
		remove_proc_entry(IT8172_MODULE_NAME, NULL);
#endif /* IT8172_DEBUG */
	synchronize_irq(s->irq);
	free_irq(s->irq, s);
	release_region(s->io, pci_resource_len(dev,0));
	unregister_sound_dsp(s->dev_audio);
	unregister_sound_mixer(s->codec.dev_mixer);
	kfree(s);
	pci_set_drvdata(dev, NULL);
}



static struct pci_device_id id_table[] = {
	{ PCI_VENDOR_ID_ITE, PCI_DEVICE_ID_ITE_IT8172G_AUDIO, PCI_ANY_ID,
	  PCI_ANY_ID, 0, 0 },
	{ 0, }
};

MODULE_DEVICE_TABLE(pci, id_table);

static struct pci_driver it8172_driver = {
	.name = IT8172_MODULE_NAME,
	.id_table = id_table,
	.probe = it8172_probe,
	.remove = __devexit_p(it8172_remove)
};

static int __init init_it8172(void)
{
	info("version v0.5 time " __TIME__ " " __DATE__);
	return pci_register_driver(&it8172_driver);
}

static void __exit cleanup_it8172(void)
{
	info("unloading");
	pci_unregister_driver(&it8172_driver);
}

module_init(init_it8172);
module_exit(cleanup_it8172);

/* --------------------------------------------------------------------- */

#ifndef MODULE

/* format is: it8172=[spdif],[i2s:<I2S format>] */

static int __init it8172_setup(char *options)
{
	char* this_opt;
	static unsigned __initdata nr_dev = 0;

	if (nr_dev >= NR_DEVICE)
		return 0;

	if (!options || !*options)
		return 0;

	while (this_opt = strsep(&options, ",")) {
		if (!*this_opt)
			continue;
		if (!strncmp(this_opt, "spdif", 5)) {
			spdif[nr_dev] = 1;
		} else if (!strncmp(this_opt, "i2s:", 4)) {
			if (!strncmp(this_opt+4, "dac", 3))
				i2s_fmt[nr_dev] = I2SMC_I2SF_DAC;
			else if (!strncmp(this_opt+4, "adc", 3))
				i2s_fmt[nr_dev] = I2SMC_I2SF_ADC;
			else if (!strncmp(this_opt+4, "i2s", 3))
				i2s_fmt[nr_dev] = I2SMC_I2SF_I2S;
		}
	}

	nr_dev++;
	return 1;
}

__setup("it8172=", it8172_setup);

#endif /* MODULE */