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.9 2006/07/28 02:17:38 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.9 2006/07/28 02:17:38 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 dev_ops sndstat_ops = { 47 { "sndstat", SND_CDEV_MAJOR, 0 }, 48 .d_open = sndstat_open, 49 .d_close = sndstat_close, 50 .d_read = sndstat_read, 51 }; 52 53 struct sndstat_entry { 54 SLIST_ENTRY(sndstat_entry) link; 55 device_t dev; 56 char *str; 57 sndstat_handler handler; 58 int type, unit; 59 }; 60 61 static struct sbuf sndstat_sbuf; 62 static int sndstat_isopen = 0; 63 static int sndstat_bufptr; 64 static int sndstat_maxunit = -1; 65 static int sndstat_files = 0; 66 67 static SLIST_HEAD(, sndstat_entry) sndstat_devlist = SLIST_HEAD_INITIALIZER(none); 68 69 static int sndstat_verbose = 1; 70 #ifdef USING_MUTEX 71 TUNABLE_INT("hw.snd.verbose", &sndstat_verbose); 72 #else 73 TUNABLE_INT_DECL("hw.snd.verbose", 1, sndstat_verbose); 74 #endif 75 76 static int sndstat_prepare(struct sbuf *s); 77 78 static int 79 sysctl_hw_sndverbose(SYSCTL_HANDLER_ARGS) 80 { 81 int error, verbose; 82 83 verbose = sndstat_verbose; 84 error = sysctl_handle_int(oidp, &verbose, sizeof(verbose), req); 85 if (error == 0 && req->newptr != NULL) { 86 crit_enter(); 87 if (verbose < 0 || verbose > 3) 88 error = EINVAL; 89 else 90 sndstat_verbose = verbose; 91 crit_exit(); 92 } 93 return error; 94 } 95 SYSCTL_PROC(_hw_snd, OID_AUTO, verbose, CTLTYPE_INT | CTLFLAG_RW, 96 0, sizeof(int), sysctl_hw_sndverbose, "I", ""); 97 98 static int 99 sndstat_open(struct dev_open_args *ap) 100 { 101 int err; 102 103 crit_enter(); 104 if (sndstat_isopen) { 105 crit_exit(); 106 return EBUSY; 107 } 108 if (sbuf_new(&sndstat_sbuf, NULL, 4096, 0) == NULL) { 109 crit_exit(); 110 return ENXIO; 111 } 112 sndstat_bufptr = 0; 113 err = (sndstat_prepare(&sndstat_sbuf) > 0)? 0 : ENOMEM; 114 if (!err) 115 sndstat_isopen = 1; 116 117 crit_exit(); 118 return err; 119 } 120 121 static int 122 sndstat_close(struct dev_close_args *ap) 123 { 124 crit_enter(); 125 if (!sndstat_isopen) { 126 crit_exit(); 127 return EBADF; 128 } 129 sbuf_delete(&sndstat_sbuf); 130 sndstat_isopen = 0; 131 132 crit_exit(); 133 return 0; 134 } 135 136 static int 137 sndstat_read(struct dev_read_args *ap) 138 { 139 struct uio *uio = ap->a_uio; 140 int l, err; 141 142 crit_enter(); 143 if (!sndstat_isopen) { 144 crit_exit(); 145 return EBADF; 146 } 147 l = min(uio->uio_resid, sbuf_len(&sndstat_sbuf) - sndstat_bufptr); 148 if (l > 0) 149 err = uiomove(sbuf_data(&sndstat_sbuf) + sndstat_bufptr, l, uio); 150 else 151 err = 0; 152 sndstat_bufptr += l; 153 154 crit_exit(); 155 return err; 156 } 157 158 /************************************************************************/ 159 160 static struct sndstat_entry * 161 sndstat_find(int type, int unit) 162 { 163 struct sndstat_entry *ent; 164 165 SLIST_FOREACH(ent, &sndstat_devlist, link) { 166 if (ent->type == type && ent->unit == unit) 167 return ent; 168 } 169 170 return NULL; 171 } 172 173 int 174 sndstat_register(device_t dev, char *str, sndstat_handler handler) 175 { 176 struct sndstat_entry *ent; 177 const char *devtype; 178 int type, unit; 179 180 if (dev) { 181 unit = device_get_unit(dev); 182 devtype = device_get_name(dev); 183 if (!strcmp(devtype, "pcm")) 184 type = SS_TYPE_PCM; 185 else if (!strcmp(devtype, "midi")) 186 type = SS_TYPE_MIDI; 187 else if (!strcmp(devtype, "sequencer")) 188 type = SS_TYPE_SEQUENCER; 189 else 190 return EINVAL; 191 } else { 192 type = SS_TYPE_MODULE; 193 unit = -1; 194 } 195 196 ent = malloc(sizeof *ent, M_DEVBUF, M_ZERO | M_WAITOK); 197 if (!ent) 198 return ENOSPC; 199 200 ent->dev = dev; 201 ent->str = str; 202 ent->type = type; 203 ent->unit = unit; 204 ent->handler = handler; 205 206 crit_enter(); 207 SLIST_INSERT_HEAD(&sndstat_devlist, ent, link); 208 if (type == SS_TYPE_MODULE) 209 sndstat_files++; 210 sndstat_maxunit = (unit > sndstat_maxunit)? unit : sndstat_maxunit; 211 crit_exit(); 212 213 return 0; 214 } 215 216 int 217 sndstat_registerfile(char *str) 218 { 219 return sndstat_register(NULL, str, NULL); 220 } 221 222 int 223 sndstat_unregister(device_t dev) 224 { 225 struct sndstat_entry *ent; 226 227 crit_enter(); 228 SLIST_FOREACH(ent, &sndstat_devlist, link) { 229 if (ent->dev == dev) { 230 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link); 231 free(ent, M_DEVBUF); 232 crit_exit(); 233 234 return 0; 235 } 236 } 237 crit_exit(); 238 239 return ENXIO; 240 } 241 242 int 243 sndstat_unregisterfile(char *str) 244 { 245 struct sndstat_entry *ent; 246 247 crit_enter(); 248 SLIST_FOREACH(ent, &sndstat_devlist, link) { 249 if (ent->dev == NULL && ent->str == str) { 250 SLIST_REMOVE(&sndstat_devlist, ent, sndstat_entry, link); 251 free(ent, M_DEVBUF); 252 sndstat_files--; 253 crit_exit(); 254 255 return 0; 256 } 257 } 258 crit_exit(); 259 260 return ENXIO; 261 } 262 263 /************************************************************************/ 264 265 static int 266 sndstat_prepare(struct sbuf *s) 267 { 268 struct sndstat_entry *ent; 269 int i, j; 270 271 sbuf_printf(s, "FreeBSD Audio Driver (newpcm)\n"); 272 if (SLIST_EMPTY(&sndstat_devlist)) { 273 sbuf_printf(s, "No devices installed.\n"); 274 sbuf_finish(s); 275 return sbuf_len(s); 276 } 277 278 sbuf_printf(s, "Installed devices:\n"); 279 280 for (i = 0; i <= sndstat_maxunit; i++) { 281 for (j = SS_TYPE_FIRST; j <= SS_TYPE_LAST; j++) { 282 ent = sndstat_find(j, i); 283 if (!ent) 284 continue; 285 sbuf_printf(s, "%s:", device_get_nameunit(ent->dev)); 286 sbuf_printf(s, " <%s>", device_get_desc(ent->dev)); 287 sbuf_printf(s, " %s", ent->str); 288 if (ent->handler) 289 ent->handler(s, ent->dev, sndstat_verbose); 290 else 291 sbuf_printf(s, " [no handler]"); 292 sbuf_printf(s, "\n"); 293 } 294 } 295 296 if (sndstat_verbose >= 3 && sndstat_files > 0) { 297 sbuf_printf(s, "\nFile Versions:\n"); 298 299 SLIST_FOREACH(ent, &sndstat_devlist, link) { 300 if (ent->dev == NULL && ent->str != NULL) 301 sbuf_printf(s, "%s\n", ent->str); 302 } 303 } 304 305 sbuf_finish(s); 306 return sbuf_len(s); 307 } 308 309 static int 310 sndstat_init(void) 311 { 312 dev_ops_add(&sndstat_ops, -1, SND_DEV_STATUS); 313 make_dev(&sndstat_ops, SND_DEV_STATUS, 314 UID_ROOT, GID_WHEEL, 0444, "sndstat"); 315 return (0); 316 } 317 318 static int 319 sndstat_uninit(void) 320 { 321 crit_enter(); 322 if (sndstat_isopen) { 323 crit_exit(); 324 return EBUSY; 325 } 326 dev_ops_remove(&sndstat_ops, -1, SND_DEV_STATUS); 327 crit_exit(); 328 return 0; 329 } 330 331 int 332 sndstat_busy(void) 333 { 334 return (sndstat_isopen); 335 } 336 337 static void 338 sndstat_sysinit(void *p) 339 { 340 sndstat_init(); 341 } 342 343 static void 344 sndstat_sysuninit(void *p) 345 { 346 sndstat_uninit(); 347 } 348 349 SYSINIT(sndstat_sysinit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysinit, NULL); 350 SYSUNINIT(sndstat_sysuninit, SI_SUB_DRIVERS, SI_ORDER_FIRST, sndstat_sysuninit, NULL); 351 352 353