Deleted Added
full compact
sndstat.c (170289) sndstat.c (170815)
1/*-
2 * Copyright (c) 2001 Cameron Grant <cg@freebsd.org>
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
27#include <dev/sound/pcm/sound.h>
28#include <dev/sound/pcm/vchan.h>
1/*-
2 * Copyright (c) 2001 Cameron Grant <cg@freebsd.org>
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
27#include <dev/sound/pcm/sound.h>
28#include <dev/sound/pcm/vchan.h>
29#include <dev/sound/version.h>
29#ifdef USING_MUTEX
30#include <sys/sx.h>
31#endif
32
30#ifdef USING_MUTEX
31#include <sys/sx.h>
32#endif
33
33SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/sndstat.c 170289 2007-06-04 18:25:08Z dwmalone $");
34SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/sndstat.c 170815 2007-06-16 03:37:28Z ariff $");
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
42static d_open_t sndstat_open;
43static d_close_t sndstat_close;
44static d_read_t sndstat_read;
45
46static struct cdevsw sndstat_cdevsw = {
47 .d_version = D_VERSION,
35
36#define SS_TYPE_MODULE 0
37#define SS_TYPE_FIRST 1
38#define SS_TYPE_PCM 1
39#define SS_TYPE_MIDI 2
40#define SS_TYPE_SEQUENCER 3
41#define SS_TYPE_LAST 3
42
43static d_open_t sndstat_open;
44static d_close_t sndstat_close;
45static d_read_t sndstat_read;
46
47static struct cdevsw sndstat_cdevsw = {
48 .d_version = D_VERSION,
48 .d_flags = D_NEEDGIANT,
49 .d_open = sndstat_open,
50 .d_close = sndstat_close,
51 .d_read = sndstat_read,
52 .d_name = "sndstat",
53};
54
55struct sndstat_entry {
56 SLIST_ENTRY(sndstat_entry) link;
57 device_t dev;
58 char *str;
59 sndstat_handler handler;
60 int type, unit;
61};
62
63#ifdef USING_MUTEX
64static struct mtx sndstat_lock;
65#endif
66static struct sbuf sndstat_sbuf;
67static struct cdev *sndstat_dev = NULL;
68static int sndstat_bufptr = -1;
69static int sndstat_maxunit = -1;
70static int sndstat_files = 0;
71
72#define SNDSTAT_PID(x) ((pid_t)((intptr_t)((x)->si_drv1)))
73#define SNDSTAT_PID_SET(x, y) (x)->si_drv1 = (void *)((intptr_t)(y))
74#define SNDSTAT_FLUSH() do { \
75 if (sndstat_bufptr != -1) { \
76 sbuf_delete(&sndstat_sbuf); \
77 sndstat_bufptr = -1; \
78 } \
79} while(0)
80
81static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(none);
82
83int snd_verbose = 1;
84#ifdef USING_MUTEX
85TUNABLE_INT("hw.snd.verbose", &snd_verbose);
86#else
87TUNABLE_INT_DECL("hw.snd.verbose", 1, snd_verbose);
88#endif
89
90#ifdef SND_DEBUG
91static int
92sysctl_hw_snd_sndstat_pid(SYSCTL_HANDLER_ARGS)
93{
94 int err, val;
95
96 if (sndstat_dev == NULL)
97 return (EINVAL);
98
99 mtx_lock(&sndstat_lock);
100 val = (int)SNDSTAT_PID(sndstat_dev);
101 mtx_unlock(&sndstat_lock);
102 err = sysctl_handle_int(oidp, &val, 0, req);
103 if (err == 0 && req->newptr != NULL && val == 0) {
104 mtx_lock(&sndstat_lock);
105 SNDSTAT_FLUSH();
106 SNDSTAT_PID_SET(sndstat_dev, 0);
107 mtx_unlock(&sndstat_lock);
108 }
109 return (err);
110}
111SYSCTL_PROC(_hw_snd, OID_AUTO, sndstat_pid, CTLTYPE_INT | CTLFLAG_RW,
112 0, sizeof(int), sysctl_hw_snd_sndstat_pid, "I", "sndstat busy pid");
113#endif
114
115static int sndstat_prepare(struct sbuf *s);
116
117static int
118sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
119{
120 int error, verbose;
121
122 verbose = snd_verbose;
123 error = sysctl_handle_int(oidp, &verbose, 0, req);
124 if (error == 0 && req->newptr != NULL) {
125 mtx_lock(&sndstat_lock);
126 if (verbose < 0 || verbose > 4)
127 error = EINVAL;
128 else
129 snd_verbose = verbose;
130 mtx_unlock(&sndstat_lock);
131 }
132 return error;
133}
134SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
135 0, sizeof(int), sysctl_hw_sndverbose, "I", "verbosity level");
136
137static int
138sndstat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
139{
49 .d_open = sndstat_open,
50 .d_close = sndstat_close,
51 .d_read = sndstat_read,
52 .d_name = "sndstat",
53};
54
55struct sndstat_entry {
56 SLIST_ENTRY(sndstat_entry) link;
57 device_t dev;
58 char *str;
59 sndstat_handler handler;
60 int type, unit;
61};
62
63#ifdef USING_MUTEX
64static struct mtx sndstat_lock;
65#endif
66static struct sbuf sndstat_sbuf;
67static struct cdev *sndstat_dev = NULL;
68static int sndstat_bufptr = -1;
69static int sndstat_maxunit = -1;
70static int sndstat_files = 0;
71
72#define SNDSTAT_PID(x) ((pid_t)((intptr_t)((x)->si_drv1)))
73#define SNDSTAT_PID_SET(x, y) (x)->si_drv1 = (void *)((intptr_t)(y))
74#define SNDSTAT_FLUSH() do { \
75 if (sndstat_bufptr != -1) { \
76 sbuf_delete(&sndstat_sbuf); \
77 sndstat_bufptr = -1; \
78 } \
79} while(0)
80
81static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(none);
82
83int snd_verbose = 1;
84#ifdef USING_MUTEX
85TUNABLE_INT("hw.snd.verbose", &snd_verbose);
86#else
87TUNABLE_INT_DECL("hw.snd.verbose", 1, snd_verbose);
88#endif
89
90#ifdef SND_DEBUG
91static int
92sysctl_hw_snd_sndstat_pid(SYSCTL_HANDLER_ARGS)
93{
94 int err, val;
95
96 if (sndstat_dev == NULL)
97 return (EINVAL);
98
99 mtx_lock(&sndstat_lock);
100 val = (int)SNDSTAT_PID(sndstat_dev);
101 mtx_unlock(&sndstat_lock);
102 err = sysctl_handle_int(oidp, &val, 0, req);
103 if (err == 0 && req->newptr != NULL && val == 0) {
104 mtx_lock(&sndstat_lock);
105 SNDSTAT_FLUSH();
106 SNDSTAT_PID_SET(sndstat_dev, 0);
107 mtx_unlock(&sndstat_lock);
108 }
109 return (err);
110}
111SYSCTL_PROC(_hw_snd, OID_AUTO, sndstat_pid, CTLTYPE_INT | CTLFLAG_RW,
112 0, sizeof(int), sysctl_hw_snd_sndstat_pid, "I", "sndstat busy pid");
113#endif
114
115static int sndstat_prepare(struct sbuf *s);
116
117static int
118sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS)
119{
120 int error, verbose;
121
122 verbose = snd_verbose;
123 error = sysctl_handle_int(oidp, &verbose, 0, req);
124 if (error == 0 && req->newptr != NULL) {
125 mtx_lock(&sndstat_lock);
126 if (verbose < 0 || verbose > 4)
127 error = EINVAL;
128 else
129 snd_verbose = verbose;
130 mtx_unlock(&sndstat_lock);
131 }
132 return error;
133}
134SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW,
135 0, sizeof(int), sysctl_hw_sndverbose, "I", "verbosity level");
136
137static int
138sndstat_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
139{
140 int error;
141
142 if (sndstat_dev == NULL || i_dev != sndstat_dev)
143 return EBADF;
144
145 mtx_lock(&sndstat_lock);
146 if (SNDSTAT_PID(i_dev) != 0) {
147 mtx_unlock(&sndstat_lock);
148 return EBUSY;
149 }
150 SNDSTAT_PID_SET(i_dev, td->td_proc->p_pid);
151 mtx_unlock(&sndstat_lock);
152 if (sbuf_new(&sndstat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
140 if (sndstat_dev == NULL || i_dev != sndstat_dev)
141 return EBADF;
142
143 mtx_lock(&sndstat_lock);
144 if (SNDSTAT_PID(i_dev) != 0) {
145 mtx_unlock(&sndstat_lock);
146 return EBUSY;
147 }
148 SNDSTAT_PID_SET(i_dev, td->td_proc->p_pid);
149 mtx_unlock(&sndstat_lock);
150 if (sbuf_new(&sndstat_sbuf, NULL, 4096, SBUF_AUTOEXTEND) == NULL) {
153 error = ENXIO;
154 goto out;
155 }
156 sndstat_bufptr = 0;
157 error = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM;
158out:
159 if (error) {
160 mtx_lock(&sndstat_lock);
151 mtx_lock(&sndstat_lock);
161 SNDSTAT_FLUSH();
162 SNDSTAT_PID_SET(i_dev, 0);
163 mtx_unlock(&sndstat_lock);
152 SNDSTAT_PID_SET(i_dev, 0);
153 mtx_unlock(&sndstat_lock);
154 return ENXIO;
164 }
155 }
165 return (error);
156 sndstat_bufptr = 0;
157 return 0;
166}
167
168static int
169sndstat_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
170{
171 if (sndstat_dev == NULL || i_dev != sndstat_dev)
172 return EBADF;
173
174 mtx_lock(&sndstat_lock);
175 if (SNDSTAT_PID(i_dev) == 0) {
176 mtx_unlock(&sndstat_lock);
177 return EBADF;
178 }
179
180 SNDSTAT_FLUSH();
181 SNDSTAT_PID_SET(i_dev, 0);
182
183 mtx_unlock(&sndstat_lock);
184
185 return 0;
186}
187
188static int
189sndstat_read(struct cdev *i_dev, struct uio *buf, int flag)
190{
191 int l, err;
192
193 if (sndstat_dev == NULL || i_dev != sndstat_dev)
194 return EBADF;
195
196 mtx_lock(&sndstat_lock);
197 if (SNDSTAT_PID(i_dev) != buf->uio_td->td_proc->p_pid ||
198 sndstat_bufptr == -1) {
199 mtx_unlock(&sndstat_lock);
200 return EBADF;
201 }
202 mtx_unlock(&sndstat_lock);
203
158}
159
160static int
161sndstat_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
162{
163 if (sndstat_dev == NULL || i_dev != sndstat_dev)
164 return EBADF;
165
166 mtx_lock(&sndstat_lock);
167 if (SNDSTAT_PID(i_dev) == 0) {
168 mtx_unlock(&sndstat_lock);
169 return EBADF;
170 }
171
172 SNDSTAT_FLUSH();
173 SNDSTAT_PID_SET(i_dev, 0);
174
175 mtx_unlock(&sndstat_lock);
176
177 return 0;
178}
179
180static int
181sndstat_read(struct cdev *i_dev, struct uio *buf, int flag)
182{
183 int l, err;
184
185 if (sndstat_dev == NULL || i_dev != sndstat_dev)
186 return EBADF;
187
188 mtx_lock(&sndstat_lock);
189 if (SNDSTAT_PID(i_dev) != buf->uio_td->td_proc->p_pid ||
190 sndstat_bufptr == -1) {
191 mtx_unlock(&sndstat_lock);
192 return EBADF;
193 }
194 mtx_unlock(&sndstat_lock);
195
196 if (sndstat_bufptr == 0) {
197 err = (sndstat_prepare(&sndstat_sbuf) > 0) ? 0 : ENOMEM;
198 if (err) {
199 mtx_lock(&sndstat_lock);
200 SNDSTAT_FLUSH();
201 mtx_unlock(&sndstat_lock);
202 return err;
203 }
204 }
205
204 l = min(buf->uio_resid, sbuf_len(&sndstat_sbuf) - sndstat_bufptr);
205 err = (l > 0)? uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr, l, buf) : 0;
206 sndstat_bufptr += l;
207
208 return err;
209}
210
211/************************************************************************/
212
213static struct sndstat_entry *
214sndstat_find(int type, int unit)
215{
216 struct sndstat_entry *ent;
217
218 SLIST_FOREACH(ent, &sndstat_devlist, link) {
219 if (ent->type == type && ent->unit == unit)
220 return ent;
221 }
222
223 return NULL;
224}
225
226int
227sndstat_acquire(struct thread *td)
228{
229 if (sndstat_dev == NULL)
230 return EBADF;
231
232 mtx_lock(&sndstat_lock);
233 if (SNDSTAT_PID(sndstat_dev) != 0) {
234 mtx_unlock(&sndstat_lock);
235 return EBUSY;
236 }
237 SNDSTAT_PID_SET(sndstat_dev, td->td_proc->p_pid);
238 mtx_unlock(&sndstat_lock);
239 return 0;
240}
241
242int
243sndstat_release(struct thread *td)
244{
245 if (sndstat_dev == NULL)
246 return EBADF;
247
248 mtx_lock(&sndstat_lock);
249 if (SNDSTAT_PID(sndstat_dev) != td->td_proc->p_pid) {
250 mtx_unlock(&sndstat_lock);
251 return EBADF;
252 }
253 SNDSTAT_PID_SET(sndstat_dev, 0);
254 mtx_unlock(&sndstat_lock);
255 return 0;
256}
257
258int
259sndstat_register(device_t dev, char *str, sndstat_handler handler)
260{
261 struct sndstat_entry *ent;
262 const char *devtype;
263 int type, unit;
264
265 if (dev) {
266 unit = device_get_unit(dev);
267 devtype = device_get_name(dev);
268 if (!strcmp(devtype, "pcm"))
269 type = SS_TYPE_PCM;
270 else if (!strcmp(devtype, "midi"))
271 type = SS_TYPE_MIDI;
272 else if (!strcmp(devtype, "sequencer"))
273 type = SS_TYPE_SEQUENCER;
274 else
275 return EINVAL;
276 } else {
277 type = SS_TYPE_MODULE;
278 unit = -1;
279 }
280
281 ent = malloc(sizeof *ent, M_DEVBUF, M_WAITOK | M_ZERO);
282 ent->dev = dev;
283 ent->str = str;
284 ent->type = type;
285 ent->unit = unit;
286 ent->handler = handler;
287
288 mtx_lock(&sndstat_lock);
289 SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
290 if (type == SS_TYPE_MODULE)
291 sndstat_files++;
292 sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
293 mtx_unlock(&sndstat_lock);
294
295 return 0;
296}
297
298int
299sndstat_registerfile(char *str)
300{
301 return sndstat_register(NULL, str, NULL);
302}
303
304int
305sndstat_unregister(device_t dev)
306{
307 struct sndstat_entry *ent;
308
309 mtx_lock(&sndstat_lock);
310 SLIST_FOREACH(ent, &sndstat_devlist, link) {
311 if (ent->dev == dev) {
312 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
313 mtx_unlock(&sndstat_lock);
314 free(ent, M_DEVBUF);
315
316 return 0;
317 }
318 }
319 mtx_unlock(&sndstat_lock);
320
321 return ENXIO;
322}
323
324int
325sndstat_unregisterfile(char *str)
326{
327 struct sndstat_entry *ent;
328
329 mtx_lock(&sndstat_lock);
330 SLIST_FOREACH(ent, &sndstat_devlist, link) {
331 if (ent->dev == NULL && ent->str == str) {
332 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
333 sndstat_files--;
334 mtx_unlock(&sndstat_lock);
335 free(ent, M_DEVBUF);
336
337 return 0;
338 }
339 }
340 mtx_unlock(&sndstat_lock);
341
342 return ENXIO;
343}
344
345/************************************************************************/
346
347static int
348sndstat_prepare(struct sbuf *s)
349{
350 struct sndstat_entry *ent;
206 l = min(buf->uio_resid, sbuf_len(&sndstat_sbuf) - sndstat_bufptr);
207 err = (l > 0)? uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr, l, buf) : 0;
208 sndstat_bufptr += l;
209
210 return err;
211}
212
213/************************************************************************/
214
215static struct sndstat_entry *
216sndstat_find(int type, int unit)
217{
218 struct sndstat_entry *ent;
219
220 SLIST_FOREACH(ent, &sndstat_devlist, link) {
221 if (ent->type == type && ent->unit == unit)
222 return ent;
223 }
224
225 return NULL;
226}
227
228int
229sndstat_acquire(struct thread *td)
230{
231 if (sndstat_dev == NULL)
232 return EBADF;
233
234 mtx_lock(&sndstat_lock);
235 if (SNDSTAT_PID(sndstat_dev) != 0) {
236 mtx_unlock(&sndstat_lock);
237 return EBUSY;
238 }
239 SNDSTAT_PID_SET(sndstat_dev, td->td_proc->p_pid);
240 mtx_unlock(&sndstat_lock);
241 return 0;
242}
243
244int
245sndstat_release(struct thread *td)
246{
247 if (sndstat_dev == NULL)
248 return EBADF;
249
250 mtx_lock(&sndstat_lock);
251 if (SNDSTAT_PID(sndstat_dev) != td->td_proc->p_pid) {
252 mtx_unlock(&sndstat_lock);
253 return EBADF;
254 }
255 SNDSTAT_PID_SET(sndstat_dev, 0);
256 mtx_unlock(&sndstat_lock);
257 return 0;
258}
259
260int
261sndstat_register(device_t dev, char *str, sndstat_handler handler)
262{
263 struct sndstat_entry *ent;
264 const char *devtype;
265 int type, unit;
266
267 if (dev) {
268 unit = device_get_unit(dev);
269 devtype = device_get_name(dev);
270 if (!strcmp(devtype, "pcm"))
271 type = SS_TYPE_PCM;
272 else if (!strcmp(devtype, "midi"))
273 type = SS_TYPE_MIDI;
274 else if (!strcmp(devtype, "sequencer"))
275 type = SS_TYPE_SEQUENCER;
276 else
277 return EINVAL;
278 } else {
279 type = SS_TYPE_MODULE;
280 unit = -1;
281 }
282
283 ent = malloc(sizeof *ent, M_DEVBUF, M_WAITOK | M_ZERO);
284 ent->dev = dev;
285 ent->str = str;
286 ent->type = type;
287 ent->unit = unit;
288 ent->handler = handler;
289
290 mtx_lock(&sndstat_lock);
291 SLIST_INSERT_HEAD(&sndstat_devlist, ent, link);
292 if (type == SS_TYPE_MODULE)
293 sndstat_files++;
294 sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit;
295 mtx_unlock(&sndstat_lock);
296
297 return 0;
298}
299
300int
301sndstat_registerfile(char *str)
302{
303 return sndstat_register(NULL, str, NULL);
304}
305
306int
307sndstat_unregister(device_t dev)
308{
309 struct sndstat_entry *ent;
310
311 mtx_lock(&sndstat_lock);
312 SLIST_FOREACH(ent, &sndstat_devlist, link) {
313 if (ent->dev == dev) {
314 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
315 mtx_unlock(&sndstat_lock);
316 free(ent, M_DEVBUF);
317
318 return 0;
319 }
320 }
321 mtx_unlock(&sndstat_lock);
322
323 return ENXIO;
324}
325
326int
327sndstat_unregisterfile(char *str)
328{
329 struct sndstat_entry *ent;
330
331 mtx_lock(&sndstat_lock);
332 SLIST_FOREACH(ent, &sndstat_devlist, link) {
333 if (ent->dev == NULL && ent->str == str) {
334 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link);
335 sndstat_files--;
336 mtx_unlock(&sndstat_lock);
337 free(ent, M_DEVBUF);
338
339 return 0;
340 }
341 }
342 mtx_unlock(&sndstat_lock);
343
344 return ENXIO;
345}
346
347/************************************************************************/
348
349static int
350sndstat_prepare(struct sbuf *s)
351{
352 struct sndstat_entry *ent;
353 struct snddev_info *d;
351 int i, j;
352
354 int i, j;
355
353 sbuf_printf(s, "FreeBSD Audio Driver (newpcm: %ubit)\n",
354 (unsigned int)sizeof(intpcm_t) << 3);
356 sbuf_printf(s, "FreeBSD Audio Driver (newpcm: %ubit %d/%s)\n",
357 (u_int)sizeof(intpcm_t) << 3, SND_DRV_VERSION, MACHINE_ARCH);
355 if (SLIST_EMPTY(&sndstat_devlist)) {
356 sbuf_printf(s, "No devices installed.\n");
357 sbuf_finish(s);
358 return sbuf_len(s);
359 }
360
361 sbuf_printf(s, "Installed devices:\n");
362
363 for (i = 0; i <= sndstat_maxunit; i++) {
364 for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) {
365 ent = sndstat_find(j, i);
366 if (!ent)
367 continue;
358 if (SLIST_EMPTY(&sndstat_devlist)) {
359 sbuf_printf(s, "No devices installed.\n");
360 sbuf_finish(s);
361 return sbuf_len(s);
362 }
363
364 sbuf_printf(s, "Installed devices:\n");
365
366 for (i = 0; i <= sndstat_maxunit; i++) {
367 for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) {
368 ent = sndstat_find(j, i);
369 if (!ent)
370 continue;
371 d = device_get_softc(ent->dev);
372 if (!PCM_REGISTERED(d))
373 continue;
374 /* XXX Need Giant magic entry ??? */
375 PCM_ACQUIRE_QUICK(d);
368 sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
369 sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
376 sbuf_printf(s, "%s:", device_get_nameunit(ent->dev));
377 sbuf_printf(s, " <%s>", device_get_desc(ent->dev));
370 sbuf_printf(s, " %s", ent->str);
378 sbuf_printf(s, " %s [%s]", ent->str,
379 (d->flags & SD_F_MPSAFE) ? "MPSAFE" : "GIANT");
371 if (ent->handler)
372 ent->handler(s, ent->dev, snd_verbose);
373 else
374 sbuf_printf(s, " [no handler]");
375 sbuf_printf(s, "\n");
380 if (ent->handler)
381 ent->handler(s, ent->dev, snd_verbose);
382 else
383 sbuf_printf(s, " [no handler]");
384 sbuf_printf(s, "\n");
385 PCM_RELEASE_QUICK(d);
376 }
377 }
378
379 if (snd_verbose >= 3 && sndstat_files > 0) {
380 sbuf_printf(s, "\nFile Versions:\n");
381
382 SLIST_FOREACH(ent, &sndstat_devlist, link) {
383 if (ent->dev == NULL && ent->str != NULL)
384 sbuf_printf(s, "%s\n", ent->str);
385 }
386 }
387
388 sbuf_finish(s);
389 return sbuf_len(s);
390}
391
392static int
393sndstat_init(void)
394{
395 if (sndstat_dev != NULL)
396 return EINVAL;
397 mtx_init(&sndstat_lock, "sndstat", "sndstat lock", MTX_DEF);
398 sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS,
399 UID_ROOT, GID_WHEEL, 0444, "sndstat");
400 return 0;
401}
402
403static int
404sndstat_uninit(void)
405{
406 if (sndstat_dev == NULL)
407 return EINVAL;
408
409 mtx_lock(&sndstat_lock);
410 if (SNDSTAT_PID(sndstat_dev) != curthread->td_proc->p_pid) {
411 mtx_unlock(&sndstat_lock);
412 return EBUSY;
413 }
414
415 SNDSTAT_FLUSH();
416
417 mtx_unlock(&sndstat_lock);
418
419 destroy_dev(sndstat_dev);
420 sndstat_dev = NULL;
421
422 mtx_destroy(&sndstat_lock);
423 return 0;
424}
425
426static void
427sndstat_sysinit(void *p)
428{
429 sndstat_init();
430}
431
432static void
433sndstat_sysuninit(void *p)
434{
435 int error;
436
437 error = sndstat_uninit();
438 KASSERT(error == 0, ("%s: error = %d", __func__, error));
439}
440
441SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
442SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);
386 }
387 }
388
389 if (snd_verbose >= 3 && sndstat_files > 0) {
390 sbuf_printf(s, "\nFile Versions:\n");
391
392 SLIST_FOREACH(ent, &sndstat_devlist, link) {
393 if (ent->dev == NULL && ent->str != NULL)
394 sbuf_printf(s, "%s\n", ent->str);
395 }
396 }
397
398 sbuf_finish(s);
399 return sbuf_len(s);
400}
401
402static int
403sndstat_init(void)
404{
405 if (sndstat_dev != NULL)
406 return EINVAL;
407 mtx_init(&sndstat_lock, "sndstat", "sndstat lock", MTX_DEF);
408 sndstat_dev = make_dev(&sndstat_cdevsw, SND_DEV_STATUS,
409 UID_ROOT, GID_WHEEL, 0444, "sndstat");
410 return 0;
411}
412
413static int
414sndstat_uninit(void)
415{
416 if (sndstat_dev == NULL)
417 return EINVAL;
418
419 mtx_lock(&sndstat_lock);
420 if (SNDSTAT_PID(sndstat_dev) != curthread->td_proc->p_pid) {
421 mtx_unlock(&sndstat_lock);
422 return EBUSY;
423 }
424
425 SNDSTAT_FLUSH();
426
427 mtx_unlock(&sndstat_lock);
428
429 destroy_dev(sndstat_dev);
430 sndstat_dev = NULL;
431
432 mtx_destroy(&sndstat_lock);
433 return 0;
434}
435
436static void
437sndstat_sysinit(void *p)
438{
439 sndstat_init();
440}
441
442static void
443sndstat_sysuninit(void *p)
444{
445 int error;
446
447 error = sndstat_uninit();
448 KASSERT(error == 0, ("%s: error = %d", __func__, error));
449}
450
451SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL);
452SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL);