summaryrefslogtreecommitdiffstats
path: root/drivers/sbus/audio/amd7930.c
blob: 9b71e0a809955e822d87dc13fb904d5b79307a73 (plain)
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
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
/*
 * drivers/sbus/audio/amd7930.c
 *
 * Copyright (C) 1996,1997 Thomas K. Dyas (tdyas@eden.rutgers.edu)
 *
 * This is the lowlevel driver for the AMD7930 audio chip found on all
 * sun4c machines and some sun4m machines.
 *
 * The amd7930 is actually an ISDN chip which has a very simple
 * integrated audio encoder/decoder. When Sun decided on what chip to
 * use for audio, they had the brilliant idea of using the amd7930 and
 * only connecting the audio encoder/decoder pins.
 *
 * Thanks to the AMD engineer who was able to get us the AMD79C30
 * databook which has all the programming information and gain tables.
 *
 * Advanced Micro Devices' Am79C30A is an ISDN/audio chip used in the
 * SparcStation 1+.  The chip provides microphone and speaker interfaces
 * which provide mono-channel audio at 8K samples per second via either
 * 8-bit A-law or 8-bit mu-law encoding.  Also, the chip features an
 * ISDN BRI Line Interface Unit (LIU), I.430 S/T physical interface,
 * which performs basic D channel LAPD processing and provides raw
 * B channel data.  The digital audio channel, the two ISDN B channels,
 * and two 64 Kbps channels to the microprocessor are all interconnected
 * via a multiplexer.
 *
 * This driver interfaces to the Linux HiSax ISDN driver, which performs
 * all high-level Q.921 and Q.931 ISDN functions.  The file is not
 * itself a hardware driver; rather it uses functions exported by
 * the AMD7930 driver in the sparcaudio subsystem (drivers/sbus/audio),
 * allowing the chip to be simultaneously used for both audio and ISDN data.
 * The hardware driver does _no_ buffering, but provides several callbacks
 * which are called during interrupt service and should therefore run quickly.
 *
 * D channel transmission is performed by passing the hardware driver the
 * address and size of an skb's data area, then waiting for a callback
 * to signal successful transmission of the packet.  A task is then
 * queued to notify the HiSax driver that another packet may be transmitted.
 *
 * D channel reception is quite simple, mainly because of:
 *   1) the slow speed of the D channel - 16 kbps, and
 *   2) the presence of an 8- or 32-byte (depending on chip version) FIFO
 *      to buffer the D channel data on the chip
 * Worst case scenario of back-to-back packets with the 8 byte buffer
 * at 16 kbps yields an service time of 4 ms - long enough to preclude
 * the need for fancy buffering.  We queue a background task that copies
 * data out of the receive buffer into an skb, and the hardware driver
 * simply does nothing until we're done with the receive buffer and
 * reset it for a new packet.
 *
 * B channel processing is more complex, because of:
 *   1) the faster speed - 64 kbps,
 *   2) the lack of any on-chip buffering (it interrupts for every byte), and
 *   3) the lack of any chip support for HDLC encapsulation
 *
 * The HiSax driver can put each B channel into one of three modes -
 * L1_MODE_NULL (channel disabled), L1_MODE_TRANS (transparent data relay),
 * and L1_MODE_HDLC (HDLC encapsulation by low-level driver).
 * L1_MODE_HDLC is the most common, used for almost all "pure" digital
 * data sessions.  L1_MODE_TRANS is used for ISDN audio.
 *
 * HDLC B channel transmission is performed via a large buffer into
 * which the skb is copied while performing HDLC bit-stuffing.  A CRC
 * is computed and attached to the end of the buffer, which is then
 * passed to the low-level routines for raw transmission.  Once
 * transmission is complete, the hardware driver is set to enter HDLC
 * idle by successive transmission of mark (all 1) bytes, waiting for
 * the ISDN driver to prepare another packet for transmission and
 * deliver it.
 *
 * HDLC B channel reception is performed via an X-byte ring buffer
 * divided into N sections of X/N bytes each.  Defaults: X=256 bytes, N=4.
 * As the hardware driver notifies us that each section is full, we
 * hand it the next section and schedule a background task to peruse
 * the received section, bit-by-bit, with an HDLC decoder.  As
 * packets are detected, they are copied into a large buffer while
 * decoding HDLC bit-stuffing.  The ending CRC is verified, and if
 * it is correct, we alloc a new skb of the correct length (which we
 * now know), copy the packet into it, and hand it to the upper layers.
 * Optimization: for large packets, we hand the buffer (which also
 * happens to be an skb) directly to the upper layer after an skb_trim,
 * and alloc a new large buffer for future packets, thus avoiding a copy.
 * Then we return to HDLC processing; state is saved between calls.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/malloc.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/soundcard.h>
#include <asm/openprom.h>
#include <asm/oplib.h>
#include <asm/system.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/sbus.h>

#include <asm/audioio.h>
#include "amd7930.h"

#if defined (AMD79C30_ISDN) || defined (LINUX_VERSION_CODE) && LINUX_VERSION_CODE > 0x200ff 
#include "../../isdn/hisax/hisax.h"
#include "../../isdn/hisax/isdnl1.h"
#include "../../isdn/hisax/foreign.h"
#endif

#define MAX_DRIVERS 1

static struct sparcaudio_driver drivers[MAX_DRIVERS];
static int num_drivers;

/* Each amd7930 chip has two bi-directional B channels and a D
 * channel available to the uproc.  This structure handles all
 * the buffering needed to transmit and receive via a single channel.
 */

#define CHANNEL_AVAILABLE	0x00
#define CHANNEL_INUSE_AUDIO_IN	0x01
#define CHANNEL_INUSE_AUDIO_OUT	0x02
#define CHANNEL_INUSE_ISDN_B1	0x04
#define CHANNEL_INUSE_ISDN_B2	0x08
#define CHANNEL_INUSE		0xff

struct amd7930_channel {
	/* Channel status */
	unsigned char channel_status;

	/* Current buffer that the driver is playing on channel */
	volatile __u8 * output_ptr;
	volatile unsigned long output_count;
	unsigned char xmit_idle_char;

	/* Callback routine (and argument) when output is done on */
	void (*output_callback)();
	void * output_callback_arg;

	/* Current buffer that the driver is recording on channel */
	volatile __u8 * input_ptr;
	volatile unsigned long input_count;
	volatile unsigned long input_limit;

	/* Callback routine (and argument) when input is done on */
	void (*input_callback)();
	void * input_callback_arg;
};

/* Private information we store for each amd7930 chip. */
struct amd7930_info {
	struct amd7930_channel D;
	struct amd7930_channel Bb;
	struct amd7930_channel Bc;

	/* Pointers to which B channels are being used for what
	 * These three fields (Baudio, Bisdn[0], and Bisdn[1]) will either
	 * be NULL or point to one of the Bb/Bc structures above.
	 */
	struct amd7930_channel *Baudio;
	struct amd7930_channel *Bisdn[2];

	/* Device registers information. */
	struct amd7930 *regs;
	unsigned long regs_size;
	struct amd7930_map map;

	/* Volume information. */
	int pgain, rgain, mgain;

	/* Device interrupt information. */
	int irq;
	volatile int ints_on;


	/* Someone to signal when the ISDN LIU state changes */
	int liu_state;
	void (*liu_callback)(void *);
	void *liu_callback_arg;
};



/* Output a 16-bit quantity in the order that the amd7930 expects. */
#define amd7930_out16(regs,v) ({ regs->dr = v & 0xFF; regs->dr = (v >> 8) & 0xFF; })


/*
 * gx, gr & stg gains.  this table must contain 256 elements with
 * the 0th being "infinity" (the magic value 9008).  The remaining
 * elements match sun's gain curve (but with higher resolution):
 * -18 to 0dB in .16dB steps then 0 to 12dB in .08dB steps.
 */
static __const__ __u16 gx_coeff[256] = {
	0x9008, 0x8b7c, 0x8b51, 0x8b45, 0x8b42, 0x8b3b, 0x8b36, 0x8b33,
	0x8b32, 0x8b2a, 0x8b2b, 0x8b2c, 0x8b25, 0x8b23, 0x8b22, 0x8b22,
	0x9122, 0x8b1a, 0x8aa3, 0x8aa3, 0x8b1c, 0x8aa6, 0x912d, 0x912b,
	0x8aab, 0x8b12, 0x8aaa, 0x8ab2, 0x9132, 0x8ab4, 0x913c, 0x8abb,
	0x9142, 0x9144, 0x9151, 0x8ad5, 0x8aeb, 0x8a79, 0x8a5a, 0x8a4a,
	0x8b03, 0x91c2, 0x91bb, 0x8a3f, 0x8a33, 0x91b2, 0x9212, 0x9213,
	0x8a2c, 0x921d, 0x8a23, 0x921a, 0x9222, 0x9223, 0x922d, 0x9231,
	0x9234, 0x9242, 0x925b, 0x92dd, 0x92c1, 0x92b3, 0x92ab, 0x92a4,
	0x92a2, 0x932b, 0x9341, 0x93d3, 0x93b2, 0x93a2, 0x943c, 0x94b2,
	0x953a, 0x9653, 0x9782, 0x9e21, 0x9d23, 0x9cd2, 0x9c23, 0x9baa,
	0x9bde, 0x9b33, 0x9b22, 0x9b1d, 0x9ab2, 0xa142, 0xa1e5, 0x9a3b,
	0xa213, 0xa1a2, 0xa231, 0xa2eb, 0xa313, 0xa334, 0xa421, 0xa54b,
	0xada4, 0xac23, 0xab3b, 0xaaab, 0xaa5c, 0xb1a3, 0xb2ca, 0xb3bd,
	0xbe24, 0xbb2b, 0xba33, 0xc32b, 0xcb5a, 0xd2a2, 0xe31d, 0x0808,
	0x72ba, 0x62c2, 0x5c32, 0x52db, 0x513e, 0x4cce, 0x43b2, 0x4243,
	0x41b4, 0x3b12, 0x3bc3, 0x3df2, 0x34bd, 0x3334, 0x32c2, 0x3224,
	0x31aa, 0x2a7b, 0x2aaa, 0x2b23, 0x2bba, 0x2c42, 0x2e23, 0x25bb,
	0x242b, 0x240f, 0x231a, 0x22bb, 0x2241, 0x2223, 0x221f, 0x1a33,
	0x1a4a, 0x1acd, 0x2132, 0x1b1b, 0x1b2c, 0x1b62, 0x1c12, 0x1c32,
	0x1d1b, 0x1e71, 0x16b1, 0x1522, 0x1434, 0x1412, 0x1352, 0x1323,
	0x1315, 0x12bc, 0x127a, 0x1235, 0x1226, 0x11a2, 0x1216, 0x0a2a,
	0x11bc, 0x11d1, 0x1163, 0x0ac2, 0x0ab2, 0x0aab, 0x0b1b, 0x0b23,
	0x0b33, 0x0c0f, 0x0bb3, 0x0c1b, 0x0c3e, 0x0cb1, 0x0d4c, 0x0ec1,
	0x079a, 0x0614, 0x0521, 0x047c, 0x0422, 0x03b1, 0x03e3, 0x0333,
	0x0322, 0x031c, 0x02aa, 0x02ba, 0x02f2, 0x0242, 0x0232, 0x0227,
	0x0222, 0x021b, 0x01ad, 0x0212, 0x01b2, 0x01bb, 0x01cb, 0x01f6,
	0x0152, 0x013a, 0x0133, 0x0131, 0x012c, 0x0123, 0x0122, 0x00a2,
	0x011b, 0x011e, 0x0114, 0x00b1, 0x00aa, 0x00b3, 0x00bd, 0x00ba,
	0x00c5, 0x00d3, 0x00f3, 0x0062, 0x0051, 0x0042, 0x003b, 0x0033,
	0x0032, 0x002a, 0x002c, 0x0025, 0x0023, 0x0022, 0x001a, 0x0021,
	0x001b, 0x001b, 0x001d, 0x0015, 0x0013, 0x0013, 0x0012, 0x0012,
	0x000a, 0x000a, 0x0011, 0x0011, 0x000b, 0x000b, 0x000c, 0x000e,
};

static __const__ __u16 ger_coeff[] = {
	0x431f, /* 5. dB */
	0x331f, /* 5.5 dB */
	0x40dd, /* 6. dB */
	0x11dd, /* 6.5 dB */
	0x440f, /* 7. dB */
	0x411f, /* 7.5 dB */
	0x311f, /* 8. dB */
	0x5520, /* 8.5 dB */
	0x10dd, /* 9. dB */
	0x4211, /* 9.5 dB */
	0x410f, /* 10. dB */
	0x111f, /* 10.5 dB */
	0x600b, /* 11. dB */
	0x00dd, /* 11.5 dB */
	0x4210, /* 12. dB */
	0x110f, /* 13. dB */
	0x7200, /* 14. dB */
	0x2110, /* 15. dB */
	0x2200, /* 15.9 dB */
	0x000b, /* 16.9 dB */
	0x000f  /* 18. dB */
};
#define NR_GER_COEFFS (sizeof(ger_coeff) / sizeof(ger_coeff[0]))

/* Enable amd7930 interrupts atomically. */
static __inline__ void amd7930_enable_ints(struct amd7930_info *info)
{
	register unsigned long flags;

	if (info->ints_on)
		return;

	save_and_cli(flags);
	info->regs->cr = AMR_INIT;
	info->regs->dr = AM_INIT_ACTIVE;
	restore_flags(flags);

	info->ints_on = 1;
}

/* Disable amd7930 interrupts atomically. */
static __inline__ void amd7930_disable_ints(struct amd7930_info *info)
{
	register unsigned long flags;

	if (!info->ints_on)
		return;

	save_and_cli(flags);
	info->regs->cr = AMR_INIT;
	info->regs->dr = AM_INIT_ACTIVE | AM_INIT_DISABLE_INTS;
	restore_flags(flags);

	info->ints_on = 0;
}  

/* Idle amd7930 (no interrupts, no audio, no data) */
static __inline__ void amd7930_idle(struct amd7930_info *info)
{
	register unsigned long flags;

	if (!info->ints_on)
		return;

	save_and_cli(flags);
	info->regs->cr = AMR_INIT;
	info->regs->dr = 0;
	restore_flags(flags);

	info->ints_on = 0;
}  

/* Commit the local copy of the MAP registers to the amd7930. */
static void amd7930_write_map(struct sparcaudio_driver *drv)
{
	struct amd7930_info *info = (struct amd7930_info *)drv->private;
	struct amd7930      *regs = info->regs;
	struct amd7930_map  *map  = &info->map;
	unsigned long flags;

	save_and_cli(flags);

	regs->cr = AMR_MAP_GX;
	amd7930_out16(regs, map->gx);

	regs->cr = AMR_MAP_GR;
	amd7930_out16(regs, map->gr);

	regs->cr = AMR_MAP_STGR;
	amd7930_out16(regs, map->stgr);

	regs->cr = AMR_MAP_GER;
	amd7930_out16(regs, map->ger);

	regs->cr = AMR_MAP_MMR1;
	regs->dr = map->mmr1;

	regs->cr = AMR_MAP_MMR2;
	regs->dr = map->mmr2;

	restore_flags(flags);
}

/* Update the MAP registers with new settings. */
static void amd7930_update_map(struct sparcaudio_driver *drv)
{
	struct amd7930_info *info = (struct amd7930_info *)drv->private;
	struct amd7930_map  *map  = &info->map;
	int level;

	map->gx = gx_coeff[info->rgain];
	map->stgr = gx_coeff[info->mgain];

	level = (info->pgain * (256 + NR_GER_COEFFS)) >> 8;
	if (level >= 256) {
		map->ger = ger_coeff[level - 256];
		map->gr = gx_coeff[255];
	} else {
		map->ger = ger_coeff[0];
		map->gr = gx_coeff[level];
	}

	amd7930_write_map(drv);
}

/* Bit of a hack here - if the HISAX ISDN driver has got INTSTAT debugging
 * turned on, we send debugging characters to the ISDN driver:
 *
 *   i# - Interrupt received - number from 0 to 7 is low three bits of IR
 *   >  - Loaded a single char into the Dchan xmit FIFO
 *   +  - Finished loading an xmit packet into the Dchan xmit FIFO
 *   <  - Read a single char from the Dchan recv FIFO
 *   !  - Finished reading a packet from the Dchan recv FIFO
 *
 * This code needs to be removed if anything other than HISAX uses the ISDN
 * driver, since D.output_callback_arg is assumed to be a certain struct ptr
 */

#ifdef L2FRAME_DEBUG

inline void debug_info(struct amd7930_info *info, char c) {
	struct IsdnCardState *cs;

	if (!info || !info->D.output_callback_arg) return;

	cs = (struct IsdnCardState *)info->D.output_callback_arg;

	if (!cs || !cs->status_write) return;

	if (cs->debug & L1_DEB_INTSTAT) {
		*(cs->status_write++) = c;
		if (cs->status_write > cs->status_end)
			cs->status_write = cs->status_buf;
	}
}

#else

#define debug_info(info,c)

#endif


static void fill_D_xmit_fifo(struct amd7930_info *info)
{
	/* Send next byte(s) of outgoing data. */
	while (info->D.output_ptr && info->D.output_count > 0 &&
               (info->regs->dsr2 & AMR_DSR2_TBE)) {

		/* Send the next byte and advance buffer pointer. */
		info->regs->dctb = *(info->D.output_ptr);
		info->D.output_ptr++;
		info->D.output_count--;

		debug_info(info, '>');
        }
}

static void transceive_Dchannel(struct amd7930_info *info)
{
	__u8 dummy;

#define D_XMIT_ERRORS (AMR_DER_COLLISION | AMR_DER_UNRN)
#define D_RECV_ERRORS (AMR_DER_RABRT | AMR_DER_RFRAME | AMR_DER_FCS | \
			AMR_DER_OVFL | AMR_DER_UNFL | AMR_DER_OVRN)

	/* Transmit if we can */
	fill_D_xmit_fifo(info);

	/* Done with the xmit buffer? Notify the midlevel driver. */
	if (info->D.output_ptr != NULL && info->D.output_count == 0) {
		info->D.output_ptr = NULL;
		info->D.output_count = 0;
		debug_info(info, '+');
		if (info->D.output_callback)
			(*info->D.output_callback)
				(info->D.output_callback_arg,
				 info->regs->der);
				 /* info->regs->der & D_XMIT_ERRORS); */
	}

	/* Read the next byte(s) of incoming data. */

	while (info->regs->dsr2 & AMR_DSR2_RBA) {

		if (info->D.input_ptr &&
		    (info->D.input_count < info->D.input_limit)) {

			/* Get the next byte and advance buffer pointer. */

			*(info->D.input_ptr) = info->regs->dcrb;
			info->D.input_ptr++;
			info->D.input_count++;

		} else {

			/* Overflow - should be detected by chip via RBLR
			 * so we'll just consume data until we see LBRP
			 */

			dummy = info->regs->dcrb;

		}

		debug_info(info, '<');

		if (info->regs->dsr2 & AMR_DSR2_LBRP) {

			/* End of recv packet? Notify the midlevel driver. */

			__u8 der;

			debug_info(info, '!');

			info->D.input_ptr = NULL;

			der = info->regs->der & D_RECV_ERRORS;

			/* Read receive byte count - advances FIFOs */
			info->regs->cr = AMR_DLC_DRCR;
			dummy = info->regs->dr;
			dummy = info->regs->dr;

			if (info->D.input_callback)
				(*info->D.input_callback)
					(info->D.input_callback_arg, der,
					 info->D.input_count);
		}

	}
}

long amd7930_xmit_idles=0;

static void transceive_Bchannel(struct amd7930_channel *channel,
	__volatile__ __u8 *io_reg)
{
	/* Send the next byte of outgoing data. */
	if (channel->output_ptr && channel->output_count > 0) {

		/* Send the next byte and advance buffer pointer. */
		*io_reg = *(channel->output_ptr);
		channel->output_ptr++;
		channel->output_count--;

		/* Done with the buffer? Notify the midlevel driver. */
		if (channel->output_count == 0) {
			channel->output_ptr = NULL;
			channel->output_count = 0;
			if (channel->output_callback)
				(*channel->output_callback)
					(channel->output_callback_arg,1);
		}
	} else {
		*io_reg = channel->xmit_idle_char;
		amd7930_xmit_idles++;
        }

	/* Read the next byte of incoming data. */
	if (channel->input_ptr && channel->input_count > 0) {

		/* Get the next byte and advance buffer pointer. */
		*(channel->input_ptr) = *io_reg;
		channel->input_ptr++;
		channel->input_count--;

		/* Done with the buffer? Notify the midlevel driver. */
		if (channel->input_count == 0) {
			channel->input_ptr = NULL;
			channel->input_count = 0;
			if (channel->input_callback)
				(*channel->input_callback)
					(channel->input_callback_arg, 1);
		}
	}
}

/* Interrupt handler (The chip takes only one byte per interrupt. Grrr!) */
static void amd7930_interrupt(int irq, void *dev_id, struct pt_regs *intr_regs)
{
	struct sparcaudio_driver *drv = (struct sparcaudio_driver *)dev_id;
	struct amd7930_info *info = (struct amd7930_info *)drv->private;
	struct amd7930 *regs = info->regs;
	__u8 ir;
	__u8 lsr;

	/* Clear the interrupt. */
	ir = regs->ir;

	if (ir & AMR_IR_BBUF) {
		if (info->Bb.channel_status == CHANNEL_INUSE)
			transceive_Bchannel(&info->Bb, &info->regs->bbtb);
		if (info->Bc.channel_status == CHANNEL_INUSE)
			transceive_Bchannel(&info->Bc, &info->regs->bctb);
	}

	if (ir & (AMR_IR_DRTHRSH | AMR_IR_DTTHRSH | AMR_IR_DSRI)) {
		debug_info(info, 'i');
		debug_info(info, '0' + (ir&7));
		transceive_Dchannel(info);
	}

	if (ir & AMR_IR_LSRI) {
		regs->cr = AMR_LIU_LSR;
		lsr = regs->dr;

                info->liu_state = (lsr&0x7) + 2;

                if (info->liu_callback)
			(*info->liu_callback)(info->liu_callback_arg);
        }
}


static int amd7930_open(struct inode * inode, struct file * file,
			struct sparcaudio_driver *drv)
{
	MOD_INC_USE_COUNT;
	return 0;
}

static void amd7930_release(struct inode * inode, struct file * file,
			    struct sparcaudio_driver *drv)
{
	/* amd7930_disable_ints(drv->private); */
	MOD_DEC_USE_COUNT;
}

static void request_Baudio(struct amd7930_info *info)
{
	if (info->Bb.channel_status == CHANNEL_AVAILABLE) {

		info->Bb.channel_status = CHANNEL_INUSE;
		info->Baudio = &info->Bb;

		/* Multiplexor map - audio (Ba) to Bb */
		info->regs->cr = AMR_MUX_MCR1;
		info->regs->dr = AM_MUX_CHANNEL_Ba | (AM_MUX_CHANNEL_Bb << 4);

		/* Enable B channel interrupts */
		info->regs->cr = AMR_MUX_MCR4;
		info->regs->dr = AM_MUX_MCR4_ENABLE_INTS;

	} else if (info->Bc.channel_status == CHANNEL_AVAILABLE) {

		info->Bc.channel_status = CHANNEL_INUSE;
		info->Baudio = &info->Bc;

		/* Multiplexor map - audio (Ba) to Bc */
		info->regs->cr = AMR_MUX_MCR1;
		info->regs->dr = AM_MUX_CHANNEL_Ba | (AM_MUX_CHANNEL_Bc << 4);

		/* Enable B channel interrupts */
		info->regs->cr = AMR_MUX_MCR4;
		info->regs->dr = AM_MUX_MCR4_ENABLE_INTS;

	}
}

static void release_Baudio(struct amd7930_info *info)
{
	if (info->Baudio) {
		info->Baudio->channel_status = CHANNEL_AVAILABLE;
		info->regs->cr = AMR_MUX_MCR1;
		info->regs->dr = 0;
		info->Baudio = NULL;

		if (info->Bb.channel_status == CHANNEL_AVAILABLE &&
		    info->Bc.channel_status == CHANNEL_AVAILABLE) {

			/* Disable B channel interrupts */
			info->regs->cr = AMR_MUX_MCR4;
			info->regs->dr = 0;
		}
	}
}

static void amd7930_start_output(struct sparcaudio_driver *drv,
				 __u8 * buffer, unsigned long count)
{
	struct amd7930_info *info = (struct amd7930_info *)drv->private;

	if (! info->Baudio) {
		request_Baudio(info);
	}

	if (info->Baudio) {
		info->Baudio->output_ptr = buffer;
		info->Baudio->output_count = count;
        	info->Baudio->output_callback = (void *) &sparcaudio_output_done;
	        info->Baudio->output_callback_arg = (void *) drv;
		info->Baudio->xmit_idle_char = 0;
	}
}

static void amd7930_stop_output(struct sparcaudio_driver *drv)
{
	struct amd7930_info *info = (struct amd7930_info *)drv->private;

	if (info->Baudio) {
		info->Baudio->output_ptr = NULL;
		info->Baudio->output_count = 0;
		if (! info->Baudio->input_ptr)
			release_Baudio(info);
	}
}

static void amd7930_start_input(struct sparcaudio_driver *drv,
				__u8 * buffer, unsigned long count)
{
	struct amd7930_info *info = (struct amd7930_info *)drv->private;

	if (! info->Baudio) {
		request_Baudio(info);
	}

	if (info->Baudio) {
		info->Baudio->input_ptr = buffer;
		info->Baudio->input_count = count;
		info->Baudio->input_callback = (void *) &sparcaudio_input_done;
		info->Baudio->input_callback_arg = (void *) drv;
	}
}

static void amd7930_stop_input(struct sparcaudio_driver *drv)
{
	struct amd7930_info *info = (struct amd7930_info *)drv->private;

	if (info->Baudio) {
		info->Baudio->input_ptr = NULL;
		info->Baudio->input_count = 0;
		if (! info->Baudio->output_ptr)
			release_Baudio(info);
	}

}

static void amd7930_sunaudio_getdev(struct sparcaudio_driver *drv,
				 audio_device_t * audinfo)
{
	strncpy(audinfo->name, "SUNW,am79c30", sizeof(audinfo->name) - 1);
	strncpy(audinfo->version, "a", sizeof(audinfo->version) - 1);
	strncpy(audinfo->config, "onboard1", sizeof(audinfo->config) - 1);
}

static int amd7930_sunaudio_getdev_sunos(struct sparcaudio_driver *drv)
{
	return AUDIO_DEV_AMD;
}

static int amd7930_get_formats(struct sparcaudio_driver *drv)
{
      return (AFMT_MU_LAW | AFMT_A_LAW);
}

static int amd7930_get_output_ports(struct sparcaudio_driver *drv)
{
      return (AUDIO_SPEAKER | AUDIO_HEADPHONE);
}

static int amd7930_get_input_ports(struct sparcaudio_driver *drv)
{
      return (AUDIO_MICROPHONE);
}

static int amd7930_set_output_volume(struct sparcaudio_driver *drv, int vol)
{
	struct amd7930_info *info = (struct amd7930_info *)drv->private;

	info->pgain = vol;
	amd7930_update_map(drv);
	return 0;
}

static int amd7930_get_output_volume(struct sparcaudio_driver *drv)
{
	struct amd7930_info *info = (struct amd7930_info *)drv->private;

	return info->pgain;
}

static int amd7930_set_input_volume(struct sparcaudio_driver *drv, int vol)
{
	struct amd7930_info *info = (struct amd7930_info *)drv->private;

	info->rgain = vol;
	amd7930_update_map(drv);
	return 0;
}

static int amd7930_get_input_volume(struct sparcaudio_driver *drv)
{
	struct amd7930_info *info = (struct amd7930_info *)drv->private;

	return info->rgain;
}

static int amd7930_set_monitor_volume(struct sparcaudio_driver *drv, int vol)
{
	struct amd7930_info *info = (struct amd7930_info *)drv->private;

	info->mgain = vol;
	amd7930_update_map(drv);
	return 0;
}

static int amd7930_get_monitor_volume(struct sparcaudio_driver *drv)
{
      struct amd7930_info *info = (struct amd7930_info *)drv->private;

      return info->mgain;
}

/* Cheats. The amd has the minimum capabilities we support */
static int amd7930_get_output_balance(struct sparcaudio_driver *drv)
{
	return AUDIO_MID_BALANCE;
}

static int amd7930_get_input_balance(struct sparcaudio_driver *drv)
{
	return AUDIO_MID_BALANCE;
}

static int amd7930_get_output_channels(struct sparcaudio_driver *drv)
{
	return AUDIO_MIN_PLAY_CHANNELS;
}

static int amd7930_set_output_channels(struct sparcaudio_driver *drv, 
				       int value)
{
  return (value == AUDIO_MIN_PLAY_CHANNELS) ? 0 : -EINVAL;
}

static int amd7930_get_input_channels(struct sparcaudio_driver *drv)
{
	return AUDIO_MIN_REC_CHANNELS;
}

static int 
amd7930_set_input_channels(struct sparcaudio_driver *drv, int value)
{
  return (value == AUDIO_MIN_REC_CHANNELS) ? 0 : -EINVAL;
}

static int amd7930_get_output_precision(struct sparcaudio_driver *drv)
{
	return AUDIO_MIN_PLAY_PRECISION;
}

static int 
amd7930_set_output_precision(struct sparcaudio_driver *drv, int value)
{
  return (value == AUDIO_MIN_PLAY_PRECISION) ? 0 : -EINVAL;
}

static int amd7930_get_input_precision(struct sparcaudio_driver *drv)
{
	return AUDIO_MIN_REC_PRECISION;
}

static int 
amd7930_set_input_precision(struct sparcaudio_driver *drv, int value)
{
  return (value == AUDIO_MIN_REC_PRECISION) ? 0 : -EINVAL;
}

static int amd7930_get_output_port(struct sparcaudio_driver *drv)
{
  struct amd7930_info *info = (struct amd7930_info *)drv->private;
  if (info->map.mmr2 & AM_MAP_MMR2_LS)
    return AUDIO_SPEAKER; 
  return AUDIO_HEADPHONE;
}

static int amd7930_set_output_port(struct sparcaudio_driver *drv, int value)
{
  struct amd7930_info *info = (struct amd7930_info *)drv->private;
  switch (value) {
  case AUDIO_HEADPHONE:
    info->map.mmr2 &= ~AM_MAP_MMR2_LS;
    break;
  case AUDIO_SPEAKER:
    info->map.mmr2 |= AM_MAP_MMR2_LS;
    break;
  default:
    return -EINVAL;
  }
  amd7930_update_map(drv);
  return 0;
}

/* Only a microphone here, so no troubles */
static int amd7930_get_input_port(struct sparcaudio_driver *drv)
{
	return AUDIO_MICROPHONE;
}

static int amd7930_get_encoding(struct sparcaudio_driver *drv)
{
  struct amd7930_info *info = (struct amd7930_info *)drv->private;
  if (info->map.mmr1 & AM_MAP_MMR1_ALAW)
    return AUDIO_ENCODING_ALAW;
  return AUDIO_ENCODING_ULAW;
}

static int 
amd7930_set_encoding(struct sparcaudio_driver *drv, int value)
{
  struct amd7930_info *info = (struct amd7930_info *)drv->private;

  switch (value) {
  case AUDIO_ENCODING_ULAW:
    info->map.mmr1 &= ~AM_MAP_MMR1_ALAW;
    break;
  case AUDIO_ENCODING_ALAW:
    info->map.mmr1 |= AM_MAP_MMR1_ALAW;
    break;
  default:
    return -EINVAL;
  }
  amd7930_update_map(drv);
  return 0;
}

/* This is what you get. Take it or leave it */
static int amd7930_get_output_rate(struct sparcaudio_driver *drv)
{
	return AMD7930_RATE;
}

static int 
amd7930_set_output_rate(struct sparcaudio_driver *drv, int value)
{
  return (value == AMD7930_RATE) ? 0 : -EINVAL;
}

static int amd7930_get_input_rate(struct sparcaudio_driver *drv)
{
	return AMD7930_RATE;
}

static int
amd7930_set_input_rate(struct sparcaudio_driver *drv, int value)
{
  return (value == AMD7930_RATE) ? 0 : -EINVAL;
}

static int amd7930_get_output_muted(struct sparcaudio_driver *drv)
{
      return 0;
}

static void amd7930_loopback(struct sparcaudio_driver *drv, unsigned int value)
{
  struct amd7930_info *info = (struct amd7930_info *)drv->private;

  if (value)
    info->map.mmr1 |= AM_MAP_MMR1_LOOPBACK;
  else
    info->map.mmr1 &= ~AM_MAP_MMR1_LOOPBACK;
  amd7930_update_map(drv);
  return;
}

static int amd7930_ioctl(struct inode * inode, struct file * file,
                         unsigned int cmd, unsigned long arg, 
                         struct sparcaudio_driver *drv)
{
  int retval = 0;
  
  switch (cmd) {
  case AUDIO_DIAG_LOOPBACK:
    amd7930_loopback(drv, (unsigned int)arg);
    break;
  default:
    retval = -EINVAL;
  }

  return retval;
}


/*
 *       ISDN operations
 *
 * Many of these routines take an "int dev" argument, which is simply
 * an index into the drivers[] array.  Currently, we only support a
 * single AMD 7930 chip, so the value should always be 0.  B channel
 * operations require an "int chan", which should be 0 for channel B1
 * and 1 for channel B2
 *
 * int amd7930_get_irqnum(int dev)
 *
 *   returns the interrupt number being used by the chip.  ISDN4linux
 *   uses this number to watch the interrupt during initialization and
 *   make sure something is happening.
 *
 * int amd7930_get_liu_state(int dev)
 *
 *   returns the current state of the ISDN Line Interface Unit (LIU)
 *   as a number between 2 (state F2) and 7 (state F7).  0 may also be
 *   returned if the chip doesn't exist or the LIU hasn't been
 *   activated.  The meanings of the states are defined in I.430, ISDN
 *   BRI Physical Layer Interface.  The most important two states are
 *   F3 (shutdown) and F7 (syncronized).
 *
 * void amd7930_liu_init(int dev, void (*callback)(), void *callback_arg)
 *
 *   initializes the LIU and optionally registers a callback to be
 *   signaled upon a change of LIU state.  The callback will be called
 *   with a single opaque callback_arg Once the callback has been
 *   triggered, amd7930_get_liu_state can be used to determine the LIU
 *   current state.
 *
 * void amd7930_liu_activate(int dev, int priority)
 *
 *   requests LIU activation at a given D-channel priority.
 *   Successful activatation is achieved upon entering state F7, which
 *   will trigger any callback previously registered with
 *   amd7930_liu_init.
 *
 * void amd7930_liu_deactivate(int dev)
 *
 *   deactivates LIU.  Outstanding D and B channel transactions are
 *   terminated rudely and without callback notification.  LIU change
 *   of state callback will be triggered, however.
 *
 * void amd7930_dxmit(int dev, __u8 *buffer, unsigned int count,
 *               void (*callback)(void *, int), void *callback_arg)
 *
 *   transmits a packet - specified with buffer, count - over the D-channel
 *   interface.  Buffer should begin with the LAPD address field and
 *   end with the information field.  FCS and flag sequences should not
 *   be included, nor is bit-stuffing required - all these functions are
 *   performed by the chip.  The callback function will be called
 *   DURING THE TOP HALF OF AN INTERRUPT HANDLER and will be passed
 *   both the arbitrary callback_arg and an integer error indication:
 *
 *       0 - successful transmission; ready for next packet
 *   non-0 - error value from chip's DER (D-Channel Error Register):
 *       4 - collision detect
 *     128 - underrun; irq routine didn't service chip fast enough
 *
 *   The callback routine should defer any time-consuming operations
 *   to a bottom-half handler; however, amd7930_dxmit may be called
 *   from within the callback to request back-to-back transmission of
 *   a second packet (without repeating the priority/collision mechanism)
 *
 *   A comment about the "collision detect" error, which is signalled
 *   whenever the echoed D-channel data didn't match the transmitted
 *   data.  This is part of ISDN's normal multi-drop T-interface
 *   operation, indicating that another device has attempted simultaneous
 *   transmission, but can also result from line noise.  An immediate
 *   requeue via amd7930_dxmit is suggested, but repeated collision
 *   errors may indicate a more serious problem.
 *
 * void amd7930_drecv(int dev, __u8 *buffer, unsigned int size,
 *               void (*callback)(void *, int, unsigned int),
 *               void *callback_arg)
 *
 *   register a buffer - buffer, size - into which a D-channel packet
 *   can be received.  The callback function will be called DURING
 *   THE TOP HALF OF AN INTERRUPT HANDLER and will be passed an
 *   arbitrary callback_arg, an integer error indication and the length
 *   of the received packet, which will start with the address field,
 *   end with the information field, and not contain flag or FCS
 *   bytes.  Bit-stuffing will already have been corrected for.
 *   Possible values of second callback argument "error":
 *
 *       0 - successful reception
 *   non-0 - error value from chip's DER (D-Channel Error Register):
 *       1 - received packet abort
 *       2 - framing error; non-integer number of bytes received
 *       8 - FCS error; CRC sequence indicated corrupted data
 *      16 - overflow error; packet exceeded size of buffer
 *      32 - underflow error; packet smaller than required five bytes
 *      64 - overrun error; irq routine didn't service chip fast enough
 *
 * int amd7930_bopen(int dev, int chan, u_char xmit_idle_char)
 *
 *   This function should be called before any other operations on a B
 *   channel.  In addition to arranging for interrupt handling and
 *   channel multiplexing, it sets the xmit_idle_char which is
 *   transmitted on the interface when no data buffer is available.
 *   Suggested values are: 0 for ISDN audio; FF for HDLC mark idle; 7E
 *   for HDLC flag idle.  Returns 0 on a successful open; -1 on error,
 *   which is quite possible if audio and the other ISDN channel are
 *   already in use, since the Am7930 can only send two of the three
 *   channels to the processor
 *
 * void amd7930_bclose(int dev, int chan)
 *
 *   Shuts down a B channel when no longer in use.
 *
 * void amd7930_bxmit(int dev, int chan, __u8 *buffer, unsigned int count,
 *               void (*callback)(void *), void *callback_arg)
 *
 *   transmits a raw data block - specified with buffer, count - over
 *   the B channel interface specified by dev/chan.  The callback
 *   function will be called DURING THE TOP HALF OF AN INTERRUPT
 *   HANDLER and will be passed the arbitrary callback_arg
 *
 *   The callback routine should defer any time-consuming operations
 *   to a bottom-half handler; however, amd7930_bxmit may be called
 *   from within the callback to request back-to-back transmission of
 *   another data block
 *
 * void amd7930_brecv(int dev, int chan, __u8 *buffer, unsigned int size,
 *               void (*callback)(void *), void *callback_arg)
 *
 *   receive a raw data block - specified with buffer, size - over the
 *   B channel interface specified by dev/chan.  The callback function
 *   will be called DURING THE TOP HALF OF AN INTERRUPT HANDLER and
 *   will be passed the arbitrary callback_arg
 *
 *   The callback routine should defer any time-consuming operations
 *   to a bottom-half handler; however, amd7930_brecv may be called
 *   from within the callback to register another buffer and ensure
 *   continuous B channel reception without loss of data
 *
 */

#if defined (AMD79C30_ISDN) || defined (LINUX_VERSION_CODE) && LINUX_VERSION_CODE > 0x200ff 
static int amd7930_get_irqnum(int dev)
{
	struct amd7930_info *info;

	if (dev > num_drivers) {
		return(0);
	}

	info = (struct amd7930_info *) drivers[dev].private;

	return info->irq;
}

static int amd7930_get_liu_state(int dev)
{
	struct amd7930_info *info;

	if (dev > num_drivers) {
		return(0);
	}

	info = (struct amd7930_info *) drivers[dev].private;

	return info->liu_state;
}

static void amd7930_liu_init(int dev, void (*callback)(), void *callback_arg)
{
	struct amd7930_info *info;
	register unsigned long flags;

	if (dev > num_drivers) {
		return;
	}

	info = (struct amd7930_info *) drivers[dev].private;

	save_and_cli(flags);

	/* Set callback for LIU state change */
        info->liu_callback = callback;
	info->liu_callback_arg = callback_arg;

	/* De-activate the ISDN Line Interface Unit (LIU) */
	info->regs->cr = AMR_LIU_LMR1;
	info->regs->dr = 0;

	/* Request interrupt when LIU changes state from/to F3/F7/F8 */
	info->regs->cr = AMR_LIU_LMR2;
	info->regs->dr = AM_LIU_LMR2_EN_F3_INT |
          AM_LIU_LMR2_EN_F7_INT | AM_LIU_LMR2_EN_F8_INT;

	/* amd7930_enable_ints(info); */

	/* Activate the ISDN Line Interface Unit (LIU) */
	info->regs->cr = AMR_LIU_LMR1;
	info->regs->dr = AM_LIU_LMR1_LIU_ENABL;

	restore_flags(flags);
}

static void amd7930_liu_activate(int dev, int priority)
{
	struct amd7930_info *info;
	register unsigned long flags;

	if (dev > num_drivers) {
		return;
	}

	info = (struct amd7930_info *) drivers[dev].private;

	save_and_cli(flags);

        /* Set D-channel access priority
         *
         * I.430 defines a priority mechanism based on counting 1s
         * in the echo channel before transmitting
         *
         * Priority 0 is eight 1s; priority 1 is ten 1s; etc
         */

        info->regs->cr = AMR_LIU_LPR;
        info->regs->dr = priority & 0x0f;

	/* request LIU activation */

	info->regs->cr = AMR_LIU_LMR1;
	info->regs->dr = AM_LIU_LMR1_LIU_ENABL | AM_LIU_LMR1_REQ_ACTIV;

	restore_flags(flags);
}

static void amd7930_liu_deactivate(int dev)
{
	struct amd7930_info *info;
	register unsigned long flags;

	if (dev > num_drivers) {
		return;
	}

	info = (struct amd7930_info *) drivers[dev].private;

	save_and_cli(flags);

	/* deactivate LIU */

	info->regs->cr = AMR_LIU_LMR1;
	info->regs->dr = 0;

	restore_flags(flags);
}

static void amd7930_dxmit(int dev, __u8 *buffer, unsigned int count,
			  void (*callback)(void *, int), void *callback_arg)
{
	struct amd7930_info *info;
	register unsigned long flags;
	__u8 dmr1;

	if (dev > num_drivers) {
		return;
	}

	info = (struct amd7930_info *) drivers[dev].private;

	save_and_cli(flags);

	if (info->D.output_ptr) {
		restore_flags(flags);
		printk("amd7930_dxmit: transmitter in use\n");
		return;
	}

	info->D.output_ptr = buffer;
	info->D.output_count = count;
	info->D.output_callback = callback;
	info->D.output_callback_arg = callback_arg;

	/* Enable D-channel Transmit Threshold interrupt; disable addressing */
	info->regs->cr = AMR_DLC_DMR1;
	dmr1 = info->regs->dr;
	dmr1 |= AMR_DLC_DMR1_DTTHRSH_INT;
	dmr1 &= ~AMR_DLC_DMR1_EN_ADDRS;
	info->regs->dr = dmr1;

	/* Begin xmit by setting D-channel Transmit Byte Count Reg (DTCR) */
	info->regs->cr = AMR_DLC_DTCR;
	info->regs->dr = count & 0xff;
	info->regs->dr = (count >> 8) & 0xff;

	/* Prime xmit FIFO */
	/* fill_D_xmit_fifo(info); */
	transceive_Dchannel(info);

	restore_flags(flags);
}

static void amd7930_drecv(int dev, __u8 *buffer, unsigned int size,
			  void (*callback)(void *, int, unsigned int),
			  void *callback_arg)
{
	struct amd7930_info *info;
	register unsigned long flags;
	__u8 dmr1;

	if (dev > num_drivers) {
		return;
	}

	info = (struct amd7930_info *) drivers[dev].private;

	save_and_cli(flags);

	if (info->D.input_ptr) {
		restore_flags(flags);
		printk("amd7930_drecv: receiver already has buffer!\n");
		return;
	}

	info->D.input_ptr = buffer;
	info->D.input_count = 0;
	info->D.input_limit = size;
	info->D.input_callback = callback;
	info->D.input_callback_arg = callback_arg;

	/* Enable D-channel Receive Threshold interrupt;
	 * Enable D-channel End of Receive Packet interrupt;
	 * Disable address recognition
	 */
	info->regs->cr = AMR_DLC_DMR1;
	dmr1 = info->regs->dr;
	dmr1 |= AMR_DLC_DMR1_DRTHRSH_INT | AMR_DLC_DMR1_EORP_INT;
	dmr1 &= ~AMR_DLC_DMR1_EN_ADDRS;
	info->regs->dr = dmr1;

	/* Set D-channel Receive Byte Count Limit Register */
	info->regs->cr = AMR_DLC_DRCR;
	info->regs->dr = size & 0xff;
	info->regs->dr = (size >> 8) & 0xff;

	restore_flags(flags);
}

static int amd7930_bopen(int dev, unsigned int chan, 
                         int mode, u_char xmit_idle_char)
{
	struct amd7930_info *info;
	register unsigned long flags;

	if (dev > num_drivers || chan<0 || chan>1) {
		return -1;
	}

         if (mode == L1_MODE_HDLC) {
                 return -1;
         }
 
	info = (struct amd7930_info *) drivers[dev].private;

	save_and_cli(flags);

	if (info->Bb.channel_status == CHANNEL_AVAILABLE) {

		info->Bb.channel_status = CHANNEL_INUSE;
		info->Bb.xmit_idle_char = xmit_idle_char;
		info->Bisdn[chan] = &info->Bb;

		/* Multiplexor map - isdn (B1/2) to Bb */
		info->regs->cr = AMR_MUX_MCR2 + chan;
		info->regs->dr = (AM_MUX_CHANNEL_B1 + chan) |
				 (AM_MUX_CHANNEL_Bb << 4);

	} else if (info->Bc.channel_status == CHANNEL_AVAILABLE) {

		info->Bc.channel_status = CHANNEL_INUSE;
		info->Bc.xmit_idle_char = xmit_idle_char;
		info->Bisdn[chan] = &info->Bc;

		/* Multiplexor map - isdn (B1/2) to Bc */
		info->regs->cr = AMR_MUX_MCR2 + chan;
		info->regs->dr = (AM_MUX_CHANNEL_B1 + chan) |
				 (AM_MUX_CHANNEL_Bc << 4);

	} else {
		restore_flags(flags);
		return (-1);
	}

	/* Enable B channel transmit */
	info->regs->cr = AMR_LIU_LMR1;
	info->regs->dr |= AM_LIU_LMR1_B1_ENABL + chan;

	/* Enable B channel interrupts */
	info->regs->cr = AMR_MUX_MCR4;
	info->regs->dr = AM_MUX_MCR4_ENABLE_INTS | AM_MUX_MCR4_REVERSE_Bb | AM_MUX_MCR4_REVERSE_Bc;

	restore_flags(flags);
	return 0;
}

static void amd7930_bclose(int dev, unsigned int chan)
{
	struct amd7930_info *info;
	register unsigned long flags;

	if (dev > num_drivers || chan<0 || chan>1) {
		return;
	}

	info = (struct amd7930_info *) drivers[dev].private;

	save_and_cli(flags);

	if (info->Bisdn[chan]) {
		info->Bisdn[chan]->channel_status = CHANNEL_AVAILABLE;
		info->regs->cr = AMR_MUX_MCR2 + chan;
		info->regs->dr = 0;
		info->Bisdn[chan] = NULL;

		/* Disable B channel transmit */
		info->regs->cr = AMR_LIU_LMR1;
		info->regs->dr &= ~(AM_LIU_LMR1_B1_ENABL + chan);

		if (info->Bb.channel_status == CHANNEL_AVAILABLE &&
		    info->Bc.channel_status == CHANNEL_AVAILABLE) {

			/* Disable B channel interrupts */
			info->regs->cr = AMR_MUX_MCR4;
			info->regs->dr = 0;
		}
	}

	restore_flags(flags);
}

static void amd7930_bxmit(int dev, unsigned int chan,
                          __u8 * buffer, unsigned long count,
			  void (*callback)(void *, int), void *callback_arg)
{
	struct amd7930_info *info;
	struct amd7930_channel *Bchan;
	register unsigned long flags;

	if (dev > num_drivers) {
		return;
	}

	info = (struct amd7930_info *) drivers[dev].private;
	Bchan = info->Bisdn[chan];

	if (Bchan) {
		save_and_cli(flags);

		Bchan->output_ptr = buffer;
		Bchan->output_count = count;
	        Bchan->output_callback = (void *) callback;
        	Bchan->output_callback_arg = callback_arg;

		restore_flags(flags);
	}
}

static void amd7930_brecv(int dev, unsigned int chan, 
                          __u8 * buffer, unsigned long size,
                          void (*callback)(void *, int, unsigned int),
                          void *callback_arg)
{
	struct amd7930_info *info;
	struct amd7930_channel *Bchan;
	register unsigned long flags;

	if (dev > num_drivers) {
		return;
	}

	info = (struct amd7930_info *) drivers[dev].private;
	Bchan = info->Bisdn[chan];

	if (Bchan) {
		save_and_cli(flags);

		Bchan->input_ptr = buffer;
		Bchan->input_count = size;
		Bchan->input_callback = (void *) callback;
		Bchan->input_callback_arg = callback_arg;

		restore_flags(flags);
	}
}

struct foreign_interface amd7930_foreign_interface = {
        amd7930_get_irqnum,
        amd7930_get_liu_state,
        amd7930_liu_init,
        amd7930_liu_activate,
        amd7930_liu_deactivate,
        amd7930_dxmit,
        amd7930_drecv,
        amd7930_bopen,
        amd7930_bclose,
        amd7930_bxmit,
        amd7930_brecv
};
EXPORT_SYMBOL(amd7930_foreign_interface);
#endif


/*
 *	Device detection and initialization.
 */

static struct sparcaudio_operations amd7930_ops = {
	amd7930_open,
	amd7930_release,
	amd7930_ioctl,
	amd7930_start_output,
	amd7930_stop_output,
	amd7930_start_input,
	amd7930_stop_input,
	amd7930_sunaudio_getdev,
	amd7930_set_output_volume,
	amd7930_get_output_volume,
	amd7930_set_input_volume,
	amd7930_get_input_volume,
	amd7930_set_monitor_volume,
	amd7930_get_monitor_volume,
	NULL,			/* amd7930_set_output_balance */
	amd7930_get_output_balance,
	NULL,			/* amd7930_set_input_balance */
	amd7930_get_input_balance,
	amd7930_set_output_channels,
	amd7930_get_output_channels,
	amd7930_set_input_channels,
	amd7930_get_input_channels,
	amd7930_set_output_precision,
	amd7930_get_output_precision,
	amd7930_set_input_precision,
	amd7930_get_input_precision,
	amd7930_set_output_port,
	amd7930_get_output_port,
	NULL,			/* amd7930_set_input_port */
	amd7930_get_input_port,
	amd7930_set_encoding,
	amd7930_get_encoding,
	amd7930_set_encoding,
	amd7930_get_encoding,
	amd7930_set_output_rate,
	amd7930_get_output_rate,
	amd7930_set_input_rate,
	amd7930_get_input_rate,
	amd7930_sunaudio_getdev_sunos,
	amd7930_get_output_ports,
	amd7930_get_input_ports,
	NULL,                    /* amd7930_set_output_muted */
	amd7930_get_output_muted,
        NULL,                   /* amd7930_set_output_pause */
        NULL,                   /* amd7930_get_output_pause */
        NULL,                   /* amd7930_set_input_pause */
        NULL,                   /* amd7930_get_input_pause */
        NULL,                   /* amd7930_set_output_samples */
        NULL,                   /* amd7930_get_output_samples */
        NULL,                   /* amd7930_set_input_samples */
        NULL,                   /* amd7930_get_input_samples */
        NULL,                   /* amd7930_set_output_error */
        NULL,                   /* amd7930_get_output_error */
        NULL,                   /* amd7930_set_input_error */
        NULL,                   /* amd7930_get_input_error */
        amd7930_get_formats,
};

/* Attach to an amd7930 chip given its PROM node. */
static int amd7930_attach(struct sparcaudio_driver *drv, int node,
			  struct linux_sbus *sbus, struct linux_sbus_device *sdev)
{
	struct linux_prom_registers regs;
	struct linux_prom_irqs irq;
	struct amd7930_info *info;
	int err;

	/* Allocate our private information structure. */
	drv->private = kmalloc(sizeof(struct amd7930_info), GFP_KERNEL);
	if (!drv->private)
		return -ENOMEM;

	/* Point at the information structure and initialize it. */
	drv->ops = &amd7930_ops;
	info = (struct amd7930_info *)drv->private;
	memset(info, 0, sizeof(*info));
	info->ints_on = 1; /* force disable below */

	drv->dev = sdev;

	/* Map the registers into memory. */
	prom_getproperty(node, "reg", (char *)&regs, sizeof(regs));
	if (sbus && sdev)
		prom_apply_sbus_ranges(sbus, &regs, 1, sdev);
	info->regs_size = regs.reg_size;
	info->regs = sparc_alloc_io(regs.phys_addr, 0, regs.reg_size,
					   "amd7930", regs.which_io, 0);
	if (!info->regs) {
		printk(KERN_ERR "amd7930: could not allocate registers\n");
		kfree(drv->private);
		return -EIO;
	}

	/* Put amd7930 in idle mode (interrupts disabled) */
	amd7930_idle(info);

	/* Enable extended FIFO operation on D-channel */
	info->regs->cr = AMR_DLC_EFCR;
	info->regs->dr = AMR_DLC_EFCR_EXTEND_FIFO;
	info->regs->cr = AMR_DLC_DMR4;
	info->regs->dr = /* AMR_DLC_DMR4_RCV_30 | */ AMR_DLC_DMR4_XMT_14;

	/* Attach the interrupt handler to the audio interrupt. */
	prom_getproperty(node, "intr", (char *)&irq, sizeof(irq));
	info->irq = irq.pri;
	request_irq(info->irq, amd7930_interrupt,
		    SA_INTERRUPT, "amd7930", drv);
	enable_irq(info->irq);
	amd7930_enable_ints(info);

	/* Initalize the local copy of the MAP registers. */
	memset(&info->map, 0, sizeof(info->map));
	info->map.mmr1 = AM_MAP_MMR1_GX | AM_MAP_MMR1_GER
			 | AM_MAP_MMR1_GR | AM_MAP_MMR1_STG;
        /* Start out with speaker, microphone */
        info->map.mmr2 |= (AM_MAP_MMR2_LS | AM_MAP_MMR2_AINB);

	/* Set the default audio parameters. */
	info->rgain = 128;
	info->pgain = 200;
	info->mgain = 0;
	amd7930_update_map(drv);

	/* Register the amd7930 with the midlevel audio driver. */
	err = register_sparcaudio_driver(drv, 1);
	if (err < 0) {
		printk(KERN_ERR "amd7930: unable to register\n");
		disable_irq(info->irq);
		free_irq(info->irq, drv);
		sparc_free_io(info->regs, info->regs_size);
		kfree(drv->private);
		return -EIO;
	}

	/* Announce the hardware to the user. */
	printk(KERN_INFO "amd7930 at 0x%lx irq %d\n",
		(unsigned long)info->regs, info->irq);

	/* Success! */
	return 0;
}

#ifdef MODULE
/* Detach from an amd7930 chip given the device structure. */
static void amd7930_detach(struct sparcaudio_driver *drv)
{
	struct amd7930_info *info = (struct amd7930_info *)drv->private;

	unregister_sparcaudio_driver(drv, 1);
	amd7930_idle(info);
	disable_irq(info->irq);
	free_irq(info->irq, drv);
	sparc_free_io(info->regs, info->regs_size);
	kfree(drv->private);
}
#endif

/* Probe for the amd7930 chip and then attach the driver. */
#ifdef MODULE
int init_module(void)
#else
__initfunc(int amd7930_init(void))
#endif
{
	struct linux_sbus *bus;
	struct linux_sbus_device *sdev;
	int node;

	/* Try to find the sun4c "audio" node first. */
	node = prom_getchild(prom_root_node);
	node = prom_searchsiblings(node, "audio");
	if (node && amd7930_attach(&drivers[0], node, NULL, NULL) == 0)
		num_drivers = 1;
	else
		num_drivers = 0;

	/* Probe each SBUS for amd7930 chips. */
	for_all_sbusdev(sdev,bus) {
		if (!strcmp(sdev->prom_name, "audio")) {
			/* Don't go over the max number of drivers. */
			if (num_drivers >= MAX_DRIVERS)
				continue;

			if (amd7930_attach(&drivers[num_drivers],
					   sdev->prom_node, sdev->my_bus, sdev) == 0)
				num_drivers++;
		}
	}

	/* Only return success if we found some amd7930 chips. */
	return (num_drivers > 0) ? 0 : -EIO;
}

#ifdef MODULE
void cleanup_module(void)
{
	register int i;

	for (i = 0; i < num_drivers; i++) {
		amd7930_detach(&drivers[i]);
		num_drivers--;
	}
}
#endif