summaryrefslogtreecommitdiffstats
path: root/drivers/sgi/audio/hal2.c
blob: da4ac9c6d58f9303a0618f9bd48fc60f0e7e5238 (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
/* $Id: hal2.c,v 1.8 1999/01/28 01:01:42 ulfc Exp $
 * 
 * drivers/sgi/audio/hal2.c
 *
 * Copyright (C) 1998-1999 Ulf Carlsson (ulfc@bun.falkenberg.se)
 * 
 */

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/malloc.h>
#include <linux/string.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>

#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/page.h>
#include <asm/io.h>
#include <linux/errno.h>

#include <linux/soundcard.h>
#include <asm/sgihpc.h>
#include <asm/indy_pbus.h>

#include "hal2.h"

#define DEBUG

struct hal2_buffer {
	struct hpc_dma_desc desc;

	struct hal2_buffer *next;
	unsigned long buf;		/* pointer in KSEG1 address space */
};

/* As you might know, dma descriptors must be 8 byte aligned, that's why we have
 * them in this special way.
 */

struct hal2_channel {
	struct hal2_buffer *ring;

	int pbus;
	int stereo;
	int little_end;
	int bres_mod;
	int bres_master;		/* 0=48.0k 1=44.1k */

	enum {DAC, ADC} type;

	int pbus_requested;
	int pbus_enabled;
	int hal2_enabled;

	int bufs;
	int free_bufs;
};

struct hal2_private {
	struct hal2_buffer *dac_head;
	struct hal2_buffer *adc_tail;

	int dac_underrun;

	unsigned long empty_buf;	/* in case of underrun */

	struct hal2_channel dac_chan;
	struct hal2_channel adc_chan;
};

struct sgiaudio_chan {
	int started;
	int rp;
	int wp;
	void *buf;
	int bufsz;			/* multiple of PAGE_SIZE */

	struct sgiaudio_chan_ops *ops;

	struct wait_queue *queue;
};

struct sgiaudio {
	struct sgiaudio_chan dac;
	struct sgiaudio_chan adc;

	void *private;			/* for internal hal2 usage .. */
};

struct sgiaudio_chan_ops {
	int (*init) (struct sgiaudio *);
	int (*stereo) (struct sgiaudio *, int);
	int (*endian) (struct sgiaudio *, int);
	int (*freq) (struct sgiaudio *, double);
	int (*configure) (struct sgiaudio *);
	void (*start) (struct sgiaudio *);
	void (*stop) (struct sgiaudio *);
	void (*free) (struct sgiaudio *);
};

#if 0
#define INDIRECT_WAIT(regs) while(regs->isr & H2_ISR_TSTATUS);
#endif

#define INDIRECT_WAIT(regs)						\
{									\
	int cnt = 1000;							\
	udelay(200);							\
	printk("hal2: waiting isr:%04hx ", regs->isr);			\
	udelay(200);							\
	printk("idr0:%04hx idr1:%04hx idr2:%04hx idr3:%04hx\n",		\
	       regs->idr0, regs->idr1, regs->idr2, regs->idr3);		\
									\
	while(regs->isr & H2_ISR_TSTATUS && --cnt)			\
		udelay(200);						\
	if (!cnt)							\
		printk("hal2: failed waiting for indirect trans.\n");	\
									\
	printk("hal2: finished waiting at cnt:%d isr:%04hx ",		\
	       cnt, regs->isr);						\
	printk("idr0:%04hx idr1:%04hx idr2:%04hx idr3:%04hx\n",		\
	       regs->idr0, regs->idr1, regs->idr2, regs->idr3);		\
	udelay(200);							\
}									\

static unsigned short ireg_read(unsigned short address)
{
	unsigned short tmp;

	udelay(200);
	h2_ctrl->iar = address;
	INDIRECT_WAIT(h2_ctrl)
	tmp = h2_ctrl->idr0;
	return tmp;
}

static void ireg_write(unsigned short address, unsigned short val)
{
	udelay(200);
	h2_ctrl->idr0 = val;
	udelay(200);
	h2_ctrl->iar = address;
	INDIRECT_WAIT(h2_ctrl)
}

static void ireg_write2(unsigned short address, unsigned short val0, unsigned
			short val1)
{
	udelay(200);
	h2_ctrl->idr0 = val0;
	udelay(200);
	h2_ctrl->idr1 = val1;
	udelay(200);
	h2_ctrl->iar = address;
	INDIRECT_WAIT(h2_ctrl)
}

static void ireg_write4(unsigned short address, unsigned short val0, unsigned
			short val1, unsigned short val2, unsigned short val3)
{
	udelay(200);
	h2_ctrl->idr0 = val0;
	udelay(200);
	h2_ctrl->idr1 = val1;
	udelay(200);
	h2_ctrl->idr2 = val2;
	udelay(200);
	h2_ctrl->idr3 = val3;
	udelay(200);
	h2_ctrl->iar = address;
	INDIRECT_WAIT(h2_ctrl)
}

static void ireg_setbit(unsigned short write_address, unsigned short
			read_address, int bit)
{
	int tmp;

	h2_ctrl->iar = read_address;
	INDIRECT_WAIT(h2_ctrl);
	tmp = h2_ctrl->idr0;
	h2_ctrl->idr0 = tmp | bit;
	udelay(200);
	h2_ctrl->iar = write_address;
	INDIRECT_WAIT(h2_ctrl);
}

static void ireg_clearbit(unsigned short write_address, unsigned short
			  read_address, int bit)
{
	unsigned short tmp;

	h2_ctrl->iar = read_address;
	INDIRECT_WAIT(h2_ctrl);
	tmp = h2_ctrl->idr0;
	h2_ctrl->idr0 = tmp & ~bit;
	udelay(200);
	h2_ctrl->iar = write_address;
	INDIRECT_WAIT(h2_ctrl);
}

static void hal2_reset(void)
{
	printk("resetting global isr:%04hx\n", h2_ctrl->isr);
	udelay(200);
	h2_ctrl->isr = 0;		/* reset the card */
	udelay(200);
	printk("reset done isr:%04hx\n", h2_ctrl->isr);
	udelay(200);
	h2_ctrl->isr = H2_ISR_GLOBAL_RESET_N | H2_ISR_CODEC_RESET_N;
	udelay(200);
	printk("reactivation done isr:%04hx\n", h2_ctrl->isr);
}

static int hal2_probe(void)
{
	unsigned short board, major, minor;

	unsigned short tmp;

	hal2_reset();

	if (h2_ctrl->rev & H2_REV_AUDIO_PRESENT) {

		printk("hal2: there was no device?\n");
		return -ENODEV;
	}
	
	board = (h2_ctrl->rev & H2_REV_BOARD_M) >> 12;
	major = (h2_ctrl->rev & H2_REV_MAJOR_CHIP_M) >> 4;
	minor = (h2_ctrl->rev & H2_REV_MINOR_CHIP_M);

	printk("SGI HAL2 Processor, Revision %i.%i.%i\n",
	       board, major, minor);

	if (board != 4 || major != 1 || minor != 0) {
		printk("hal2: Other revision than 4.1.0 detected\n");
		printk("hal2: Your card is probably not supported\n");
	}

#ifdef DEBUG
	printk("hal2: checking registers\n");
#endif

	/* check that the indirect registers are working by writing some bogus
	 * stuff and then reading it back */

	ireg_write(H2IW_DAC_C1, 0x123);			/* 16 bit register */
	printk("hal2: wrote #1\n");
	ireg_write2(H2IW_BRES2_C2, 0x132, 0x231);	/* 32 bit register */
	printk("hal2: wrote #2\n");

	if ((tmp = ireg_read(H2IR_DAC_C1)) != 0x123) {
		printk("hal2: Didn't pass register check #1 (%04hx)\n", tmp);
		return -ENODEV;
	}
	printk("hal2: read #1\n");
	if ((tmp = ireg_read(H2IR_BRES2_C2_0)) != 0x132) {
		printk("hal2: Didn't pass register check #2 (%04hx)\n", tmp);
		return -ENODEV;
	}
	printk("hal2: read #2\n");
	if ((tmp = ireg_read(H2IR_BRES2_C2_1)) != 0x231) {
		printk("hal2: Didn't pass register check #3 (%04hx)\n", tmp);
		return -ENODEV;
	}
	printk("hal2: read #3\n");

#ifdef DEBUG
	printk("hal2: card found\n");
#endif

	return 0;
}

static int hal2_init(struct sgiaudio *sa)
{
	static int initialized = 0;

	if (initialized) {
		printk("hal2_init: already initialized?\n");
		goto out;
	}

	if (!sa->private) {
#ifdef DEBUG
		printk("hal2: allocating private hal2 pointer\n");
#endif
		sa->private = kmalloc(sizeof(struct hal2_private), GFP_KERNEL);
		if (!sa->private)
			return -ENOMEM;
		memset(sa->private, 0, sizeof(struct hal2_private));
	}

#ifdef DEBUG
	printk("hal2: resetting chip\n");
#endif

	hal2_reset();

	/* setup indigo codec mode */
	h2_ctrl->isr &= ~H2_ISR_CODEC_MODE;
	printk("hal2 init done..\n");

	initialized = 1;
out:
	return 0;
}

static int hal2_setup_ring(struct hal2_private *hp, struct hal2_channel *chan)
{
	struct hal2_buffer *hbuf;
	unsigned long buffer;
	int j = 0;

	/* FIXME: Clear up the memory leaks here */

	chan->ring = kmalloc(sizeof (struct hal2_buffer), GFP_KERNEL);
	if (!chan->ring)
		return -ENOMEM;

	hbuf = chan->ring;
	buffer = get_free_page(GFP_KERNEL);
	if (!buffer)
		return -ENOMEM;

	hbuf->desc.pbuf = PHYSADDR(buffer);
	hbuf->buf       = KSEG1ADDR(buffer);

	while (j++ < chan->bufs - 1) {
		hbuf->next = kmalloc(sizeof (struct hal2_buffer), GFP_KERNEL);
		if (!chan->ring)
			return -ENOMEM;

		buffer = get_free_page(GFP_KERNEL);
		if (!buffer)
			return -ENOMEM;

		/* we have to link the physical DMA buffers with phyical
		 * addresses first so that the HPC3 knows what to do */

		hbuf->desc.pnext = PHYSADDR(buffer);

		hbuf = hbuf->next;
		hbuf->buf       = KSEG1ADDR(buffer);
		hbuf->desc.pbuf = PHYSADDR(buffer);
	}

	hbuf->next = chan->ring;
	hbuf->desc.pnext = chan->ring->desc.pbuf;

	return 0;
}

static int hal2_init_dac(struct sgiaudio *sa)
{
	struct hal2_private *hp;
	struct hal2_channel *chan;
	int err;

	printk("initializing dac\n");
	err = hal2_init(sa);
	if (err)
		return err;

	hp = (struct hal2_private *) sa->private;
	chan = &hp->dac_chan;

	err = hal2_setup_ring(hp, chan);
	if (err)
		return err;

	hp->adc_tail = chan->ring;

	return 0;
}

static int hal2_init_adc(struct sgiaudio *sa)
{
	struct hal2_private *hp;
	struct hal2_channel *chan;
	int err;

	printk("initializing adc\n");

	err = hal2_init(sa);
	if (err)
		return err;

	hp = (struct hal2_private *) sa->private;
	chan = &hp->dac_chan;

	err = hal2_setup_ring(hp, chan);
	if (err)
		return err;

	hp->dac_head = chan->ring;

	return 0;
}

static __inline__ int hal2_sample_size(struct hal2_channel *chan)
{
	return (chan->stereo ? 4 : 2);
}

static int hal2_setup_pbus(struct hal2_private *hp, struct hal2_channel *chan)
{
	int sample_sz, highwater, fifosize;
	unsigned long dmacfg;
	unsigned long flags;
	char *device_id;
	char *s1 = "hal2 ";
	char *s2 = (chan->type == ADC ? "adc" : "dac");
	int i;
	int err;

	sample_sz = hal2_sample_size(chan);	/* bytes/sample */

	/* an audio fifo should be 4 samples long and doubleword aligned,
	   highwater should be half the fifo size */

	fifosize  = (sample_sz * 4) >> 3;	/* doublewords */
	highwater = (sample_sz * 2) >> 1;	/* halfwords */

	device_id = (char *) kmalloc(strlen(s1) + strlen(s2), GFP_KERNEL);
	if (!device_id)
		return -ENOMEM;

	device_id[0] = 0;

	strcat(device_id, s1);
	strcat(device_id, s2);

	if (!chan->pbus_requested) {
		for (i = 0; i < MAX_PBUS_CHANNELS; i++) {
			if(sgipbus_request(i, device_id) == 0) {
				chan->pbus = i;
				chan->pbus_requested = 1;
				printk("got pbus %d\n", chan->pbus);
				break;
			}
		}

		if (i == MAX_PBUS_CHANNELS)
			return -EBUSY;
	}

	flags = HPC3_PDMACTRL_RT | (chan->type==ADC ? HPC3_PDMACTRL_RCV : 0) |
		(chan->little_end ? HPC3_PDMACTRL_SEL : 0);
	dmacfg = 0x8248844; /* realtime, 16-bit, cycles to spend etc.. */

	err = sgipbus_setup(chan->pbus, fifosize, highwater, flags, dmacfg);
	if (err)
		return err;

	return 0;
}

static void hal2_update_dac_buf(struct sgiaudio *sa, struct hal2_private *hp)
{
	int rp = sa->dac.rp;
	int wp = sa->dac.wp;
	int bufsz = sa->dac.bufsz;
	struct hal2_buffer *hbuf = hp->dac_head;
	struct hal2_channel *chan = &hp->dac_chan;
	int left;
	int count = 0;

	printk("updating dac buffer!\n");

	if (!hbuf) {
		printk("Oops, ADC head wasn't set..\n");
		return;
	}

	/* bytes left to read in the fifo */
	left = (wp - rp + bufsz) % bufsz;

	if (hp->dac_underrun) {
		if (left < hal2_sample_size(&hp->dac_chan)) {
			/* we're still in underrun... */
			return;
		}
		else {
			/* new enforcements have arrived, go back to normal
			 * ring buffer behavior */
			hbuf->desc.pbuf  = PHYSADDR(hbuf->buf);
			hbuf->desc.pnext = PHYSADDR(hbuf->next->buf);

			hbuf = hbuf->next;
		}
	}

	/* oops, a newly discovered underrun .. can this happen? */
	if (left < hal2_sample_size(&hp->dac_chan))
		goto out;

	if (chan->free_bufs < 1) {
		printk("Oops, lost DAC buffer count!\n");
		return;
	} else
		chan->free_bufs--;

	count = (left>PAGE_SIZE)?PAGE_SIZE:left;

	if (rp + count < bufsz)
		memcpy((void *) hbuf->buf, sa->dac.buf + rp, count);
	else {
		/* crosses the buffer boundary */
		int first = bufsz - rp - 1;

		memcpy((void *) hbuf->buf, sa->dac.buf + rp, first);
		memcpy((void *) hbuf->buf + first, sa->dac.buf, count - first);
	}

	sa->dac.rp = (rp + count) % bufsz;
	hbuf->desc.cntinfo = HPCDMA_XIE | (count & HPCDMA_BCNT);

	hbuf = hbuf->next;

	wake_up_interruptible(&sa->dac.queue);
out:

	/* was this the last buffer before underrun? */
	if (left - count < hal2_sample_size(&hp->dac_chan)) {
		/* ok, loop the empty buf.. */
		hbuf = hbuf->next;
		hbuf->desc.pnext = hbuf->desc.pbuf = PHYSADDR(hp->empty_buf);
		hbuf->desc.cntinfo = (PAGE_SIZE & HPCDMA_BCNT);

		hp->dac_underrun = 1;
	}
	hp->dac_head = hbuf;
}

static void hal2_update_adc_buf(struct sgiaudio *sa, struct hal2_private *hp)
{
	int rp = sa->adc.rp;
	int wp = sa->adc.wp;
	int bufsz = sa->adc.bufsz;
	struct hal2_buffer *hbuf = hp->dac_head;
	struct hal2_channel *chan = &hp->dac_chan;
	int left;
	int count;

	if (!hbuf) {
		printk("Oops, ADC head wasn't set..\n");
		return;
	}

	/* bytes left to write in the fifo */
	left = bufsz - (wp - rp + bufsz) % bufsz;

	if (chan->free_bufs >= chan->bufs) {
		printk("Oops, lost ADC buffer count!\n");
		return;
	} else
		chan->free_bufs++;

	/* check overrun */
	if (left < PAGE_SIZE)
		return;		/* XXX some better way? */

	count = PAGE_SIZE;

	memcpy(sa->adc.buf + wp, (void *) hbuf->buf, count);
	sa->adc.wp = (wp + count) % bufsz;

	wake_up_interruptible(&sa->adc.queue);
}

static void hal2_reset_adc_ring(struct hal2_private *hp, struct hal2_channel
				*chan)
{
	struct hal2_buffer *first, *hbuf; 

	first = chan->ring;

	for (hbuf = first; hbuf->next != first; hbuf = hbuf->next)
		hbuf->desc.cntinfo = HPCDMA_XIE;
}

static void hal2_enable_port(struct hal2_private *hp, struct hal2_channel *chan)
{
	if (!chan->hal2_enabled) {
		/* pick a dma port */
		ireg_setbit(H2IW_DMA_DRV, H2IR_DMA_DRV, (1 << chan->pbus));

		/* activate it! */
		switch (chan->type) {
		case DAC:
			ireg_setbit(H2IW_DMA_PORT_EN, H2IR_DMA_PORT_EN,
				    H2I_DMA_PORT_EN_CODECTX);
			break;
		case ADC:
			ireg_setbit(H2IW_DMA_PORT_EN, H2IR_DMA_PORT_EN,
				    H2I_DMA_PORT_EN_CODECR);
			break;
		}
		chan->hal2_enabled = 1;
	}
}

static void hal2_disable_port(struct hal2_private *hp,
			      struct hal2_channel *chan)
{
	if (chan->hal2_enabled) {
		switch (chan->type) {
		case DAC:
			ireg_clearbit(H2IW_DMA_PORT_EN, H2IR_DMA_PORT_EN,
				      H2I_DMA_PORT_EN_CODECTX);
			break;
		case ADC:
			ireg_clearbit(H2IW_DMA_PORT_EN, H2IR_DMA_PORT_EN,
				      H2I_DMA_PORT_EN_CODECR);
			break;
		}
		chan->hal2_enabled = 0;
	}
}

/* We have three bresenham clock generators, which we can use independantly.
 *
 * There is one 44.1k and one 48.0k master clock for each of them. We can
 * adjust the inc and the mod values for those clocks, and thus reduce the
 * frequency.
 * 
 * freq = master_clock * (inc / mod); where (inc / mod) is a positive fraction
 * in range [0,1]. Inc should always be set to 4 for codecs
 */
static __inline__ int hal2_calc_mod(int *mod, int master, double freq)
{
	double master_freq = (master == 0 ? 44800 : 44100);
	int tmp;

	tmp = (4.0 * master_freq) / freq;
	if (tmp > 65536 || tmp < 4)
		return -EINVAL;

	*mod = tmp;

	return 0;
}

static __inline__ double hal2_calc_freq(double *freq, int master, int mod)
{
	double master_freq = (master == 0 ? 44800 : 44100);
	double tmp;

	tmp = (4.0 * master_freq) / mod;
	if (tmp < 0 || tmp > 48000)
		return -EINVAL;

	*freq = tmp;

	return 0;
}

static int hal2_set_freq(struct hal2_channel *chan, double freq)
{
	int mod;
	int err;

	/* Try both 44.1k master and 48.0k master .. */

	err = hal2_calc_mod(&mod, 0, freq);
	if (!err) {
		chan->bres_master = 0;
		chan->bres_mod = mod;
		return 0;
	}

	err = hal2_calc_mod(&mod, 1, freq);
	if (!err) {
		chan->bres_master = 1;
		chan->bres_mod = mod;
		return 0;
	}

	/* We failed to find a matching mod value for either 44.1 master or
	 * 48.0k master, try something else..
	 */
	return -EINVAL;
}


/* Exploit this little neat gcc extension to build our three functions :) */

#define __BUILD_CONF_BRESN_CLOCK(clock)					\
static void hal2_configure_bres##clock(struct hal2_private *hp,		\
				       struct hal2_channel *chan)	\
{									\
	int master = chan->bres_master;					\
		int mod = chan->bres_mod;				\
		int inc = 4;						\
									\
		ireg_write(H2IW_BRES##clock##_C1 , master);		\
		ireg_write2(H2IW_BRES##clock##_C2 , inc, mod);		\
}									\

__BUILD_CONF_BRESN_CLOCK(1)
__BUILD_CONF_BRESN_CLOCK(2)
__BUILD_CONF_BRESN_CLOCK(3)

static int hal2_configure_dac(struct sgiaudio *sa)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->dac_chan;
	int datatype = (chan->stereo?2:0) << 8;
	int clock = 1 << 3;

	/* Let's be sure that the dma port is disabled */
	hal2_disable_port(hp, chan);

	hal2_configure_bres1(hp, chan);

	if (chan->little_end)
		ireg_setbit(H2IW_DMA_END, H2IR_DMA_END,
			    H2I_DMA_PORT_EN_CODECTX);
	else
		ireg_clearbit(H2IW_DMA_END, H2IR_DMA_END,
			      H2I_DMA_PORT_EN_CODECTX);

	ireg_write(H2IW_DAC_C1, chan->pbus | clock | datatype);

	hal2_setup_pbus(hp, chan);

	/* dac ctrl 2? */

	/* Enable the dma port (note: we're not starting the PBUS yet) */
	hal2_enable_port(hp, chan);

	return 0;
}

static int hal2_configure_adc(struct sgiaudio *sa)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->dac_chan;
	int datatype = (chan->stereo?2:0) << 8;
	int clock = 2 << 3;

	hal2_disable_port(hp, chan);

	hal2_configure_bres2(hp, chan);

	if (chan->little_end)
		ireg_setbit(H2IW_DMA_END, H2IR_DMA_END, H2I_DMA_PORT_EN_CODECR);
	else
		ireg_clearbit(H2IW_DMA_END, H2IR_DMA_END,
			      H2I_DMA_PORT_EN_CODECR);

	ireg_write(H2IW_ADC_C1, chan->pbus | clock | datatype);

	hal2_setup_pbus(hp, chan);

	/* adc ctrl 2? */

	/* Enable the dma port (note: we're not starting the PBUS yet) */
	hal2_enable_port(hp, chan);

	return 0;
}

static int hal2_set_dac_stereo(struct sgiaudio *sa, int stereo)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->dac_chan;

	chan->stereo = stereo;

	return 0;
}

static int hal2_set_adc_stereo(struct sgiaudio *sa, int stereo)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->dac_chan;

	chan->stereo = stereo;

	return 0;
}

static int hal2_set_dac_endian(struct sgiaudio *sa, int little_end)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->dac_chan;

	chan->little_end = little_end;

	return 0;
}

static int hal2_set_adc_endian(struct sgiaudio *sa, int little_end)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->adc_chan;

	chan->little_end = little_end;

	return 0;
}

static int hal2_set_dac_freq(struct sgiaudio *sa, double freq)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->dac_chan;
	int err;

	err = hal2_set_freq(chan, freq);
	if (err)
		return err;

	return 0;
}

static int hal2_set_adc_freq(struct sgiaudio *sa, double freq)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->adc_chan;
	int err;

	err = hal2_set_freq(chan, freq);
	if (err)
		return err;
	return 0;
}

static void hal2_go(struct hal2_channel *chan)
{
	/* The hal2 *has* to be enabled before we enable PBUS DMA transfers,
	 * atleast I say so.. (we need some kind of "order") */
	if (!chan->hal2_enabled)
		return;

	if (!chan->pbus_enabled) {
		sgipbus_enable(chan->pbus, PHYSADDR(&chan->ring->desc));
		chan->pbus_enabled = 1;
	}
#ifdef DEBUG
	else
		printk("Attempt to enable the HAL2 DMA channel twice\n");
#endif
}

static void hal2_stop_pbus(struct hal2_channel *chan)
{
	if (chan->pbus_enabled) {
		sgipbus_enable(chan->pbus, PHYSADDR(&chan->ring->desc));
		chan->pbus_enabled = 0;
	}
#ifdef DEBUG
	else
		printk("Trying to stop already stopped DMA channel\n");
#endif
}

static void hal2_start_adc(struct sgiaudio *sa)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->adc_chan;

	hal2_reset_adc_ring(hp, chan);

	hal2_go(chan);
}

static void hal2_start_dac(struct sgiaudio *sa)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->dac_chan;

	/* fill da funky ring buffer */
	while (chan->free_bufs && !hp->dac_underrun)
		hal2_update_dac_buf(sa, hp);

	hal2_go(chan);
}

static void hal2_stop_dac(struct sgiaudio *sa)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->dac_chan;

	hal2_disable_port(hp, chan);

	hal2_stop_pbus(chan);
}

static void hal2_stop_adc(struct sgiaudio *sa)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->adc_chan;

	hal2_disable_port(hp, chan);

	hal2_stop_pbus(chan);
}

static void hal2_free_dac(struct sgiaudio *sa)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->dac_chan;

	if (!hp)
		return;

	if (chan->pbus_requested)
		sgipbus_free(chan->pbus);

	kfree(hp);
}

static void hal2_free_adc(struct sgiaudio *sa)
{
	struct hal2_private *hp = (struct hal2_private *) sa->private;
	struct hal2_channel *chan = &hp->adc_chan;

	if (!hp)
		return;

	if (chan->pbus_requested)
		sgipbus_free(chan->pbus);

	kfree(hp);
}

void hal2_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct sgiaudio *sa = (struct sgiaudio *) dev_id;
	struct hal2_private *hp = (struct hal2_private *) sa->private;

	struct hal2_channel *chan;

	chan = &hp->dac_chan;
	if (chan->pbus_enabled && sgipbus_interrupted(chan->pbus))
		while (chan->free_bufs && !hp->dac_underrun)
			hal2_update_dac_buf(sa, hp);

	chan = &hp->adc_chan;
	if (chan->pbus_enabled && sgipbus_interrupted(chan->pbus))
		;
}

/*
 * My bogus OSS interface, please excuse my sins..
 */

static struct sgiaudio_chan_ops dac_ops = {
	hal2_init_dac,
	hal2_set_dac_stereo,
	hal2_set_dac_endian,
	hal2_set_dac_freq,
	hal2_configure_dac,
	hal2_start_dac,
	hal2_stop_dac,
	hal2_free_dac,
};

static struct sgiaudio_chan_ops adc_ops = {
	hal2_init_adc,
	hal2_set_adc_stereo,
	hal2_set_adc_endian,
	hal2_set_adc_freq,
	hal2_configure_adc,
	hal2_start_adc,
	hal2_stop_adc,
	hal2_free_adc,
};

static ssize_t sgiaudio_dsp_read(struct file *filp,
				 char *buf, size_t count, loff_t *ppos)
{
	return count;
}

static ssize_t sgiaudio_dsp_write(struct file *filp,
				  const char *buf, size_t count, loff_t *ppos)
{
	struct sgiaudio *sa = (struct sgiaudio *) filp->private_data;
	struct sgiaudio_chan *chan = &sa->dac;

	int wp = chan->wp;
	int rp = chan->rp;
	int bufsz = chan->bufsz;
	int left;

	unsigned long flags;

	printk("attempt to write..\n");

	if (count >= bufsz) {
		printk("hal: short write?\n");
		return -EINVAL;	/* XXX we should support this, but not yet */
	}

	if (!chan->started) {
		printk("configuring dac.\n");
		chan->ops->configure(sa);
		printk("dac configured\n");
	}

	save_and_cli(flags);

	left = (rp - wp + bufsz) % bufsz - 1;

	if (filp->f_flags | O_NONBLOCK) {
		count = count > left ? left : count;
	} else {
		/* assume that the channel is started since we're here */
		while (left < count) {
			printk("oups, overflow, let's sleep..\n");
			interruptible_sleep_on(&chan->queue);
			left = (rp - wp + bufsz) % bufsz - 1;
		}
	}

	restore_flags(flags);

	printk("copying buffer\n");

	if (!access_ok(VERIFY_READ, buf, count))
		return -EFAULT;

	if (wp + count < bufsz)
		__copy_from_user(chan->buf + wp, buf, count);
	else {
		/* crosses the buffer boundary */
		int first = bufsz - wp - 1;

		__copy_from_user(chan->buf + wp, buf, first);
		__copy_from_user(chan->buf + first, buf, count - first);
	}

	chan->wp = (wp + count) % bufsz;

	if (!chan->started) {
		printk("starting channel\n");
		chan->ops->start(sa);
		printk("channel started\n");
		sa->dac.started = 1;
	}

	return count;
}

static int sgiaudio_dsp_ioctl(struct inode *inode, struct file *filp,
			      unsigned int cmd, unsigned long arg)
{
	return 0;
}

static int sgiaudio_init_dsp(struct sgiaudio *sa, struct sgiaudio_chan *chan,
			     struct sgiaudio_chan_ops *ops)
{
	int err;

	chan->ops = ops;

	chan->bufsz = 4 * PAGE_SIZE;
	chan->wp = 0;
	chan->rp = 1;

	chan->buf = kmalloc(chan->bufsz, GFP_KERNEL);
	if (!chan->buf)
		return -ENOMEM;

	err = chan->ops->init(sa);
	if (err)
		goto cleanup;

	err = chan->ops->stereo(sa, 0);
	if (err)
		goto cleanup;

	err = chan->ops->endian(sa, 0);
	if (err)
		goto cleanup;

	err = chan->ops->freq(sa, 8000);
	if (err)
		goto cleanup;

	return 0;

cleanup:
	kfree(chan->buf);
	return err;
}

static int sgiaudio_dsp_open(struct inode *inode, struct file *filp)
{
	struct sgiaudio *sa = (struct sgiaudio *) filp->private_data;
	struct sgiaudio_chan *dac, *adc;
	int err;

	dac = &sa->dac;
	adc = &sa->adc;

	if (filp->f_flags & O_RDONLY || filp->f_flags & O_RDWR) {
		printk("opened with read perm\n");
		err = sgiaudio_init_dsp(sa, &sa->adc, &adc_ops);
		if (err)
			return err;
	}
	if (filp->f_flags & O_WRONLY || filp->f_flags & O_RDWR) {
		printk("opened with write perm\n");
		err = sgiaudio_init_dsp(sa, &sa->dac, &dac_ops);
		if (err)
			return err;
	}

	MOD_INC_USE_COUNT;
#ifdef DEBUG
	printk("DSP opened successfully\n");
#endif
	return 0;

}

static int sgiaudio_dsp_release(struct inode *inode, struct file *filp)
{
	struct sgiaudio *sa = (struct sgiaudio *) filp->private_data;
	struct sgiaudio_chan *dac, *adc;

	printk("in dsp release..\n");

	dac = &sa->dac;
	adc = &sa->adc;

	if (filp->f_flags & O_RDONLY || filp->f_flags & O_RDWR)
		adc->ops->free(sa);

	if (filp->f_flags & O_WRONLY || filp->f_flags & O_RDWR)
		dac->ops->free(sa);

	/* we have to call the default release file operation, to free up
	 * structures and decrease usage, this is a little workaround .. */
	printk("dispatching release..");
	inode->i_op->default_file_ops->release(inode, filp);

	return 0;
}

struct file_operations sgiaudio_dsp_ops = {
	NULL,		/* sgiaudio_dsp_lseek */
	sgiaudio_dsp_read,
	sgiaudio_dsp_write,
	NULL,		/* sgiaudio_dsp_readdir */
	NULL,		/* sgiaudio_dsp_select */
	sgiaudio_dsp_ioctl,
	NULL,		/* sgiaudio_dsp_mmap */
	sgiaudio_dsp_open,
	NULL,		/* sgiaudio_dsp_flush */
	sgiaudio_dsp_release,
};

static int sgiaudio_mixer_ioctl(struct inode *inode, struct file *filp,
				unsigned int cmd, unsigned long arg)
{
	return 0;
}

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

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

struct file_operations sgiaudio_mixer_ops = {
	NULL,		/* sgiaudio_mixer_lseek */
	NULL,		/* sgiaudio_mixer_read */
	NULL,		/* sgiaudio_mixer_write */
	NULL,		/* sgiaudio_mixer_readdir */
	NULL,		/* sgiaudio_mixer_select */
	sgiaudio_mixer_ioctl,
	NULL,		/* sgiaudio_mixer_mmap */
	sgiaudio_mixer_open,
	NULL,		/* sgiaudio_mixer_flush */
	sgiaudio_mixer_release,
};

static int sgiaudio_open(struct inode *inode, struct file *filp)
{
	static struct sgiaudio *sa;
	int minor = MINOR(inode->i_rdev);

	static int initialized = 0;

	if (!initialized) {
		sa = kmalloc(sizeof(struct sgiaudio), GFP_KERNEL);
		if (!sa)
			return -ENOMEM;

		memset(sa, 0, sizeof(struct sgiaudio));

		filp->private_data = (void *) sa;

		initialized++;
	}

	switch (minor) {
	case 3:
		filp->f_op = &sgiaudio_dsp_ops;

		/* dispatch the call to the specific open routine .. */
		return filp->f_op->open(inode, filp);

		/* not reached */
		break;
	default:
		return -ENODEV;

		/* not reached */
		break;
	}
	return 0;
}

static int sgiaudio_release(struct inode *inode, struct file *filp)
{
	struct sgiaudio *sa = (struct sgiaudio *) filp->private_data;

	printk("release dispatched!\n");

	if (sa)
		kfree(sa);

	MOD_DEC_USE_COUNT;

	return 0;
}

static struct file_operations sgiaudio_ops = {
	NULL,		/* sgiaudio_lseek */
	NULL,		/* sgiaudio_read */
	NULL,		/* sgiaudio_write */
	NULL,		/* sgiaudio_readdir */
	NULL,		/* sgiaudio_select */
	NULL,		/* sgiaudio_ioctl */
	NULL,		/* sgiaudio_mmap */
	sgiaudio_open,
	NULL,		/* sgiaudio_flush */
	sgiaudio_release,
};

__initfunc(void sgiaudio_init(void))
{
	int result;
	int err;


	err = hal2_probe();
	if (err) {
		printk("sgiaudio: unable to find hal2 subsystem\n");
		return;
	}

	printk("sgiaudio: initializing\n");
	result = register_chrdev(SOUND_MAJOR, "sound", &sgiaudio_ops);
	if (result < 0) {
		printk("sgiaudio: unable to get major %d", SOUND_MAJOR);
	}
}

#ifdef MODULE

int init_module(void)
{
	int err;
	int result;

	err = hal2_probe();
	if (err) {
		printk("sgiaudio: unable to find hal2 subsystem\n");
		return -ENODEV;
	}

	result = register_chrdev(SOUND_MAJOR, "sound", &sgiaudio_ops);
	if (result < 0) {
		printk("sgiaudio: unable to get major %d", SOUND_MAJOR);
		return result;
	}

	return 0;
}

void cleanup_module(void)
{
	unregister_chrdev(SOUND_MAJOR, "sound");
}

#endif