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