summaryrefslogtreecommitdiffstats
path: root/uClinux-2.4.20-uc1/drivers/sound/w90n745_audio.c
blob: 48067c7665736bb4d1613174895032ababb6f055 (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
/****************************************************************************
 * 
 * Copyright (c) 2004 - 2007 Winbond Electronics Corp. All rights reserved.     
 *
 *
 * FILENAME
 *     w90n745_Audio.c
 *
 * VERSION
 *     1.0 
 *
 * DESCRIPTION
 *	Winbond w90n745 Audio Driver for Linux 2.4.x with OSS frame
 *
 * DATA STRUCTURES
 *     None
 *
 * FUNCTIONS
 *
 *     
 * HISTORY
 *	 2005.11.24		Created by PC34 QFu
 *
 * REMARK
 *     None
 *
 **************************************************************************/
#include <linux/config.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/soundcard.h>
#include <linux/sound.h>

#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <asm/arch/irqs.h>

#include "w90n745_audio_regs.h"
#include "w90n745_audio.h"

#define AUDIO_BUFFER_ORDER	4		/*  64K x 2 (play + record) */

#define FRAG_SIZE	(( PAGE_SIZE << AUDIO_BUFFER_ORDER ) / 2)

#define WB_AUDIO_DEFAULT_SAMPLERATE		AU_SAMPLE_RATE_44100
#define WB_AUDIO_DEFAULT_CHANNEL			2
#define WB_AUDIO_DEFAULT_VOLUME			30

//#define WB_AU_DEBUG
//#define WB_AU_DEBUG_ENTER_LEAVE
//#define WB_AU_DEBUG_MSG
//#define WB_AU_DEBUG_MSG2

#ifdef WB_AU_DEBUG
#define DBG(fmt, arg...)			printk(fmt, ##arg)
#else
#define DBG(fmt, arg...)
#endif

#ifdef WB_AU_DEBUG_ENTER_LEAVE
#define ENTER()					DBG("[%-10s] : Enter\n", __FUNCTION__)
#define LEAVE()					DBG("[%-10s] : Leave\n", __FUNCTION__)
#else
#define ENTER()
#define LEAVE()
#endif

#ifdef WB_AU_DEBUG_MSG
#define MSG(fmt)					DBG("[%-10s] : "fmt, __FUNCTION__)
#else
#define MSG(fmt)
#endif

#ifdef WB_AU_DEBUG_MSG2
#define MSG2(fmt, arg...)			DBG("[%-10s] : "fmt, __FUNCTION__, ##arg)
#else
#define MSG2(fmt, arg...)
#endif


static WB_AUDIO_T audio_dev;
static WB_AUDIO_CODEC_T *all_codecs[2] = {&wb_i2s_codec,  &wb_ac97_codec};
static int dev_dsp[2] = {-1, -1}, dev_mixer[2] = {-1, -1};
static int nSamplingRate[]={AU_SAMPLE_RATE_8000,
							AU_SAMPLE_RATE_11025,
							AU_SAMPLE_RATE_16000,
							AU_SAMPLE_RATE_22050,
							AU_SAMPLE_RATE_24000,
							AU_SAMPLE_RATE_32000,
							AU_SAMPLE_RATE_44100,
							AU_SAMPLE_RATE_48000};

static void wb_audio_stop_play(void)
{
	MSG("Stop Play! \n");

	if ( audio_dev.state & AU_STATE_PLAYING){
		audio_dev.codec->set_play_volume(0, 0);
		audio_dev.codec->stop_play();
		audio_dev.state &= ~AU_STATE_PLAYING;
		wake_up_interruptible(&audio_dev.write_wait_queue);
		MSG2("Buf[0] : %x, Buf[1] : %x\n",
			audio_dev.play_half_buf[0].ptr,
			audio_dev.play_half_buf[1].ptr);

	}
}

static void wb_audio_stop_record(void)
{
	MSG("Stop Record!\n");
	
	if ( audio_dev.state & AU_STATE_RECORDING){
		audio_dev.codec->set_record_volume(0, 0);
		audio_dev.codec->stop_record();
		audio_dev.state &= ~AU_STATE_RECORDING;
		wake_up_interruptible(&audio_dev.read_wait_queue);
	}
}

static int play_callback(UINT32 uAddr, UINT32 uLen)
{
	int i = 1;

	ENTER();

	if(uAddr == audio_dev.play_buf_addr){
		MSG("<");
		i = 0;
	}
	else
		MSG(">");

	audio_dev.play_half_buf[i].ptr = 0;		/* set buffer empty flag */

	wake_up_interruptible(&audio_dev.write_wait_queue);	/* wake up all block write system call */

	if(audio_dev.play_half_buf[i^1].ptr /*== FRAG_SIZE*/ > 0){	/* check whether next buffer is full ? */
		LEAVE();
		return 0;
	}
	else{
		wb_audio_stop_play();
		return 1;
	}
}

static int record_callback(UINT32 uAddr, UINT32 uLen)
{
	int i = 1;

	if(uAddr == audio_dev.record_buf_addr)
		i = 0;

	audio_dev.record_half_buf[i].ptr =0;		/* indicate read from buffer[0] */

	wake_up_interruptible(&audio_dev.read_wait_queue);	/* wake up all blocked read system call */

	if ( audio_dev.record_half_buf[i ^ 1].ptr == 0) {	/* last block wan't take off , user may stop record */
		wb_audio_stop_record();
		return 1;
	}
	else
		return 0;
}

static int wb_audio_start_play(void)
{
	int ret = 0;

	MSG("Start Playing ... \n");

	if ( audio_dev.state & AU_STATE_PLAYING)	/* playing? */
		return 0;

	if ( audio_dev.state & AU_STATE_RECORDING ) {		/* recording? */
		if (!(audio_dev.codec->get_capacity() & DSP_CAP_DUPLEX))		/* not support full duplex */
			wb_audio_stop_record();
	}

	audio_dev.state |= AU_STATE_PLAYING;

	MSG2("Buf[0] : %x, Buf[1] : %x\n",
		audio_dev.play_half_buf[0].ptr,
		audio_dev.play_half_buf[1].ptr);
	
	if ( audio_dev.codec->start_play(play_callback, 
								audio_dev.nSamplingRate,
								audio_dev.nChannels)) {
		audio_dev.state &= ~AU_STATE_PLAYING;
		MSG("Play error\n");

		ret = -EIO;
	}
	else{
		audio_dev.codec->set_play_volume(audio_dev.nPlayVolumeLeft, audio_dev.nPlayVolumeRight);
	}

	return ret;
}

static int wb_audio_start_record(void)
{
	int ret=0;

	MSG("Start Recording ... \n");

	if (audio_dev.state & AU_STATE_RECORDING)
		return 0;

	if (audio_dev.state & AU_STATE_PLAYING) {
		if (!(audio_dev.codec->get_capacity() & DSP_CAP_DUPLEX))
			wb_audio_stop_play();
	}

	audio_dev.record_half_buf[0].ptr =FRAG_SIZE;
	audio_dev.record_half_buf[1].ptr = FRAG_SIZE;
	audio_dev.state |= AU_STATE_RECORDING;

	if ( audio_dev.codec->start_record((AU_CB_FUN_T)record_callback,
								audio_dev.nSamplingRate,
								audio_dev.nChannels)) {						
		audio_dev.state &= ~AU_STATE_RECORDING;
		ret = -EIO;
	}
	else{
		audio_dev.codec->set_record_volume(audio_dev.nRecordVolumeLeft, audio_dev.nRecordVolumeRight);
	}	

	return ret;
}

static int wb_mixer_open(struct inode *inode, struct file *file)
{
	int retval;
	
	int minor = MINOR(inode->i_rdev);

	ENTER();

	if(minor != 0 && minor != 16)
		return -ENODEV;

	if(minor == 16)
		minor = 1;

	down(&audio_dev.mixer_sem);

	retval = -EBUSY;

	if(audio_dev.open_flags != 0){
		if(audio_dev.mixer_openflag != 0)
			goto quit;
		else{
			if(audio_dev.dsp_openflag != 0 && audio_dev.dsp_dev != minor)
				goto quit;
		}
	}
	
	audio_dev.open_flags = 1;
	audio_dev.mixer_openflag = 1;
	audio_dev.mixer_dev = minor;

	audio_dev.codec = all_codecs[minor];

	MSG2("Mixer[%d] opened\n", minor);

	retval = 0;

	MOD_INC_USE_COUNT;

quit:
	up(&audio_dev.mixer_sem);

	LEAVE();
	
	return retval;
}

static int wb_mixer_release(struct inode *inode, struct file *file)
{
	audio_dev.mixer_openflag = 0;
	if(audio_dev.dsp_openflag == 0)
		audio_dev.open_flags = 0;

	MOD_DEC_USE_COUNT;
	
	return 0;
}

static int wb_mixer_ioctl(struct inode *inode, struct file *file,
			       unsigned int cmd, unsigned long arg)
{
	int ret = 0, val=0, err = 0;
	int tmpVolumeLeft, tmpVolumeRight;

	if (cmd == SOUND_MIXER_INFO) {
		mixer_info info;
		memset(&info,0,sizeof(info));
              strncpy(info.id,"w90n745",sizeof(info.id)-1);
              strncpy(info.name,"Winbond w90n745 Audio",sizeof(info.name)-1);
              info.modify_counter = 0;
              if (copy_to_user((void *)arg, &info, sizeof(info)))
                      return -EFAULT;
		return 0;
	}
	if (cmd == SOUND_OLD_MIXER_INFO) {
		_old_mixer_info info;
		memset(&info,0,sizeof(info));
              strncpy(info.id,"w90n745",sizeof(info.id)-1);
              strncpy(info.name,"Winbond w90n745 Audio",sizeof(info.name)-1);
              if (copy_to_user((void *)arg, &info, sizeof(info)))
                      return -EFAULT;
		return 0;
	}
	if (cmd == OSS_GETVERSION)
		return put_user(SOUND_VERSION, (int *)arg);

	/* read */
	if (_SIOC_DIR(cmd) & _SIOC_WRITE)
		if (get_user(val, (int *)arg))
			return -EFAULT;

	switch (cmd) {
		case MIXER_READ(SOUND_MIXER_CAPS):
			ret = SOUND_CAP_EXCL_INPUT;		/* only one input can be selected */
			break;
		case MIXER_READ(SOUND_MIXER_STEREODEVS):
			ret = 1;									/* check whether support stereo */
			break;
		
		case MIXER_READ(SOUND_MIXER_RECMASK):		/* get input channels mask */
			ret = SOUND_MASK_MIC;
		
		case MIXER_READ(SOUND_MIXER_DEVMASK):		/* get all channels mask */
			ret |= SOUND_MASK_PCM + SOUND_MASK_VOLUME;
			break;

		case MIXER_WRITE(SOUND_MIXER_RECSRC):
			if ( val != SOUND_MASK_MIC )
				err = -EPERM;
			
		case MIXER_READ(SOUND_MIXER_RECSRC):
			ret = SOUND_MASK_MIC;
			break;

		case MIXER_WRITE(SOUND_MIXER_VOLUME):
		case MIXER_WRITE(SOUND_MIXER_PCM):
		case MIXER_WRITE(SOUND_MIXER_MIC):
			tmpVolumeLeft = (val & 0xff) * 31  / 100;
			tmpVolumeRight = ((val >> 8) & 0xff) * 31 / 100;
			if (cmd == MIXER_WRITE(SOUND_MIXER_MIC)){	/* set mic volume */
				audio_dev.codec->set_record_volume(tmpVolumeLeft, tmpVolumeRight);
				audio_dev.nRecordVolumeLeft = tmpVolumeLeft;
				audio_dev.nRecordVolumeRight = tmpVolumeRight;
			}
			else{
				audio_dev.codec->set_play_volume(tmpVolumeLeft, tmpVolumeRight);		/* set play volume */
				audio_dev.nPlayVolumeLeft = tmpVolumeLeft;
				audio_dev.nPlayVolumeRight = tmpVolumeRight;
			}
			
			ret = (val & 0xffff);

			break;

		case MIXER_READ(SOUND_MIXER_VOLUME):
		case MIXER_READ(SOUND_MIXER_PCM):
			ret = ((audio_dev.nPlayVolumeLeft * 100  / 31) | 
				((audio_dev.nPlayVolumeRight * 100  / 31 ) << 8));
			break;

		case MIXER_READ(SOUND_MIXER_MIC):
			ret =((audio_dev.nRecordVolumeLeft * 100  / 31) | 
				((audio_dev.nRecordVolumeRight * 100  / 31 ) << 8));
			break;
		
		default:
			return -EINVAL;
	}

	if (put_user(ret, (int *)arg))
		return -EFAULT;

	return err;
}


static struct file_operations wb_mixer_fops = {
	owner:	THIS_MODULE,
	ioctl:	wb_mixer_ioctl,
	open:	wb_mixer_open,
	release:	wb_mixer_release,
};

static void wb_dsp_irq(int irq, void *dev_id, struct pt_regs * regs)
{
	ENTER();
	
	if( audio_dev.state & AU_STATE_PLAYING)
		audio_dev.codec->play_interrupt();
	if(audio_dev.state & AU_STATE_RECORDING)
		audio_dev.codec->record_interrupt();

	LEAVE();
}

static int wb_dsp_open(struct inode *inode, struct file *file)
{
	int minor = MINOR(inode->i_rdev);
	int retval = -EBUSY;

	ENTER();

	if(minor != 3 && minor != 19){
		MSG2("Minor error : %d\n", minor);
		return -ENODEV;
	}

	if(minor == 3)
		minor = 0;
	else
		minor = 1;

	if(audio_dev.open_flags != 0){
		if(audio_dev.dsp_openflag != 0)
			goto quit;
		else{
			if(audio_dev.mixer_openflag != 0 && audio_dev.mixer_dev != minor)
				goto quit;
		}
	}

	if((retval = request_irq(INT_AC97, wb_dsp_irq, SA_INTERRUPT, "wb audio", NULL))){
		printk("wb_audio_init : Request IRQ error\n");
		goto quit;
	}

	//enable_irq(INT_AC97);

	audio_dev.open_flags = 1;
	audio_dev.dsp_openflag = 1;
	audio_dev.dsp_dev = minor;
	audio_dev.state = AU_STATE_NOP;

	audio_dev.play_buf_addr = __get_free_pages(GFP_KERNEL, AUDIO_BUFFER_ORDER);
	if(audio_dev.play_buf_addr == NULL){
		free_irq(INT_AC97, NULL);
		MSG("Not enough memory\n");
		return -ENOMEM;
	}

	audio_dev.record_buf_addr = __get_free_pages(GFP_KERNEL, AUDIO_BUFFER_ORDER);
	if(audio_dev.record_buf_addr == NULL){
		free_pages(audio_dev.play_buf_addr, AUDIO_BUFFER_ORDER);
		free_irq(INT_AC97, NULL);
		MSG("Not enough memory\n");
		return -ENOMEM;
	}

	audio_dev.play_buf_addr |= 0x80000000;	// none - cache
	audio_dev.play_buf_length = ( PAGE_SIZE << AUDIO_BUFFER_ORDER );
	audio_dev.record_buf_addr |= 0x80000000;	// none - cache
	audio_dev.record_buf_length = ( PAGE_SIZE << AUDIO_BUFFER_ORDER );

	MSG2("Audio_Dev.play_buf_addr : %x, Length: %x\n", 
		audio_dev.play_buf_addr, audio_dev.play_buf_length);

	init_waitqueue_head(&audio_dev.read_wait_queue);
	init_waitqueue_head(&audio_dev.write_wait_queue);

	memset(&audio_dev.play_half_buf, 0, sizeof(audio_dev.play_half_buf));
	memset(&audio_dev.record_half_buf, 0, sizeof(audio_dev.record_half_buf));

	audio_dev.nSamplingRate = WB_AUDIO_DEFAULT_SAMPLERATE;
	audio_dev.nChannels = WB_AUDIO_DEFAULT_CHANNEL;
	audio_dev.nPlayVolumeLeft = WB_AUDIO_DEFAULT_VOLUME;
	audio_dev.nPlayVolumeRight = WB_AUDIO_DEFAULT_VOLUME;
	audio_dev.nRecordVolumeLeft = WB_AUDIO_DEFAULT_VOLUME;
	audio_dev.nRecordVolumeRight = WB_AUDIO_DEFAULT_VOLUME;
	
	audio_dev.codec = all_codecs[minor];

	/* set dma buffer */
	audio_dev.codec->set_play_buffer(audio_dev.play_buf_addr,
					audio_dev.play_buf_length);
	audio_dev.codec->set_record_buffer(audio_dev.record_buf_addr,
					audio_dev.record_buf_length);

	audio_dev.codec->reset();

	MOD_INC_USE_COUNT;

	LEAVE();

	return 0;

quit:

	MSG2("Open failed : %d\n", retval);

	return retval;
}

static int wb_dsp_release(struct inode *inode, struct file *file)
{
	ENTER();

	if(audio_dev.state & AU_STATE_PLAYING){
		/* wait until stop playing */
		wait_event_interruptible(audio_dev.write_wait_queue,
								(audio_dev.state & AU_STATE_PLAYING)  == 0 ||
								(audio_dev.play_half_buf[0].ptr == 0 &&
								audio_dev.play_half_buf[1].ptr == 0));
	}
	wb_audio_stop_play();
	wb_audio_stop_record();

	free_irq(INT_AC97, NULL);

	free_pages(audio_dev.play_buf_addr & 0x7fffffff, AUDIO_BUFFER_ORDER);
	free_pages(audio_dev.record_buf_addr & 0x7fffffff, AUDIO_BUFFER_ORDER);

	audio_dev.dsp_openflag = 0;
	if(audio_dev.mixer_openflag == 0)
		audio_dev.open_flags = 0;

	MOD_DEC_USE_COUNT;

	LEAVE();
	
	return 0;
}

static ssize_t wb_dsp_read(struct file *file, char *buffer,
				size_t swcount, loff_t *ppos)
{
	int i, tmp, length, block_len, retval, bpos;
	char *dma_buf = (char *)audio_dev.record_buf_addr;

	ENTER();

	if(swcount == 0)
		return 0;

	if(down_interruptible(&audio_dev.dsp_read_sem))
		return -ERESTARTSYS;

again:
	
	if((audio_dev.state & AU_STATE_RECORDING) == 0) {	/* if record not start, then start it */
		retval = wb_audio_start_record();
		if ( retval )
			goto quit;
	}

	length = swcount;
	block_len = FRAG_SIZE;
	retval = -EFAULT;

	if(audio_dev.record_half_buf[0].ptr == FRAG_SIZE && 
	   audio_dev.record_half_buf[1].ptr == FRAG_SIZE){	/* buffer empty */
		if( file->f_flags & O_NONBLOCK){
			retval = -EAGAIN;
			goto quit;
		}
		else {
			wait_event_interruptible(audio_dev.read_wait_queue, 
					(audio_dev.state & AU_STATE_RECORDING) == 0 ||
					audio_dev.record_half_buf[0].ptr == 0 || 
					audio_dev.record_half_buf[1].ptr == 0 );
			if ( (audio_dev.state & AU_STATE_RECORDING)  == 0){
				retval = 0;
				goto quit;
			}
		}

	}

	retval = 0;
	bpos = 0;
	for(i = 0; i < 2; i++){
		tmp = block_len - audio_dev.record_half_buf[i].ptr;

		if(swcount < tmp)
			tmp = swcount;

		if(tmp){
			retval = -EFAULT;
			if(copy_to_user(buffer + bpos, 
				dma_buf + i * block_len + audio_dev.record_half_buf[i].ptr , tmp))
				goto quit;
		}
		else
			continue;

		swcount -= tmp;
		audio_dev.record_half_buf[i].ptr += tmp;
		bpos += tmp;

		if(swcount == 0)
			break;
	
	}


	retval = length - swcount;

	if(swcount != 0){
		if( file->f_flags & O_NONBLOCK 
			&& audio_dev.play_half_buf[0].ptr == FRAG_SIZE 
			&& audio_dev.play_half_buf[1].ptr == FRAG_SIZE)
			goto quit;
		
		buffer += retval;
		goto again;
	}

quit:
	up(&audio_dev.dsp_read_sem);

	LEAVE();

	return retval;
}

static ssize_t wb_dsp_write(struct file *file, const char *buffer,
				 size_t count, loff_t *ppos)
{
	int tmp, retval, length, i;
	char *dma_buf = (char *)audio_dev.play_buf_addr;

	ENTER();

	MSG2("DSP Write : Buffer : %08x Count : %x\n", buffer, count);

	if(count == 0)
		return 0;

	if(down_interruptible(&audio_dev.dsp_write_sem))
		return -ERESTARTSYS;

	
again:
	
	length = count;

	if(audio_dev.state & AU_STATE_PLAYING){
		if(audio_dev.play_half_buf[0].ptr == FRAG_SIZE &&
			audio_dev.play_half_buf[1].ptr == FRAG_SIZE){
			if( file->f_flags & O_NONBLOCK) {
				retval = -EAGAIN;
				goto quit;
			}
			else{
				MSG("Write block ...\n");
				wait_event_interruptible(audio_dev.write_wait_queue,
									(audio_dev.state & AU_STATE_PLAYING) == 0 ||
									audio_dev.play_half_buf[0].ptr == 0 ||
									audio_dev.play_half_buf[1].ptr == 0);
			}
		}
	}


	retval = -EFAULT;


	for(i = 0; i < 2; i ++){
		tmp = FRAG_SIZE - audio_dev.play_half_buf[i].ptr;

		if(tmp > count)
			tmp = count;

		if(tmp){
			if(copy_from_user(dma_buf + audio_dev.play_half_buf[i].ptr + i * FRAG_SIZE, 
					buffer + length - count, tmp))
					goto quit;
		}

		count -= tmp;
		audio_dev.play_half_buf[i].ptr += tmp;
		if( count == 0)
			break;
	}

	retval = length - count;

	if((audio_dev.state & AU_STATE_PLAYING ) == 0
		&& audio_dev.play_half_buf[0].ptr == FRAG_SIZE 
		&& audio_dev.play_half_buf[1].ptr == FRAG_SIZE){

		if (wb_audio_start_play()){
			retval =  -EIO;
			goto quit;
		}
	}

	if(count != 0){
		if( file->f_flags & O_NONBLOCK 
			&& audio_dev.play_half_buf[0].ptr == FRAG_SIZE 
			&& audio_dev.play_half_buf[1].ptr == FRAG_SIZE)
			goto quit;
		
		buffer += retval;
		goto again;
	}

quit:

	up(&audio_dev.dsp_write_sem);

	DBG("W");
	
	LEAVE();

	return retval;
}

static int wb_dsp_ioctl(struct inode *inode, struct file *file,
			     unsigned int cmd, unsigned long arg)
{
	int val = 0, i, err = 0;
	audio_buf_info info;

	ENTER();
	
	switch (cmd) {

	       case OSS_GETVERSION:
       	       val = SOUND_VERSION;

			break;
				
	       case SNDCTL_DSP_GETCAPS:

			if (audio_dev.codec->get_capacity() & DSP_CAP_DUPLEX)
				val |= DSP_CAP_DUPLEX;
			
		   	val |= DSP_CAP_TRIGGER;

			break;

	       case SNDCTL_DSP_SPEED:
			if (get_user(val, (int*)arg))
				return -EFAULT;

			for(i = 0; i < sizeof(nSamplingRate)/sizeof(unsigned int); i++)
				if(val == nSamplingRate[i])
					break;

			if(i >= sizeof(nSamplingRate)/sizeof(unsigned int)){	/* not supported */
				val = audio_dev.nSamplingRate;
				err = -EPERM;
			}
			else
				audio_dev.nSamplingRate = val;
		
			break;


	       case SOUND_PCM_READ_RATE:
			val = audio_dev.nSamplingRate;
			break;

	       case SNDCTL_DSP_STEREO:
			if (get_user(val, (int*)arg))
				return -EFAULT;

			audio_dev.nChannels = val ? 2:1;
		
			break;	

	       case SNDCTL_DSP_CHANNELS:
			if (get_user(val, (int*)arg))
				return -EFAULT;

			if(val != 1 && val != 2){
				val = audio_dev.nChannels;
				err = -EPERM;
			}

			audio_dev.nChannels = val;
			break;

	       case SOUND_PCM_READ_CHANNELS:
			val = audio_dev.nChannels;
		
			break;

	       case SNDCTL_DSP_SETFMT:
		   	if (get_user(val, (int*)arg))
				return -EFAULT;

			if ( (val & (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE |AFMT_U16_BE)) == 0)
				err = -EPERM;
			
		case SNDCTL_DSP_GETFMTS:

			val =  (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE |AFMT_U16_BE);
			break;

	       case SOUND_PCM_READ_BITS:
			val = 16;
			break;

	       case SNDCTL_DSP_NONBLOCK:
       	       file->f_flags |= O_NONBLOCK;
			break;

	       case SNDCTL_DSP_RESET:
			wb_audio_stop_play();
			wb_audio_stop_record();	
			
			return 0;
		
	       case SNDCTL_DSP_GETBLKSIZE:
			val =FRAG_SIZE;
			break;
		
		case SNDCTL_DSP_GETISPACE:

			info.fragsize = FRAG_SIZE;
			info.fragstotal = 2;
			info.bytes = audio_dev.record_buf_length - audio_dev.record_half_buf[0].ptr - audio_dev.record_half_buf[1].ptr;
			info.fragments = info.bytes / info.fragsize;
		
			MSG2("SNDCTL_DSP_GETISPACE returns %d/%d/%d/%d\n",
				       info.fragsize, info.fragstotal,
				       info.bytes, info.fragments);
		
			if (copy_to_user((void *)arg, &info, sizeof(info)))
				return -EFAULT;
		
			return 0;

		case SNDCTL_DSP_GETOSPACE:

			info.fragsize = FRAG_SIZE;
			info.fragstotal = 2;
			info.bytes = audio_dev.play_buf_length - audio_dev.play_half_buf[0].ptr - audio_dev.play_half_buf[1].ptr;
			info.fragments = info.bytes / info.fragsize;
		
			MSG2("SNDCTL_DSP_GETISPACE returns %d/%d/%d/%d\n",
				       info.fragsize, info.fragstotal,
				       info.bytes, info.fragments);
		
			if (copy_to_user((void *)arg, &info, sizeof(info)))
				return -EFAULT;
		
			return 0;

	       case SNDCTL_DSP_SYNC:
			/* no data */
			if ( audio_dev.play_half_buf[0].ptr == 0 &&
				audio_dev.play_half_buf[1].ptr == 0)
				return 0;
			
		   	wb_audio_start_play();

			/* wait until stop playing */
			wait_event_interruptible(audio_dev.write_wait_queue,
									(audio_dev.state & AU_STATE_PLAYING)  == 0 ||
									(audio_dev.play_half_buf[0].ptr == 0 &&
									audio_dev.play_half_buf[1].ptr == 0));
			wb_audio_stop_play();
			
		   	return 0;

		case SNDCTL_DSP_GETTRIGGER:
			val = 0;
			if ( audio_dev.state & AU_STATE_PLAYING )
				val |= PCM_ENABLE_OUTPUT;
			if ( audio_dev.state & AU_STATE_RECORDING)
				val |= PCM_ENABLE_INPUT;
			break;

		case SNDCTL_DSP_SETTRIGGER:
			if (get_user(val, (int*)arg))
				return -EFAULT;
			if ( val & PCM_ENABLE_OUTPUT){
				if ( wb_audio_start_play()){
					val &= ~PCM_ENABLE_OUTPUT;
					err = -EPERM ;
				}
			}
			else{
				wb_audio_stop_play();
			}

			if ( val & PCM_ENABLE_INPUT){
				if ( wb_audio_start_record()){
					val &= ~PCM_ENABLE_INPUT;
					err = -EPERM ;
				}
			}
			else{
				wb_audio_stop_record();
			}

			break;
				
		default:
			return -EINVAL;
	}

	LEAVE();

	return err?-EPERM:put_user(val, (int *)arg);

}

static unsigned int wb_dsp_poll(struct file *file, struct poll_table_struct *wait)
{
	int mask = 0;

	ENTER();

	if (file->f_mode & FMODE_WRITE){
		poll_wait(file, &audio_dev.write_wait_queue, wait);
		
		/* check if has data */
		if(audio_dev.play_half_buf[0].ptr != FRAG_SIZE ||
			audio_dev.play_half_buf[1].ptr != FRAG_SIZE )
			mask |= POLLOUT | POLLWRNORM;
	}
			
	if (file->f_mode & FMODE_READ){
		poll_wait(file, &audio_dev.read_wait_queue, wait);

		/* check if can read */
		if(audio_dev.record_half_buf[0].ptr !=  FRAG_SIZE  ||
			audio_dev.record_half_buf[1].ptr !=  FRAG_SIZE )
			mask |= POLLIN | POLLRDNORM;
	}

	LEAVE();

	return mask;
}

static struct file_operations wb_dsp_fops = {
	owner:	THIS_MODULE,
	llseek:	no_llseek,
	read:	wb_dsp_read,
	write:	wb_dsp_write,
	poll:		wb_dsp_poll,
	ioctl:	wb_dsp_ioctl,
	open:	wb_dsp_open,
	release:	wb_dsp_release,
};

static int __init wb_dsp_init(void)
{
	int i;

	for(i=0;i<2;i++){
		dev_dsp[i] = register_sound_dsp(&wb_dsp_fops, i);
		MSG2("Dsp Device No : %d\n", dev_dsp[i]);
		if(dev_dsp[i]< 0)
			goto quit;
	}

	return 0;

quit:
	printk("Register DSP device failed\n");
	return -1;
}


static int __init wb_mixer_init(void)
{
	int i;

	for(i=0;i<2;i++){
		dev_mixer[i] = register_sound_mixer(&wb_mixer_fops, i);
		MSG2("Mixer Device No : %d\n", dev_mixer[i]);
		if(dev_mixer[i]< 0)
			goto quit;
	}

	return 0;

quit:
	printk("Register Mixer device failed\n");
	return -1;
}

static void wb_dsp_unload(void)
{
	int i;
	for(i=0;i<2;i++)
		unregister_sound_dsp(dev_dsp[i]);
}

static void wb_mixer_unload(void)
{
	int i;
	for(i=0;i<2;i++)
		unregister_sound_mixer(dev_mixer[i]);
}


MODULE_AUTHOR("QFu");
MODULE_DESCRIPTION("Winbond w90n745 Audio Driver");
MODULE_LICENSE("GPL");

static void wb_audio_exit (void)
{
	wb_mixer_unload();
	wb_dsp_unload();
	free_irq(INT_AC97, NULL);
}
extern struct semaphore ac97_sem; // move here to ensure it's initialized before accesign it.
static int __init wb_audio_init(void)
{

	Disable_Int(INT_AC97);

	memset(&audio_dev, 0, sizeof(audio_dev));
	sema_init(& audio_dev.dsp_read_sem, 1);
	sema_init(&audio_dev.dsp_write_sem, 1);
	sema_init(&audio_dev.mixer_sem, 1);
	sema_init(&ac97_sem, 1);

	if(wb_dsp_init())
		goto quit;

	if(wb_mixer_init())
		goto quit;

	printk("Winbond Audio Driver v1.0 Initialization successfully.\n");

	return 0;

quit:
	printk("Winbond Audio Driver Initialization failed\n");
	wb_audio_exit();
	return -1;
}

module_init(wb_audio_init);
module_exit(wb_audio_exit);