Deleted Added
sdiff udiff text old ( 167647 ) new ( 170161 )
full compact
1/*-
2 * Copyright (c) 1999 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

--- 10 unchanged lines hidden (view full) ---

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 <sys/param.h>
28#include <sys/queue.h>
29
30#include <dev/sound/pcm/sound.h>
31
32SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/dsp.c 167647 2007-03-16 17:17:25Z ariff $");
33
34#define OLDPCM_IOCTL
35
36static d_open_t dsp_open;
37static d_close_t dsp_close;
38static d_read_t dsp_read;
39static d_write_t dsp_write;
40static d_ioctl_t dsp_ioctl;
41static d_poll_t dsp_poll;

--- 8 unchanged lines hidden (view full) ---

50 .d_write = dsp_write,
51 .d_ioctl = dsp_ioctl,
52 .d_poll = dsp_poll,
53 .d_mmap = dsp_mmap,
54 .d_name = "dsp",
55};
56
57#ifdef USING_DEVFS
58static eventhandler_tag dsp_ehtag;
59#endif
60
61static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group);
62static int dsp_oss_syncstart(int sg_id);
63static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy);
64#ifdef OSSV4_EXPERIMENT
65static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled);
66static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
67static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
68static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
69static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
70static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
71static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
72static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name);
73#endif
74
75static struct snddev_info *
76dsp_get_info(struct cdev *dev)
77{
78 struct snddev_info *d;
79 int unit;
80
81 unit = PCMUNIT(dev);
82 if (unit >= devclass_get_maxunit(pcm_devclass))
83 return NULL;
84 d = devclass_get_softc(pcm_devclass, unit);
85
86 return d;
87}
88
89static u_int32_t
90dsp_get_flags(struct cdev *dev)
91{
92 device_t bdev;
93 int unit;
94
95 unit = PCMUNIT(dev);
96 if (unit >= devclass_get_maxunit(pcm_devclass))
97 return 0xffffffff;
98 bdev = devclass_get_device(pcm_devclass, unit);
99
100 return pcm_getflags(bdev);
101}
102
103static void
104dsp_set_flags(struct cdev *dev, u_int32_t flags)
105{
106 device_t bdev;
107 int unit;
108
109 unit = PCMUNIT(dev);
110 if (unit >= devclass_get_maxunit(pcm_devclass))
111 return;
112 bdev = devclass_get_device(pcm_devclass, unit);
113
114 pcm_setflags(bdev, flags);
115}
116
117/*
118 * return the channels associated with an open device instance.
119 * set the priority if the device is simplex and one direction (only) is
120 * specified.
121 * lock channels specified.
122 */
123static int
124getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch, u_int32_t prio)
125{
126 struct snddev_info *d;
127 u_int32_t flags;
128
129 flags = dsp_get_flags(dev);
130 d = dsp_get_info(dev);
131 pcm_inprog(d, 1);
132 pcm_lock(d);
133 KASSERT((flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
134 ("getchns: read and write both prioritised"));
135
136 if ((flags & SD_F_PRIO_SET) == 0 && (prio != (SD_F_PRIO_RD | SD_F_PRIO_WR))) {
137 flags |= prio & (SD_F_PRIO_RD | SD_F_PRIO_WR);
138 dsp_set_flags(dev, flags);
139 }
140
141 *rdch = dev->si_drv1;
142 *wrch = dev->si_drv2;
143 if ((flags & SD_F_SIMPLEX) && (flags & SD_F_PRIO_SET)) {
144 if (prio) {
145 if (*rdch && flags & SD_F_PRIO_WR) {
146 dev->si_drv1 = NULL;
147 *rdch = pcm_getfakechan(d);
148 } else if (*wrch && flags & SD_F_PRIO_RD) {
149 dev->si_drv2 = NULL;
150 *wrch = pcm_getfakechan(d);
151 }
152 }
153
154 pcm_getfakechan(d)->flags |= CHN_F_BUSY;
155 }
156 pcm_unlock(d);
157

--- 7 unchanged lines hidden (view full) ---

165
166/* unlock specified channels */
167static void
168relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch, u_int32_t prio)
169{
170 struct snddev_info *d;
171
172 d = dsp_get_info(dev);
173 if (wrch && wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
174 CHN_UNLOCK(wrch);
175 if (rdch && rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
176 CHN_UNLOCK(rdch);
177 pcm_inprog(d, -1);
178}
179
180static int
181dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
182{
183 struct pcm_channel *rdch, *wrch;
184 struct snddev_info *d;
185 u_int32_t fmt;
186 int devtype;
187 int error;
188 int chnum;
189
190 if (i_dev == NULL || td == NULL)
191 return ENODEV;
192
193 if ((flags & (FREAD | FWRITE)) == 0)
194 return EINVAL;
195
196 d = dsp_get_info(i_dev);
197 devtype = PCMDEV(i_dev);
198 chnum = -1;
199
200 /* decide default format */
201 switch (devtype) {
202 case SND_DEV_DSP16:
203 fmt = AFMT_S16_LE;
204 break;
205
206 case SND_DEV_DSP:
207 fmt = AFMT_U8;
208 break;
209
210 case SND_DEV_AUDIO:
211 fmt = AFMT_MU_LAW;
212 break;
213
214 case SND_DEV_NORESET:
215 fmt = 0;
216 break;
217
218 case SND_DEV_DSPHW:
219 /*
220 * HW *specific* access without channel numbering confusion
221 * caused by "first come first served" by dsp_clone().
222 */
223 fmt = AFMT_U8;
224 chnum = PCMCHAN(i_dev);
225 break;
226
227 default:
228 panic("impossible devtype %d", devtype);
229 }
230
231 /* lock snddev so nobody else can monkey with it */
232 pcm_lock(d);
233
234 rdch = i_dev->si_drv1;
235 wrch = i_dev->si_drv2;
236
237 if (rdch || wrch || ((dsp_get_flags(i_dev) & SD_F_SIMPLEX) &&
238 (flags & (FREAD | FWRITE)) == (FREAD | FWRITE))) {
239 /* simplex or not, better safe than sorry. */
240 pcm_unlock(d);
241 return EBUSY;
242 }
243
244 /*
245 * if we get here, the open request is valid- either:
246 * * we were previously not open
247 * * we were open for play xor record and the opener wants
248 * the non-open direction
249 */
250 if (flags & FREAD) {
251 /* open for read */
252 pcm_unlock(d);
253 error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, chnum);
254 if (error != 0 && error != EBUSY && chnum != -1 && (flags & FWRITE))
255 error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, -1);
256
257 if (error == 0 && (chn_reset(rdch, fmt) ||
258 (fmt && chn_setspeed(rdch, DSP_DEFAULT_SPEED))))
259 error = ENODEV;
260
261 if (error != 0) {
262 if (rdch)
263 pcm_chnrelease(rdch);
264 return error;
265 }
266
267 if (flags & O_NONBLOCK)
268 rdch->flags |= CHN_F_NBIO;
269 pcm_chnref(rdch, 1);
270 CHN_UNLOCK(rdch);
271 pcm_lock(d);
272 }
273
274 if (flags & FWRITE) {
275 /* open for write */
276 pcm_unlock(d);
277 error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, chnum);
278 if (error != 0 && error != EBUSY && chnum != -1 && (flags & FREAD))
279 error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, -1);
280
281 if (error == 0 && (chn_reset(wrch, fmt) ||
282 (fmt && chn_setspeed(wrch, DSP_DEFAULT_SPEED))))
283 error = ENODEV;
284
285 if (error != 0) {
286 if (wrch)
287 pcm_chnrelease(wrch);
288 if (rdch) {
289 /*
290 * Lock, deref and release previously created record channel
291 */
292 CHN_LOCK(rdch);
293 pcm_chnref(rdch, -1);
294 pcm_chnrelease(rdch);
295 }
296
297 return error;
298 }
299
300 if (flags & O_NONBLOCK)
301 wrch->flags |= CHN_F_NBIO;
302 pcm_chnref(wrch, 1);
303 CHN_UNLOCK(wrch);
304 pcm_lock(d);
305 }
306
307 i_dev->si_drv1 = rdch;
308 i_dev->si_drv2 = wrch;
309
310 pcm_unlock(d);
311 return 0;
312}
313
314static int
315dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
316{
317 struct pcm_channel *rdch, *wrch;
318 struct snddev_info *d;
319 int refs, sg_ids[2];
320
321 d = dsp_get_info(i_dev);
322 pcm_lock(d);
323 rdch = i_dev->si_drv1;
324 wrch = i_dev->si_drv2;
325 pcm_unlock(d);
326
327 /*
328 * Free_unr() may sleep, so store released syncgroup IDs until after
329 * all locks are released.
330 */
331 sg_ids[0] = sg_ids[1] = 0;
332
333 if (rdch || wrch) {
334 refs = 0;
335 if (rdch) {
336 /*
337 * The channel itself need not be locked because:
338 * a) Adding a channel to a syncgroup happens only in dsp_ioctl(),
339 * which cannot run concurrently to dsp_close().
340 * b) The syncmember pointer (sm) is protected by the global
341 * syncgroup list lock.
342 * c) A channel can't just disappear, invalidating pointers,

--- 23 unchanged lines hidden (view full) ---

366 chn_flush(wrch); /* may sleep */
367 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
368 chn_reset(wrch, 0);
369 pcm_chnrelease(wrch);
370 }
371
372 pcm_lock(d);
373 if (rdch)
374 i_dev->si_drv1 = NULL;
375 if (wrch)
376 i_dev->si_drv2 = NULL;
377 /*
378 * If there are no more references, release the channels.
379 */
380 if (refs == 0 && i_dev->si_drv1 == NULL &&
381 i_dev->si_drv2 == NULL) {
382 if (pcm_getfakechan(d))
383 pcm_getfakechan(d)->flags = 0;
384 /* What is this?!? */
385 dsp_set_flags(i_dev, dsp_get_flags(i_dev) & ~SD_F_TRANSIENT);
386 }
387 pcm_unlock(d);
388 }
389
390
391 if (sg_ids[0])
392 free_unr(pcmsg_unrhdr, sg_ids[0]);
393 if (sg_ids[1])
394 free_unr(pcmsg_unrhdr, sg_ids[1]);
395
396 return 0;
397}
398
399static int
400dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
401{
402 struct pcm_channel *rdch, *wrch;
403 int ret;
404
405 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD);
406
407 KASSERT(rdch, ("dsp_read: nonexistant channel"));
408 KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
409
410 if (rdch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
411 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
412 return EINVAL;
413 }
414 if (!(rdch->flags & CHN_F_RUNNING))
415 rdch->flags |= CHN_F_RUNNING;
416 ret = chn_read(rdch, buf);

--- 5 unchanged lines hidden (view full) ---

422static int
423dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
424{
425 struct pcm_channel *rdch, *wrch;
426 int ret;
427
428 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_WR);
429
430 KASSERT(wrch, ("dsp_write: nonexistant channel"));
431 KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
432
433 if (wrch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
434 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
435 return EINVAL;
436 }
437 if (!(wrch->flags & CHN_F_RUNNING))
438 wrch->flags |= CHN_F_RUNNING;
439

--- 24 unchanged lines hidden (view full) ---

464 xcmd = 0;
465
466 /*
467 * this is an evil hack to allow broken apps to perform mixer ioctls
468 * on dsp devices.
469 */
470
471 d = dsp_get_info(i_dev);
472 if (IOCGROUP(cmd) == 'M') {
473 /*
474 * This is at least, a bug to bug compatible with OSS.
475 */
476 if (d->mixer_dev != NULL)
477 return mixer_ioctl(d->mixer_dev, cmd, arg, -1, td);
478 else
479 return EBADF;

--- 31 unchanged lines hidden (view full) ---

511 if (kill == 3) {
512 relchns(i_dev, rdch, wrch, 0);
513 return EINVAL;
514 }
515 if (kill & 1)
516 wrch = NULL;
517 if (kill & 2)
518 rdch = NULL;
519
520 switch(cmd) {
521#ifdef OLDPCM_IOCTL
522 /*
523 * we start with the new ioctl interface.
524 */
525 case AIONWRITE: /* how many bytes can write ? */
526 if (wrch) {
527 CHN_LOCK(wrch);

--- 970 unchanged lines hidden (view full) ---

1498 *paddr = vtophys(sndbuf_getbufofs(c->bufsoft, offset));
1499 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1500
1501 return 0;
1502}
1503
1504#ifdef USING_DEVFS
1505
1506/*
1507 * Clone logic is this:
1508 * x E X = {dsp, dspW, audio}
1509 * x -> x${sysctl("hw.snd.unit")}
1510 * xN->
1511 * for i N = 1 to channels of device N
1512 * if xN.i isn't busy, return its dev_t
1513 */
1514static void
1515dsp_clone(void *arg, struct ucred *cred, char *name, int namelen,
1516 struct cdev **dev)
1517{
1518 struct cdev *pdev;
1519 struct snddev_info *pcm_dev;
1520 struct snddev_channel *pcm_chan;
1521 int i, unit, devtype;
1522 static int devtypes[3] = {SND_DEV_DSP, SND_DEV_DSP16, SND_DEV_AUDIO};
1523 static char *devnames[3] = {"dsp", "dspW", "audio"};
1524
1525 if (*dev != NULL)
1526 return;
1527 if (pcm_devclass == NULL)
1528 return;
1529
1530 devtype = 0;
1531 unit = -1;
1532 for (i = 0; (i < 3) && (unit == -1); i++) {
1533 devtype = devtypes[i];
1534 if (strcmp(name, devnames[i]) == 0) {
1535 unit = snd_unit;
1536 } else {
1537 if (dev_stdclone(name, NULL, devnames[i], &unit) != 1)
1538 unit = -1;
1539 }
1540 }
1541 if (unit == -1 || unit >= devclass_get_maxunit(pcm_devclass))
1542 return;
1543
1544 pcm_dev = devclass_get_softc(pcm_devclass, unit);
1545
1546 if (pcm_dev == NULL)
1547 return;
1548
1549 SLIST_FOREACH(pcm_chan, &pcm_dev->channels, link) {
1550
1551 switch(devtype) {
1552 case SND_DEV_DSP:
1553 pdev = pcm_chan->dsp_devt;
1554 break;
1555 case SND_DEV_DSP16:
1556 pdev = pcm_chan->dspW_devt;
1557 break;
1558 case SND_DEV_AUDIO:
1559 pdev = pcm_chan->audio_devt;
1560 break;
1561 default:
1562 panic("Unknown devtype %d", devtype);
1563 }
1564
1565 if ((pdev != NULL) && (pdev->si_drv1 == NULL) && (pdev->si_drv2 == NULL)) {
1566 *dev = pdev;
1567 dev_ref(*dev);
1568 return;
1569 }
1570 }
1571}
1572
1573static void
1574dsp_sysinit(void *p)
1575{
1576 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
1577}
1578
1579static void
1580dsp_sysuninit(void *p)
1581{
1582 if (dsp_ehtag != NULL)
1583 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
1584}
1585
1586SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
1587SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
1588#endif
1589
1590/**
1591 * @brief Handler for SNDCTL_AUDIOINFO.
1592 *
1593 * Gathers information about the audio device specified in ai->dev. If
1594 * ai->dev == -1, then this function gathers information about the current
1595 * device. If the call comes in on a non-audio device and ai->dev == -1,
1596 * return EINVAL.
1597 *

--- 19 unchanged lines hidden (view full) ---

1617 * @retval 0 success
1618 * @retval EINVAL ai->dev specifies an invalid device
1619 *
1620 * @todo Verify correctness of Doxygen tags. ;)
1621 */
1622int
1623dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
1624{
1625 struct snddev_channel *sce;
1626 struct pcmchan_caps *caps;
1627 struct pcm_channel *ch;
1628 struct snddev_info *d;
1629 struct cdev *t_cdev;
1630 uint32_t fmts;
1631 int i, nchan, ret, *rates, minch, maxch;
1632
1633 /*
1634 * If probing the device that received the ioctl, make sure it's a
1635 * DSP device. (Users may use this ioctl with /dev/mixer and
1636 * /dev/midi.)
1637 */
1638 if ((ai->dev == -1) && (i_dev->si_devsw != &dsp_cdevsw))
1639 return EINVAL;
1640
1641 ch = NULL;
1642 t_cdev = NULL;
1643 nchan = 0;
1644 ret = 0;
1645
1646 /*
1647 * Search for the requested audio device (channel). Start by
1648 * iterating over pcm devices.
1649 */
1650 for (i = 0; i < devclass_get_maxunit(pcm_devclass); i++) {
1651 d = devclass_get_softc(pcm_devclass, i);
1652 if (d == NULL)
1653 continue;
1654
1655 /* See the note in function docblock */
1656 mtx_assert(d->lock, MA_NOTOWNED);
1657 pcm_inprog(d, 1);
1658 pcm_lock(d);
1659
1660 SLIST_FOREACH(sce, &d->channels, link) {
1661 ch = sce->channel;
1662 mtx_assert(ch->lock, MA_NOTOWNED);
1663 CHN_LOCK(ch);
1664 if (ai->dev == -1) {
1665 if ((ch == i_dev->si_drv1) || /* record ch */
1666 (ch == i_dev->si_drv2)) { /* playback ch */
1667 t_cdev = i_dev;
1668 goto dspfound;
1669 }
1670 } else if (ai->dev == nchan) {
1671 t_cdev = sce->dsp_devt;
1672 goto dspfound;
1673 }
1674 CHN_UNLOCK(ch);
1675 ++nchan;
1676 }
1677
1678 pcm_unlock(d);
1679 pcm_inprog(d, -1);
1680 }
1681
1682 /* Exhausted the search -- nothing is locked, so return. */
1683 return EINVAL;
1684
1685dspfound:
1686 /* Should've found the device, but something isn't right */
1687 if (t_cdev == NULL) {
1688 ret = EINVAL;
1689 goto out;
1690 }
1691
1692 /*
1693 * At this point, the following synchronization stuff has happened:
1694 * - a specific PCM device is locked and its "in progress
1695 * operations" counter has been incremented, so be sure to unlock

--- 20 unchanged lines hidden (view full) ---

1716
1717 /**
1718 * @note
1719 * @c cmd - OSSv4 docs: "Only supported under Linux at this moment."
1720 * Cop-out, I know, but I'll save running around in the process
1721 * table for later. Is there a risk of leaking information?
1722 */
1723 ai->pid = ch->pid;
1724
1725 /*
1726 * These flags stolen from SNDCTL_DSP_GETCAPS handler. Note, however,
1727 * that a single channel operates in only one direction, so
1728 * DSP_CAP_DUPLEX is out.
1729 */
1730 /**
1731 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep these in
1732 * pcmchan::caps?

--- 46 unchanged lines hidden (view full) ---

1779 */
1780 ai->port_number = -1;
1781 ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
1782 /**
1783 * @note
1784 * @c real_device - OSSv4 docs: "Obsolete."
1785 */
1786 ai->real_device = -1;
1787 strlcpy(ai->devnode, t_cdev->si_name, sizeof(ai->devnode));
1788 ai->enabled = device_is_attached(d->dev) ? 1 : 0;
1789 /**
1790 * @note
1791 * @c flags - OSSv4 docs: "Reserved for future use."
1792 *
1793 * @note
1794 * @c binding - OSSv4 docs: "Reserved for future use."
1795 *

--- 207 unchanged lines hidden (view full) ---

2003 */
2004static int
2005dsp_oss_syncstart(int sg_id)
2006{
2007 struct pcmchan_syncmember *sm, *sm_tmp;
2008 struct pcmchan_syncgroup *sg;
2009 struct pcm_channel *c;
2010 int ret, needlocks;
2011
2012 /* Get the synclists lock */
2013 PCM_SG_LOCK();
2014
2015 do {
2016 ret = 0;
2017 needlocks = 0;
2018
2019 /* Search for syncgroup by ID */

--- 341 unchanged lines hidden ---