proc->thread stage 2: MAJOR revamping of system calls, ucred, jail API,
[dragonfly.git] / sys / dev / sound / pcm / sndstat.c
1 /*
2  * Copyright (c) 2001 Cameron Grant <gandalf@vilnya.demon.co.uk>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/sound/pcm/sndstat.c,v 1.4.2.2 2002/04/22 15:49:36 cg Exp $
27  * $DragonFly: src/sys/dev/sound/pcm/sndstat.c,v 1.3 2003/06/23 17:55:34 dillon Exp $
28  */
29
30 #include <dev/sound/pcm/sound.h>
31 #include <dev/sound/pcm/vchan.h>
32
33 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/sndstat.c,v 1.3 2003/06/23 17:55:34 dillon Exp $");
34
35 #define SS_TYPE_MODULE          0
36 #define SS_TYPE_FIRST           1
37 #define SS_TYPE_PCM             1
38 #define SS_TYPE_MIDI            2
39 #define SS_TYPE_SEQUENCER       3
40 #define SS_TYPE_LAST            3
41
42 static d_open_t sndstat_open;
43 static d_close_t sndstat_close;
44 static d_read_t sndstat_read;
45
46 static struct cdevsw sndstat_cdevsw = {
47         /* open */      sndstat_open,
48         /* close */     sndstat_close,
49         /* read */      sndstat_read,
50         /* write */     nowrite,
51         /* ioctl */     noioctl,
52         /* poll */      nopoll,
53         /* mmap */      nommap,
54         /* strategy */  nostrategy,
55         /* name */      "sndstat",
56         /* maj */       SND_CDEV_MAJOR,
57         /* dump */      nodump,
58         /* psize */     nopsize,
59         /* flags */     0,
60 };
61
62 struct sndstat_entry {
63         SLIST_ENTRY(sndstat_entry) link;
64         device_t dev;
65         char *str;
66         sndstat_handler handler;
67         int type, unit;
68 };
69
70 static struct sbuf sndstat_sbuf;
71 static dev_t sndstat_dev = 0;
72 static int sndstat_isopen = 0;
73 static int sndstat_bufptr;
74 static int sndstat_maxunit = -1;
75 static int sndstat_files = 0;
76
77 static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(none);
78
79 static int sndstat_verbose = 1;
80 #ifdef  USING_MUTEX
81 TUNABLE_INT("hw.snd.verbose", &sndstat_verbose);
82 #else
83 TUNABLE_INT_DECL("hw.snd.verbose", 1, sndstat_verbose);
84 #endif
85
86 static int sndstat_prepare(struct sbuf *s);
87
88 static int
89 sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
90 {
91         intrmask_t s;
92         int error, verbose;
93
94         verbose = sndstat_verbose;
95         error = sysctl_handle_int(oidp, &verbose, sizeof(verbose), req);
96         if (error == 0 && req->newptr != NULL) {
97                 s = spltty();
98                 if (verbose < 0 || verbose > 3)
99                         error = EINVAL;
100                 else
101                         sndstat_verbose = verbose;
102                 splx(s);
103         }
104         return error;
105 }
106 SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
107             0, sizeof(int), sysctl_hw_sndverbose, "I", "");
108
109 static int
110 sndstat_open(dev_t i_dev, int flags, int mode, struct thread *td)
111 {
112         intrmask_t s;
113         int err;
114
115         s = spltty();
116         if (sndstat_isopen) {
117                 splx(s);
118                 return EBUSY;
119         }
120         if (sbuf_new(&sndstat_sbuf, NULL, 4096, 0) == NULL) {
121                 splx(s);
122                 return ENXIO;
123         }
124         sndstat_bufptr = 0;
125         err = (sndstat_prepare(&sndstat_sbuf) > 0)? 0 : ENOMEM;
126         if (!err)
127                 sndstat_isopen = 1;
128
129         splx(s);
130         return err;
131 }
132
133 static int
134 sndstat_close(dev_t i_dev, int flags, int mode, struct thread *td)
135 {
136         intrmask_t s;
137
138         s = spltty();
139         if (!sndstat_isopen) {
140                 splx(s);
141                 return EBADF;
142         }
143         sbuf_delete(&sndstat_sbuf);
144         sndstat_isopen = 0;
145
146         splx(s);
147         return 0;
148 }
149
150 static int
151 sndstat_read(dev_t i_dev, struct uio *buf, int flag)
152 {
153         intrmask_t s;
154         int l, err;
155
156         s = spltty();
157         if (!sndstat_isopen) {
158                 splx(s);
159                 return EBADF;
160         }
161         l = min(buf->uio_resid, sbuf_len(&sndstat_sbuf) - sndstat_bufptr);
162         err = (l > 0)? uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr, l, buf) : 0;
163         sndstat_bufptr += l;
164
165         splx(s);
166         return err;
167 }
168
169 /************************************************************************/
170
171 static struct sndstat_entry *
172 sndstat_find(int type, int unit)
173 {
174         struct sndstat_entry *ent;
175
176         SLIST_FOREACH(ent, &sndstat_devlist, link) {
177                 if (ent->type == type && ent->unit == unit)
178                         return ent;
179         }
180
181         return NULL;
182 }
183
184 int
185 sndstat_register(device_t dev, char *str, sndstat_handler handler)
186 {
187         intrmask_t s;
188         struct sndstat_entry *ent;
189         const char *devtype;
190         int type, unit;
191
192         if (dev) {
193                 unit = device_get_unit(dev);
194                 devtype = device_get_name(dev);
195                 if (!strcmp(devtype, "pcm"))
196                         type = SS_TYPE_PCM;
197                 else if (!strcmp(devtype, "midi"))
198                         type = SS_TYPE_MIDI;
199                 else if (!strcmp(devtype, "sequencer"))
200                         type = SS_TYPE_SEQUENCER;
201                 else
202                         return EINVAL;
203         } else {
204                 type = SS_TYPE_MODULE;
205                 unit = -1;
206         }
207
208         ent = malloc(sizeof *ent, M_DEVBUF, M_ZERO | M_WAITOK);
209         if (!ent)
210                 return ENOSPC;
211
212         ent->dev = dev;
213         ent->str = str;
214         ent->type = type;
215         ent->unit = unit;
216         ent->handler = handler;
217
218         s = spltty();
219         SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
220         if (type == SS_TYPE_MODULE)
221                 sndstat_files++;
222         sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
223         splx(s);
224
225         return 0;
226 }
227
228 int
229 sndstat_registerfile(char *str)
230 {
231         return sndstat_register(NULL, str, NULL);
232 }
233
234 int
235 sndstat_unregister(device_t dev)
236 {
237         intrmask_t s;
238         struct sndstat_entry *ent;
239
240         s = spltty();
241         SLIST_FOREACH(ent, &sndstat_devlist, link) {
242                 if (ent->dev == dev) {
243                         SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
244                         free(ent, M_DEVBUF);
245                         splx(s);
246
247                         return 0;
248                 }
249         }
250         splx(s);
251
252         return ENXIO;
253 }
254
255 int
256 sndstat_unregisterfile(char *str)
257 {
258         intrmask_t s;
259         struct sndstat_entry *ent;
260
261         s = spltty();
262         SLIST_FOREACH(ent, &sndstat_devlist, link) {
263                 if (ent->dev == NULL && ent->str == str) {
264                         SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
265                         free(ent, M_DEVBUF);
266                         sndstat_files--;
267                         splx(s);
268
269                         return 0;
270                 }
271         }
272         splx(s);
273
274         return ENXIO;
275 }
276
277 /************************************************************************/
278
279 static int
280 sndstat_prepare(struct sbuf *s)
281 {
282         struct sndstat_entry *ent;
283         int i, j;
284
285         sbuf_printf(s, "FreeBSD Audio Driver (newpcm)\n");
286         if (SLIST_EMPTY(&sndstat_devlist)) {
287                 sbuf_printf(s, "No devices installed.\n");
288                 sbuf_finish(s);
289                 return sbuf_len(s);
290         }
291
292         sbuf_printf(s, "Installed devices:\n");
293
294         for (i = 0; i <= sndstat_maxunit; i++) {
295                 for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) {
296                         ent = sndstat_find(j, i);
297                         if (!ent)
298                                 continue;
299                         sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
300                         sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
301                         sbuf_printf(s, " %s", ent->str);
302                         if (ent->handler)
303                                 ent->handler(s, ent->dev, sndstat_verbose);
304                         else
305                                 sbuf_printf(s, " [no handler]");
306                         sbuf_printf(s, "\n");
307                 }
308         }
309
310         if (sndstat_verbose >= 3 && sndstat_files > 0) {
311                 sbuf_printf(s, "\nFile Versions:\n");
312
313                 SLIST_FOREACH(ent, &sndstat_devlist, link) {
314                         if (ent->dev == NULL && ent->str != NULL)
315                                 sbuf_printf(s, "%s\n", ent->str);
316                 }
317         }
318
319         sbuf_finish(s);
320         return sbuf_len(s);
321 }
322
323 static int
324 sndstat_init(void)
325 {
326         sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS, UID_ROOT, GID_WHEEL, 0444, "sndstat");
327
328         return (sndstat_dev != 0)? 0 : ENXIO;
329 }
330
331 static int
332 sndstat_uninit(void)
333 {
334         intrmask_t s;
335
336         s = spltty();
337         if (sndstat_isopen) {
338                 splx(s);
339                 return EBUSY;
340         }
341
342         if (sndstat_dev)
343                 destroy_dev(sndstat_dev);
344         sndstat_dev = 0;
345
346         splx(s);
347         return 0;
348 }
349
350 int
351 sndstat_busy(void)
352 {
353         return (sndstat_isopen);
354 }
355
356 static void
357 sndstat_sysinit(void *p)
358 {
359         sndstat_init();
360 }
361
362 static void
363 sndstat_sysuninit(void *p)
364 {
365         sndstat_uninit();
366 }
367
368 SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
369 SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);
370
371