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