kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / sound / isa / i386 / sound_switch.c
1 /*
2  * sound/sound_switch.c
3  * 
4  * The system call switch
5  * 
6  * Copyright by Hannu Savolainen 1993
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met: 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer. 2.
12  * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  * 
28  * $DragonFly: src/sys/dev/sound/isa/i386/Attic/sound_switch.c,v 1.2 2003/08/07 21:17:12 dillon Exp $
29  */
30
31 #include "sound_config.h"
32
33 #if NSND > 0
34
35 #define SNDSTAT_BUF_SIZE        4000
36
37 /*
38  * /dev/sndstatus -device
39  */
40 static char    *status_buf = NULL;
41 static int      status_len, status_ptr;
42 static int      status_busy = 0;
43
44 static int
45 put_status(char *s)
46 {
47     int             l = strlen(s);
48
49     if (status_len + l >= SNDSTAT_BUF_SIZE)
50         return 0;
51
52     bcopy(s, &status_buf[status_len], l);
53     status_len += l;
54
55     return 1;
56 }
57
58 /*
59  * in principle, we never overflow the buffer. But... if radix=2 ...
60  * and if smaller... lr970711
61  */
62
63 static int
64 put_status_int(u_int val, int radix)
65 {
66     int             l, v;
67     static char     hx[] = "0123456789abcdef";
68     char            buf[33]; /* int is 32 bit+null, in base 2 */
69
70     if (radix < 2 || radix > 16)        /* better than panic */
71         return put_status("???");
72
73     if (!val)
74         return put_status("0");
75
76     l = 0;
77     buf[10] = 0;
78
79     while (val) {
80         v = val % radix;
81         val = val / radix;
82
83         buf[9 - l] = hx[v];
84         l++;
85     }
86
87     if (status_len + l >= SNDSTAT_BUF_SIZE)
88         return 0;
89
90     bcopy(&buf[10 - l], &status_buf[status_len], l);
91     status_len += l;
92
93     return 1;
94 }
95
96 static void
97 init_status(void)
98 {
99     /*
100      * Write the status information to the status_buf and update
101      * status_len. There is a limit of SNDSTAT_BUF_SIZE bytes for the data.
102      * put_status handles this and returns 0 in case of failure. Since
103      * it never oveflows the buffer, we do not care to check.
104      */
105
106     int             i;
107
108     status_ptr = 0;
109
110 #ifdef SOUND_UNAME_A
111     put_status("VoxWare Sound Driver:" SOUND_VERSION_STRING
112                " (" SOUND_CONFIG_DATE " " SOUND_CONFIG_BY ",\n"
113                SOUND_UNAME_A ")\n");
114 #else
115     put_status("VoxWare Sound Driver:" SOUND_VERSION_STRING
116                " (" SOUND_CONFIG_DATE " " SOUND_CONFIG_BY "@"
117                SOUND_CONFIG_HOST "." SOUND_CONFIG_DOMAIN ")\n");
118 #endif
119
120     put_status("Config options: ") ;
121     /*   put_status_int(SELECTED_SOUND_OPTIONS, 16) ; */
122     put_status("\n\nInstalled drivers: \n") ;
123
124     for (i = 0; i < num_sound_drivers; i++)
125         if (sound_drivers[i].card_type != 0) {
126             put_status("Type ") ;
127             put_status_int(sound_drivers[i].card_type, 10);
128             put_status(": ") ;
129             put_status(sound_drivers[i].name) ;
130             put_status("\n") ;
131         }
132     put_status("\n\nCard config: \n") ;
133
134     for (i = 0; i < num_sound_cards; i++)
135         if (snd_installed_cards[i].card_type != 0) {
136             int             drv, tmp;
137
138             if (!snd_installed_cards[i].enabled)
139                 put_status("(") ;
140
141             if ((drv = snd_find_driver(snd_installed_cards[i].card_type)) != -1)
142                 put_status(sound_drivers[drv].name) ;
143
144             put_status(" at 0x") ;
145             put_status_int(snd_installed_cards[i].config.io_base, 16);
146
147             put_status(" irq ") ;
148             tmp = snd_installed_cards[i].config.irq;
149             if (tmp < 0)
150                 tmp = -tmp;
151             put_status_int(tmp, 10) ;
152
153             if (snd_installed_cards[i].config.dma != -1) {
154                 put_status(" drq ") ;
155                 put_status_int(snd_installed_cards[i].config.dma, 10) ;
156                 if (snd_installed_cards[i].config.dma2 != -1) {
157                     put_status(",") ;
158                     put_status_int(snd_installed_cards[i].config.dma2, 10) ;
159                 }
160             }
161             if (!snd_installed_cards[i].enabled)
162                 put_status(")") ;
163
164             put_status("\n") ;
165         }
166     if (!sound_started) {
167         put_status("\n\n***** Sound driver not started *****\n\n");
168         return;
169     }
170 #ifndef CONFIG_AUDIO
171     put_status("\nAudio devices: NOT ENABLED IN CONFIG\n") ;
172 #else
173     put_status("\nAudio devices:\n") ;
174
175     for (i = 0; i < num_audiodevs; i++) {
176         put_status_int(i, 10) ;
177         put_status(": ") ;
178         put_status(audio_devs[i]->name) ;
179
180         if (audio_devs[i]->flags & DMA_DUPLEX)
181             put_status(" (DUPLEX)") ;
182
183         put_status("\n") ;
184     }
185 #endif
186
187 #ifndef CONFIG_SEQUENCER
188     put_status("\nSynth devices: NOT ENABLED IN CONFIG\n");
189 #else
190     put_status("\nSynth devices:\n") ;
191
192     for (i = 0; i < num_synths; i++) {
193         put_status_int(i, 10) ;
194         put_status(": ") ;
195         put_status(synth_devs[i]->info->name) ;
196         put_status("\n") ;
197     }
198 #endif
199
200 #ifndef CONFIG_MIDI
201     put_status("\nMidi devices: NOT ENABLED IN CONFIG\n") ;
202 #else
203     put_status("\nMidi devices:\n") ;
204
205     for (i = 0; i < num_midis; i++) {
206         put_status_int(i, 10) ;
207         put_status(": ") ;
208         put_status(midi_devs[i]->info.name) ;
209         put_status("\n") ;
210     }
211 #endif
212
213     put_status("\nTimers:\n");
214
215     for (i = 0; i < num_sound_timers; i++) {
216         put_status_int(i, 10);
217         put_status(": ");
218         put_status(sound_timer_devs[i]->info.name);
219         put_status("\n");
220     }
221
222     put_status("\nMixers:\n");
223
224     for (i = 0; i < num_mixers; i++) {
225         put_status_int(i, 10);
226         put_status(": ");
227         put_status(mixer_devs[i]->name);
228         put_status("\n");
229     }
230 }
231
232 static int
233 read_status(snd_rw_buf * buf, int count)
234 {
235     /*
236      * Return at most 'count' bytes from the status_buf.
237      */
238     int             l, c;
239
240     l = count;
241     c = status_len - status_ptr;
242
243     if (l > c)
244         l = c;
245     if (l <= 0)
246         return 0;
247
248
249     if (uiomove(&status_buf[status_ptr], l, buf)) {
250         printf("sb: Bad copyout()!\n");
251     };
252     status_ptr += l;
253
254     return l;
255 }
256
257 int
258 sound_read_sw(int dev, struct fileinfo * file, snd_rw_buf * buf, int count)
259 {
260     DEB(printf("sound_read_sw(dev=%d, count=%d)\n", dev, count));
261
262     switch (dev & 0x0f) {
263     case SND_DEV_STATUS:
264         return read_status(buf, count);
265         break;
266
267 #ifdef CONFIG_AUDIO
268     case SND_DEV_DSP:
269     case SND_DEV_DSP16:
270     case SND_DEV_AUDIO:
271         return audio_read(dev, file, buf, count);
272         break;
273 #endif
274
275 #ifdef CONFIG_SEQUENCER
276     case SND_DEV_SEQ:
277     case SND_DEV_SEQ2:
278         return sequencer_read(dev, file, buf, count);
279         break;
280 #endif
281
282 #ifdef CONFIG_MIDI
283     case SND_DEV_MIDIN:
284         return MIDIbuf_read(dev, file, buf, count);
285 #endif
286
287     default:
288         printf("Sound: Undefined minor device %d\n", dev);
289     }
290
291     return -(EPERM);
292 }
293
294 int
295 sound_write_sw(int dev, struct fileinfo * file, snd_rw_buf * buf, int count)
296 {
297
298     DEB(printf("sound_write_sw(dev=%d, count=%d)\n", dev, count));
299
300     switch (dev & 0x0f) {
301
302 #ifdef CONFIG_SEQUENCER
303     case SND_DEV_SEQ:
304     case SND_DEV_SEQ2:
305         return sequencer_write(dev, file, buf, count);
306         break;
307 #endif
308
309 #ifdef CONFIG_AUDIO
310     case SND_DEV_DSP:
311     case SND_DEV_DSP16:
312     case SND_DEV_AUDIO:
313         return audio_write(dev, file, buf, count);
314         break;
315 #endif
316
317 #ifdef CONFIG_MIDI
318     case SND_DEV_MIDIN:
319         return MIDIbuf_write(dev, file, buf, count);
320 #endif
321
322     default:
323         return -(EPERM);
324     }
325
326     return count;
327 }
328
329 int
330 sound_open_sw(int dev, struct fileinfo * file)
331 {
332     int             retval;
333
334     DEB(printf("sound_open_sw(dev=%d)\n", dev));
335
336     if ((dev >= SND_NDEVS) || (dev < 0)) {
337         printf("Invalid minor device %d\n", dev);
338         return -(ENXIO);
339     }
340     switch (dev & 0x0f) {
341     case SND_DEV_STATUS:
342         if (status_busy)
343             return -(EBUSY);
344         status_busy = 1;
345         if ((status_buf = (char *) malloc(SNDSTAT_BUF_SIZE, M_TEMP, M_WAITOK)) == NULL)
346             return -(EIO);
347         status_len = status_ptr = 0;
348         init_status();
349         break;
350
351     case SND_DEV_CTL:
352         return 0;
353         break;
354
355 #ifdef CONFIG_SEQUENCER
356     case SND_DEV_SEQ:
357     case SND_DEV_SEQ2:
358         if ((retval = sequencer_open(dev, file)) < 0)
359             return retval;
360         break;
361 #endif
362
363 #ifdef CONFIG_MIDI
364     case SND_DEV_MIDIN:
365         if ((retval = MIDIbuf_open(dev, file)) < 0)
366             return retval;
367         break;
368 #endif
369
370 #ifdef CONFIG_AUDIO
371     case SND_DEV_DSP:
372     case SND_DEV_DSP16:
373     case SND_DEV_AUDIO:
374         if ((retval = audio_open(dev, file)) < 0)
375             return retval;
376         break;
377 #endif
378
379     default:
380         printf("Invalid minor device %d\n", dev);
381         return -(ENXIO);
382     }
383
384     return 0;
385 }
386
387 void
388 sound_release_sw(int dev, struct fileinfo * file)
389 {
390
391     DEB(printf("sound_release_sw(dev=%d)\n", dev));
392
393     switch (dev & 0x0f) {
394     case SND_DEV_STATUS:
395         if (status_buf)
396             free(status_buf, M_TEMP);
397         status_buf = NULL;
398         status_busy = 0;
399         break;
400
401     case SND_DEV_CTL:
402         break;
403
404 #ifdef CONFIG_SEQUENCER
405     case SND_DEV_SEQ:
406     case SND_DEV_SEQ2:
407         sequencer_release(dev, file);
408         break;
409 #endif
410
411 #ifdef CONFIG_MIDI
412     case SND_DEV_MIDIN:
413         MIDIbuf_release(dev, file);
414         break;
415 #endif
416
417 #ifdef CONFIG_AUDIO
418     case SND_DEV_DSP:
419     case SND_DEV_DSP16:
420     case SND_DEV_AUDIO:
421         audio_release(dev, file);
422         break;
423 #endif
424
425     default:
426         printf("Sound error: Releasing unknown device 0x%02x\n", dev);
427     }
428 }
429
430 int
431 sound_ioctl_sw(int dev, struct fileinfo * file, u_int cmd, ioctl_arg arg)
432 {
433     DEB(printf("sound_ioctl_sw(dev=%d, cmd=0x%x, arg=0x%x)\n", dev, cmd, arg));
434
435     if (((cmd >> 8) & 0xff) == 'M' && num_mixers > 0)   /* Mixer ioctl */
436         if ((dev & 0x0f) != SND_DEV_CTL) {
437             int             dtype = dev & 0x0f;
438             int             mixdev;
439
440             switch (dtype) {
441 #ifdef CONFIG_AUDIO
442             case SND_DEV_DSP:
443             case SND_DEV_DSP16:
444             case SND_DEV_AUDIO:
445                 mixdev = audio_devs[dev >> 4]->mixer_dev;
446                 if (mixdev < 0 || mixdev >= num_mixers)
447                     return -(ENXIO);
448                 return mixer_devs[mixdev]->ioctl(mixdev, cmd, arg);
449                 break;
450 #endif
451
452             default:
453                 return mixer_devs[0]->ioctl(0, cmd, arg);
454             }
455         }
456     switch (dev & 0x0f) {
457
458     case SND_DEV_CTL:
459
460         if (!num_mixers)
461             return -(ENXIO);
462
463         dev = dev >> 4;
464
465         if (dev >= num_mixers)
466             return -(ENXIO);
467
468         return mixer_devs[dev]->ioctl(dev, cmd, arg);
469             break;
470
471 #ifdef CONFIG_SEQUENCER
472     case SND_DEV_SEQ:
473     case SND_DEV_SEQ2:
474         return sequencer_ioctl(dev, file, cmd, arg);
475         break;
476 #endif
477
478 #ifdef CONFIG_AUDIO
479     case SND_DEV_DSP:
480     case SND_DEV_DSP16:
481     case SND_DEV_AUDIO:
482         return audio_ioctl(dev, file, cmd, arg);
483         break;
484 #endif
485
486 #ifdef CONFIG_MIDI
487     case SND_DEV_MIDIN:
488         return MIDIbuf_ioctl(dev, file, cmd, arg);
489         break;
490 #endif
491
492     default:
493         return -(EPERM);
494         break;
495     }
496
497     return -(EPERM);
498 }
499
500 #endif