1 /* 2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 3 * (C) 1997 Luigi Rizzo (luigi@iet.unipi.it) 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 #include <dev/sound/pcm/sound.h> 31 32 static int status_isopen = 0; 33 static int status_init(char *buf, int size); 34 static int status_read(struct uio *buf); 35 36 static d_open_t sndopen; 37 static d_close_t sndclose; 38 static d_ioctl_t sndioctl; 39 static d_read_t sndread; 40 static d_write_t sndwrite; 41 static d_mmap_t sndmmap; 42 static d_poll_t sndpoll; 43 44 #define CDEV_MAJOR 30 45 static struct cdevsw snd_cdevsw = { 46 /* open */ sndopen, 47 /* close */ sndclose, 48 /* read */ sndread, 49 /* write */ sndwrite, 50 /* ioctl */ sndioctl, 51 /* poll */ sndpoll, 52 /* mmap */ sndmmap, 53 /* strategy */ nostrategy, 54 /* name */ "snd", 55 /* maj */ CDEV_MAJOR, 56 /* dump */ nodump, 57 /* psize */ nopsize, 58 /* flags */ 0, 59 /* bmaj */ -1 60 }; 61 62 /* PROPOSAL: 63 each unit needs: 64 status, mixer, dsp, dspW, audio, sequencer, midi-in, seq2, sndproc = 9 devices 65 dspW and audio are deprecated. 66 dsp needs min 64 channels, will give it 256 67 68 minor = (unit << 12) + (dev << 8) + channel 69 currently minor = (channel << 8) + (unit << 4) + dev 70 71 nomenclature: 72 /dev/pcmX/dsp.(0..255) 73 /dev/pcmX/dspW 74 /dev/pcmX/audio 75 /dev/pcmX/status 76 /dev/pcmX/mixer 77 [etc.] 78 79 currently: 80 minor = (channel << 8) + (unit << 4) + dev 81 */ 82 83 #define PCMMINOR(x) (minor(x)) 84 #define PCMCHAN(x) ((PCMMINOR(x) & 0x0000ff00) >> 8) 85 #define PCMUNIT(x) ((PCMMINOR(x) & 0x000000f0) >> 4) 86 #define PCMDEV(x) (PCMMINOR(x) & 0x0000000f) 87 #define PCMMKMINOR(u, d, c) ((((c) & 0xff) << 8) | (((u) & 0x0f) << 4) | ((d) & 0x0f)) 88 89 static devclass_t pcm_devclass; 90 91 static snddev_info * 92 gsd(int unit) 93 { 94 return devclass_get_softc(pcm_devclass, unit); 95 } 96 97 int 98 pcm_addchan(device_t dev, int dir, pcm_channel *templ, void *devinfo) 99 { 100 int unit = device_get_unit(dev); 101 snddev_info *d = device_get_softc(dev); 102 pcm_channel *ch; 103 104 ch = (dir == PCMDIR_PLAY)? &d->play[d->playcount] : &d->rec[d->reccount]; 105 *ch = *templ; 106 if (chn_init(ch, devinfo, dir)) { 107 device_printf(dev, "chn_init() for %s:%d failed\n", 108 (dir == PCMDIR_PLAY)? "play" : "record", 109 (dir == PCMDIR_PLAY)? d->playcount : d->reccount); 110 return 1; 111 } 112 if (dir == PCMDIR_PLAY) d->playcount++; else d->reccount++; 113 make_dev(&snd_cdevsw, PCMMKMINOR(unit, SND_DEV_DSP, d->chancount), 114 UID_ROOT, GID_WHEEL, 0666, "dsp%d.%d", unit, d->chancount); 115 make_dev(&snd_cdevsw, PCMMKMINOR(unit, SND_DEV_AUDIO, d->chancount), 116 UID_ROOT, GID_WHEEL, 0666, "audio%d.%d", unit, d->chancount); 117 make_dev(&snd_cdevsw, PCMMKMINOR(unit, SND_DEV_DSP16, d->chancount), 118 UID_ROOT, GID_WHEEL, 0666, "dspW%d.%d", unit, d->chancount); 119 /* XXX SND_DEV_NORESET? */ 120 d->chancount++; 121 return 0; 122 } 123 124 int 125 pcm_setstatus(device_t dev, char *str) 126 { 127 snddev_info *d = device_get_softc(dev); 128 strncpy(d->status, str, SND_STATUSLEN); 129 return 0; 130 } 131 132 u_int32_t 133 pcm_getflags(device_t dev) 134 { 135 snddev_info *d = device_get_softc(dev); 136 return d->flags; 137 } 138 139 void 140 pcm_setflags(device_t dev, u_int32_t val) 141 { 142 snddev_info *d = device_get_softc(dev); 143 d->flags = val; 144 } 145 146 void 147 pcm_setswap(device_t dev, pcm_swap_t *swap) 148 { 149 snddev_info *d = device_get_softc(dev); 150 d->swap = swap; 151 } 152 /* This is the generic init routine */ 153 int 154 pcm_register(device_t dev, void *devinfo, int numplay, int numrec) 155 { 156 int sz, unit = device_get_unit(dev); 157 snddev_info *d = device_get_softc(dev); 158 159 if (!pcm_devclass) { 160 pcm_devclass = device_get_devclass(dev); 161 make_dev(&snd_cdevsw, PCMMKMINOR(0, SND_DEV_STATUS, 0), 162 UID_ROOT, GID_WHEEL, 0444, "sndstat"); 163 } 164 make_dev(&snd_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0), 165 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit); 166 167 d->devinfo = devinfo; 168 d->chancount = d->playcount = d->reccount = 0; 169 sz = (numplay + numrec) * sizeof(pcm_channel *); 170 171 if (sz > 0) { 172 d->aplay = (pcm_channel **)malloc(sz, M_DEVBUF, M_NOWAIT); 173 if (!d->aplay) goto no; 174 bzero(d->aplay, sz); 175 176 d->arec = (pcm_channel **)malloc(sz, M_DEVBUF, M_NOWAIT); 177 if (!d->arec) goto no; 178 bzero(d->arec, sz); 179 } 180 181 if (numplay > 0) { 182 d->play = (pcm_channel *)malloc(numplay * sizeof(pcm_channel), 183 M_DEVBUF, M_NOWAIT); 184 if (!d->play) goto no; 185 bzero(d->play, numplay * sizeof(pcm_channel)); 186 } 187 188 if (numrec > 0) { 189 d->rec = (pcm_channel *)malloc(numrec * sizeof(pcm_channel), 190 M_DEVBUF, M_NOWAIT); 191 if (!d->rec) goto no; 192 bzero(d->rec, numrec * sizeof(pcm_channel)); 193 } 194 195 fkchan_setup(&d->fakechan); 196 chn_init(&d->fakechan, NULL, 0); 197 d->magic = MAGIC(unit); /* debugging... */ 198 d->swap = NULL; 199 200 return 0; 201 no: 202 if (d->aplay) free(d->aplay, M_DEVBUF); 203 if (d->play) free(d->play, M_DEVBUF); 204 if (d->arec) free(d->arec, M_DEVBUF); 205 if (d->rec) free(d->rec, M_DEVBUF); 206 return ENXIO; 207 } 208 209 /* 210 * a small utility function which, given a device number, returns 211 * a pointer to the associated snddev_info struct, and sets the unit 212 * number. 213 */ 214 static snddev_info * 215 get_snddev_info(dev_t i_dev, int *unit, int *dev, int *chan) 216 { 217 int u, d, c; 218 219 u = PCMUNIT(i_dev); 220 d = PCMDEV(i_dev); 221 c = PCMCHAN(i_dev); 222 if (u > devclass_get_maxunit(pcm_devclass)) u = -1; 223 if (unit) *unit = u; 224 if (dev) *dev = d; 225 if (chan) *chan = c; 226 if (u < 0) return NULL; 227 228 switch(d) { 229 case SND_DEV_CTL: /* /dev/mixer handled by pcm */ 230 case SND_DEV_STATUS: /* /dev/sndstat handled by pcm */ 231 case SND_DEV_DSP: 232 case SND_DEV_DSP16: 233 case SND_DEV_AUDIO: 234 return gsd(u); 235 236 case SND_DEV_SEQ: /* XXX when enabled... */ 237 case SND_DEV_SEQ2: 238 case SND_DEV_MIDIN: 239 case SND_DEV_SNDPROC: /* /dev/sndproc handled by pcm */ 240 default: 241 printf("unsupported subdevice %d\n", d); 242 return NULL; 243 } 244 } 245 246 static int 247 sndopen(dev_t i_dev, int flags, int mode, struct proc *p) 248 { 249 int dev, unit, chan; 250 snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan); 251 252 DEB(printf("open snd%d subdev %d flags 0x%08x mode 0x%08x\n", 253 unit, dev, flags, mode)); 254 255 switch(dev) { 256 case SND_DEV_STATUS: 257 if (status_isopen) return EBUSY; 258 status_isopen = 1; 259 return 0; 260 261 case SND_DEV_CTL: 262 return d? 0 : ENXIO; 263 264 case SND_DEV_AUDIO: 265 case SND_DEV_DSP: 266 case SND_DEV_DSP16: 267 case SND_DEV_NORESET: 268 return d? dsp_open(d, chan, flags, dev) : ENXIO; 269 270 default: 271 return ENXIO; 272 } 273 } 274 275 static int 276 sndclose(dev_t i_dev, int flags, int mode, struct proc *p) 277 { 278 int dev, unit, chan; 279 snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan); 280 281 DEB(printf("close snd%d subdev %d\n", unit, dev)); 282 283 switch(dev) { /* only those for which close makes sense */ 284 case SND_DEV_STATUS: 285 if (!status_isopen) return EBADF; 286 status_isopen = 0; 287 return 0; 288 289 case SND_DEV_CTL: 290 return d? 0 : ENXIO; 291 292 case SND_DEV_AUDIO: 293 case SND_DEV_DSP: 294 case SND_DEV_DSP16: 295 return d? dsp_close(d, chan, dev) : ENXIO; 296 297 default: 298 return ENXIO; 299 } 300 } 301 302 static int 303 sndread(dev_t i_dev, struct uio *buf, int flag) 304 { 305 int dev, unit, chan; 306 snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan); 307 DEB(printf("read snd%d subdev %d flag 0x%08x\n", unit, dev, flag)); 308 309 switch(dev) { 310 case SND_DEV_STATUS: 311 return status_isopen? status_read(buf) : EBADF; 312 313 case SND_DEV_AUDIO: 314 case SND_DEV_DSP: 315 case SND_DEV_DSP16: 316 return d? dsp_read(d, chan, buf, flag) : EBADF; 317 318 default: 319 return ENXIO; 320 } 321 } 322 323 static int 324 sndwrite(dev_t i_dev, struct uio *buf, int flag) 325 { 326 int dev, unit, chan; 327 snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan); 328 329 DEB(printf("write snd%d subdev %d flag 0x%08x\n", unit, dev & 0xf, flag)); 330 331 switch(dev) { /* only writeable devices */ 332 case SND_DEV_DSP: 333 case SND_DEV_DSP16: 334 case SND_DEV_AUDIO: 335 return d? dsp_write(d, chan, buf, flag) : EBADF; 336 337 default: 338 return EPERM; /* for non-writeable devices ; */ 339 } 340 } 341 342 static int 343 sndioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct proc * p) 344 { 345 int dev, chan; 346 snddev_info *d = get_snddev_info(i_dev, NULL, &dev, &chan); 347 348 if (d == NULL) return ENXIO; 349 350 switch(dev) { 351 case SND_DEV_CTL: 352 return mixer_ioctl(d, cmd, arg); 353 354 case SND_DEV_AUDIO: 355 case SND_DEV_DSP: 356 case SND_DEV_DSP16: 357 if (IOCGROUP(cmd) == 'M') 358 return mixer_ioctl(d, cmd, arg); 359 else 360 return dsp_ioctl(d, chan, cmd, arg); 361 362 default: 363 return ENXIO; 364 } 365 } 366 367 static int 368 sndpoll(dev_t i_dev, int events, struct proc *p) 369 { 370 int dev, chan; 371 snddev_info *d = get_snddev_info(i_dev, NULL, &dev, &chan); 372 373 DEB(printf("sndpoll d 0x%p dev 0x%04x events 0x%08x\n", d, dev, events)); 374 375 if (d == NULL) return ENXIO; 376 377 switch(dev) { 378 case SND_DEV_AUDIO: 379 case SND_DEV_DSP: 380 case SND_DEV_DSP16: 381 return dsp_poll(d, chan, events, p); 382 383 default: 384 return (events & 385 (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)) | POLLHUP; 386 } 387 } 388 389 /* 390 * The mmap interface allows access to the play and read buffer, 391 * plus the device descriptor. 392 * The various blocks are accessible at the following offsets: 393 * 394 * 0x00000000 ( 0 ) : write buffer ; 395 * 0x01000000 (16 MB) : read buffer ; 396 * 0x02000000 (32 MB) : device descriptor (dangerous!) 397 * 398 * WARNING: the mmap routines assume memory areas are aligned. This 399 * is true (probably) for the dma buffers, but likely false for the 400 * device descriptor. As a consequence, we do not know where it is 401 * located in the requested area. 402 */ 403 static int 404 sndmmap(dev_t i_dev, vm_offset_t offset, int nprot) 405 { 406 int unit, dev, chan; 407 snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan); 408 409 DEB(printf("sndmmap d 0x%p dev 0x%04x ofs 0x%08x nprot 0x%08x\n", 410 d, dev, offset, nprot)); 411 412 if (d == NULL || nprot & PROT_EXEC) return -1; /* forbidden */ 413 414 switch(dev) { 415 case SND_DEV_AUDIO: 416 case SND_DEV_DSP: 417 case SND_DEV_DSP16: 418 return dsp_mmap(d, chan, offset, nprot); 419 420 default: 421 return -1; 422 } 423 } 424 425 static int 426 status_init(char *buf, int size) 427 { 428 int i; 429 device_t dev; 430 snddev_info *d; 431 432 snprintf(buf, size, "FreeBSD Audio Driver (newpcm) %s %s\n" 433 "Installed devices:\n", __DATE__, __TIME__); 434 435 for (i = 0; i <= devclass_get_maxunit(pcm_devclass); i++) { 436 d = gsd(i); 437 if (!d) continue; 438 dev = devclass_get_device(pcm_devclass, i); 439 if (1) { 440 snprintf(buf + strlen(buf), size - strlen(buf), 441 "pcm%d: <%s> %s", 442 i, device_get_desc(dev), d->status); 443 if (d->chancount > 0) 444 snprintf(buf + strlen(buf), size - strlen(buf), 445 " (%dp/%dr channels%s)\n", 446 d->playcount, d->reccount, 447 (!(d->flags & SD_F_SIMPLEX))? " duplex" : ""); 448 else 449 snprintf(buf + strlen(buf), size - strlen(buf), 450 " (mixer only)\n"); 451 } 452 } 453 return strlen(buf); 454 } 455 456 static int 457 status_read(struct uio *buf) 458 { 459 static char status_buf[4096]; 460 static int bufptr = 0, buflen = 0; 461 int l; 462 463 if (status_isopen == 1) { 464 status_isopen++; 465 bufptr = 0; 466 buflen = status_init(status_buf, sizeof status_buf); 467 } 468 469 l = min(buf->uio_resid, buflen - bufptr); 470 bufptr += l; 471 return (l > 0)? uiomove(status_buf + bufptr - l, l, buf) : 0; 472 } 473