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#include <linux/module.h>
179#include <linux/slab.h>
180#include <linux/sound.h>
181#include <linux/init.h>
182#include <linux/soundcard.h>
183#include <linux/poll.h>
184#include <linux/smp_lock.h>
185
186#include <asm/uaccess.h>
187
188#include "dmasound.h"
189
190#define DMASOUND_CORE_REVISION 1
191#define DMASOUND_CORE_EDITION 6
192
193
194
195
196
197int dmasound_catchRadius = 0;
198module_param(dmasound_catchRadius, int, 0);
199
200static unsigned int numWriteBufs = DEFAULT_N_BUFFERS;
201module_param(numWriteBufs, int, 0);
202static unsigned int writeBufSize = DEFAULT_BUFF_SIZE ;
203module_param(writeBufSize, int, 0);
204
205MODULE_LICENSE("GPL");
206
207#ifdef MODULE
208static int sq_unit = -1;
209static int mixer_unit = -1;
210static int state_unit = -1;
211static int irq_installed;
212#endif
213
214
215static fmode_t shared_resource_owner;
216static int shared_resources_initialised;
217
218
219
220
221
222struct sound_settings dmasound = {
223 .lock = __SPIN_LOCK_UNLOCKED(dmasound.lock)
224};
225
226static inline void sound_silence(void)
227{
228 dmasound.mach.silence();
229}
230
231static inline int sound_set_format(int format)
232{
233 return dmasound.mach.setFormat(format);
234}
235
236
237static int sound_set_speed(int speed)
238{
239 if (speed < 0)
240 return dmasound.soft.speed;
241
242
243
244
245
246
247 if (dmasound.mach.max_dsp_speed &&
248 (speed > dmasound.mach.max_dsp_speed))
249 speed = dmasound.mach.max_dsp_speed ;
250
251 dmasound.soft.speed = speed;
252
253 if (dmasound.minDev == SND_DEV_DSP)
254 dmasound.dsp.speed = dmasound.soft.speed;
255
256 return dmasound.soft.speed;
257}
258
259static int sound_set_stereo(int stereo)
260{
261 if (stereo < 0)
262 return dmasound.soft.stereo;
263
264 stereo = !!stereo;
265
266 dmasound.soft.stereo = stereo;
267 if (dmasound.minDev == SND_DEV_DSP)
268 dmasound.dsp.stereo = stereo;
269
270 return stereo;
271}
272
273static ssize_t sound_copy_translate(TRANS *trans, const u_char __user *userPtr,
274 size_t userCount, u_char frame[],
275 ssize_t *frameUsed, ssize_t frameLeft)
276{
277 ssize_t (*ct_func)(const u_char __user *, size_t, u_char *, ssize_t *, ssize_t);
278
279 switch (dmasound.soft.format) {
280 case AFMT_MU_LAW:
281 ct_func = trans->ct_ulaw;
282 break;
283 case AFMT_A_LAW:
284 ct_func = trans->ct_alaw;
285 break;
286 case AFMT_S8:
287 ct_func = trans->ct_s8;
288 break;
289 case AFMT_U8:
290 ct_func = trans->ct_u8;
291 break;
292 case AFMT_S16_BE:
293 ct_func = trans->ct_s16be;
294 break;
295 case AFMT_U16_BE:
296 ct_func = trans->ct_u16be;
297 break;
298 case AFMT_S16_LE:
299 ct_func = trans->ct_s16le;
300 break;
301 case AFMT_U16_LE:
302 ct_func = trans->ct_u16le;
303 break;
304 default:
305 return 0;
306 }
307
308
309
310 if (ct_func)
311 return ct_func(userPtr, userCount, frame, frameUsed, frameLeft);
312 return 0;
313}
314
315
316
317
318
319static struct {
320 int busy;
321 int modify_counter;
322} mixer;
323
324static int mixer_open(struct inode *inode, struct file *file)
325{
326 if (!try_module_get(dmasound.mach.owner))
327 return -ENODEV;
328 mixer.busy = 1;
329 return 0;
330}
331
332static int mixer_release(struct inode *inode, struct file *file)
333{
334 lock_kernel();
335 mixer.busy = 0;
336 module_put(dmasound.mach.owner);
337 unlock_kernel();
338 return 0;
339}
340static int mixer_ioctl(struct inode *inode, struct file *file, u_int cmd,
341 u_long arg)
342{
343 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
344 mixer.modify_counter++;
345 switch (cmd) {
346 case OSS_GETVERSION:
347 return IOCTL_OUT(arg, SOUND_VERSION);
348 case SOUND_MIXER_INFO:
349 {
350 mixer_info info;
351 memset(&info, 0, sizeof(info));
352 strlcpy(info.id, dmasound.mach.name2, sizeof(info.id));
353 strlcpy(info.name, dmasound.mach.name2, sizeof(info.name));
354 info.modify_counter = mixer.modify_counter;
355 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
356 return -EFAULT;
357 return 0;
358 }
359 }
360 if (dmasound.mach.mixer_ioctl)
361 return dmasound.mach.mixer_ioctl(cmd, arg);
362 return -EINVAL;
363}
364
365static const struct file_operations mixer_fops =
366{
367 .owner = THIS_MODULE,
368 .llseek = no_llseek,
369 .ioctl = mixer_ioctl,
370 .open = mixer_open,
371 .release = mixer_release,
372};
373
374static void mixer_init(void)
375{
376#ifndef MODULE
377 int mixer_unit;
378#endif
379 mixer_unit = register_sound_mixer(&mixer_fops, -1);
380 if (mixer_unit < 0)
381 return;
382
383 mixer.busy = 0;
384 dmasound.treble = 0;
385 dmasound.bass = 0;
386 if (dmasound.mach.mixer_init)
387 dmasound.mach.mixer_init();
388}
389
390
391
392
393
394
395struct sound_queue dmasound_write_sq;
396static void sq_reset_output(void) ;
397
398static int sq_allocate_buffers(struct sound_queue *sq, int num, int size)
399{
400 int i;
401
402 if (sq->buffers)
403 return 0;
404 sq->numBufs = num;
405 sq->bufSize = size;
406 sq->buffers = kmalloc (num * sizeof(char *), GFP_KERNEL);
407 if (!sq->buffers)
408 return -ENOMEM;
409 for (i = 0; i < num; i++) {
410 sq->buffers[i] = dmasound.mach.dma_alloc(size, GFP_KERNEL);
411 if (!sq->buffers[i]) {
412 while (i--)
413 dmasound.mach.dma_free(sq->buffers[i], size);
414 kfree(sq->buffers);
415 sq->buffers = NULL;
416 return -ENOMEM;
417 }
418 }
419 return 0;
420}
421
422static void sq_release_buffers(struct sound_queue *sq)
423{
424 int i;
425
426 if (sq->buffers) {
427 for (i = 0; i < sq->numBufs; i++)
428 dmasound.mach.dma_free(sq->buffers[i], sq->bufSize);
429 kfree(sq->buffers);
430 sq->buffers = NULL;
431 }
432}
433
434
435static int sq_setup(struct sound_queue *sq)
436{
437 int (*setup_func)(void) = NULL;
438 int hard_frame ;
439
440 if (sq->locked) {
441#ifdef DEBUG_DMASOUND
442printk("dmasound_core: tried to sq_setup a locked queue\n") ;
443#endif
444 return -EINVAL ;
445 }
446 sq->locked = 1 ;
447
448
449
450
451
452 dmasound.mach.init();
453
454
455
456
457
458
459
460
461
462
463
464
465
466 if (sq->user_frags <= 0) {
467 sq->max_count = sq->numBufs ;
468 sq->max_active = sq->numBufs ;
469 sq->block_size = sq->bufSize;
470
471 sq->user_frags = sq->numBufs ;
472 sq->user_frag_size = sq->bufSize ;
473 sq->user_frag_size *=
474 (dmasound.soft.size * (dmasound.soft.stereo+1) ) ;
475 sq->user_frag_size /=
476 (dmasound.hard.size * (dmasound.hard.stereo+1) ) ;
477 } else {
478
479 sq->block_size = sq->user_frag_size ;
480 sq->block_size *=
481 (dmasound.hard.size * (dmasound.hard.stereo+1) ) ;
482 sq->block_size /=
483 (dmasound.soft.size * (dmasound.soft.stereo+1) ) ;
484
485 sq->block_size *= dmasound.hard.speed ;
486 sq->block_size /= dmasound.soft.speed ;
487
488 hard_frame =
489 (dmasound.hard.size * (dmasound.hard.stereo+1))/8 ;
490 sq->block_size += (hard_frame - 1) ;
491 sq->block_size &= ~(hard_frame - 1) ;
492
493 if ( sq->block_size <= 0 || sq->block_size > sq->bufSize) {
494#ifdef DEBUG_DMASOUND
495printk("dmasound_core: invalid frag size (user set %d)\n", sq->user_frag_size) ;
496#endif
497 sq->block_size = sq->bufSize ;
498 }
499 if ( sq->user_frags <= sq->numBufs ) {
500 sq->max_count = sq->user_frags ;
501
502 sq->max_active = (sq->max_active <= sq->max_count) ?
503 sq->max_active : sq->max_count ;
504 } else {
505#ifdef DEBUG_DMASOUND
506printk("dmasound_core: invalid frag count (user set %d)\n", sq->user_frags) ;
507#endif
508 sq->max_count =
509 sq->max_active = sq->numBufs ;
510 }
511 }
512 sq->front = sq->count = sq->rear_size = 0;
513 sq->syncing = 0;
514 sq->active = 0;
515
516 if (sq == &write_sq) {
517 sq->rear = -1;
518 setup_func = dmasound.mach.write_sq_setup;
519 }
520 if (setup_func)
521 return setup_func();
522 return 0 ;
523}
524
525static inline void sq_play(void)
526{
527 dmasound.mach.play();
528}
529
530static ssize_t sq_write(struct file *file, const char __user *src, size_t uLeft,
531 loff_t *ppos)
532{
533 ssize_t uWritten = 0;
534 u_char *dest;
535 ssize_t uUsed = 0, bUsed, bLeft;
536 unsigned long flags ;
537
538
539
540
541
542 if (uLeft == 0)
543 return 0;
544
545
546
547
548
549
550 if (shared_resources_initialised == 0) {
551 dmasound.mach.init() ;
552 shared_resources_initialised = 1 ;
553 }
554
555
556
557
558
559
560
561
562 if (write_sq.locked == 0) {
563 if ((uWritten = sq_setup(&write_sq)) < 0) return uWritten ;
564 uWritten = 0 ;
565 }
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587 spin_lock_irqsave(&dmasound.lock, flags);
588 write_sq.syncing &= ~2 ;
589 spin_unlock_irqrestore(&dmasound.lock, flags);
590
591 if (write_sq.count > 0 &&
592 (bLeft = write_sq.block_size-write_sq.rear_size) > 0) {
593 dest = write_sq.buffers[write_sq.rear];
594 bUsed = write_sq.rear_size;
595 uUsed = sound_copy_translate(dmasound.trans_write, src, uLeft,
596 dest, &bUsed, bLeft);
597 if (uUsed <= 0)
598 return uUsed;
599 src += uUsed;
600 uWritten += uUsed;
601 uLeft = (uUsed <= uLeft) ? (uLeft - uUsed) : 0 ;
602 write_sq.rear_size = bUsed;
603 }
604
605 while (uLeft) {
606 while (write_sq.count >= write_sq.max_active) {
607 sq_play();
608 if (write_sq.non_blocking)
609 return uWritten > 0 ? uWritten : -EAGAIN;
610 SLEEP(write_sq.action_queue);
611 if (signal_pending(current))
612 return uWritten > 0 ? uWritten : -EINTR;
613 }
614
615
616
617
618
619
620
621
622 dest = write_sq.buffers[(write_sq.rear+1) % write_sq.max_count];
623 bUsed = 0;
624 bLeft = write_sq.block_size;
625 uUsed = sound_copy_translate(dmasound.trans_write, src, uLeft,
626 dest, &bUsed, bLeft);
627 if (uUsed <= 0)
628 break;
629 src += uUsed;
630 uWritten += uUsed;
631 uLeft = (uUsed <= uLeft) ? (uLeft - uUsed) : 0 ;
632 if (bUsed) {
633 write_sq.rear = (write_sq.rear+1) % write_sq.max_count;
634 write_sq.rear_size = bUsed;
635 write_sq.count++;
636 }
637 }
638
639 sq_play();
640
641 return uUsed < 0? uUsed: uWritten;
642}
643
644static unsigned int sq_poll(struct file *file, struct poll_table_struct *wait)
645{
646 unsigned int mask = 0;
647 int retVal;
648
649 if (write_sq.locked == 0) {
650 if ((retVal = sq_setup(&write_sq)) < 0)
651 return retVal;
652 return 0;
653 }
654 if (file->f_mode & FMODE_WRITE )
655 poll_wait(file, &write_sq.action_queue, wait);
656 if (file->f_mode & FMODE_WRITE)
657 if (write_sq.count < write_sq.max_active || write_sq.block_size - write_sq.rear_size > 0)
658 mask |= POLLOUT | POLLWRNORM;
659 return mask;
660
661}
662
663static inline void sq_init_waitqueue(struct sound_queue *sq)
664{
665 init_waitqueue_head(&sq->action_queue);
666 init_waitqueue_head(&sq->open_queue);
667 init_waitqueue_head(&sq->sync_queue);
668 sq->busy = 0;
669}
670
671#if 0
672static inline void sq_wake_up(struct sound_queue *sq, struct file *file,
673 fmode_t mode)
674{
675 if (file->f_mode & mode) {
676 sq->busy = 0;
677 WAKE_UP(sq->open_queue);
678 }
679}
680#endif
681
682static int sq_open2(struct sound_queue *sq, struct file *file, fmode_t mode,
683 int numbufs, int bufsize)
684{
685 int rc = 0;
686
687 if (file->f_mode & mode) {
688 if (sq->busy) {
689#if 0
690 rc = -EBUSY;
691 if (file->f_flags & O_NONBLOCK)
692 return rc;
693 rc = -EINTR;
694 while (sq->busy) {
695 SLEEP(sq->open_queue);
696 if (signal_pending(current))
697 return rc;
698 }
699 rc = 0;
700#else
701
702
703
704 return -EBUSY ;
705#endif
706 }
707 sq->busy = 1;
708
709
710
711
712
713
714 if (( rc = sq_allocate_buffers(sq, numbufs, bufsize))) {
715#if 0
716 sq_wake_up(sq, file, mode);
717#else
718 sq->busy = 0 ;
719#endif
720 return rc;
721 }
722
723 sq->non_blocking = file->f_flags & O_NONBLOCK;
724 }
725 return rc;
726}
727
728#define write_sq_init_waitqueue() sq_init_waitqueue(&write_sq)
729#if 0
730#define write_sq_wake_up(file) sq_wake_up(&write_sq, file, FMODE_WRITE)
731#endif
732#define write_sq_release_buffers() sq_release_buffers(&write_sq)
733#define write_sq_open(file) \
734 sq_open2(&write_sq, file, FMODE_WRITE, numWriteBufs, writeBufSize )
735
736static int sq_open(struct inode *inode, struct file *file)
737{
738 int rc;
739
740 if (!try_module_get(dmasound.mach.owner))
741 return -ENODEV;
742
743 rc = write_sq_open(file);
744 if (rc)
745 goto out;
746 if (file->f_mode & FMODE_READ) {
747
748 rc = -ENXIO ;
749 goto out;
750 }
751
752 if (dmasound.mach.sq_open)
753 dmasound.mach.sq_open(file->f_mode);
754
755
756
757
758
759 dmasound.minDev = iminor(inode) & 0x0f;
760
761
762
763
764
765
766 if (shared_resource_owner == 0) {
767
768
769 dmasound.soft = dmasound.mach.default_soft ;
770 dmasound.dsp = dmasound.mach.default_soft ;
771 dmasound.hard = dmasound.mach.default_hard ;
772 }
773
774#ifndef DMASOUND_STRICT_OSS_COMPLIANCE
775
776
777
778 if (dmasound.minDev == SND_DEV_AUDIO) {
779 sound_set_speed(8000);
780 sound_set_stereo(0);
781 sound_set_format(AFMT_MU_LAW);
782 }
783#endif
784
785 return 0;
786 out:
787 module_put(dmasound.mach.owner);
788 return rc;
789}
790
791static void sq_reset_output(void)
792{
793 sound_silence();
794 write_sq.active = 0;
795 write_sq.count = 0;
796 write_sq.rear_size = 0;
797
798 write_sq.front = 0 ;
799 write_sq.rear = -1 ;
800
801
802 write_sq.locked = 0 ;
803 write_sq.user_frags = 0 ;
804 write_sq.user_frag_size = 0 ;
805}
806
807static void sq_reset(void)
808{
809 sq_reset_output() ;
810
811
812
813
814
815 shared_resources_initialised = 0 ;
816}
817
818static int sq_fsync(struct file *filp, struct dentry *dentry)
819{
820 int rc = 0;
821 int timeout = 5;
822
823 write_sq.syncing |= 1;
824 sq_play();
825
826 while (write_sq.active) {
827 SLEEP(write_sq.sync_queue);
828 if (signal_pending(current)) {
829
830
831
832 sq_reset_output();
833 rc = -EINTR;
834 break;
835 }
836 if (!--timeout) {
837 printk(KERN_WARNING "dmasound: Timeout draining output\n");
838 sq_reset_output();
839 rc = -EIO;
840 break;
841 }
842 }
843
844
845 write_sq.syncing = 0 ;
846 return rc;
847}
848
849static int sq_release(struct inode *inode, struct file *file)
850{
851 int rc = 0;
852
853 lock_kernel();
854
855 if (file->f_mode & FMODE_WRITE) {
856 if (write_sq.busy)
857 rc = sq_fsync(file, file->f_path.dentry);
858
859 sq_reset_output() ;
860 write_sq_release_buffers();
861 write_sq.busy = 0;
862 }
863
864 if (file->f_mode & shared_resource_owner) {
865 shared_resource_owner = 0 ;
866 shared_resources_initialised = 0 ;
867 dmasound.hard = dmasound.mach.default_hard ;
868 }
869
870 module_put(dmasound.mach.owner);
871
872#if 0
873
874
875
876
877
878
879
880 read_sq_wake_up(file);
881 write_sq_wake_up(file);
882#endif
883
884 unlock_kernel();
885
886 return rc;
887}
888
889
890
891
892
893
894
895
896static int shared_resources_are_mine(fmode_t md)
897{
898 if (shared_resource_owner)
899 return (shared_resource_owner & md) != 0;
900 else {
901 shared_resource_owner = md ;
902 return 1 ;
903 }
904}
905
906
907
908
909static int queues_are_quiescent(void)
910{
911 if (write_sq.locked)
912 return 0 ;
913 return 1 ;
914}
915
916
917
918
919
920
921
922
923
924
925
926static int set_queue_frags(struct sound_queue *sq, int bufs, int size)
927{
928 if (sq->locked) {
929#ifdef DEBUG_DMASOUND
930printk("dmasound_core: tried to set_queue_frags on a locked queue\n") ;
931#endif
932 return -EINVAL ;
933 }
934
935 if ((size < MIN_FRAG_SIZE) || (size > MAX_FRAG_SIZE))
936 return -EINVAL ;
937 size = (1<<size) ;
938 if (size > sq->bufSize)
939 return -EINVAL ;
940
941 if (bufs <= 0)
942 return -EINVAL ;
943 if (bufs > sq->numBufs)
944 bufs = sq->numBufs ;
945
946
947
948
949
950
951 sq->user_frags =
952 sq->max_active = bufs ;
953 sq->user_frag_size = size ;
954
955 return 0 ;
956}
957
958static int sq_ioctl(struct inode *inode, struct file *file, u_int cmd,
959 u_long arg)
960{
961 int val, result;
962 u_long fmt;
963 int data;
964 int size, nbufs;
965 audio_buf_info info;
966
967 switch (cmd) {
968 case SNDCTL_DSP_RESET:
969 sq_reset();
970 return 0;
971 break ;
972 case SNDCTL_DSP_GETFMTS:
973 fmt = dmasound.mach.hardware_afmts ;
974 return IOCTL_OUT(arg, fmt);
975 break ;
976 case SNDCTL_DSP_GETBLKSIZE:
977
978
979
980
981
982
983
984
985 size = 0 ;
986 if (file->f_mode & FMODE_WRITE) {
987 if ( !write_sq.locked )
988 sq_setup(&write_sq) ;
989 size = write_sq.user_frag_size ;
990 }
991 return IOCTL_OUT(arg, size);
992 break ;
993 case SNDCTL_DSP_POST:
994
995
996
997
998
999 write_sq.syncing |= 0x2 ;
1000 sq_play() ;
1001 return 0 ;
1002 case SNDCTL_DSP_SYNC:
1003
1004
1005
1006
1007 result = 0 ;
1008 if (file->f_mode & FMODE_WRITE) {
1009 result = sq_fsync(file, file->f_path.dentry);
1010 sq_reset_output() ;
1011 }
1012
1013 if (file->f_mode & shared_resource_owner)
1014 shared_resources_initialised = 0 ;
1015 return result ;
1016 break ;
1017 case SOUND_PCM_READ_RATE:
1018 return IOCTL_OUT(arg, dmasound.soft.speed);
1019 case SNDCTL_DSP_SPEED:
1020
1021
1022
1023
1024
1025
1026 if (shared_resources_are_mine(file->f_mode)) {
1027 IOCTL_IN(arg, data);
1028 data = sound_set_speed(data) ;
1029 shared_resources_initialised = 0 ;
1030 return IOCTL_OUT(arg, data);
1031 } else
1032 return -EINVAL ;
1033 break ;
1034
1035
1036
1037
1038
1039 case SNDCTL_DSP_STEREO:
1040 if (shared_resources_are_mine(file->f_mode) &&
1041 queues_are_quiescent()) {
1042 IOCTL_IN(arg, data);
1043 shared_resources_initialised = 0 ;
1044 return IOCTL_OUT(arg, sound_set_stereo(data));
1045 } else
1046 return -EINVAL ;
1047 break ;
1048 case SOUND_PCM_WRITE_CHANNELS:
1049 if (shared_resources_are_mine(file->f_mode) &&
1050 queues_are_quiescent()) {
1051 IOCTL_IN(arg, data);
1052
1053 shared_resources_initialised = 0 ;
1054 return IOCTL_OUT(arg, sound_set_stereo(data-1)+1);
1055 } else
1056 return -EINVAL ;
1057 break ;
1058 case SNDCTL_DSP_SETFMT:
1059 if (shared_resources_are_mine(file->f_mode) &&
1060 queues_are_quiescent()) {
1061 int format;
1062 IOCTL_IN(arg, data);
1063 shared_resources_initialised = 0 ;
1064 format = sound_set_format(data);
1065 result = IOCTL_OUT(arg, format);
1066 if (result < 0)
1067 return result;
1068 if (format != data && data != AFMT_QUERY)
1069 return -EINVAL;
1070 return 0;
1071 } else
1072 return -EINVAL ;
1073 case SNDCTL_DSP_SUBDIVIDE:
1074 return -EINVAL ;
1075 case SNDCTL_DSP_SETFRAGMENT:
1076
1077
1078
1079
1080
1081
1082 IOCTL_IN(arg, data);
1083 result = 0 ;
1084 nbufs = (data >> 16) & 0x7fff ;
1085 size = data & 0xffff;
1086 if (file->f_mode & FMODE_WRITE) {
1087 result = set_queue_frags(&write_sq, nbufs, size) ;
1088 if (result)
1089 return result ;
1090 }
1091
1092
1093
1094 return IOCTL_OUT(arg, data);
1095 break ;
1096 case SNDCTL_DSP_GETOSPACE:
1097
1098
1099 if (file->f_mode & FMODE_WRITE) {
1100 if ( !write_sq.locked )
1101 sq_setup(&write_sq) ;
1102 info.fragments = write_sq.max_active - write_sq.count;
1103 info.fragstotal = write_sq.max_active;
1104 info.fragsize = write_sq.user_frag_size;
1105 info.bytes = info.fragments * info.fragsize;
1106 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
1107 return -EFAULT;
1108 return 0;
1109 } else
1110 return -EINVAL ;
1111 break ;
1112 case SNDCTL_DSP_GETCAPS:
1113 val = dmasound.mach.capabilities & 0xffffff00;
1114 return IOCTL_OUT(arg,val);
1115
1116 default:
1117 return mixer_ioctl(inode, file, cmd, arg);
1118 }
1119 return -EINVAL;
1120}
1121
1122static const struct file_operations sq_fops =
1123{
1124 .owner = THIS_MODULE,
1125 .llseek = no_llseek,
1126 .write = sq_write,
1127 .poll = sq_poll,
1128 .ioctl = sq_ioctl,
1129 .open = sq_open,
1130 .release = sq_release,
1131};
1132
1133static int sq_init(void)
1134{
1135 const struct file_operations *fops = &sq_fops;
1136#ifndef MODULE
1137 int sq_unit;
1138#endif
1139
1140 sq_unit = register_sound_dsp(fops, -1);
1141 if (sq_unit < 0) {
1142 printk(KERN_ERR "dmasound_core: couldn't register fops\n") ;
1143 return sq_unit ;
1144 }
1145
1146 write_sq_init_waitqueue();
1147
1148
1149
1150
1151
1152
1153 if (shared_resource_owner == 0) {
1154 dmasound.soft = dmasound.mach.default_soft ;
1155 dmasound.hard = dmasound.mach.default_hard ;
1156 dmasound.dsp = dmasound.mach.default_soft ;
1157 shared_resources_initialised = 0 ;
1158 }
1159 return 0 ;
1160}
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172#define STAT_BUFF_LEN 768
1173
1174
1175
1176
1177
1178
1179#define LOW_LEVEL_STAT_ALLOC 162
1180
1181static struct {
1182 int busy;
1183 char buf[STAT_BUFF_LEN];
1184 int len, ptr;
1185} state;
1186
1187
1188
1189static char *get_afmt_string(int afmt)
1190{
1191 switch(afmt) {
1192 case AFMT_MU_LAW:
1193 return "mu-law";
1194 break;
1195 case AFMT_A_LAW:
1196 return "A-law";
1197 break;
1198 case AFMT_U8:
1199 return "unsigned 8 bit";
1200 break;
1201 case AFMT_S8:
1202 return "signed 8 bit";
1203 break;
1204 case AFMT_S16_BE:
1205 return "signed 16 bit BE";
1206 break;
1207 case AFMT_U16_BE:
1208 return "unsigned 16 bit BE";
1209 break;
1210 case AFMT_S16_LE:
1211 return "signed 16 bit LE";
1212 break;
1213 case AFMT_U16_LE:
1214 return "unsigned 16 bit LE";
1215 break;
1216 case 0:
1217 return "format not set" ;
1218 break ;
1219 default:
1220 break ;
1221 }
1222 return "ERROR: Unsupported AFMT_XXXX code" ;
1223}
1224
1225static int state_open(struct inode *inode, struct file *file)
1226{
1227 char *buffer = state.buf;
1228 int len = 0;
1229
1230 if (state.busy)
1231 return -EBUSY;
1232
1233 if (!try_module_get(dmasound.mach.owner))
1234 return -ENODEV;
1235 state.ptr = 0;
1236 state.busy = 1;
1237
1238 len += sprintf(buffer+len, "%sDMA sound driver rev %03d :\n",
1239 dmasound.mach.name, (DMASOUND_CORE_REVISION<<4) +
1240 ((dmasound.mach.version>>8) & 0x0f));
1241 len += sprintf(buffer+len,
1242 "Core driver edition %02d.%02d : %s driver edition %02d.%02d\n",
1243 DMASOUND_CORE_REVISION, DMASOUND_CORE_EDITION, dmasound.mach.name2,
1244 (dmasound.mach.version >> 8), (dmasound.mach.version & 0xff)) ;
1245
1246
1247
1248
1249
1250 if (dmasound.mach.state_info)
1251 len += dmasound.mach.state_info(buffer+len,
1252 (size_t) LOW_LEVEL_STAT_ALLOC) ;
1253
1254
1255
1256
1257
1258
1259
1260
1261 len += sprintf(buffer+len,"\t\t === Formats & settings ===\n") ;
1262 len += sprintf(buffer+len,"Parameter %20s%20s\n","soft","hard") ;
1263 len += sprintf(buffer+len,"Format :%20s%20s\n",
1264 get_afmt_string(dmasound.soft.format),
1265 get_afmt_string(dmasound.hard.format));
1266
1267 len += sprintf(buffer+len,"Samp Rate:%14d s/sec%14d s/sec\n",
1268 dmasound.soft.speed, dmasound.hard.speed);
1269
1270 len += sprintf(buffer+len,"Channels :%20s%20s\n",
1271 dmasound.soft.stereo ? "stereo" : "mono",
1272 dmasound.hard.stereo ? "stereo" : "mono" );
1273
1274
1275
1276 len += sprintf(buffer+len,"\t\t === Sound Queue status ===\n");
1277 len += sprintf(buffer+len,"Allocated:%8s%6s\n","Buffers","Size") ;
1278 len += sprintf(buffer+len,"%9s:%8d%6d\n",
1279 "write", write_sq.numBufs, write_sq.bufSize) ;
1280 len += sprintf(buffer+len,
1281 "Current : MaxFrg FragSiz MaxAct Frnt Rear "
1282 "Cnt RrSize A B S L xruns\n") ;
1283 len += sprintf(buffer+len,"%9s:%7d%8d%7d%5d%5d%4d%7d%2d%2d%2d%2d%7d\n",
1284 "write", write_sq.max_count, write_sq.block_size,
1285 write_sq.max_active, write_sq.front, write_sq.rear,
1286 write_sq.count, write_sq.rear_size, write_sq.active,
1287 write_sq.busy, write_sq.syncing, write_sq.locked, write_sq.xruns) ;
1288#ifdef DEBUG_DMASOUND
1289printk("dmasound: stat buffer used %d bytes\n", len) ;
1290#endif
1291
1292 if (len >= STAT_BUFF_LEN)
1293 printk(KERN_ERR "dmasound_core: stat buffer overflowed!\n");
1294
1295 state.len = len;
1296 return 0;
1297}
1298
1299static int state_release(struct inode *inode, struct file *file)
1300{
1301 lock_kernel();
1302 state.busy = 0;
1303 module_put(dmasound.mach.owner);
1304 unlock_kernel();
1305 return 0;
1306}
1307
1308static ssize_t state_read(struct file *file, char __user *buf, size_t count,
1309 loff_t *ppos)
1310{
1311 int n = state.len - state.ptr;
1312 if (n > count)
1313 n = count;
1314 if (n <= 0)
1315 return 0;
1316 if (copy_to_user(buf, &state.buf[state.ptr], n))
1317 return -EFAULT;
1318 state.ptr += n;
1319 return n;
1320}
1321
1322static const struct file_operations state_fops = {
1323 .owner = THIS_MODULE,
1324 .llseek = no_llseek,
1325 .read = state_read,
1326 .open = state_open,
1327 .release = state_release,
1328};
1329
1330static int state_init(void)
1331{
1332#ifndef MODULE
1333 int state_unit;
1334#endif
1335 state_unit = register_sound_special(&state_fops, SND_DEV_STATUS);
1336 if (state_unit < 0)
1337 return state_unit ;
1338 state.busy = 0;
1339 return 0 ;
1340}
1341
1342
1343
1344
1345
1346
1347
1348
1349int dmasound_init(void)
1350{
1351 int res ;
1352#ifdef MODULE
1353 if (irq_installed)
1354 return -EBUSY;
1355#endif
1356
1357
1358
1359
1360 if ((res = sq_init()) < 0)
1361 return res ;
1362
1363
1364 if ((res = state_init()) < 0)
1365 return res ;
1366
1367
1368 mixer_init();
1369
1370 if (!dmasound.mach.irqinit()) {
1371 printk(KERN_ERR "DMA sound driver: Interrupt initialization failed\n");
1372 return -ENODEV;
1373 }
1374#ifdef MODULE
1375 irq_installed = 1;
1376#endif
1377
1378 printk(KERN_INFO "%s DMA sound driver rev %03d installed\n",
1379 dmasound.mach.name, (DMASOUND_CORE_REVISION<<4) +
1380 ((dmasound.mach.version>>8) & 0x0f));
1381 printk(KERN_INFO
1382 "Core driver edition %02d.%02d : %s driver edition %02d.%02d\n",
1383 DMASOUND_CORE_REVISION, DMASOUND_CORE_EDITION, dmasound.mach.name2,
1384 (dmasound.mach.version >> 8), (dmasound.mach.version & 0xff)) ;
1385 printk(KERN_INFO "Write will use %4d fragments of %7d bytes as default\n",
1386 numWriteBufs, writeBufSize) ;
1387 return 0;
1388}
1389
1390#ifdef MODULE
1391
1392void dmasound_deinit(void)
1393{
1394 if (irq_installed) {
1395 sound_silence();
1396 dmasound.mach.irqcleanup();
1397 irq_installed = 0;
1398 }
1399
1400 write_sq_release_buffers();
1401
1402 if (mixer_unit >= 0)
1403 unregister_sound_mixer(mixer_unit);
1404 if (state_unit >= 0)
1405 unregister_sound_special(state_unit);
1406 if (sq_unit >= 0)
1407 unregister_sound_dsp(sq_unit);
1408}
1409
1410#else
1411
1412static int dmasound_setup(char *str)
1413{
1414 int ints[6], size;
1415
1416 str = get_options(str, ARRAY_SIZE(ints), ints);
1417
1418
1419
1420
1421
1422
1423
1424 switch (ints[0]) {
1425 case 3:
1426 if ((ints[3] < 0) || (ints[3] > MAX_CATCH_RADIUS))
1427 printk("dmasound_setup: invalid catch radius, using default = %d\n", catchRadius);
1428 else
1429 catchRadius = ints[3];
1430
1431 case 2:
1432 if (ints[1] < MIN_BUFFERS)
1433 printk("dmasound_setup: invalid number of buffers, using default = %d\n", numWriteBufs);
1434 else
1435 numWriteBufs = ints[1];
1436
1437 case 1:
1438 if ((size = ints[2]) < 256)
1439 size <<= 10 ;
1440 if (size < MIN_BUFSIZE || size > MAX_BUFSIZE)
1441 printk("dmasound_setup: invalid write buffer size, using default = %d\n", writeBufSize);
1442 else
1443 writeBufSize = size;
1444 case 0:
1445 break;
1446 default:
1447 printk("dmasound_setup: invalid number of arguments\n");
1448 return 0;
1449 }
1450 return 1;
1451}
1452
1453__setup("dmasound=", dmasound_setup);
1454
1455#endif
1456
1457
1458
1459
1460
1461#ifdef HAS_8BIT_TABLES
1462
1463
1464char dmasound_ulaw2dma8[] = {
1465 -126, -122, -118, -114, -110, -106, -102, -98,
1466 -94, -90, -86, -82, -78, -74, -70, -66,
1467 -63, -61, -59, -57, -55, -53, -51, -49,
1468 -47, -45, -43, -41, -39, -37, -35, -33,
1469 -31, -30, -29, -28, -27, -26, -25, -24,
1470 -23, -22, -21, -20, -19, -18, -17, -16,
1471 -16, -15, -15, -14, -14, -13, -13, -12,
1472 -12, -11, -11, -10, -10, -9, -9, -8,
1473 -8, -8, -7, -7, -7, -7, -6, -6,
1474 -6, -6, -5, -5, -5, -5, -4, -4,
1475 -4, -4, -4, -4, -3, -3, -3, -3,
1476 -3, -3, -3, -3, -2, -2, -2, -2,
1477 -2, -2, -2, -2, -2, -2, -2, -2,
1478 -1, -1, -1, -1, -1, -1, -1, -1,
1479 -1, -1, -1, -1, -1, -1, -1, -1,
1480 -1, -1, -1, -1, -1, -1, -1, 0,
1481 125, 121, 117, 113, 109, 105, 101, 97,
1482 93, 89, 85, 81, 77, 73, 69, 65,
1483 62, 60, 58, 56, 54, 52, 50, 48,
1484 46, 44, 42, 40, 38, 36, 34, 32,
1485 30, 29, 28, 27, 26, 25, 24, 23,
1486 22, 21, 20, 19, 18, 17, 16, 15,
1487 15, 14, 14, 13, 13, 12, 12, 11,
1488 11, 10, 10, 9, 9, 8, 8, 7,
1489 7, 7, 6, 6, 6, 6, 5, 5,
1490 5, 5, 4, 4, 4, 4, 3, 3,
1491 3, 3, 3, 3, 2, 2, 2, 2,
1492 2, 2, 2, 2, 1, 1, 1, 1,
1493 1, 1, 1, 1, 1, 1, 1, 1,
1494 0, 0, 0, 0, 0, 0, 0, 0,
1495 0, 0, 0, 0, 0, 0, 0, 0,
1496 0, 0, 0, 0, 0, 0, 0, 0
1497};
1498
1499
1500
1501char dmasound_alaw2dma8[] = {
1502 -22, -21, -24, -23, -18, -17, -20, -19,
1503 -30, -29, -32, -31, -26, -25, -28, -27,
1504 -11, -11, -12, -12, -9, -9, -10, -10,
1505 -15, -15, -16, -16, -13, -13, -14, -14,
1506 -86, -82, -94, -90, -70, -66, -78, -74,
1507 -118, -114, -126, -122, -102, -98, -110, -106,
1508 -43, -41, -47, -45, -35, -33, -39, -37,
1509 -59, -57, -63, -61, -51, -49, -55, -53,
1510 -2, -2, -2, -2, -2, -2, -2, -2,
1511 -2, -2, -2, -2, -2, -2, -2, -2,
1512 -1, -1, -1, -1, -1, -1, -1, -1,
1513 -1, -1, -1, -1, -1, -1, -1, -1,
1514 -6, -6, -6, -6, -5, -5, -5, -5,
1515 -8, -8, -8, -8, -7, -7, -7, -7,
1516 -3, -3, -3, -3, -3, -3, -3, -3,
1517 -4, -4, -4, -4, -4, -4, -4, -4,
1518 21, 20, 23, 22, 17, 16, 19, 18,
1519 29, 28, 31, 30, 25, 24, 27, 26,
1520 10, 10, 11, 11, 8, 8, 9, 9,
1521 14, 14, 15, 15, 12, 12, 13, 13,
1522 86, 82, 94, 90, 70, 66, 78, 74,
1523 118, 114, 126, 122, 102, 98, 110, 106,
1524 43, 41, 47, 45, 35, 33, 39, 37,
1525 59, 57, 63, 61, 51, 49, 55, 53,
1526 1, 1, 1, 1, 1, 1, 1, 1,
1527 1, 1, 1, 1, 1, 1, 1, 1,
1528 0, 0, 0, 0, 0, 0, 0, 0,
1529 0, 0, 0, 0, 0, 0, 0, 0,
1530 5, 5, 5, 5, 4, 4, 4, 4,
1531 7, 7, 7, 7, 6, 6, 6, 6,
1532 2, 2, 2, 2, 2, 2, 2, 2,
1533 3, 3, 3, 3, 3, 3, 3, 3
1534};
1535#endif
1536
1537
1538
1539
1540
1541EXPORT_SYMBOL(dmasound);
1542EXPORT_SYMBOL(dmasound_init);
1543#ifdef MODULE
1544EXPORT_SYMBOL(dmasound_deinit);
1545#endif
1546EXPORT_SYMBOL(dmasound_write_sq);
1547EXPORT_SYMBOL(dmasound_catchRadius);
1548#ifdef HAS_8BIT_TABLES
1549EXPORT_SYMBOL(dmasound_ulaw2dma8);
1550EXPORT_SYMBOL(dmasound_alaw2dma8);
1551#endif
1552