Deleted Added
full compact
dsp.c (167647) dsp.c (170161)
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
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
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
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 <sys/param.h>
28#include <sys/queue.h>
29
30#include <dev/sound/pcm/sound.h>
27#include <dev/sound/pcm/sound.h>
28#include <sys/ctype.h>
31
29
32SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/dsp.c 167647 2007-03-16 17:17:25Z ariff $");
30SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/dsp.c 170161 2007-05-31 18:43:33Z ariff $");
33
31
32struct dsp_cdevinfo {
33 struct pcm_channel *rdch, *wrch;
34};
35
36#define PCM_RDCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->rdch)
37#define PCM_WRCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->wrch)
38
39#define PCMDEV_ACQUIRE(x) do { \
40 if ((x)->si_drv1 == NULL) \
41 (x)->si_drv1 = x; \
42} while(0)
43
44#define PCMDEV_RELEASE(x) do { \
45 if ((x)->si_drv1 == x) \
46 (x)->si_drv1 = NULL; \
47} while(0)
48
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;
42static d_mmap_t dsp_mmap;
43
44struct cdevsw dsp_cdevsw = {
45 .d_version = D_VERSION,
46 .d_flags = D_NEEDGIANT,
47 .d_open = dsp_open,
48 .d_close = dsp_close,
49 .d_read = dsp_read,
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
49#define OLDPCM_IOCTL
50
51static d_open_t dsp_open;
52static d_close_t dsp_close;
53static d_read_t dsp_read;
54static d_write_t dsp_write;
55static d_ioctl_t dsp_ioctl;
56static d_poll_t dsp_poll;
57static d_mmap_t dsp_mmap;
58
59struct cdevsw dsp_cdevsw = {
60 .d_version = D_VERSION,
61 .d_flags = D_NEEDGIANT,
62 .d_open = dsp_open,
63 .d_close = dsp_close,
64 .d_read = dsp_read,
65 .d_write = dsp_write,
66 .d_ioctl = dsp_ioctl,
67 .d_poll = dsp_poll,
68 .d_mmap = dsp_mmap,
69 .d_name = "dsp",
70};
71
72#ifdef USING_DEVFS
58static eventhandler_tag dsp_ehtag;
73static eventhandler_tag dsp_ehtag = NULL;
74static int dsp_umax = -1;
75static int dsp_cmax = -1;
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{
76#endif
77
78static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group);
79static int dsp_oss_syncstart(int sg_id);
80static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy);
81#ifdef OSSV4_EXPERIMENT
82static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled);
83static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
84static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
85static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
86static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
87static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
88static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
89static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name);
90#endif
91
92static struct snddev_info *
93dsp_get_info(struct cdev *dev)
94{
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;
95 return (devclass_get_softc(pcm_devclass, PCMUNIT(dev)));
87}
88
89static u_int32_t
90dsp_get_flags(struct cdev *dev)
91{
92 device_t bdev;
96}
97
98static u_int32_t
99dsp_get_flags(struct cdev *dev)
100{
101 device_t bdev;
93 int unit;
94
102
95 unit = PCMUNIT(dev);
96 if (unit >= devclass_get_maxunit(pcm_devclass))
97 return 0xffffffff;
98 bdev = devclass_get_device(pcm_devclass, unit);
103 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
99
104
100 return pcm_getflags(bdev);
105 return ((bdev != NULL) ? pcm_getflags(bdev) : 0xffffffff);
101}
102
103static void
104dsp_set_flags(struct cdev *dev, u_int32_t flags)
105{
106 device_t bdev;
106}
107
108static void
109dsp_set_flags(struct cdev *dev, u_int32_t flags)
110{
111 device_t bdev;
107 int unit;
108
112
109 unit = PCMUNIT(dev);
110 if (unit >= devclass_get_maxunit(pcm_devclass))
111 return;
112 bdev = devclass_get_device(pcm_devclass, unit);
113 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
113
114
114 pcm_setflags(bdev, flags);
115 if (bdev != NULL)
116 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
117}
118
119/*
120 * return the channels associated with an open device instance.
121 * set the priority if the device is simplex and one direction (only) is
122 * specified.
123 * lock channels specified.
124 */
125static int
126getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch, u_int32_t prio)
127{
128 struct snddev_info *d;
129 u_int32_t flags;
130
129 flags = dsp_get_flags(dev);
130 d = dsp_get_info(dev);
131 d = dsp_get_info(dev);
132 if (d == NULL)
133 return -1;
131 pcm_inprog(d, 1);
132 pcm_lock(d);
134 pcm_inprog(d, 1);
135 pcm_lock(d);
136 flags = dsp_get_flags(dev);
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
137 KASSERT((flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
138 ("getchns: read and write both prioritised"));
139
140 if ((flags & SD_F_PRIO_SET) == 0 && (prio != (SD_F_PRIO_RD | SD_F_PRIO_WR))) {
141 flags |= prio & (SD_F_PRIO_RD | SD_F_PRIO_WR);
142 dsp_set_flags(dev, flags);
143 }
144
141 *rdch = dev->si_drv1;
142 *wrch = dev->si_drv2;
145 *rdch = PCM_RDCH(dev);
146 *wrch = PCM_WRCH(dev);
143 if ((flags & SD_F_SIMPLEX) && (flags & SD_F_PRIO_SET)) {
144 if (prio) {
145 if (*rdch && flags & SD_F_PRIO_WR) {
147 if ((flags & SD_F_SIMPLEX) && (flags & SD_F_PRIO_SET)) {
148 if (prio) {
149 if (*rdch && flags & SD_F_PRIO_WR) {
146 dev->si_drv1 = NULL;
150 PCM_RDCH(dev) = NULL;
147 *rdch = pcm_getfakechan(d);
148 } else if (*wrch && flags & SD_F_PRIO_RD) {
151 *rdch = pcm_getfakechan(d);
152 } else if (*wrch && flags & SD_F_PRIO_RD) {
149 dev->si_drv2 = NULL;
153 PCM_WRCH(dev) = NULL;
150 *wrch = pcm_getfakechan(d);
151 }
152 }
153
154 pcm_getfakechan(d)->flags |= CHN_F_BUSY;
155 }
156 pcm_unlock(d);
157
158 if (*rdch && *rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
159 CHN_LOCK(*rdch);
160 if (*wrch && *wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
161 CHN_LOCK(*wrch);
162
163 return 0;
164}
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);
154 *wrch = pcm_getfakechan(d);
155 }
156 }
157
158 pcm_getfakechan(d)->flags |= CHN_F_BUSY;
159 }
160 pcm_unlock(d);
161
162 if (*rdch && *rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
163 CHN_LOCK(*rdch);
164 if (*wrch && *wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
165 CHN_LOCK(*wrch);
166
167 return 0;
168}
169
170/* unlock specified channels */
171static void
172relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch, u_int32_t prio)
173{
174 struct snddev_info *d;
175
176 d = dsp_get_info(dev);
177 if (d == NULL)
178 return;
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
179 if (wrch && wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
180 CHN_UNLOCK(wrch);
181 if (rdch && rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
182 CHN_UNLOCK(rdch);
183 pcm_inprog(d, -1);
184}
185
186static void
187dsp_cdevinfo_alloc(struct cdev *dev,
188 struct pcm_channel *rdch, struct pcm_channel *wrch)
189{
190 KASSERT(dev != NULL && dev->si_drv1 == dev && rdch != wrch,
191 ("bogus %s(), what are you trying to accomplish here?", __func__));
192
193 dev->si_drv1 = malloc(sizeof(struct dsp_cdevinfo), M_DEVBUF,
194 M_WAITOK | M_ZERO);
195 PCM_RDCH(dev) = rdch;
196 PCM_WRCH(dev) = wrch;
197}
198
199static void
200dsp_cdevinfo_free(struct cdev *dev)
201{
202 KASSERT(dev != NULL && dev->si_drv1 != NULL &&
203 PCM_RDCH(dev) == NULL && PCM_WRCH(dev) == NULL,
204 ("bogus %s(), what are you trying to accomplish here?", __func__));
205
206 free(dev->si_drv1, M_DEVBUF);
207 dev->si_drv1 = NULL;
208}
209
210/* duplex / simplex cdev type */
211enum {
212 DSP_CDEV_TYPE_RDONLY, /* simplex read-only (record) */
213 DSP_CDEV_TYPE_WRONLY, /* simplex write-only (play) */
214 DSP_CDEV_TYPE_RDWR, /* duplex read, write, or both */
215};
216
217#define DSP_F_VALID(x) ((x) & (FREAD | FWRITE))
218#define DSP_F_DUPLEX(x) (((x) & (FREAD | FWRITE)) == (FREAD | FWRITE))
219#define DSP_F_SIMPLEX(x) (!DSP_F_DUPLEX(x))
220#define DSP_F_READ(x) ((x) & FREAD)
221#define DSP_F_WRITE(x) ((x) & FWRITE)
222
223static const struct {
224 int type;
225 char *name;
226 char *sep;
227 int use_sep;
228 int hw;
229 int max;
230 uint32_t fmt, spd;
231 int query;
232} dsp_cdevs[] = {
233 { SND_DEV_DSP, "dsp", ".", 0, 0, 0,
234 AFMT_U8, DSP_DEFAULT_SPEED, DSP_CDEV_TYPE_RDWR },
235 { SND_DEV_AUDIO, "audio", ".", 0, 0, 0,
236 AFMT_MU_LAW, DSP_DEFAULT_SPEED, DSP_CDEV_TYPE_RDWR },
237 { SND_DEV_DSP16, "dspW", ".", 0, 0, 0,
238 AFMT_S16_LE, DSP_DEFAULT_SPEED, DSP_CDEV_TYPE_RDWR },
239 { SND_DEV_DSPHW_PLAY, "dsp", ".p", 1, 1, SND_MAXHWCHAN,
240 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_WRONLY },
241 { SND_DEV_DSPHW_VPLAY, "dsp", ".vp", 1, 1, SND_MAXVCHANS,
242 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_WRONLY },
243 { SND_DEV_DSPHW_REC, "dsp", ".r", 1, 1, SND_MAXHWCHAN,
244 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_RDONLY },
245 { SND_DEV_DSPHW_VREC, "dsp", ".vr", 1, 1, SND_MAXVCHANS,
246 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_RDONLY },
247};
248
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;
249static int
250dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
251{
252 struct pcm_channel *rdch, *wrch;
253 struct snddev_info *d;
185 u_int32_t fmt;
186 int devtype;
187 int error;
188 int chnum;
254 uint32_t fmt, spd;
255 int i, error, devtype;
256 int wdevunit, rdevunit;
189
257
258 /* Kind of impossible.. */
190 if (i_dev == NULL || td == NULL)
191 return ENODEV;
192
259 if (i_dev == NULL || td == NULL)
260 return ENODEV;
261
193 if ((flags & (FREAD | FWRITE)) == 0)
194 return EINVAL;
195
262 /* This too.. */
196 d = dsp_get_info(i_dev);
263 d = dsp_get_info(i_dev);
197 devtype = PCMDEV(i_dev);
198 chnum = -1;
264 if (d == NULL)
265 return EBADF;
199
266
200 /* decide default format */
201 switch (devtype) {
202 case SND_DEV_DSP16:
203 fmt = AFMT_S16_LE;
204 break;
267 /* Lock snddev so nobody else can monkey with it. */
268 pcm_lock(d);
205
269
206 case SND_DEV_DSP:
207 fmt = AFMT_U8;
208 break;
270 /*
271 * Try to acquire cloned device before someone else pick it.
272 * ENODEV means this is not a cloned droids.
273 */
274 error = snd_clone_acquire(i_dev);
275 if (!(error == 0 || error == ENODEV)) {
276 pcm_unlock(d);
277 return error;
278 }
209
279
210 case SND_DEV_AUDIO:
211 fmt = AFMT_MU_LAW;
212 break;
280 if (!DSP_F_VALID(flags))
281 error = EINVAL;
282 else if (i_dev->si_drv1 != NULL)
283 error = EBUSY;
284 else if (DSP_F_DUPLEX(flags) &&
285 (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
286 error = ENOTSUP;
287 else
288 error = 0;
213
289
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);
290 if (error != 0) {
291 (void)snd_clone_release(i_dev);
292 pcm_unlock(d);
293 return error;
229 }
230
294 }
295
231 /* lock snddev so nobody else can monkey with it */
232 pcm_lock(d);
296 /*
297 * Fake busy state by pointing si_drv1 to something else since
298 * we have to give up locking somewhere during setup process.
299 */
300 PCMDEV_ACQUIRE(i_dev);
233
301
234 rdch = i_dev->si_drv1;
235 wrch = i_dev->si_drv2;
302 devtype = PCMDEV(i_dev);
303 wdevunit = -1;
304 rdevunit = -1;
305 fmt = 0;
306 spd = 0;
236
307
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;
308 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
309 if (devtype != dsp_cdevs[i].type)
310 continue;
311 if (DSP_F_SIMPLEX(flags) &&
312 ((dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY &&
313 DSP_F_READ(flags)) ||
314 (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY &&
315 DSP_F_WRITE(flags)))) {
316 /*
317 * simplex, opposite direction? Please be gone..
318 */
319 (void)snd_clone_release(i_dev);
320 PCMDEV_RELEASE(i_dev);
321 pcm_unlock(d);
322 return ENOTSUP;
323 }
324 if (dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY)
325 wdevunit = dev2unit(i_dev);
326 else if (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY)
327 rdevunit = dev2unit(i_dev);
328 fmt = dsp_cdevs[i].fmt;
329 spd = dsp_cdevs[i].spd;
330 break;
242 }
243
331 }
332
333 /* No matching devtype? */
334 if (fmt == 0 || spd == 0)
335 panic("impossible devtype %d", devtype);
336
337 rdch = NULL;
338 wrch = NULL;
339
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 */
340 /*
341 * if we get here, the open request is valid- either:
342 * * we were previously not open
343 * * we were open for play xor record and the opener wants
344 * the non-open direction
345 */
250 if (flags & FREAD) {
346 if (DSP_F_READ(flags)) {
251 /* open for read */
252 pcm_unlock(d);
347 /* open for read */
348 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);
349 error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid,
350 rdevunit);
256
351
257 if (error == 0 && (chn_reset(rdch, fmt) ||
258 (fmt && chn_setspeed(rdch, DSP_DEFAULT_SPEED))))
352 if (error == 0 && (chn_reset(rdch, fmt) != 0 ||
353 (chn_setspeed(rdch, spd) != 0)))
259 error = ENODEV;
260
261 if (error != 0) {
354 error = ENODEV;
355
356 if (error != 0) {
262 if (rdch)
357 if (rdch != NULL)
263 pcm_chnrelease(rdch);
358 pcm_chnrelease(rdch);
359 pcm_lock(d);
360 (void)snd_clone_release(i_dev);
361 PCMDEV_RELEASE(i_dev);
362 pcm_unlock(d);
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
363 return error;
364 }
365
366 if (flags & O_NONBLOCK)
367 rdch->flags |= CHN_F_NBIO;
368 pcm_chnref(rdch, 1);
369 CHN_UNLOCK(rdch);
370 pcm_lock(d);
371 }
372
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);
373 if (DSP_F_WRITE(flags)) {
374 /* open for write */
375 pcm_unlock(d);
376 error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid,
377 wdevunit);
280
378
281 if (error == 0 && (chn_reset(wrch, fmt) ||
282 (fmt && chn_setspeed(wrch, DSP_DEFAULT_SPEED))))
283 error = ENODEV;
379 if (error == 0 && (chn_reset(wrch, fmt) != 0 ||
380 (chn_setspeed(wrch, spd) != 0)))
381 error = ENODEV;
284
382
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);
383 if (error != 0) {
384 if (wrch != NULL)
385 pcm_chnrelease(wrch);
386 if (rdch != NULL) {
387 /*
388 * Lock, deref and release previously
389 * created record channel
390 */
391 CHN_LOCK(rdch);
392 pcm_chnref(rdch, -1);
393 pcm_chnrelease(rdch);
394 }
395 pcm_lock(d);
396 (void)snd_clone_release(i_dev);
397 PCMDEV_RELEASE(i_dev);
398 pcm_unlock(d);
399 return error;
295 }
296
400 }
401
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);
402 if (flags & O_NONBLOCK)
403 wrch->flags |= CHN_F_NBIO;
404 pcm_chnref(wrch, 1);
405 CHN_UNLOCK(wrch);
406 pcm_lock(d);
305 }
306
407 }
408
307 i_dev->si_drv1 = rdch;
308 i_dev->si_drv2 = wrch;
409 /*
410 * Increase clone refcount for its automatic garbage collector.
411 */
412 (void)snd_clone_ref(i_dev);
309
310 pcm_unlock(d);
413
414 pcm_unlock(d);
415
416 /*
417 * We're done. Allocate and point si_drv1 to a real
418 * allocated structure.
419 */
420 dsp_cdevinfo_alloc(i_dev, rdch, wrch);
421
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);
422 return 0;
423}
424
425static int
426dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
427{
428 struct pcm_channel *rdch, *wrch;
429 struct snddev_info *d;
430 int refs, sg_ids[2];
431
432 d = dsp_get_info(i_dev);
433 if (d == NULL)
434 return EBADF;
322 pcm_lock(d);
435 pcm_lock(d);
323 rdch = i_dev->si_drv1;
324 wrch = i_dev->si_drv2;
325 pcm_unlock(d);
436 rdch = PCM_RDCH(i_dev);
437 wrch = PCM_WRCH(i_dev);
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;
438
439 /*
440 * Free_unr() may sleep, so store released syncgroup IDs until after
441 * all locks are released.
442 */
443 sg_ids[0] = sg_ids[1] = 0;
444
445 if (rdch || wrch) {
446 refs = 0;
447 pcm_unlock(d);
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,
343 * unless it's closed/dereferenced first.
344 */
345 PCM_SG_LOCK();
346 sg_ids[0] = chn_syncdestroy(rdch);
347 PCM_SG_UNLOCK();
348
349 CHN_LOCK(rdch);
350 refs += pcm_chnref(rdch, -1);
351 chn_abort(rdch); /* won't sleep */
352 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
353 chn_reset(rdch, 0);
354 pcm_chnrelease(rdch);
355 }
356 if (wrch) {
357 /*
358 * Please see block above.
359 */
360 PCM_SG_LOCK();
361 sg_ids[1] = chn_syncdestroy(wrch);
362 PCM_SG_UNLOCK();
363
364 CHN_LOCK(wrch);
365 refs += pcm_chnref(wrch, -1);
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)
448 if (rdch) {
449 /*
450 * The channel itself need not be locked because:
451 * a) Adding a channel to a syncgroup happens only in dsp_ioctl(),
452 * which cannot run concurrently to dsp_close().
453 * b) The syncmember pointer (sm) is protected by the global
454 * syncgroup list lock.
455 * c) A channel can't just disappear, invalidating pointers,
456 * unless it's closed/dereferenced first.
457 */
458 PCM_SG_LOCK();
459 sg_ids[0] = chn_syncdestroy(rdch);
460 PCM_SG_UNLOCK();
461
462 CHN_LOCK(rdch);
463 refs += pcm_chnref(rdch, -1);
464 chn_abort(rdch); /* won't sleep */
465 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
466 chn_reset(rdch, 0);
467 pcm_chnrelease(rdch);
468 }
469 if (wrch) {
470 /*
471 * Please see block above.
472 */
473 PCM_SG_LOCK();
474 sg_ids[1] = chn_syncdestroy(wrch);
475 PCM_SG_UNLOCK();
476
477 CHN_LOCK(wrch);
478 refs += pcm_chnref(wrch, -1);
479 chn_flush(wrch); /* may sleep */
480 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
481 chn_reset(wrch, 0);
482 pcm_chnrelease(wrch);
483 }
484
485 pcm_lock(d);
486 if (rdch)
374 i_dev->si_drv1 = NULL;
487 PCM_RDCH(i_dev) = NULL;
375 if (wrch)
488 if (wrch)
376 i_dev->si_drv2 = NULL;
489 PCM_WRCH(i_dev) = NULL;
377 /*
378 * If there are no more references, release the channels.
379 */
490 /*
491 * If there are no more references, release the channels.
492 */
380 if (refs == 0 && i_dev->si_drv1 == NULL &&
381 i_dev->si_drv2 == NULL) {
493 if (refs == 0 && PCM_RDCH(i_dev) == NULL &&
494 PCM_WRCH(i_dev) == 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);
495 if (pcm_getfakechan(d))
496 pcm_getfakechan(d)->flags = 0;
497 /* What is this?!? */
498 dsp_set_flags(i_dev, dsp_get_flags(i_dev) & ~SD_F_TRANSIENT);
499 dsp_cdevinfo_free(i_dev);
500 /*
501 * Release clone busy state and unref it
502 * so the automatic garbage collector will
503 * get the hint and do the remaining cleanup
504 * process.
505 */
506 (void)snd_clone_release(i_dev);
507 (void)snd_clone_unref(i_dev);
386 }
508 }
387 pcm_unlock(d);
388 }
389
509 }
510
511 pcm_unlock(d);
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
512
513 if (sg_ids[0])
514 free_unr(pcmsg_unrhdr, sg_ids[0]);
515 if (sg_ids[1])
516 free_unr(pcmsg_unrhdr, sg_ids[1]);
517
518 return 0;
519}
520
521static int
522dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
523{
524 struct pcm_channel *rdch, *wrch;
525 int ret;
526
527 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD);
528
407 KASSERT(rdch, ("dsp_read: nonexistant channel"));
408 KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
529 if (rdch == NULL || !(rdch->flags & CHN_F_BUSY))
530 return EBADF;
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);
417 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
418
419 return ret;
420}
421
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
531
532 if (rdch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
533 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
534 return EINVAL;
535 }
536 if (!(rdch->flags & CHN_F_RUNNING))
537 rdch->flags |= CHN_F_RUNNING;
538 ret = chn_read(rdch, buf);
539 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
540
541 return ret;
542}
543
544static int
545dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
546{
547 struct pcm_channel *rdch, *wrch;
548 int ret;
549
550 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_WR);
551
430 KASSERT(wrch, ("dsp_write: nonexistant channel"));
431 KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
552 if (wrch == NULL || !(wrch->flags & CHN_F_BUSY))
553 return EBADF;
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
440 /*
441 * Chn_write() must give up channel lock in order to copy bytes from
442 * userland, so up the "in progress" counter to make sure someone
443 * else doesn't come along and muss up the buffer.
444 */
445 ++wrch->inprog;
446 ret = chn_write(wrch, buf);
447 --wrch->inprog;
448 cv_signal(&wrch->cv);
449
450 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
451
452 return ret;
453}
454
455static int
456dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
457{
458 struct pcm_channel *chn, *rdch, *wrch;
459 struct snddev_info *d;
460 int kill;
461 int ret = 0, *arg_i = (int *)arg, tmp;
462 int xcmd;
463
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);
554
555 if (wrch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
556 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
557 return EINVAL;
558 }
559 if (!(wrch->flags & CHN_F_RUNNING))
560 wrch->flags |= CHN_F_RUNNING;
561
562 /*
563 * Chn_write() must give up channel lock in order to copy bytes from
564 * userland, so up the "in progress" counter to make sure someone
565 * else doesn't come along and muss up the buffer.
566 */
567 ++wrch->inprog;
568 ret = chn_write(wrch, buf);
569 --wrch->inprog;
570 cv_signal(&wrch->cv);
571
572 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
573
574 return ret;
575}
576
577static int
578dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
579{
580 struct pcm_channel *chn, *rdch, *wrch;
581 struct snddev_info *d;
582 int kill;
583 int ret = 0, *arg_i = (int *)arg, tmp;
584 int xcmd;
585
586 xcmd = 0;
587
588 /*
589 * this is an evil hack to allow broken apps to perform mixer ioctls
590 * on dsp devices.
591 */
592
593 d = dsp_get_info(i_dev);
594 if (d == NULL)
595 return EBADF;
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;
480 }
481
482 /*
483 * Certain ioctls may be made on any type of device (audio, mixer,
484 * and MIDI). Handle those special cases here.
485 */
486 if (IOCGROUP(cmd) == 'X') {
487 switch(cmd) {
488 case SNDCTL_SYSINFO:
489 sound_oss_sysinfo((oss_sysinfo *)arg);
490 break;
491 case SNDCTL_AUDIOINFO:
492 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
493 break;
494 case SNDCTL_MIXERINFO:
495 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
496 break;
497 default:
498 ret = EINVAL;
499 }
500
501 return ret;
502 }
503
504 getchns(i_dev, &rdch, &wrch, 0);
505
506 kill = 0;
507 if (wrch && (wrch->flags & CHN_F_DEAD))
508 kill |= 1;
509 if (rdch && (rdch->flags & CHN_F_DEAD))
510 kill |= 2;
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;
596 if (IOCGROUP(cmd) == 'M') {
597 /*
598 * This is at least, a bug to bug compatible with OSS.
599 */
600 if (d->mixer_dev != NULL)
601 return mixer_ioctl(d->mixer_dev, cmd, arg, -1, td);
602 else
603 return EBADF;
604 }
605
606 /*
607 * Certain ioctls may be made on any type of device (audio, mixer,
608 * and MIDI). Handle those special cases here.
609 */
610 if (IOCGROUP(cmd) == 'X') {
611 switch(cmd) {
612 case SNDCTL_SYSINFO:
613 sound_oss_sysinfo((oss_sysinfo *)arg);
614 break;
615 case SNDCTL_AUDIOINFO:
616 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
617 break;
618 case SNDCTL_MIXERINFO:
619 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
620 break;
621 default:
622 ret = EINVAL;
623 }
624
625 return ret;
626 }
627
628 getchns(i_dev, &rdch, &wrch, 0);
629
630 kill = 0;
631 if (wrch && (wrch->flags & CHN_F_DEAD))
632 kill |= 1;
633 if (rdch && (rdch->flags & CHN_F_DEAD))
634 kill |= 2;
635 if (kill == 3) {
636 relchns(i_dev, rdch, wrch, 0);
637 return EINVAL;
638 }
639 if (kill & 1)
640 wrch = NULL;
641 if (kill & 2)
642 rdch = NULL;
519
643
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);
528/*
529 if (wrch && wrch->bufhard.dl)
530 while (chn_wrfeed(wrch) == 0);
531*/
532 *arg_i = sndbuf_getfree(wrch->bufsoft);
533 CHN_UNLOCK(wrch);
534 } else {
535 *arg_i = 0;
536 ret = EINVAL;
537 }
538 break;
539
540 case AIOSSIZE: /* set the current blocksize */
541 {
542 struct snd_size *p = (struct snd_size *)arg;
543
544 p->play_size = 0;
545 p->rec_size = 0;
546 if (wrch) {
547 CHN_LOCK(wrch);
548 chn_setblocksize(wrch, 2, p->play_size);
549 p->play_size = sndbuf_getblksz(wrch->bufsoft);
550 CHN_UNLOCK(wrch);
551 }
552 if (rdch) {
553 CHN_LOCK(rdch);
554 chn_setblocksize(rdch, 2, p->rec_size);
555 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
556 CHN_UNLOCK(rdch);
557 }
558 }
559 break;
560 case AIOGSIZE: /* get the current blocksize */
561 {
562 struct snd_size *p = (struct snd_size *)arg;
563
564 if (wrch) {
565 CHN_LOCK(wrch);
566 p->play_size = sndbuf_getblksz(wrch->bufsoft);
567 CHN_UNLOCK(wrch);
568 }
569 if (rdch) {
570 CHN_LOCK(rdch);
571 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
572 CHN_UNLOCK(rdch);
573 }
574 }
575 break;
576
577 case AIOSFMT:
578 case AIOGFMT:
579 {
580 snd_chan_param *p = (snd_chan_param *)arg;
581
582 if (cmd == AIOSFMT &&
583 ((p->play_format != 0 && p->play_rate == 0) ||
584 (p->rec_format != 0 && p->rec_rate == 0))) {
585 ret = EINVAL;
586 break;
587 }
588 if (wrch) {
589 CHN_LOCK(wrch);
590 if (cmd == AIOSFMT && p->play_format != 0) {
591 chn_setformat(wrch, p->play_format);
592 chn_setspeed(wrch, p->play_rate);
593 }
594 p->play_rate = wrch->speed;
595 p->play_format = wrch->format;
596 CHN_UNLOCK(wrch);
597 } else {
598 p->play_rate = 0;
599 p->play_format = 0;
600 }
601 if (rdch) {
602 CHN_LOCK(rdch);
603 if (cmd == AIOSFMT && p->rec_format != 0) {
604 chn_setformat(rdch, p->rec_format);
605 chn_setspeed(rdch, p->rec_rate);
606 }
607 p->rec_rate = rdch->speed;
608 p->rec_format = rdch->format;
609 CHN_UNLOCK(rdch);
610 } else {
611 p->rec_rate = 0;
612 p->rec_format = 0;
613 }
614 }
615 break;
616
617 case AIOGCAP: /* get capabilities */
618 {
619 snd_capabilities *p = (snd_capabilities *)arg;
620 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
621 struct cdev *pdev;
622
623 if (rdch) {
624 CHN_LOCK(rdch);
625 rcaps = chn_getcaps(rdch);
626 }
627 if (wrch) {
628 CHN_LOCK(wrch);
629 pcaps = chn_getcaps(wrch);
630 }
631 p->rate_min = max(rcaps? rcaps->minspeed : 0,
632 pcaps? pcaps->minspeed : 0);
633 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
634 pcaps? pcaps->maxspeed : 1000000);
635 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
636 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
637 /* XXX bad on sb16 */
638 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
639 (wrch? chn_getformats(wrch) : 0xffffffff);
640 if (rdch && wrch)
641 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
642 pdev = d->mixer_dev;
643 p->mixers = 1; /* default: one mixer */
644 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
645 p->left = p->right = 100;
646 if (rdch)
647 CHN_UNLOCK(rdch);
648 if (wrch)
649 CHN_UNLOCK(wrch);
650 }
651 break;
652
653 case AIOSTOP:
654 if (*arg_i == AIOSYNC_PLAY && wrch) {
655 CHN_LOCK(wrch);
656 *arg_i = chn_abort(wrch);
657 CHN_UNLOCK(wrch);
658 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
659 CHN_LOCK(rdch);
660 *arg_i = chn_abort(rdch);
661 CHN_UNLOCK(rdch);
662 } else {
663 printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
664 *arg_i = 0;
665 }
666 break;
667
668 case AIOSYNC:
669 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
670 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
671 break;
672#endif
673 /*
674 * here follow the standard ioctls (filio.h etc.)
675 */
676 case FIONREAD: /* get # bytes to read */
677 if (rdch) {
678 CHN_LOCK(rdch);
679/* if (rdch && rdch->bufhard.dl)
680 while (chn_rdfeed(rdch) == 0);
681*/
682 *arg_i = sndbuf_getready(rdch->bufsoft);
683 CHN_UNLOCK(rdch);
684 } else {
685 *arg_i = 0;
686 ret = EINVAL;
687 }
688 break;
689
690 case FIOASYNC: /*set/clear async i/o */
691 DEB( printf("FIOASYNC\n") ; )
692 break;
693
694 case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */
695 case FIONBIO: /* set/clear non-blocking i/o */
696 if (rdch) {
697 CHN_LOCK(rdch);
698 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
699 rdch->flags |= CHN_F_NBIO;
700 else
701 rdch->flags &= ~CHN_F_NBIO;
702 CHN_UNLOCK(rdch);
703 }
704 if (wrch) {
705 CHN_LOCK(wrch);
706 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
707 wrch->flags |= CHN_F_NBIO;
708 else
709 wrch->flags &= ~CHN_F_NBIO;
710 CHN_UNLOCK(wrch);
711 }
712 break;
713
714 /*
715 * Finally, here is the linux-compatible ioctl interface
716 */
717#define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
718 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
719 case SNDCTL_DSP_GETBLKSIZE:
720 chn = wrch ? wrch : rdch;
721 if (chn) {
722 CHN_LOCK(chn);
723 *arg_i = sndbuf_getblksz(chn->bufsoft);
724 CHN_UNLOCK(chn);
725 } else {
726 *arg_i = 0;
727 ret = EINVAL;
728 }
729 break ;
730
731 case SNDCTL_DSP_SETBLKSIZE:
732 RANGE(*arg_i, 16, 65536);
733 if (wrch) {
734 CHN_LOCK(wrch);
735 chn_setblocksize(wrch, 2, *arg_i);
736 CHN_UNLOCK(wrch);
737 }
738 if (rdch) {
739 CHN_LOCK(rdch);
740 chn_setblocksize(rdch, 2, *arg_i);
741 CHN_UNLOCK(rdch);
742 }
743 break;
744
745 case SNDCTL_DSP_RESET:
746 DEB(printf("dsp reset\n"));
747 if (wrch) {
748 CHN_LOCK(wrch);
749 chn_abort(wrch);
750 chn_resetbuf(wrch);
751 CHN_UNLOCK(wrch);
752 }
753 if (rdch) {
754 CHN_LOCK(rdch);
755 chn_abort(rdch);
756 chn_resetbuf(rdch);
757 CHN_UNLOCK(rdch);
758 }
759 break;
760
761 case SNDCTL_DSP_SYNC:
762 DEB(printf("dsp sync\n"));
763 /* chn_sync may sleep */
764 if (wrch) {
765 CHN_LOCK(wrch);
766 chn_sync(wrch, 0);
767 CHN_UNLOCK(wrch);
768 }
769 break;
770
771 case SNDCTL_DSP_SPEED:
772 /* chn_setspeed may sleep */
773 tmp = 0;
774 if (wrch) {
775 CHN_LOCK(wrch);
776 ret = chn_setspeed(wrch, *arg_i);
777 tmp = wrch->speed;
778 CHN_UNLOCK(wrch);
779 }
780 if (rdch && ret == 0) {
781 CHN_LOCK(rdch);
782 ret = chn_setspeed(rdch, *arg_i);
783 if (tmp == 0)
784 tmp = rdch->speed;
785 CHN_UNLOCK(rdch);
786 }
787 *arg_i = tmp;
788 break;
789
790 case SOUND_PCM_READ_RATE:
791 chn = wrch ? wrch : rdch;
792 if (chn) {
793 CHN_LOCK(chn);
794 *arg_i = chn->speed;
795 CHN_UNLOCK(chn);
796 } else {
797 *arg_i = 0;
798 ret = EINVAL;
799 }
800 break;
801
802 case SNDCTL_DSP_STEREO:
803 tmp = -1;
804 *arg_i = (*arg_i)? AFMT_STEREO : 0;
805 if (wrch) {
806 CHN_LOCK(wrch);
807 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
808 tmp = (wrch->format & AFMT_STEREO)? 1 : 0;
809 CHN_UNLOCK(wrch);
810 }
811 if (rdch && ret == 0) {
812 CHN_LOCK(rdch);
813 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
814 if (tmp == -1)
815 tmp = (rdch->format & AFMT_STEREO)? 1 : 0;
816 CHN_UNLOCK(rdch);
817 }
818 *arg_i = tmp;
819 break;
820
821 case SOUND_PCM_WRITE_CHANNELS:
822/* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
823 if (*arg_i != 0) {
824 tmp = 0;
825 *arg_i = (*arg_i != 1)? AFMT_STEREO : 0;
826 if (wrch) {
827 CHN_LOCK(wrch);
828 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
829 tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
830 CHN_UNLOCK(wrch);
831 }
832 if (rdch && ret == 0) {
833 CHN_LOCK(rdch);
834 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
835 if (tmp == 0)
836 tmp = (rdch->format & AFMT_STEREO)? 2 : 1;
837 CHN_UNLOCK(rdch);
838 }
839 *arg_i = tmp;
840 } else {
841 chn = wrch ? wrch : rdch;
842 CHN_LOCK(chn);
843 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
844 CHN_UNLOCK(chn);
845 }
846 break;
847
848 case SOUND_PCM_READ_CHANNELS:
849 chn = wrch ? wrch : rdch;
850 if (chn) {
851 CHN_LOCK(chn);
852 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
853 CHN_UNLOCK(chn);
854 } else {
855 *arg_i = 0;
856 ret = EINVAL;
857 }
858 break;
859
860 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
861 chn = wrch ? wrch : rdch;
862 if (chn) {
863 CHN_LOCK(chn);
864 *arg_i = chn_getformats(chn);
865 CHN_UNLOCK(chn);
866 } else {
867 *arg_i = 0;
868 ret = EINVAL;
869 }
870 break ;
871
872 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
873 if ((*arg_i != AFMT_QUERY)) {
874 tmp = 0;
875 if (wrch) {
876 CHN_LOCK(wrch);
877 ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
878 tmp = wrch->format & ~AFMT_STEREO;
879 CHN_UNLOCK(wrch);
880 }
881 if (rdch && ret == 0) {
882 CHN_LOCK(rdch);
883 ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
884 if (tmp == 0)
885 tmp = rdch->format & ~AFMT_STEREO;
886 CHN_UNLOCK(rdch);
887 }
888 *arg_i = tmp;
889 } else {
890 chn = wrch ? wrch : rdch;
891 CHN_LOCK(chn);
892 *arg_i = chn->format & ~AFMT_STEREO;
893 CHN_UNLOCK(chn);
894 }
895 break;
896
897 case SNDCTL_DSP_SETFRAGMENT:
898 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
899 {
900 u_int32_t fragln = (*arg_i) & 0x0000ffff;
901 u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
902 u_int32_t fragsz;
903 u_int32_t r_maxfrags, r_fragsz;
904
905 RANGE(fragln, 4, 16);
906 fragsz = 1 << fragln;
907
908 if (maxfrags == 0)
909 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
910 if (maxfrags < 2)
911 maxfrags = 2;
912 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
913 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
914
915 DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
916 if (rdch) {
917 CHN_LOCK(rdch);
918 ret = chn_setblocksize(rdch, maxfrags, fragsz);
919 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
920 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
921 CHN_UNLOCK(rdch);
922 } else {
923 r_maxfrags = maxfrags;
924 r_fragsz = fragsz;
925 }
926 if (wrch && ret == 0) {
927 CHN_LOCK(wrch);
928 ret = chn_setblocksize(wrch, maxfrags, fragsz);
929 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
930 fragsz = sndbuf_getblksz(wrch->bufsoft);
931 CHN_UNLOCK(wrch);
932 } else { /* use whatever came from the read channel */
933 maxfrags = r_maxfrags;
934 fragsz = r_fragsz;
935 }
936
937 fragln = 0;
938 while (fragsz > 1) {
939 fragln++;
940 fragsz >>= 1;
941 }
942 *arg_i = (maxfrags << 16) | fragln;
943 }
944 break;
945
946 case SNDCTL_DSP_GETISPACE:
947 /* return the size of data available in the input queue */
948 {
949 audio_buf_info *a = (audio_buf_info *)arg;
950 if (rdch) {
951 struct snd_dbuf *bs = rdch->bufsoft;
952
953 CHN_LOCK(rdch);
954 a->bytes = sndbuf_getready(bs);
955 a->fragments = a->bytes / sndbuf_getblksz(bs);
956 a->fragstotal = sndbuf_getblkcnt(bs);
957 a->fragsize = sndbuf_getblksz(bs);
958 CHN_UNLOCK(rdch);
959 }
960 }
961 break;
962
963 case SNDCTL_DSP_GETOSPACE:
964 /* return space available in the output queue */
965 {
966 audio_buf_info *a = (audio_buf_info *)arg;
967 if (wrch) {
968 struct snd_dbuf *bs = wrch->bufsoft;
969
970 CHN_LOCK(wrch);
971 /* XXX abusive DMA update: chn_wrupdate(wrch); */
972 a->bytes = sndbuf_getfree(bs);
973 a->fragments = a->bytes / sndbuf_getblksz(bs);
974 a->fragstotal = sndbuf_getblkcnt(bs);
975 a->fragsize = sndbuf_getblksz(bs);
976 CHN_UNLOCK(wrch);
977 }
978 }
979 break;
980
981 case SNDCTL_DSP_GETIPTR:
982 {
983 count_info *a = (count_info *)arg;
984 if (rdch) {
985 struct snd_dbuf *bs = rdch->bufsoft;
986
987 CHN_LOCK(rdch);
988 /* XXX abusive DMA update: chn_rdupdate(rdch); */
989 a->bytes = sndbuf_gettotal(bs);
990 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
991 a->ptr = sndbuf_getreadyptr(bs);
992 rdch->blocks = sndbuf_getblocks(bs);
993 CHN_UNLOCK(rdch);
994 } else
995 ret = EINVAL;
996 }
997 break;
998
999 case SNDCTL_DSP_GETOPTR:
1000 {
1001 count_info *a = (count_info *)arg;
1002 if (wrch) {
1003 struct snd_dbuf *bs = wrch->bufsoft;
1004
1005 CHN_LOCK(wrch);
1006 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1007 a->bytes = sndbuf_gettotal(bs);
1008 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
1009 a->ptr = sndbuf_getreadyptr(bs);
1010 wrch->blocks = sndbuf_getblocks(bs);
1011 CHN_UNLOCK(wrch);
1012 } else
1013 ret = EINVAL;
1014 }
1015 break;
1016
1017 case SNDCTL_DSP_GETCAPS:
1018 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
1019 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1020 *arg_i |= DSP_CAP_DUPLEX;
1021 break;
1022
1023 case SOUND_PCM_READ_BITS:
1024 chn = wrch ? wrch : rdch;
1025 if (chn) {
1026 CHN_LOCK(chn);
1027 if (chn->format & AFMT_8BIT)
1028 *arg_i = 8;
1029 else if (chn->format & AFMT_16BIT)
1030 *arg_i = 16;
1031 else if (chn->format & AFMT_24BIT)
1032 *arg_i = 24;
1033 else if (chn->format & AFMT_32BIT)
1034 *arg_i = 32;
1035 else
1036 ret = EINVAL;
1037 CHN_UNLOCK(chn);
1038 } else {
1039 *arg_i = 0;
1040 ret = EINVAL;
1041 }
1042 break;
1043
1044 case SNDCTL_DSP_SETTRIGGER:
1045 if (rdch) {
1046 CHN_LOCK(rdch);
1047 rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
1048 if (*arg_i & PCM_ENABLE_INPUT)
1049 chn_start(rdch, 1);
1050 else
1051 rdch->flags |= CHN_F_NOTRIGGER;
1052 CHN_UNLOCK(rdch);
1053 }
1054 if (wrch) {
1055 CHN_LOCK(wrch);
1056 wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
1057 if (*arg_i & PCM_ENABLE_OUTPUT)
1058 chn_start(wrch, 1);
1059 else
1060 wrch->flags |= CHN_F_NOTRIGGER;
1061 CHN_UNLOCK(wrch);
1062 }
1063 break;
1064
1065 case SNDCTL_DSP_GETTRIGGER:
1066 *arg_i = 0;
1067 if (wrch) {
1068 CHN_LOCK(wrch);
1069 if (wrch->flags & CHN_F_TRIGGERED)
1070 *arg_i |= PCM_ENABLE_OUTPUT;
1071 CHN_UNLOCK(wrch);
1072 }
1073 if (rdch) {
1074 CHN_LOCK(rdch);
1075 if (rdch->flags & CHN_F_TRIGGERED)
1076 *arg_i |= PCM_ENABLE_INPUT;
1077 CHN_UNLOCK(rdch);
1078 }
1079 break;
1080
1081 case SNDCTL_DSP_GETODELAY:
1082 if (wrch) {
1083 struct snd_dbuf *bs = wrch->bufsoft;
1084
1085 CHN_LOCK(wrch);
1086 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1087 *arg_i = sndbuf_getready(bs);
1088 CHN_UNLOCK(wrch);
1089 } else
1090 ret = EINVAL;
1091 break;
1092
1093 case SNDCTL_DSP_POST:
1094 if (wrch) {
1095 CHN_LOCK(wrch);
1096 wrch->flags &= ~CHN_F_NOTRIGGER;
1097 chn_start(wrch, 1);
1098 CHN_UNLOCK(wrch);
1099 }
1100 break;
1101
1102 case SNDCTL_DSP_SETDUPLEX:
1103 /*
1104 * switch to full-duplex mode if card is in half-duplex
1105 * mode and is able to work in full-duplex mode
1106 */
1107 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1108 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1109 break;
1110
1111 /*
1112 * The following four ioctls are simple wrappers around mixer_ioctl
1113 * with no further processing. xcmd is short for "translated
1114 * command".
1115 */
1116 case SNDCTL_DSP_GETRECVOL:
1117 if (xcmd == 0)
1118 xcmd = SOUND_MIXER_READ_RECLEV;
1119 /* FALLTHROUGH */
1120 case SNDCTL_DSP_SETRECVOL:
1121 if (xcmd == 0)
1122 xcmd = SOUND_MIXER_WRITE_RECLEV;
1123 /* FALLTHROUGH */
1124 case SNDCTL_DSP_GETPLAYVOL:
1125 if (xcmd == 0)
1126 xcmd = SOUND_MIXER_READ_PCM;
1127 /* FALLTHROUGH */
1128 case SNDCTL_DSP_SETPLAYVOL:
1129 if (xcmd == 0)
1130 xcmd = SOUND_MIXER_WRITE_PCM;
1131
1132 if (d->mixer_dev != NULL)
1133 ret = mixer_ioctl(d->mixer_dev, xcmd, arg, -1, td);
1134 else
1135 ret = ENOTSUP;
1136 break;
1137
1138 case SNDCTL_DSP_GET_RECSRC_NAMES:
1139 case SNDCTL_DSP_GET_RECSRC:
1140 case SNDCTL_DSP_SET_RECSRC:
1141 if (d->mixer_dev != NULL)
1142 ret = mixer_ioctl(d->mixer_dev, cmd, arg, -1, td);
1143 else
1144 ret = ENOTSUP;
1145 break;
1146
1147 /*
1148 * The following 3 ioctls aren't very useful at the moment. For
1149 * now, only a single channel is associated with a cdev (/dev/dspN
1150 * instance), so there's only a single output routing to use (i.e.,
1151 * the wrch bound to this cdev).
1152 */
1153 case SNDCTL_DSP_GET_PLAYTGT_NAMES:
1154 {
1155 oss_mixer_enuminfo *ei;
1156 ei = (oss_mixer_enuminfo *)arg;
1157 ei->dev = 0;
1158 ei->ctrl = 0;
1159 ei->version = 0; /* static for now */
1160 ei->strindex[0] = 0;
1161
1162 if (wrch != NULL) {
1163 ei->nvalues = 1;
1164 strlcpy(ei->strings, wrch->name,
1165 sizeof(ei->strings));
1166 } else {
1167 ei->nvalues = 0;
1168 ei->strings[0] = '\0';
1169 }
1170 }
1171 break;
1172 case SNDCTL_DSP_GET_PLAYTGT:
1173 case SNDCTL_DSP_SET_PLAYTGT: /* yes, they are the same for now */
1174 /*
1175 * Re: SET_PLAYTGT
1176 * OSSv4: "The value that was accepted by the device will
1177 * be returned back in the variable pointed by the
1178 * argument."
1179 */
1180 if (wrch != NULL)
1181 *arg_i = 0;
1182 else
1183 ret = EINVAL;
1184 break;
1185
1186 case SNDCTL_DSP_SILENCE:
1187 /*
1188 * Flush the software (pre-feed) buffer, but try to minimize playback
1189 * interruption. (I.e., record unplayed samples with intent to
1190 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
1191 * functionality.
1192 */
1193 if (wrch == NULL)
1194 ret = EINVAL;
1195 else {
1196 struct snd_dbuf *bs;
1197 CHN_LOCK(wrch);
1198 while (wrch->inprog != 0)
1199 cv_wait(&wrch->cv, wrch->lock);
1200 bs = wrch->bufsoft;
1201 if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
1202 bs->sl = sndbuf_getready(bs);
1203 sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs));
1204 sndbuf_fillsilence(bs);
1205 chn_start(wrch, 0);
1206 }
1207 CHN_UNLOCK(wrch);
1208 }
1209 break;
1210
1211 case SNDCTL_DSP_SKIP:
1212 /*
1213 * OSSv4 docs: "This ioctl call discards all unplayed samples in the
1214 * playback buffer by moving the current write position immediately
1215 * before the point where the device is currently reading the samples."
1216 */
1217 if (wrch == NULL)
1218 ret = EINVAL;
1219 else {
1220 struct snd_dbuf *bs;
1221 CHN_LOCK(wrch);
1222 while (wrch->inprog != 0)
1223 cv_wait(&wrch->cv, wrch->lock);
1224 bs = wrch->bufsoft;
1225 if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
1226 sndbuf_softreset(bs);
1227 sndbuf_acquire(bs, bs->shadbuf, bs->sl);
1228 bs->sl = 0;
1229 chn_start(wrch, 0);
1230 }
1231 CHN_UNLOCK(wrch);
1232 }
1233 break;
1234
1235 case SNDCTL_DSP_CURRENT_OPTR:
1236 case SNDCTL_DSP_CURRENT_IPTR:
1237 /**
1238 * @note Changing formats resets the buffer counters, which differs
1239 * from the 4Front drivers. However, I don't expect this to be
1240 * much of a problem.
1241 *
1242 * @note In a test where @c CURRENT_OPTR is called immediately after write
1243 * returns, this driver is about 32K samples behind whereas
1244 * 4Front's is about 8K samples behind. Should determine source
1245 * of discrepancy, even if only out of curiosity.
1246 *
1247 * @todo Actually test SNDCTL_DSP_CURRENT_IPTR.
1248 */
1249 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
1250 if (chn == NULL)
1251 ret = EINVAL;
1252 else {
1253 struct snd_dbuf *bs;
1254 /* int tmp; */
1255
1256 oss_count_t *oc = (oss_count_t *)arg;
1257
1258 CHN_LOCK(chn);
1259 bs = chn->bufsoft;
1260#if 0
1261 tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b);
1262 oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getbps(b);
1263 oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getbps(b);
1264#else
1265 oc->samples = sndbuf_gettotal(bs) / sndbuf_getbps(bs);
1266 oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getbps(bs);
1267#endif
1268 CHN_UNLOCK(chn);
1269 }
1270 break;
1271
1272 case SNDCTL_DSP_HALT_OUTPUT:
1273 case SNDCTL_DSP_HALT_INPUT:
1274 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
1275 if (chn == NULL)
1276 ret = EINVAL;
1277 else {
1278 CHN_LOCK(chn);
1279 chn_abort(chn);
1280 CHN_UNLOCK(chn);
1281 }
1282 break;
1283
1284 case SNDCTL_DSP_LOW_WATER:
1285 /*
1286 * Set the number of bytes required to attract attention by
1287 * select/poll.
1288 */
1289 if (wrch != NULL) {
1290 CHN_LOCK(wrch);
1291 wrch->lw = (*arg_i > 1) ? *arg_i : 1;
1292 CHN_UNLOCK(wrch);
1293 }
1294 if (rdch != NULL) {
1295 CHN_LOCK(rdch);
1296 rdch->lw = (*arg_i > 1) ? *arg_i : 1;
1297 CHN_UNLOCK(rdch);
1298 }
1299 break;
1300
1301 case SNDCTL_DSP_GETERROR:
1302 /*
1303 * OSSv4 docs: "All errors and counters will automatically be
1304 * cleared to zeroes after the call so each call will return only
1305 * the errors that occurred after the previous invocation. ... The
1306 * play_underruns and rec_overrun fields are the only usefull fields
1307 * returned by OSS 4.0."
1308 */
1309 {
1310 audio_errinfo *ei = (audio_errinfo *)arg;
1311
1312 bzero((void *)ei, sizeof(*ei));
1313
1314 if (wrch != NULL) {
1315 CHN_LOCK(wrch);
1316 ei->play_underruns = wrch->xruns;
1317 wrch->xruns = 0;
1318 CHN_UNLOCK(wrch);
1319 }
1320 if (rdch != NULL) {
1321 CHN_LOCK(rdch);
1322 ei->rec_overruns = rdch->xruns;
1323 rdch->xruns = 0;
1324 CHN_UNLOCK(rdch);
1325 }
1326 }
1327 break;
1328
1329 case SNDCTL_DSP_SYNCGROUP:
1330 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
1331 break;
1332
1333 case SNDCTL_DSP_SYNCSTART:
1334 ret = dsp_oss_syncstart(*arg_i);
1335 break;
1336
1337 case SNDCTL_DSP_POLICY:
1338 ret = dsp_oss_policy(wrch, rdch, *arg_i);
1339 break;
1340
1341#ifdef OSSV4_EXPERIMENT
1342 /*
1343 * XXX The following ioctls are not yet supported and just return
1344 * EINVAL.
1345 */
1346 case SNDCTL_DSP_GETOPEAKS:
1347 case SNDCTL_DSP_GETIPEAKS:
1348 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
1349 if (chn == NULL)
1350 ret = EINVAL;
1351 else {
1352 oss_peaks_t *op = (oss_peaks_t *)arg;
1353 int lpeak, rpeak;
1354
1355 CHN_LOCK(chn);
1356 ret = chn_getpeaks(chn, &lpeak, &rpeak);
1357 if (ret == -1)
1358 ret = EINVAL;
1359 else {
1360 (*op)[0] = lpeak;
1361 (*op)[1] = rpeak;
1362 }
1363 CHN_UNLOCK(chn);
1364 }
1365 break;
1366
1367 case SNDCTL_DSP_COOKEDMODE:
1368 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
1369 break;
1370 case SNDCTL_DSP_GET_CHNORDER:
1371 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
1372 break;
1373 case SNDCTL_DSP_SET_CHNORDER:
1374 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
1375 break;
1376 case SNDCTL_GETLABEL:
1377 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
1378 break;
1379 case SNDCTL_SETLABEL:
1380 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
1381 break;
1382 case SNDCTL_GETSONG:
1383 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
1384 break;
1385 case SNDCTL_SETSONG:
1386 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
1387 break;
1388 case SNDCTL_SETNAME:
1389 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
1390 break;
1391#if 0
1392 /**
1393 * @note The SNDCTL_CARDINFO ioctl was omitted per 4Front developer
1394 * documentation. "The usability of this call is very limited. It's
1395 * provided only for completeness of the API. OSS API doesn't have
1396 * any concept of card. Any information returned by this ioctl calld
1397 * is reserved exclusively for the utility programs included in the
1398 * OSS package. Applications should not try to use for this
1399 * information in any ways."
1400 */
1401 case SNDCTL_CARDINFO:
1402 ret = EINVAL;
1403 break;
1404 /**
1405 * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and
1406 * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of
1407 * 4Front Technologies.
1408 */
1409 case SNDCTL_DSP_READCTL:
1410 case SNDCTL_DSP_WRITECTL:
1411 ret = EINVAL;
1412 break;
1413#endif /* !0 (explicitly omitted ioctls) */
1414
1415#endif /* !OSSV4_EXPERIMENT */
1416 case SNDCTL_DSP_MAPINBUF:
1417 case SNDCTL_DSP_MAPOUTBUF:
1418 case SNDCTL_DSP_SETSYNCRO:
1419 /* undocumented */
1420
1421 case SNDCTL_DSP_SUBDIVIDE:
1422 case SOUND_PCM_WRITE_FILTER:
1423 case SOUND_PCM_READ_FILTER:
1424 /* dunno what these do, don't sound important */
1425
1426 default:
1427 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
1428 ret = EINVAL;
1429 break;
1430 }
1431 relchns(i_dev, rdch, wrch, 0);
1432 return ret;
1433}
1434
1435static int
1436dsp_poll(struct cdev *i_dev, int events, struct thread *td)
1437{
1438 struct pcm_channel *wrch = NULL, *rdch = NULL;
1439 int ret, e;
1440
1441 ret = 0;
1442 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1443
1444 if (wrch) {
1445 e = (events & (POLLOUT | POLLWRNORM));
1446 if (e)
1447 ret |= chn_poll(wrch, e, td);
1448 }
1449 if (rdch) {
1450 e = (events & (POLLIN | POLLRDNORM));
1451 if (e)
1452 ret |= chn_poll(rdch, e, td);
1453 }
1454 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1455
1456 return ret;
1457}
1458
1459static int
1460dsp_mmap(struct cdev *i_dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
1461{
1462 struct pcm_channel *wrch = NULL, *rdch = NULL, *c;
1463
1464 if (nprot & PROT_EXEC)
1465 return -1;
1466
1467 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1468#if 0
1469 /*
1470 * XXX the linux api uses the nprot to select read/write buffer
1471 * our vm system doesn't allow this, so force write buffer
1472 */
1473
1474 if (wrch && (nprot & PROT_WRITE)) {
1475 c = wrch;
1476 } else if (rdch && (nprot & PROT_READ)) {
1477 c = rdch;
1478 } else {
1479 return -1;
1480 }
1481#else
1482 c = wrch;
1483#endif
1484
1485 if (c == NULL) {
1486 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1487 return -1;
1488 }
1489
1490 if (offset >= sndbuf_getsize(c->bufsoft)) {
1491 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1492 return -1;
1493 }
1494
1495 if (!(c->flags & CHN_F_MAPPED))
1496 c->flags |= CHN_F_MAPPED;
1497
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
644 switch(cmd) {
645#ifdef OLDPCM_IOCTL
646 /*
647 * we start with the new ioctl interface.
648 */
649 case AIONWRITE: /* how many bytes can write ? */
650 if (wrch) {
651 CHN_LOCK(wrch);
652/*
653 if (wrch && wrch->bufhard.dl)
654 while (chn_wrfeed(wrch) == 0);
655*/
656 *arg_i = sndbuf_getfree(wrch->bufsoft);
657 CHN_UNLOCK(wrch);
658 } else {
659 *arg_i = 0;
660 ret = EINVAL;
661 }
662 break;
663
664 case AIOSSIZE: /* set the current blocksize */
665 {
666 struct snd_size *p = (struct snd_size *)arg;
667
668 p->play_size = 0;
669 p->rec_size = 0;
670 if (wrch) {
671 CHN_LOCK(wrch);
672 chn_setblocksize(wrch, 2, p->play_size);
673 p->play_size = sndbuf_getblksz(wrch->bufsoft);
674 CHN_UNLOCK(wrch);
675 }
676 if (rdch) {
677 CHN_LOCK(rdch);
678 chn_setblocksize(rdch, 2, p->rec_size);
679 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
680 CHN_UNLOCK(rdch);
681 }
682 }
683 break;
684 case AIOGSIZE: /* get the current blocksize */
685 {
686 struct snd_size *p = (struct snd_size *)arg;
687
688 if (wrch) {
689 CHN_LOCK(wrch);
690 p->play_size = sndbuf_getblksz(wrch->bufsoft);
691 CHN_UNLOCK(wrch);
692 }
693 if (rdch) {
694 CHN_LOCK(rdch);
695 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
696 CHN_UNLOCK(rdch);
697 }
698 }
699 break;
700
701 case AIOSFMT:
702 case AIOGFMT:
703 {
704 snd_chan_param *p = (snd_chan_param *)arg;
705
706 if (cmd == AIOSFMT &&
707 ((p->play_format != 0 && p->play_rate == 0) ||
708 (p->rec_format != 0 && p->rec_rate == 0))) {
709 ret = EINVAL;
710 break;
711 }
712 if (wrch) {
713 CHN_LOCK(wrch);
714 if (cmd == AIOSFMT && p->play_format != 0) {
715 chn_setformat(wrch, p->play_format);
716 chn_setspeed(wrch, p->play_rate);
717 }
718 p->play_rate = wrch->speed;
719 p->play_format = wrch->format;
720 CHN_UNLOCK(wrch);
721 } else {
722 p->play_rate = 0;
723 p->play_format = 0;
724 }
725 if (rdch) {
726 CHN_LOCK(rdch);
727 if (cmd == AIOSFMT && p->rec_format != 0) {
728 chn_setformat(rdch, p->rec_format);
729 chn_setspeed(rdch, p->rec_rate);
730 }
731 p->rec_rate = rdch->speed;
732 p->rec_format = rdch->format;
733 CHN_UNLOCK(rdch);
734 } else {
735 p->rec_rate = 0;
736 p->rec_format = 0;
737 }
738 }
739 break;
740
741 case AIOGCAP: /* get capabilities */
742 {
743 snd_capabilities *p = (snd_capabilities *)arg;
744 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
745 struct cdev *pdev;
746
747 if (rdch) {
748 CHN_LOCK(rdch);
749 rcaps = chn_getcaps(rdch);
750 }
751 if (wrch) {
752 CHN_LOCK(wrch);
753 pcaps = chn_getcaps(wrch);
754 }
755 p->rate_min = max(rcaps? rcaps->minspeed : 0,
756 pcaps? pcaps->minspeed : 0);
757 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
758 pcaps? pcaps->maxspeed : 1000000);
759 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
760 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
761 /* XXX bad on sb16 */
762 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
763 (wrch? chn_getformats(wrch) : 0xffffffff);
764 if (rdch && wrch)
765 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
766 pdev = d->mixer_dev;
767 p->mixers = 1; /* default: one mixer */
768 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
769 p->left = p->right = 100;
770 if (rdch)
771 CHN_UNLOCK(rdch);
772 if (wrch)
773 CHN_UNLOCK(wrch);
774 }
775 break;
776
777 case AIOSTOP:
778 if (*arg_i == AIOSYNC_PLAY && wrch) {
779 CHN_LOCK(wrch);
780 *arg_i = chn_abort(wrch);
781 CHN_UNLOCK(wrch);
782 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
783 CHN_LOCK(rdch);
784 *arg_i = chn_abort(rdch);
785 CHN_UNLOCK(rdch);
786 } else {
787 printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
788 *arg_i = 0;
789 }
790 break;
791
792 case AIOSYNC:
793 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
794 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
795 break;
796#endif
797 /*
798 * here follow the standard ioctls (filio.h etc.)
799 */
800 case FIONREAD: /* get # bytes to read */
801 if (rdch) {
802 CHN_LOCK(rdch);
803/* if (rdch && rdch->bufhard.dl)
804 while (chn_rdfeed(rdch) == 0);
805*/
806 *arg_i = sndbuf_getready(rdch->bufsoft);
807 CHN_UNLOCK(rdch);
808 } else {
809 *arg_i = 0;
810 ret = EINVAL;
811 }
812 break;
813
814 case FIOASYNC: /*set/clear async i/o */
815 DEB( printf("FIOASYNC\n") ; )
816 break;
817
818 case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */
819 case FIONBIO: /* set/clear non-blocking i/o */
820 if (rdch) {
821 CHN_LOCK(rdch);
822 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
823 rdch->flags |= CHN_F_NBIO;
824 else
825 rdch->flags &= ~CHN_F_NBIO;
826 CHN_UNLOCK(rdch);
827 }
828 if (wrch) {
829 CHN_LOCK(wrch);
830 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
831 wrch->flags |= CHN_F_NBIO;
832 else
833 wrch->flags &= ~CHN_F_NBIO;
834 CHN_UNLOCK(wrch);
835 }
836 break;
837
838 /*
839 * Finally, here is the linux-compatible ioctl interface
840 */
841#define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
842 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
843 case SNDCTL_DSP_GETBLKSIZE:
844 chn = wrch ? wrch : rdch;
845 if (chn) {
846 CHN_LOCK(chn);
847 *arg_i = sndbuf_getblksz(chn->bufsoft);
848 CHN_UNLOCK(chn);
849 } else {
850 *arg_i = 0;
851 ret = EINVAL;
852 }
853 break ;
854
855 case SNDCTL_DSP_SETBLKSIZE:
856 RANGE(*arg_i, 16, 65536);
857 if (wrch) {
858 CHN_LOCK(wrch);
859 chn_setblocksize(wrch, 2, *arg_i);
860 CHN_UNLOCK(wrch);
861 }
862 if (rdch) {
863 CHN_LOCK(rdch);
864 chn_setblocksize(rdch, 2, *arg_i);
865 CHN_UNLOCK(rdch);
866 }
867 break;
868
869 case SNDCTL_DSP_RESET:
870 DEB(printf("dsp reset\n"));
871 if (wrch) {
872 CHN_LOCK(wrch);
873 chn_abort(wrch);
874 chn_resetbuf(wrch);
875 CHN_UNLOCK(wrch);
876 }
877 if (rdch) {
878 CHN_LOCK(rdch);
879 chn_abort(rdch);
880 chn_resetbuf(rdch);
881 CHN_UNLOCK(rdch);
882 }
883 break;
884
885 case SNDCTL_DSP_SYNC:
886 DEB(printf("dsp sync\n"));
887 /* chn_sync may sleep */
888 if (wrch) {
889 CHN_LOCK(wrch);
890 chn_sync(wrch, 0);
891 CHN_UNLOCK(wrch);
892 }
893 break;
894
895 case SNDCTL_DSP_SPEED:
896 /* chn_setspeed may sleep */
897 tmp = 0;
898 if (wrch) {
899 CHN_LOCK(wrch);
900 ret = chn_setspeed(wrch, *arg_i);
901 tmp = wrch->speed;
902 CHN_UNLOCK(wrch);
903 }
904 if (rdch && ret == 0) {
905 CHN_LOCK(rdch);
906 ret = chn_setspeed(rdch, *arg_i);
907 if (tmp == 0)
908 tmp = rdch->speed;
909 CHN_UNLOCK(rdch);
910 }
911 *arg_i = tmp;
912 break;
913
914 case SOUND_PCM_READ_RATE:
915 chn = wrch ? wrch : rdch;
916 if (chn) {
917 CHN_LOCK(chn);
918 *arg_i = chn->speed;
919 CHN_UNLOCK(chn);
920 } else {
921 *arg_i = 0;
922 ret = EINVAL;
923 }
924 break;
925
926 case SNDCTL_DSP_STEREO:
927 tmp = -1;
928 *arg_i = (*arg_i)? AFMT_STEREO : 0;
929 if (wrch) {
930 CHN_LOCK(wrch);
931 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
932 tmp = (wrch->format & AFMT_STEREO)? 1 : 0;
933 CHN_UNLOCK(wrch);
934 }
935 if (rdch && ret == 0) {
936 CHN_LOCK(rdch);
937 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
938 if (tmp == -1)
939 tmp = (rdch->format & AFMT_STEREO)? 1 : 0;
940 CHN_UNLOCK(rdch);
941 }
942 *arg_i = tmp;
943 break;
944
945 case SOUND_PCM_WRITE_CHANNELS:
946/* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
947 if (*arg_i != 0) {
948 tmp = 0;
949 *arg_i = (*arg_i != 1)? AFMT_STEREO : 0;
950 if (wrch) {
951 CHN_LOCK(wrch);
952 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
953 tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
954 CHN_UNLOCK(wrch);
955 }
956 if (rdch && ret == 0) {
957 CHN_LOCK(rdch);
958 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
959 if (tmp == 0)
960 tmp = (rdch->format & AFMT_STEREO)? 2 : 1;
961 CHN_UNLOCK(rdch);
962 }
963 *arg_i = tmp;
964 } else {
965 chn = wrch ? wrch : rdch;
966 CHN_LOCK(chn);
967 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
968 CHN_UNLOCK(chn);
969 }
970 break;
971
972 case SOUND_PCM_READ_CHANNELS:
973 chn = wrch ? wrch : rdch;
974 if (chn) {
975 CHN_LOCK(chn);
976 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
977 CHN_UNLOCK(chn);
978 } else {
979 *arg_i = 0;
980 ret = EINVAL;
981 }
982 break;
983
984 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
985 chn = wrch ? wrch : rdch;
986 if (chn) {
987 CHN_LOCK(chn);
988 *arg_i = chn_getformats(chn);
989 CHN_UNLOCK(chn);
990 } else {
991 *arg_i = 0;
992 ret = EINVAL;
993 }
994 break ;
995
996 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
997 if ((*arg_i != AFMT_QUERY)) {
998 tmp = 0;
999 if (wrch) {
1000 CHN_LOCK(wrch);
1001 ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
1002 tmp = wrch->format & ~AFMT_STEREO;
1003 CHN_UNLOCK(wrch);
1004 }
1005 if (rdch && ret == 0) {
1006 CHN_LOCK(rdch);
1007 ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
1008 if (tmp == 0)
1009 tmp = rdch->format & ~AFMT_STEREO;
1010 CHN_UNLOCK(rdch);
1011 }
1012 *arg_i = tmp;
1013 } else {
1014 chn = wrch ? wrch : rdch;
1015 CHN_LOCK(chn);
1016 *arg_i = chn->format & ~AFMT_STEREO;
1017 CHN_UNLOCK(chn);
1018 }
1019 break;
1020
1021 case SNDCTL_DSP_SETFRAGMENT:
1022 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
1023 {
1024 u_int32_t fragln = (*arg_i) & 0x0000ffff;
1025 u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
1026 u_int32_t fragsz;
1027 u_int32_t r_maxfrags, r_fragsz;
1028
1029 RANGE(fragln, 4, 16);
1030 fragsz = 1 << fragln;
1031
1032 if (maxfrags == 0)
1033 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1034 if (maxfrags < 2)
1035 maxfrags = 2;
1036 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
1037 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1038
1039 DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
1040 if (rdch) {
1041 CHN_LOCK(rdch);
1042 ret = chn_setblocksize(rdch, maxfrags, fragsz);
1043 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
1044 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
1045 CHN_UNLOCK(rdch);
1046 } else {
1047 r_maxfrags = maxfrags;
1048 r_fragsz = fragsz;
1049 }
1050 if (wrch && ret == 0) {
1051 CHN_LOCK(wrch);
1052 ret = chn_setblocksize(wrch, maxfrags, fragsz);
1053 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
1054 fragsz = sndbuf_getblksz(wrch->bufsoft);
1055 CHN_UNLOCK(wrch);
1056 } else { /* use whatever came from the read channel */
1057 maxfrags = r_maxfrags;
1058 fragsz = r_fragsz;
1059 }
1060
1061 fragln = 0;
1062 while (fragsz > 1) {
1063 fragln++;
1064 fragsz >>= 1;
1065 }
1066 *arg_i = (maxfrags << 16) | fragln;
1067 }
1068 break;
1069
1070 case SNDCTL_DSP_GETISPACE:
1071 /* return the size of data available in the input queue */
1072 {
1073 audio_buf_info *a = (audio_buf_info *)arg;
1074 if (rdch) {
1075 struct snd_dbuf *bs = rdch->bufsoft;
1076
1077 CHN_LOCK(rdch);
1078 a->bytes = sndbuf_getready(bs);
1079 a->fragments = a->bytes / sndbuf_getblksz(bs);
1080 a->fragstotal = sndbuf_getblkcnt(bs);
1081 a->fragsize = sndbuf_getblksz(bs);
1082 CHN_UNLOCK(rdch);
1083 }
1084 }
1085 break;
1086
1087 case SNDCTL_DSP_GETOSPACE:
1088 /* return space available in the output queue */
1089 {
1090 audio_buf_info *a = (audio_buf_info *)arg;
1091 if (wrch) {
1092 struct snd_dbuf *bs = wrch->bufsoft;
1093
1094 CHN_LOCK(wrch);
1095 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1096 a->bytes = sndbuf_getfree(bs);
1097 a->fragments = a->bytes / sndbuf_getblksz(bs);
1098 a->fragstotal = sndbuf_getblkcnt(bs);
1099 a->fragsize = sndbuf_getblksz(bs);
1100 CHN_UNLOCK(wrch);
1101 }
1102 }
1103 break;
1104
1105 case SNDCTL_DSP_GETIPTR:
1106 {
1107 count_info *a = (count_info *)arg;
1108 if (rdch) {
1109 struct snd_dbuf *bs = rdch->bufsoft;
1110
1111 CHN_LOCK(rdch);
1112 /* XXX abusive DMA update: chn_rdupdate(rdch); */
1113 a->bytes = sndbuf_gettotal(bs);
1114 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
1115 a->ptr = sndbuf_getreadyptr(bs);
1116 rdch->blocks = sndbuf_getblocks(bs);
1117 CHN_UNLOCK(rdch);
1118 } else
1119 ret = EINVAL;
1120 }
1121 break;
1122
1123 case SNDCTL_DSP_GETOPTR:
1124 {
1125 count_info *a = (count_info *)arg;
1126 if (wrch) {
1127 struct snd_dbuf *bs = wrch->bufsoft;
1128
1129 CHN_LOCK(wrch);
1130 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1131 a->bytes = sndbuf_gettotal(bs);
1132 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
1133 a->ptr = sndbuf_getreadyptr(bs);
1134 wrch->blocks = sndbuf_getblocks(bs);
1135 CHN_UNLOCK(wrch);
1136 } else
1137 ret = EINVAL;
1138 }
1139 break;
1140
1141 case SNDCTL_DSP_GETCAPS:
1142 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
1143 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1144 *arg_i |= DSP_CAP_DUPLEX;
1145 break;
1146
1147 case SOUND_PCM_READ_BITS:
1148 chn = wrch ? wrch : rdch;
1149 if (chn) {
1150 CHN_LOCK(chn);
1151 if (chn->format & AFMT_8BIT)
1152 *arg_i = 8;
1153 else if (chn->format & AFMT_16BIT)
1154 *arg_i = 16;
1155 else if (chn->format & AFMT_24BIT)
1156 *arg_i = 24;
1157 else if (chn->format & AFMT_32BIT)
1158 *arg_i = 32;
1159 else
1160 ret = EINVAL;
1161 CHN_UNLOCK(chn);
1162 } else {
1163 *arg_i = 0;
1164 ret = EINVAL;
1165 }
1166 break;
1167
1168 case SNDCTL_DSP_SETTRIGGER:
1169 if (rdch) {
1170 CHN_LOCK(rdch);
1171 rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
1172 if (*arg_i & PCM_ENABLE_INPUT)
1173 chn_start(rdch, 1);
1174 else
1175 rdch->flags |= CHN_F_NOTRIGGER;
1176 CHN_UNLOCK(rdch);
1177 }
1178 if (wrch) {
1179 CHN_LOCK(wrch);
1180 wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
1181 if (*arg_i & PCM_ENABLE_OUTPUT)
1182 chn_start(wrch, 1);
1183 else
1184 wrch->flags |= CHN_F_NOTRIGGER;
1185 CHN_UNLOCK(wrch);
1186 }
1187 break;
1188
1189 case SNDCTL_DSP_GETTRIGGER:
1190 *arg_i = 0;
1191 if (wrch) {
1192 CHN_LOCK(wrch);
1193 if (wrch->flags & CHN_F_TRIGGERED)
1194 *arg_i |= PCM_ENABLE_OUTPUT;
1195 CHN_UNLOCK(wrch);
1196 }
1197 if (rdch) {
1198 CHN_LOCK(rdch);
1199 if (rdch->flags & CHN_F_TRIGGERED)
1200 *arg_i |= PCM_ENABLE_INPUT;
1201 CHN_UNLOCK(rdch);
1202 }
1203 break;
1204
1205 case SNDCTL_DSP_GETODELAY:
1206 if (wrch) {
1207 struct snd_dbuf *bs = wrch->bufsoft;
1208
1209 CHN_LOCK(wrch);
1210 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1211 *arg_i = sndbuf_getready(bs);
1212 CHN_UNLOCK(wrch);
1213 } else
1214 ret = EINVAL;
1215 break;
1216
1217 case SNDCTL_DSP_POST:
1218 if (wrch) {
1219 CHN_LOCK(wrch);
1220 wrch->flags &= ~CHN_F_NOTRIGGER;
1221 chn_start(wrch, 1);
1222 CHN_UNLOCK(wrch);
1223 }
1224 break;
1225
1226 case SNDCTL_DSP_SETDUPLEX:
1227 /*
1228 * switch to full-duplex mode if card is in half-duplex
1229 * mode and is able to work in full-duplex mode
1230 */
1231 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1232 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1233 break;
1234
1235 /*
1236 * The following four ioctls are simple wrappers around mixer_ioctl
1237 * with no further processing. xcmd is short for "translated
1238 * command".
1239 */
1240 case SNDCTL_DSP_GETRECVOL:
1241 if (xcmd == 0)
1242 xcmd = SOUND_MIXER_READ_RECLEV;
1243 /* FALLTHROUGH */
1244 case SNDCTL_DSP_SETRECVOL:
1245 if (xcmd == 0)
1246 xcmd = SOUND_MIXER_WRITE_RECLEV;
1247 /* FALLTHROUGH */
1248 case SNDCTL_DSP_GETPLAYVOL:
1249 if (xcmd == 0)
1250 xcmd = SOUND_MIXER_READ_PCM;
1251 /* FALLTHROUGH */
1252 case SNDCTL_DSP_SETPLAYVOL:
1253 if (xcmd == 0)
1254 xcmd = SOUND_MIXER_WRITE_PCM;
1255
1256 if (d->mixer_dev != NULL)
1257 ret = mixer_ioctl(d->mixer_dev, xcmd, arg, -1, td);
1258 else
1259 ret = ENOTSUP;
1260 break;
1261
1262 case SNDCTL_DSP_GET_RECSRC_NAMES:
1263 case SNDCTL_DSP_GET_RECSRC:
1264 case SNDCTL_DSP_SET_RECSRC:
1265 if (d->mixer_dev != NULL)
1266 ret = mixer_ioctl(d->mixer_dev, cmd, arg, -1, td);
1267 else
1268 ret = ENOTSUP;
1269 break;
1270
1271 /*
1272 * The following 3 ioctls aren't very useful at the moment. For
1273 * now, only a single channel is associated with a cdev (/dev/dspN
1274 * instance), so there's only a single output routing to use (i.e.,
1275 * the wrch bound to this cdev).
1276 */
1277 case SNDCTL_DSP_GET_PLAYTGT_NAMES:
1278 {
1279 oss_mixer_enuminfo *ei;
1280 ei = (oss_mixer_enuminfo *)arg;
1281 ei->dev = 0;
1282 ei->ctrl = 0;
1283 ei->version = 0; /* static for now */
1284 ei->strindex[0] = 0;
1285
1286 if (wrch != NULL) {
1287 ei->nvalues = 1;
1288 strlcpy(ei->strings, wrch->name,
1289 sizeof(ei->strings));
1290 } else {
1291 ei->nvalues = 0;
1292 ei->strings[0] = '\0';
1293 }
1294 }
1295 break;
1296 case SNDCTL_DSP_GET_PLAYTGT:
1297 case SNDCTL_DSP_SET_PLAYTGT: /* yes, they are the same for now */
1298 /*
1299 * Re: SET_PLAYTGT
1300 * OSSv4: "The value that was accepted by the device will
1301 * be returned back in the variable pointed by the
1302 * argument."
1303 */
1304 if (wrch != NULL)
1305 *arg_i = 0;
1306 else
1307 ret = EINVAL;
1308 break;
1309
1310 case SNDCTL_DSP_SILENCE:
1311 /*
1312 * Flush the software (pre-feed) buffer, but try to minimize playback
1313 * interruption. (I.e., record unplayed samples with intent to
1314 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
1315 * functionality.
1316 */
1317 if (wrch == NULL)
1318 ret = EINVAL;
1319 else {
1320 struct snd_dbuf *bs;
1321 CHN_LOCK(wrch);
1322 while (wrch->inprog != 0)
1323 cv_wait(&wrch->cv, wrch->lock);
1324 bs = wrch->bufsoft;
1325 if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
1326 bs->sl = sndbuf_getready(bs);
1327 sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs));
1328 sndbuf_fillsilence(bs);
1329 chn_start(wrch, 0);
1330 }
1331 CHN_UNLOCK(wrch);
1332 }
1333 break;
1334
1335 case SNDCTL_DSP_SKIP:
1336 /*
1337 * OSSv4 docs: "This ioctl call discards all unplayed samples in the
1338 * playback buffer by moving the current write position immediately
1339 * before the point where the device is currently reading the samples."
1340 */
1341 if (wrch == NULL)
1342 ret = EINVAL;
1343 else {
1344 struct snd_dbuf *bs;
1345 CHN_LOCK(wrch);
1346 while (wrch->inprog != 0)
1347 cv_wait(&wrch->cv, wrch->lock);
1348 bs = wrch->bufsoft;
1349 if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
1350 sndbuf_softreset(bs);
1351 sndbuf_acquire(bs, bs->shadbuf, bs->sl);
1352 bs->sl = 0;
1353 chn_start(wrch, 0);
1354 }
1355 CHN_UNLOCK(wrch);
1356 }
1357 break;
1358
1359 case SNDCTL_DSP_CURRENT_OPTR:
1360 case SNDCTL_DSP_CURRENT_IPTR:
1361 /**
1362 * @note Changing formats resets the buffer counters, which differs
1363 * from the 4Front drivers. However, I don't expect this to be
1364 * much of a problem.
1365 *
1366 * @note In a test where @c CURRENT_OPTR is called immediately after write
1367 * returns, this driver is about 32K samples behind whereas
1368 * 4Front's is about 8K samples behind. Should determine source
1369 * of discrepancy, even if only out of curiosity.
1370 *
1371 * @todo Actually test SNDCTL_DSP_CURRENT_IPTR.
1372 */
1373 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
1374 if (chn == NULL)
1375 ret = EINVAL;
1376 else {
1377 struct snd_dbuf *bs;
1378 /* int tmp; */
1379
1380 oss_count_t *oc = (oss_count_t *)arg;
1381
1382 CHN_LOCK(chn);
1383 bs = chn->bufsoft;
1384#if 0
1385 tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b);
1386 oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getbps(b);
1387 oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getbps(b);
1388#else
1389 oc->samples = sndbuf_gettotal(bs) / sndbuf_getbps(bs);
1390 oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getbps(bs);
1391#endif
1392 CHN_UNLOCK(chn);
1393 }
1394 break;
1395
1396 case SNDCTL_DSP_HALT_OUTPUT:
1397 case SNDCTL_DSP_HALT_INPUT:
1398 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
1399 if (chn == NULL)
1400 ret = EINVAL;
1401 else {
1402 CHN_LOCK(chn);
1403 chn_abort(chn);
1404 CHN_UNLOCK(chn);
1405 }
1406 break;
1407
1408 case SNDCTL_DSP_LOW_WATER:
1409 /*
1410 * Set the number of bytes required to attract attention by
1411 * select/poll.
1412 */
1413 if (wrch != NULL) {
1414 CHN_LOCK(wrch);
1415 wrch->lw = (*arg_i > 1) ? *arg_i : 1;
1416 CHN_UNLOCK(wrch);
1417 }
1418 if (rdch != NULL) {
1419 CHN_LOCK(rdch);
1420 rdch->lw = (*arg_i > 1) ? *arg_i : 1;
1421 CHN_UNLOCK(rdch);
1422 }
1423 break;
1424
1425 case SNDCTL_DSP_GETERROR:
1426 /*
1427 * OSSv4 docs: "All errors and counters will automatically be
1428 * cleared to zeroes after the call so each call will return only
1429 * the errors that occurred after the previous invocation. ... The
1430 * play_underruns and rec_overrun fields are the only usefull fields
1431 * returned by OSS 4.0."
1432 */
1433 {
1434 audio_errinfo *ei = (audio_errinfo *)arg;
1435
1436 bzero((void *)ei, sizeof(*ei));
1437
1438 if (wrch != NULL) {
1439 CHN_LOCK(wrch);
1440 ei->play_underruns = wrch->xruns;
1441 wrch->xruns = 0;
1442 CHN_UNLOCK(wrch);
1443 }
1444 if (rdch != NULL) {
1445 CHN_LOCK(rdch);
1446 ei->rec_overruns = rdch->xruns;
1447 rdch->xruns = 0;
1448 CHN_UNLOCK(rdch);
1449 }
1450 }
1451 break;
1452
1453 case SNDCTL_DSP_SYNCGROUP:
1454 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
1455 break;
1456
1457 case SNDCTL_DSP_SYNCSTART:
1458 ret = dsp_oss_syncstart(*arg_i);
1459 break;
1460
1461 case SNDCTL_DSP_POLICY:
1462 ret = dsp_oss_policy(wrch, rdch, *arg_i);
1463 break;
1464
1465#ifdef OSSV4_EXPERIMENT
1466 /*
1467 * XXX The following ioctls are not yet supported and just return
1468 * EINVAL.
1469 */
1470 case SNDCTL_DSP_GETOPEAKS:
1471 case SNDCTL_DSP_GETIPEAKS:
1472 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
1473 if (chn == NULL)
1474 ret = EINVAL;
1475 else {
1476 oss_peaks_t *op = (oss_peaks_t *)arg;
1477 int lpeak, rpeak;
1478
1479 CHN_LOCK(chn);
1480 ret = chn_getpeaks(chn, &lpeak, &rpeak);
1481 if (ret == -1)
1482 ret = EINVAL;
1483 else {
1484 (*op)[0] = lpeak;
1485 (*op)[1] = rpeak;
1486 }
1487 CHN_UNLOCK(chn);
1488 }
1489 break;
1490
1491 case SNDCTL_DSP_COOKEDMODE:
1492 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
1493 break;
1494 case SNDCTL_DSP_GET_CHNORDER:
1495 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
1496 break;
1497 case SNDCTL_DSP_SET_CHNORDER:
1498 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
1499 break;
1500 case SNDCTL_GETLABEL:
1501 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
1502 break;
1503 case SNDCTL_SETLABEL:
1504 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
1505 break;
1506 case SNDCTL_GETSONG:
1507 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
1508 break;
1509 case SNDCTL_SETSONG:
1510 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
1511 break;
1512 case SNDCTL_SETNAME:
1513 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
1514 break;
1515#if 0
1516 /**
1517 * @note The SNDCTL_CARDINFO ioctl was omitted per 4Front developer
1518 * documentation. "The usability of this call is very limited. It's
1519 * provided only for completeness of the API. OSS API doesn't have
1520 * any concept of card. Any information returned by this ioctl calld
1521 * is reserved exclusively for the utility programs included in the
1522 * OSS package. Applications should not try to use for this
1523 * information in any ways."
1524 */
1525 case SNDCTL_CARDINFO:
1526 ret = EINVAL;
1527 break;
1528 /**
1529 * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and
1530 * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of
1531 * 4Front Technologies.
1532 */
1533 case SNDCTL_DSP_READCTL:
1534 case SNDCTL_DSP_WRITECTL:
1535 ret = EINVAL;
1536 break;
1537#endif /* !0 (explicitly omitted ioctls) */
1538
1539#endif /* !OSSV4_EXPERIMENT */
1540 case SNDCTL_DSP_MAPINBUF:
1541 case SNDCTL_DSP_MAPOUTBUF:
1542 case SNDCTL_DSP_SETSYNCRO:
1543 /* undocumented */
1544
1545 case SNDCTL_DSP_SUBDIVIDE:
1546 case SOUND_PCM_WRITE_FILTER:
1547 case SOUND_PCM_READ_FILTER:
1548 /* dunno what these do, don't sound important */
1549
1550 default:
1551 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
1552 ret = EINVAL;
1553 break;
1554 }
1555 relchns(i_dev, rdch, wrch, 0);
1556 return ret;
1557}
1558
1559static int
1560dsp_poll(struct cdev *i_dev, int events, struct thread *td)
1561{
1562 struct pcm_channel *wrch = NULL, *rdch = NULL;
1563 int ret, e;
1564
1565 ret = 0;
1566 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1567
1568 if (wrch) {
1569 e = (events & (POLLOUT | POLLWRNORM));
1570 if (e)
1571 ret |= chn_poll(wrch, e, td);
1572 }
1573 if (rdch) {
1574 e = (events & (POLLIN | POLLRDNORM));
1575 if (e)
1576 ret |= chn_poll(rdch, e, td);
1577 }
1578 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1579
1580 return ret;
1581}
1582
1583static int
1584dsp_mmap(struct cdev *i_dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
1585{
1586 struct pcm_channel *wrch = NULL, *rdch = NULL, *c;
1587
1588 if (nprot & PROT_EXEC)
1589 return -1;
1590
1591 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1592#if 0
1593 /*
1594 * XXX the linux api uses the nprot to select read/write buffer
1595 * our vm system doesn't allow this, so force write buffer
1596 */
1597
1598 if (wrch && (nprot & PROT_WRITE)) {
1599 c = wrch;
1600 } else if (rdch && (nprot & PROT_READ)) {
1601 c = rdch;
1602 } else {
1603 return -1;
1604 }
1605#else
1606 c = wrch;
1607#endif
1608
1609 if (c == NULL) {
1610 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1611 return -1;
1612 }
1613
1614 if (offset >= sndbuf_getsize(c->bufsoft)) {
1615 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1616 return -1;
1617 }
1618
1619 if (!(c->flags & CHN_F_MAPPED))
1620 c->flags |= CHN_F_MAPPED;
1621
1622 *paddr = vtophys(sndbuf_getbufofs(c->bufsoft, offset));
1623 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1624
1625 return 0;
1626}
1627
1628#ifdef USING_DEVFS
1629
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 */
1630/* So much for dev_stdclone() */
1631static int
1632dsp_stdclone(char *name, char *namep, char *sep, int use_sep, int *u, int *c)
1633{
1634 size_t len;
1635
1636 len = strlen(namep);
1637
1638 if (bcmp(name, namep, len) != 0)
1639 return (ENODEV);
1640
1641 name += len;
1642
1643 if (isdigit(*name) == 0)
1644 return (ENODEV);
1645
1646 len = strlen(sep);
1647
1648 if (*name == '0' && !(name[1] == '\0' || bcmp(name + 1, sep, len) == 0))
1649 return (ENODEV);
1650
1651 for (*u = 0; isdigit(*name) != 0; name++) {
1652 *u *= 10;
1653 *u += *name - '0';
1654 if (*u > dsp_umax)
1655 return (ENODEV);
1656 }
1657
1658 if (*name == '\0')
1659 return ((use_sep == 0) ? 0 : ENODEV);
1660
1661 if (bcmp(name, sep, len) != 0 || isdigit(name[len]) == 0)
1662 return (ENODEV);
1663
1664 name += len;
1665
1666 if (*name == '0' && name[1] != '\0')
1667 return (ENODEV);
1668
1669 for (*c = 0; isdigit(*name) != 0; name++) {
1670 *c *= 10;
1671 *c += *name - '0';
1672 if (*c > dsp_cmax)
1673 return (ENODEV);
1674 }
1675
1676 if (*name != '\0')
1677 return (ENODEV);
1678
1679 return (0);
1680}
1681
1514static void
1682static void
1515dsp_clone(void *arg, struct ucred *cred, char *name, int namelen,
1516 struct cdev **dev)
1683dsp_clone(void *arg,
1684#if __FreeBSD_version >= 600034
1685 struct ucred *cred,
1686#endif
1687 char *name, int namelen, struct cdev **dev)
1517{
1688{
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"};
1689 struct snddev_info *d;
1690 struct snd_clone_entry *ce;
1691 struct pcm_channel *c;
1692 int i, unit, udcmask, cunit, devtype, devhw, devcmax, tumax;
1693 char *devname, *devsep;
1524
1694
1695 KASSERT(dsp_umax >= 0 && dsp_cmax >= 0, ("Uninitialized unit!"));
1696
1525 if (*dev != NULL)
1526 return;
1697 if (*dev != NULL)
1698 return;
1527 if (pcm_devclass == NULL)
1528 return;
1529
1699
1530 devtype = 0;
1531 unit = -1;
1700 unit = -1;
1532 for (i = 0; (i < 3) && (unit == -1); i++) {
1533 devtype = devtypes[i];
1534 if (strcmp(name, devnames[i]) == 0) {
1701 cunit = -1;
1702 devtype = -1;
1703 devhw = 0;
1704 devcmax = -1;
1705 tumax = -1;
1706 devname = NULL;
1707 devsep = NULL;
1708
1709 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])) &&
1710 unit == -1; i++) {
1711 devtype = dsp_cdevs[i].type;
1712 devname = dsp_cdevs[i].name;
1713 devsep = dsp_cdevs[i].sep;
1714 devhw = dsp_cdevs[i].hw;
1715 devcmax = dsp_cdevs[i].max - 1;
1716 if (strcmp(name, devname) == 0)
1535 unit = snd_unit;
1717 unit = snd_unit;
1536 } else {
1537 if (dev_stdclone(name, NULL, devnames[i], &unit) != 1)
1538 unit = -1;
1718 else if (dsp_stdclone(name, devname, devsep,
1719 dsp_cdevs[i].use_sep, &unit, &cunit) != 0) {
1720 unit = -1;
1721 cunit = -1;
1539 }
1540 }
1722 }
1723 }
1541 if (unit == -1 || unit >= devclass_get_maxunit(pcm_devclass))
1724
1725 d = devclass_get_softc(pcm_devclass, unit);
1726 if (d == NULL || d->clones == NULL)
1542 return;
1543
1727 return;
1728
1544 pcm_dev = devclass_get_softc(pcm_devclass, unit);
1545
1546 if (pcm_dev == NULL)
1729 pcm_lock(d);
1730 if (snd_clone_disabled(d->clones)) {
1731 pcm_unlock(d);
1547 return;
1732 return;
1733 }
1548
1734
1549 SLIST_FOREACH(pcm_chan, &pcm_dev->channels, link) {
1735 udcmask = snd_u2unit(unit) | snd_d2unit(devtype);
1550
1736
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);
1737 if (devhw != 0) {
1738 KASSERT(devcmax <= dsp_cmax,
1739 ("overflow: devcmax=%d, dsp_cmax=%d", devcmax, dsp_cmax));
1740 if (cunit > devcmax) {
1741 pcm_unlock(d);
1568 return;
1569 }
1742 return;
1743 }
1744 udcmask |= snd_c2unit(cunit);
1745 CHN_FOREACH(c, d, channels.pcm) {
1746 CHN_LOCK(c);
1747 if (c->unit != udcmask) {
1748 CHN_UNLOCK(c);
1749 continue;
1750 }
1751 CHN_UNLOCK(c);
1752 udcmask &= ~snd_c2unit(cunit);
1753 /*
1754 * Temporarily increase clone maxunit to overcome
1755 * vchan flexibility.
1756 *
1757 * # sysctl dev.pcm.0.play.vchans=256
1758 * dev.pcm.0.play.vchans: 1 -> 256
1759 * # cat /dev/zero > /dev/dsp0.vp255 &
1760 * [1] 17296
1761 * # sysctl dev.pcm.0.play.vchans=0
1762 * dev.pcm.0.play.vchans: 256 -> 1
1763 * # fg
1764 * [1] + running cat /dev/zero > /dev/dsp0.vp255
1765 * ^C
1766 * # cat /dev/zero > /dev/dsp0.vp255
1767 * zsh: operation not supported: /dev/dsp0.vp255
1768 */
1769 tumax = snd_clone_getmaxunit(d->clones);
1770 if (cunit > tumax)
1771 snd_clone_setmaxunit(d->clones, cunit);
1772 else
1773 tumax = -1;
1774 goto dsp_clone_alloc;
1775 }
1776 /*
1777 * Ok, so we're requesting unallocated vchan, but still
1778 * within maximum vchan limit.
1779 */
1780 if (((devtype == SND_DEV_DSPHW_VPLAY && d->pvchancount > 0) ||
1781 (devtype == SND_DEV_DSPHW_VREC && d->rvchancount > 0)) &&
1782 cunit < snd_maxautovchans) {
1783 udcmask &= ~snd_c2unit(cunit);
1784 tumax = snd_clone_getmaxunit(d->clones);
1785 if (cunit > tumax)
1786 snd_clone_setmaxunit(d->clones, cunit);
1787 else
1788 tumax = -1;
1789 goto dsp_clone_alloc;
1790 }
1791 pcm_unlock(d);
1792 return;
1570 }
1793 }
1794
1795dsp_clone_alloc:
1796 ce = snd_clone_alloc(d->clones, dev, &cunit, udcmask);
1797 if (tumax != -1)
1798 snd_clone_setmaxunit(d->clones, tumax);
1799 if (ce != NULL) {
1800 udcmask |= snd_c2unit(cunit);
1801 pcm_unlock(d);
1802 *dev = make_dev(&dsp_cdevsw, unit2minor(udcmask),
1803 UID_ROOT, GID_WHEEL, 0666, "%s%d%s%d",
1804 devname, unit, devsep, cunit);
1805 pcm_lock(d);
1806 snd_clone_register(ce, *dev);
1807 }
1808 pcm_unlock(d);
1809
1810 if (*dev != NULL)
1811 dev_ref(*dev);
1571}
1572
1573static void
1574dsp_sysinit(void *p)
1575{
1812}
1813
1814static void
1815dsp_sysinit(void *p)
1816{
1817 if (dsp_ehtag != NULL)
1818 return;
1819 /* initialize unit numbering */
1820 snd_unit_init();
1821 dsp_umax = PCMMAXUNIT;
1822 dsp_cmax = PCMMAXCHAN;
1576 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
1577}
1578
1579static void
1580dsp_sysuninit(void *p)
1581{
1823 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
1824}
1825
1826static void
1827dsp_sysuninit(void *p)
1828{
1582 if (dsp_ehtag != NULL)
1583 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
1829 if (dsp_ehtag == NULL)
1830 return;
1831 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
1832 dsp_ehtag = NULL;
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
1833}
1834
1835SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
1836SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
1837#endif
1838
1839char *
1840dsp_unit2name(char *buf, size_t len, int unit)
1841{
1842 int i, dtype;
1843
1844 KASSERT(buf != NULL && len != 0, ("bogus buf=%p len=%u", buf, len));
1845
1846 dtype = snd_unit2d(unit);
1847
1848 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
1849 if (dtype != dsp_cdevs[i].type)
1850 continue;
1851 snprintf(buf, len, "%s%d%s%d", dsp_cdevs[i].name,
1852 snd_unit2u(unit), dsp_cdevs[i].sep, snd_unit2c(unit));
1853 return (buf);
1854 }
1855
1856 return (NULL);
1857}
1858
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 *
1598 * This routine is supposed to go practically straight to the hardware,
1599 * getting capabilities directly from the sound card driver, side-stepping
1600 * the intermediate channel interface.
1601 *
1602 * Note, however, that the usefulness of this command is significantly
1603 * decreased when requesting info about any device other than the one serving
1604 * the request. While each snddev_channel refers to a specific device node,
1605 * the converse is *not* true. Currently, when a sound device node is opened,
1606 * the sound subsystem scans for an available audio channel (or channels, if
1607 * opened in read+write) and then assigns them to the si_drv[12] private
1608 * data fields. As a result, any information returned linking a channel to
1609 * a specific character device isn't necessarily accurate.
1610 *
1611 * @note
1612 * Calling threads must not hold any snddev_info or pcm_channel locks.
1613 *
1614 * @param dev device on which the ioctl was issued
1615 * @param ai ioctl request data container
1616 *
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{
1859/**
1860 * @brief Handler for SNDCTL_AUDIOINFO.
1861 *
1862 * Gathers information about the audio device specified in ai->dev. If
1863 * ai->dev == -1, then this function gathers information about the current
1864 * device. If the call comes in on a non-audio device and ai->dev == -1,
1865 * return EINVAL.
1866 *
1867 * This routine is supposed to go practically straight to the hardware,
1868 * getting capabilities directly from the sound card driver, side-stepping
1869 * the intermediate channel interface.
1870 *
1871 * Note, however, that the usefulness of this command is significantly
1872 * decreased when requesting info about any device other than the one serving
1873 * the request. While each snddev_channel refers to a specific device node,
1874 * the converse is *not* true. Currently, when a sound device node is opened,
1875 * the sound subsystem scans for an available audio channel (or channels, if
1876 * opened in read+write) and then assigns them to the si_drv[12] private
1877 * data fields. As a result, any information returned linking a channel to
1878 * a specific character device isn't necessarily accurate.
1879 *
1880 * @note
1881 * Calling threads must not hold any snddev_info or pcm_channel locks.
1882 *
1883 * @param dev device on which the ioctl was issued
1884 * @param ai ioctl request data container
1885 *
1886 * @retval 0 success
1887 * @retval EINVAL ai->dev specifies an invalid device
1888 *
1889 * @todo Verify correctness of Doxygen tags. ;)
1890 */
1891int
1892dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
1893{
1625 struct snddev_channel *sce;
1626 struct pcmchan_caps *caps;
1627 struct pcm_channel *ch;
1628 struct snddev_info *d;
1894 struct pcmchan_caps *caps;
1895 struct pcm_channel *ch;
1896 struct snddev_info *d;
1629 struct cdev *t_cdev;
1630 uint32_t fmts;
1631 int i, nchan, ret, *rates, minch, maxch;
1897 uint32_t fmts;
1898 int i, nchan, ret, *rates, minch, maxch;
1899 char *devname, buf[CHN_NAMELEN];
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;
1900
1901 /*
1902 * If probing the device that received the ioctl, make sure it's a
1903 * DSP device. (Users may use this ioctl with /dev/mixer and
1904 * /dev/midi.)
1905 */
1906 if ((ai->dev == -1) && (i_dev->si_devsw != &dsp_cdevsw))
1907 return EINVAL;
1908
1909 ch = NULL;
1642 t_cdev = NULL;
1910 devname = NULL;
1643 nchan = 0;
1644 ret = 0;
1911 nchan = 0;
1912 ret = 0;
1645
1913 bzero(buf, sizeof(buf));
1914
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
1915 /*
1916 * Search for the requested audio device (channel). Start by
1917 * iterating over pcm devices.
1918 */
1919 for (i = 0; i < devclass_get_maxunit(pcm_devclass); i++) {
1920 d = devclass_get_softc(pcm_devclass, i);
1921 if (d == NULL)
1922 continue;
1923
1924 /* See the note in function docblock */
1925 mtx_assert(d->lock, MA_NOTOWNED);
1926 pcm_inprog(d, 1);
1927 pcm_lock(d);
1928
1660 SLIST_FOREACH(sce, &d->channels, link) {
1661 ch = sce->channel;
1929 CHN_FOREACH(ch, d, channels.pcm) {
1662 mtx_assert(ch->lock, MA_NOTOWNED);
1663 CHN_LOCK(ch);
1664 if (ai->dev == -1) {
1930 mtx_assert(ch->lock, MA_NOTOWNED);
1931 CHN_LOCK(ch);
1932 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;
1933 if ((ch == PCM_RDCH(i_dev)) || /* record ch */
1934 (ch == PCM_WRCH(i_dev))) { /* playback ch */
1935 devname = i_dev->si_name;
1668 goto dspfound;
1669 }
1670 } else if (ai->dev == nchan) {
1936 goto dspfound;
1937 }
1938 } else if (ai->dev == nchan) {
1671 t_cdev = sce->dsp_devt;
1939 devname = dsp_unit2name(buf, sizeof(buf),
1940 ch->unit);
1672 goto dspfound;
1673 }
1674 CHN_UNLOCK(ch);
1941 goto dspfound;
1942 }
1943 CHN_UNLOCK(ch);
1944 /*
1945 * XXX I really doubt if this is correct.
1946 */
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 */
1947 ++nchan;
1948 }
1949
1950 pcm_unlock(d);
1951 pcm_inprog(d, -1);
1952 }
1953
1954 /* Exhausted the search -- nothing is locked, so return. */
1955 return EINVAL;
1956
1957dspfound:
1958 /* Should've found the device, but something isn't right */
1687 if (t_cdev == NULL) {
1959 if (devname == 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
1696 * and decrement when exiting;
1697 * - a specific audio channel has been locked, so be sure to unlock
1698 * when exiting;
1699 */
1700
1701 caps = chn_getcaps(ch);
1702
1703 /*
1704 * With all handles collected, zero out the user's container and
1705 * begin filling in its fields.
1706 */
1707 bzero((void *)ai, sizeof(oss_audioinfo));
1708
1709 ai->dev = nchan;
1710 strlcpy(ai->name, ch->name, sizeof(ai->name));
1711
1712 if ((ch->flags & CHN_F_BUSY) == 0)
1713 ai->busy = 0;
1714 else
1715 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
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;
1960 ret = EINVAL;
1961 goto out;
1962 }
1963
1964 /*
1965 * At this point, the following synchronization stuff has happened:
1966 * - a specific PCM device is locked and its "in progress
1967 * operations" counter has been incremented, so be sure to unlock
1968 * and decrement when exiting;
1969 * - a specific audio channel has been locked, so be sure to unlock
1970 * when exiting;
1971 */
1972
1973 caps = chn_getcaps(ch);
1974
1975 /*
1976 * With all handles collected, zero out the user's container and
1977 * begin filling in its fields.
1978 */
1979 bzero((void *)ai, sizeof(oss_audioinfo));
1980
1981 ai->dev = nchan;
1982 strlcpy(ai->name, ch->name, sizeof(ai->name));
1983
1984 if ((ch->flags & CHN_F_BUSY) == 0)
1985 ai->busy = 0;
1986 else
1987 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
1988
1989 /**
1990 * @note
1991 * @c cmd - OSSv4 docs: "Only supported under Linux at this moment."
1992 * Cop-out, I know, but I'll save running around in the process
1993 * table for later. Is there a risk of leaking information?
1994 */
1995 ai->pid = ch->pid;
1724
1996
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?
1733 */
1734 ai->caps = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
1735
1736 /*
1737 * Collect formats supported @b natively by the device. Also
1738 * determine min/max channels. (I.e., mono, stereo, or both?)
1739 *
1740 * If any channel is stereo, maxch = 2;
1741 * if all channels are stereo, minch = 2, too;
1742 * if any channel is mono, minch = 1;
1743 * and if all channels are mono, maxch = 1.
1744 */
1745 minch = 0;
1746 maxch = 0;
1747 fmts = 0;
1748 for (i = 0; caps->fmtlist[i]; i++) {
1749 fmts |= caps->fmtlist[i];
1750 if (caps->fmtlist[i] & AFMT_STEREO) {
1751 minch = (minch == 0) ? 2 : minch;
1752 maxch = 2;
1753 } else {
1754 minch = 1;
1755 maxch = (maxch == 0) ? 1 : maxch;
1756 }
1757 }
1758
1759 if (ch->direction == PCMDIR_PLAY)
1760 ai->oformats = fmts;
1761 else
1762 ai->iformats = fmts;
1763
1764 /**
1765 * @note
1766 * @c magic - OSSv4 docs: "Reserved for internal use by OSS."
1767 *
1768 * @par
1769 * @c card_number - OSSv4 docs: "Number of the sound card where this
1770 * device belongs or -1 if this information is not available.
1771 * Applications should normally not use this field for any
1772 * purpose."
1773 */
1774 ai->card_number = -1;
1775 /**
1776 * @todo @c song_name - depends first on SNDCTL_[GS]ETSONG
1777 * @todo @c label - depends on SNDCTL_[GS]ETLABEL
1778 * @todo @c port_number - routing information?
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;
1997 /*
1998 * These flags stolen from SNDCTL_DSP_GETCAPS handler. Note, however,
1999 * that a single channel operates in only one direction, so
2000 * DSP_CAP_DUPLEX is out.
2001 */
2002 /**
2003 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep these in
2004 * pcmchan::caps?
2005 */
2006 ai->caps = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
2007
2008 /*
2009 * Collect formats supported @b natively by the device. Also
2010 * determine min/max channels. (I.e., mono, stereo, or both?)
2011 *
2012 * If any channel is stereo, maxch = 2;
2013 * if all channels are stereo, minch = 2, too;
2014 * if any channel is mono, minch = 1;
2015 * and if all channels are mono, maxch = 1.
2016 */
2017 minch = 0;
2018 maxch = 0;
2019 fmts = 0;
2020 for (i = 0; caps->fmtlist[i]; i++) {
2021 fmts |= caps->fmtlist[i];
2022 if (caps->fmtlist[i] & AFMT_STEREO) {
2023 minch = (minch == 0) ? 2 : minch;
2024 maxch = 2;
2025 } else {
2026 minch = 1;
2027 maxch = (maxch == 0) ? 1 : maxch;
2028 }
2029 }
2030
2031 if (ch->direction == PCMDIR_PLAY)
2032 ai->oformats = fmts;
2033 else
2034 ai->iformats = fmts;
2035
2036 /**
2037 * @note
2038 * @c magic - OSSv4 docs: "Reserved for internal use by OSS."
2039 *
2040 * @par
2041 * @c card_number - OSSv4 docs: "Number of the sound card where this
2042 * device belongs or -1 if this information is not available.
2043 * Applications should normally not use this field for any
2044 * purpose."
2045 */
2046 ai->card_number = -1;
2047 /**
2048 * @todo @c song_name - depends first on SNDCTL_[GS]ETSONG
2049 * @todo @c label - depends on SNDCTL_[GS]ETLABEL
2050 * @todo @c port_number - routing information?
2051 */
2052 ai->port_number = -1;
2053 ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
2054 /**
2055 * @note
2056 * @c real_device - OSSv4 docs: "Obsolete."
2057 */
2058 ai->real_device = -1;
1787 strlcpy(ai->devnode, t_cdev->si_name, sizeof(ai->devnode));
2059 strlcpy(ai->devnode, devname, 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 *
1796 * @todo @c handle - haven't decided how to generate this yet; bus,
1797 * vendor, device IDs?
1798 */
1799 ai->min_rate = caps->minspeed;
1800 ai->max_rate = caps->maxspeed;
1801
1802 ai->min_channels = minch;
1803 ai->max_channels = maxch;
1804
1805 ai->nrates = chn_getrates(ch, &rates);
1806 if (ai->nrates > OSS_MAX_SAMPLE_RATES)
1807 ai->nrates = OSS_MAX_SAMPLE_RATES;
1808
1809 for (i = 0; i < ai->nrates; i++)
1810 ai->rates[i] = rates[i];
1811
1812out:
1813 CHN_UNLOCK(ch);
1814 pcm_unlock(d);
1815 pcm_inprog(d, -1);
1816
1817 return ret;
1818}
1819
1820/**
1821 * @brief Assigns a PCM channel to a sync group.
1822 *
1823 * Sync groups are used to enable audio operations on multiple devices
1824 * simultaneously. They may be used with any number of devices and may
1825 * span across applications. Devices are added to groups with
1826 * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the
1827 * SNDCTL_DSP_SYNCSTART ioctl.
1828 *
1829 * If the @c id field of the @c group parameter is set to zero, then a new
1830 * sync group is created. Otherwise, wrch and rdch (if set) are added to
1831 * the group specified.
1832 *
1833 * @todo As far as memory allocation, should we assume that things are
1834 * okay and allocate with M_WAITOK before acquiring channel locks,
1835 * freeing later if not?
1836 *
1837 * @param wrch output channel associated w/ device (if any)
1838 * @param rdch input channel associated w/ device (if any)
1839 * @param group Sync group parameters
1840 *
1841 * @retval 0 success
1842 * @retval non-zero error to be propagated upstream
1843 */
1844static int
1845dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
1846{
1847 struct pcmchan_syncmember *smrd, *smwr;
1848 struct pcmchan_syncgroup *sg;
1849 int ret, sg_ids[3];
1850
1851 smrd = NULL;
1852 smwr = NULL;
1853 sg = NULL;
1854 ret = 0;
1855
1856 /*
1857 * Free_unr() may sleep, so store released syncgroup IDs until after
1858 * all locks are released.
1859 */
1860 sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
1861
1862 PCM_SG_LOCK();
1863
1864 /*
1865 * - Insert channel(s) into group's member list.
1866 * - Set CHN_F_NOTRIGGER on channel(s).
1867 * - Stop channel(s).
1868 */
1869
1870 /*
1871 * If device's channels are already mapped to a group, unmap them.
1872 */
1873 if (wrch) {
1874 CHN_LOCK(wrch);
1875 sg_ids[0] = chn_syncdestroy(wrch);
1876 }
1877
1878 if (rdch) {
1879 CHN_LOCK(rdch);
1880 sg_ids[1] = chn_syncdestroy(rdch);
1881 }
1882
1883 /*
1884 * Verify that mode matches character device properites.
1885 * - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
1886 * - Bail if PCM_ENABLE_INPUT && rdch == NULL.
1887 */
1888 if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
1889 ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
1890 ret = EINVAL;
1891 goto out;
1892 }
1893
1894 /*
1895 * An id of zero indicates the user wants to create a new
1896 * syncgroup.
1897 */
1898 if (group->id == 0) {
1899 sg = (struct pcmchan_syncgroup *)malloc(sizeof(*sg), M_DEVBUF, M_NOWAIT);
1900 if (sg != NULL) {
1901 SLIST_INIT(&sg->members);
1902 sg->id = alloc_unr(pcmsg_unrhdr);
1903
1904 group->id = sg->id;
1905 SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
1906 } else
1907 ret = ENOMEM;
1908 } else {
1909 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
1910 if (sg->id == group->id)
1911 break;
1912 }
1913 if (sg == NULL)
1914 ret = EINVAL;
1915 }
1916
1917 /* Couldn't create or find a syncgroup. Fail. */
1918 if (sg == NULL)
1919 goto out;
1920
1921 /*
1922 * Allocate a syncmember, assign it and a channel together, and
1923 * insert into syncgroup.
1924 */
1925 if (group->mode & PCM_ENABLE_INPUT) {
1926 smrd = (struct pcmchan_syncmember *)malloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT);
1927 if (smrd == NULL) {
1928 ret = ENOMEM;
1929 goto out;
1930 }
1931
1932 SLIST_INSERT_HEAD(&sg->members, smrd, link);
1933 smrd->parent = sg;
1934 smrd->ch = rdch;
1935
1936 chn_abort(rdch);
1937 rdch->flags |= CHN_F_NOTRIGGER;
1938 rdch->sm = smrd;
1939 }
1940
1941 if (group->mode & PCM_ENABLE_OUTPUT) {
1942 smwr = (struct pcmchan_syncmember *)malloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT);
1943 if (smwr == NULL) {
1944 ret = ENOMEM;
1945 goto out;
1946 }
1947
1948 SLIST_INSERT_HEAD(&sg->members, smwr, link);
1949 smwr->parent = sg;
1950 smwr->ch = wrch;
1951
1952 chn_abort(wrch);
1953 wrch->flags |= CHN_F_NOTRIGGER;
1954 wrch->sm = smwr;
1955 }
1956
1957
1958out:
1959 if (ret != 0) {
1960 if (smrd != NULL)
1961 free(smrd, M_DEVBUF);
1962 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
1963 sg_ids[2] = sg->id;
1964 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
1965 free(sg, M_DEVBUF);
1966 }
1967
1968 if (wrch)
1969 wrch->sm = NULL;
1970 if (rdch)
1971 rdch->sm = NULL;
1972 }
1973
1974 if (wrch)
1975 CHN_UNLOCK(wrch);
1976 if (rdch)
1977 CHN_UNLOCK(rdch);
1978
1979 PCM_SG_UNLOCK();
1980
1981 if (sg_ids[0])
1982 free_unr(pcmsg_unrhdr, sg_ids[0]);
1983 if (sg_ids[1])
1984 free_unr(pcmsg_unrhdr, sg_ids[1]);
1985 if (sg_ids[2])
1986 free_unr(pcmsg_unrhdr, sg_ids[2]);
1987
1988 return ret;
1989}
1990
1991/**
1992 * @brief Launch a sync group into action
1993 *
1994 * Sync groups are established via SNDCTL_DSP_SYNCGROUP. This function
1995 * iterates over all members, triggering them along the way.
1996 *
1997 * @note Caller must not hold any channel locks.
1998 *
1999 * @param sg_id sync group identifier
2000 *
2001 * @retval 0 success
2002 * @retval non-zero error worthy of propagating upstream to user
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;
2060 ai->enabled = device_is_attached(d->dev) ? 1 : 0;
2061 /**
2062 * @note
2063 * @c flags - OSSv4 docs: "Reserved for future use."
2064 *
2065 * @note
2066 * @c binding - OSSv4 docs: "Reserved for future use."
2067 *
2068 * @todo @c handle - haven't decided how to generate this yet; bus,
2069 * vendor, device IDs?
2070 */
2071 ai->min_rate = caps->minspeed;
2072 ai->max_rate = caps->maxspeed;
2073
2074 ai->min_channels = minch;
2075 ai->max_channels = maxch;
2076
2077 ai->nrates = chn_getrates(ch, &rates);
2078 if (ai->nrates > OSS_MAX_SAMPLE_RATES)
2079 ai->nrates = OSS_MAX_SAMPLE_RATES;
2080
2081 for (i = 0; i < ai->nrates; i++)
2082 ai->rates[i] = rates[i];
2083
2084out:
2085 CHN_UNLOCK(ch);
2086 pcm_unlock(d);
2087 pcm_inprog(d, -1);
2088
2089 return ret;
2090}
2091
2092/**
2093 * @brief Assigns a PCM channel to a sync group.
2094 *
2095 * Sync groups are used to enable audio operations on multiple devices
2096 * simultaneously. They may be used with any number of devices and may
2097 * span across applications. Devices are added to groups with
2098 * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the
2099 * SNDCTL_DSP_SYNCSTART ioctl.
2100 *
2101 * If the @c id field of the @c group parameter is set to zero, then a new
2102 * sync group is created. Otherwise, wrch and rdch (if set) are added to
2103 * the group specified.
2104 *
2105 * @todo As far as memory allocation, should we assume that things are
2106 * okay and allocate with M_WAITOK before acquiring channel locks,
2107 * freeing later if not?
2108 *
2109 * @param wrch output channel associated w/ device (if any)
2110 * @param rdch input channel associated w/ device (if any)
2111 * @param group Sync group parameters
2112 *
2113 * @retval 0 success
2114 * @retval non-zero error to be propagated upstream
2115 */
2116static int
2117dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
2118{
2119 struct pcmchan_syncmember *smrd, *smwr;
2120 struct pcmchan_syncgroup *sg;
2121 int ret, sg_ids[3];
2122
2123 smrd = NULL;
2124 smwr = NULL;
2125 sg = NULL;
2126 ret = 0;
2127
2128 /*
2129 * Free_unr() may sleep, so store released syncgroup IDs until after
2130 * all locks are released.
2131 */
2132 sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
2133
2134 PCM_SG_LOCK();
2135
2136 /*
2137 * - Insert channel(s) into group's member list.
2138 * - Set CHN_F_NOTRIGGER on channel(s).
2139 * - Stop channel(s).
2140 */
2141
2142 /*
2143 * If device's channels are already mapped to a group, unmap them.
2144 */
2145 if (wrch) {
2146 CHN_LOCK(wrch);
2147 sg_ids[0] = chn_syncdestroy(wrch);
2148 }
2149
2150 if (rdch) {
2151 CHN_LOCK(rdch);
2152 sg_ids[1] = chn_syncdestroy(rdch);
2153 }
2154
2155 /*
2156 * Verify that mode matches character device properites.
2157 * - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
2158 * - Bail if PCM_ENABLE_INPUT && rdch == NULL.
2159 */
2160 if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
2161 ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
2162 ret = EINVAL;
2163 goto out;
2164 }
2165
2166 /*
2167 * An id of zero indicates the user wants to create a new
2168 * syncgroup.
2169 */
2170 if (group->id == 0) {
2171 sg = (struct pcmchan_syncgroup *)malloc(sizeof(*sg), M_DEVBUF, M_NOWAIT);
2172 if (sg != NULL) {
2173 SLIST_INIT(&sg->members);
2174 sg->id = alloc_unr(pcmsg_unrhdr);
2175
2176 group->id = sg->id;
2177 SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
2178 } else
2179 ret = ENOMEM;
2180 } else {
2181 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2182 if (sg->id == group->id)
2183 break;
2184 }
2185 if (sg == NULL)
2186 ret = EINVAL;
2187 }
2188
2189 /* Couldn't create or find a syncgroup. Fail. */
2190 if (sg == NULL)
2191 goto out;
2192
2193 /*
2194 * Allocate a syncmember, assign it and a channel together, and
2195 * insert into syncgroup.
2196 */
2197 if (group->mode & PCM_ENABLE_INPUT) {
2198 smrd = (struct pcmchan_syncmember *)malloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT);
2199 if (smrd == NULL) {
2200 ret = ENOMEM;
2201 goto out;
2202 }
2203
2204 SLIST_INSERT_HEAD(&sg->members, smrd, link);
2205 smrd->parent = sg;
2206 smrd->ch = rdch;
2207
2208 chn_abort(rdch);
2209 rdch->flags |= CHN_F_NOTRIGGER;
2210 rdch->sm = smrd;
2211 }
2212
2213 if (group->mode & PCM_ENABLE_OUTPUT) {
2214 smwr = (struct pcmchan_syncmember *)malloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT);
2215 if (smwr == NULL) {
2216 ret = ENOMEM;
2217 goto out;
2218 }
2219
2220 SLIST_INSERT_HEAD(&sg->members, smwr, link);
2221 smwr->parent = sg;
2222 smwr->ch = wrch;
2223
2224 chn_abort(wrch);
2225 wrch->flags |= CHN_F_NOTRIGGER;
2226 wrch->sm = smwr;
2227 }
2228
2229
2230out:
2231 if (ret != 0) {
2232 if (smrd != NULL)
2233 free(smrd, M_DEVBUF);
2234 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
2235 sg_ids[2] = sg->id;
2236 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2237 free(sg, M_DEVBUF);
2238 }
2239
2240 if (wrch)
2241 wrch->sm = NULL;
2242 if (rdch)
2243 rdch->sm = NULL;
2244 }
2245
2246 if (wrch)
2247 CHN_UNLOCK(wrch);
2248 if (rdch)
2249 CHN_UNLOCK(rdch);
2250
2251 PCM_SG_UNLOCK();
2252
2253 if (sg_ids[0])
2254 free_unr(pcmsg_unrhdr, sg_ids[0]);
2255 if (sg_ids[1])
2256 free_unr(pcmsg_unrhdr, sg_ids[1]);
2257 if (sg_ids[2])
2258 free_unr(pcmsg_unrhdr, sg_ids[2]);
2259
2260 return ret;
2261}
2262
2263/**
2264 * @brief Launch a sync group into action
2265 *
2266 * Sync groups are established via SNDCTL_DSP_SYNCGROUP. This function
2267 * iterates over all members, triggering them along the way.
2268 *
2269 * @note Caller must not hold any channel locks.
2270 *
2271 * @param sg_id sync group identifier
2272 *
2273 * @retval 0 success
2274 * @retval non-zero error worthy of propagating upstream to user
2275 */
2276static int
2277dsp_oss_syncstart(int sg_id)
2278{
2279 struct pcmchan_syncmember *sm, *sm_tmp;
2280 struct pcmchan_syncgroup *sg;
2281 struct pcm_channel *c;
2282 int ret, needlocks;
2011
2283
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 */
2020 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2021 if (sg->id == sg_id)
2022 break;
2023 }
2024
2025 /* Return EINVAL if not found */
2026 if (sg == NULL) {
2027 ret = EINVAL;
2028 break;
2029 }
2030
2031 /* Any removals resulting in an empty group should've handled this */
2032 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
2033
2034 /*
2035 * Attempt to lock all member channels - if any are already
2036 * locked, unlock those acquired, sleep for a bit, and try
2037 * again.
2038 */
2039 SLIST_FOREACH(sm, &sg->members, link) {
2040 if (CHN_TRYLOCK(sm->ch) == 0) {
2041 int timo = hz * 5/1000;
2042 if (timo < 1)
2043 timo = 1;
2044
2045 /* Release all locked channels so far, retry */
2046 SLIST_FOREACH(sm_tmp, &sg->members, link) {
2047 /* sm is the member already locked */
2048 if (sm == sm_tmp)
2049 break;
2050 CHN_UNLOCK(sm_tmp->ch);
2051 }
2052
2053 /** @todo Is PRIBIO correct/ */
2054 ret = msleep(sm, &snd_pcm_syncgroups_mtx, PRIBIO | PCATCH, "pcmsgrp", timo);
2055 if (ret == EINTR || ret == ERESTART)
2056 break;
2057
2058 needlocks = 1;
2059 ret = 0; /* Assumes ret == EAGAIN... */
2060 }
2061 }
2062 } while (needlocks && ret == 0);
2063
2064 /* Proceed only if no errors encountered. */
2065 if (ret == 0) {
2066 /* Launch channels */
2067 while((sm = SLIST_FIRST(&sg->members)) != NULL) {
2068 SLIST_REMOVE_HEAD(&sg->members, link);
2069
2070 c = sm->ch;
2071 c->sm = NULL;
2072 chn_start(c, 1);
2073 c->flags &= ~CHN_F_NOTRIGGER;
2074 CHN_UNLOCK(c);
2075
2076 free(sm, M_DEVBUF);
2077 }
2078
2079 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2080 free(sg, M_DEVBUF);
2081 }
2082
2083 PCM_SG_UNLOCK();
2084
2085 /*
2086 * Free_unr() may sleep, so be sure to give up the syncgroup lock
2087 * first.
2088 */
2089 if (ret == 0)
2090 free_unr(pcmsg_unrhdr, sg_id);
2091
2092 return ret;
2093}
2094
2095/**
2096 * @brief Handler for SNDCTL_DSP_POLICY
2097 *
2098 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
2099 * size and count like with SNDCTL_DSP_SETFRAGMENT. Instead of the user
2100 * specifying those two parameters, s/he simply selects a number from 0..10
2101 * which corresponds to a buffer size. Smaller numbers request smaller
2102 * buffers with lower latencies (at greater overhead from more frequent
2103 * interrupts), while greater numbers behave in the opposite manner.
2104 *
2105 * The 4Front spec states that a value of 5 should be the default. However,
2106 * this implementation deviates slightly by using a linear scale without
2107 * consulting drivers. I.e., even though drivers may have different default
2108 * buffer sizes, a policy argument of 5 will have the same result across
2109 * all drivers.
2110 *
2111 * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for
2112 * more information.
2113 *
2114 * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to
2115 * work with hardware drivers directly.
2116 *
2117 * @note PCM channel arguments must not be locked by caller.
2118 *
2119 * @param wrch Pointer to opened playback channel (optional; may be NULL)
2120 * @param rdch " recording channel (optional; may be NULL)
2121 * @param policy Integer from [0:10]
2122 *
2123 * @retval 0 constant (for now)
2124 */
2125static int
2126dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
2127{
2128 int ret;
2129
2130 if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
2131 return EIO;
2132
2133 /* Default: success */
2134 ret = 0;
2135
2136 if (rdch) {
2137 CHN_LOCK(rdch);
2138 ret = chn_setlatency(rdch, policy);
2139 CHN_UNLOCK(rdch);
2140 }
2141
2142 if (wrch && ret == 0) {
2143 CHN_LOCK(wrch);
2144 ret = chn_setlatency(wrch, policy);
2145 CHN_UNLOCK(wrch);
2146 }
2147
2148 if (ret)
2149 ret = EIO;
2150
2151 return ret;
2152}
2153
2154#ifdef OSSV4_EXPERIMENT
2155/**
2156 * @brief Enable or disable "cooked" mode
2157 *
2158 * This is a handler for @c SNDCTL_DSP_COOKEDMODE. When in cooked mode, which
2159 * is the default, the sound system handles rate and format conversions
2160 * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only
2161 * operates with 44100Hz/16bit/signed samples).
2162 *
2163 * Disabling cooked mode is intended for applications wanting to mmap()
2164 * a sound card's buffer space directly, bypassing the FreeBSD 2-stage
2165 * feeder architecture, presumably to gain as much control over audio
2166 * hardware as possible.
2167 *
2168 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html
2169 * for more details.
2170 *
2171 * @note Currently, this function is just a stub that always returns EINVAL.
2172 *
2173 * @todo Figure out how to and actually implement this.
2174 *
2175 * @param wrch playback channel (optional; may be NULL)
2176 * @param rdch recording channel (optional; may be NULL)
2177 * @param enabled 0 = raw mode, 1 = cooked mode
2178 *
2179 * @retval EINVAL Operation not yet supported.
2180 */
2181static int
2182dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
2183{
2184 return EINVAL;
2185}
2186
2187/**
2188 * @brief Retrieve channel interleaving order
2189 *
2190 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
2191 *
2192 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html
2193 * for more details.
2194 *
2195 * @note As the ioctl definition is still under construction, FreeBSD
2196 * does not currently support SNDCTL_DSP_GET_CHNORDER.
2197 *
2198 * @param wrch playback channel (optional; may be NULL)
2199 * @param rdch recording channel (optional; may be NULL)
2200 * @param map channel map (result will be stored there)
2201 *
2202 * @retval EINVAL Operation not yet supported.
2203 */
2204static int
2205dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
2206{
2207 return EINVAL;
2208}
2209
2210/**
2211 * @brief Specify channel interleaving order
2212 *
2213 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
2214 *
2215 * @note As the ioctl definition is still under construction, FreeBSD
2216 * does not currently support @c SNDCTL_DSP_SET_CHNORDER.
2217 *
2218 * @param wrch playback channel (optional; may be NULL)
2219 * @param rdch recording channel (optional; may be NULL)
2220 * @param map channel map
2221 *
2222 * @retval EINVAL Operation not yet supported.
2223 */
2224static int
2225dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
2226{
2227 return EINVAL;
2228}
2229
2230/**
2231 * @brief Retrieve an audio device's label
2232 *
2233 * This is a handler for the @c SNDCTL_GETLABEL ioctl.
2234 *
2235 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
2236 * for more details.
2237 *
2238 * From Hannu@4Front: "For example ossxmix (just like some HW mixer
2239 * consoles) can show variable "labels" for certain controls. By default
2240 * the application name (say quake) is shown as the label but
2241 * applications may change the labels themselves."
2242 *
2243 * @note As the ioctl definition is still under construction, FreeBSD
2244 * does not currently support @c SNDCTL_GETLABEL.
2245 *
2246 * @param wrch playback channel (optional; may be NULL)
2247 * @param rdch recording channel (optional; may be NULL)
2248 * @param label label gets copied here
2249 *
2250 * @retval EINVAL Operation not yet supported.
2251 */
2252static int
2253dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
2254{
2255 return EINVAL;
2256}
2257
2258/**
2259 * @brief Specify an audio device's label
2260 *
2261 * This is a handler for the @c SNDCTL_SETLABEL ioctl. Please see the
2262 * comments for @c dsp_oss_getlabel immediately above.
2263 *
2264 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
2265 * for more details.
2266 *
2267 * @note As the ioctl definition is still under construction, FreeBSD
2268 * does not currently support SNDCTL_SETLABEL.
2269 *
2270 * @param wrch playback channel (optional; may be NULL)
2271 * @param rdch recording channel (optional; may be NULL)
2272 * @param label label gets copied from here
2273 *
2274 * @retval EINVAL Operation not yet supported.
2275 */
2276static int
2277dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
2278{
2279 return EINVAL;
2280}
2281
2282/**
2283 * @brief Retrieve name of currently played song
2284 *
2285 * This is a handler for the @c SNDCTL_GETSONG ioctl. Audio players could
2286 * tell the system the name of the currently playing song, which would be
2287 * visible in @c /dev/sndstat.
2288 *
2289 * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html
2290 * for more details.
2291 *
2292 * @note As the ioctl definition is still under construction, FreeBSD
2293 * does not currently support SNDCTL_GETSONG.
2294 *
2295 * @param wrch playback channel (optional; may be NULL)
2296 * @param rdch recording channel (optional; may be NULL)
2297 * @param song song name gets copied here
2298 *
2299 * @retval EINVAL Operation not yet supported.
2300 */
2301static int
2302dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
2303{
2304 return EINVAL;
2305}
2306
2307/**
2308 * @brief Retrieve name of currently played song
2309 *
2310 * This is a handler for the @c SNDCTL_SETSONG ioctl. Audio players could
2311 * tell the system the name of the currently playing song, which would be
2312 * visible in @c /dev/sndstat.
2313 *
2314 * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html
2315 * for more details.
2316 *
2317 * @note As the ioctl definition is still under construction, FreeBSD
2318 * does not currently support SNDCTL_SETSONG.
2319 *
2320 * @param wrch playback channel (optional; may be NULL)
2321 * @param rdch recording channel (optional; may be NULL)
2322 * @param song song name gets copied from here
2323 *
2324 * @retval EINVAL Operation not yet supported.
2325 */
2326static int
2327dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
2328{
2329 return EINVAL;
2330}
2331
2332/**
2333 * @brief Rename a device
2334 *
2335 * This is a handler for the @c SNDCTL_SETNAME ioctl.
2336 *
2337 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for
2338 * more details.
2339 *
2340 * From Hannu@4Front: "This call is used to change the device name
2341 * reported in /dev/sndstat and ossinfo. So instead of using some generic
2342 * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull
2343 * name depending on the current context (for example 'OSS virtual wave table
2344 * synth' or 'VoIP link to London')."
2345 *
2346 * @note As the ioctl definition is still under construction, FreeBSD
2347 * does not currently support SNDCTL_SETNAME.
2348 *
2349 * @param wrch playback channel (optional; may be NULL)
2350 * @param rdch recording channel (optional; may be NULL)
2351 * @param name new device name gets copied from here
2352 *
2353 * @retval EINVAL Operation not yet supported.
2354 */
2355static int
2356dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
2357{
2358 return EINVAL;
2359}
2360#endif /* !OSSV4_EXPERIMENT */
2284 /* Get the synclists lock */
2285 PCM_SG_LOCK();
2286
2287 do {
2288 ret = 0;
2289 needlocks = 0;
2290
2291 /* Search for syncgroup by ID */
2292 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2293 if (sg->id == sg_id)
2294 break;
2295 }
2296
2297 /* Return EINVAL if not found */
2298 if (sg == NULL) {
2299 ret = EINVAL;
2300 break;
2301 }
2302
2303 /* Any removals resulting in an empty group should've handled this */
2304 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
2305
2306 /*
2307 * Attempt to lock all member channels - if any are already
2308 * locked, unlock those acquired, sleep for a bit, and try
2309 * again.
2310 */
2311 SLIST_FOREACH(sm, &sg->members, link) {
2312 if (CHN_TRYLOCK(sm->ch) == 0) {
2313 int timo = hz * 5/1000;
2314 if (timo < 1)
2315 timo = 1;
2316
2317 /* Release all locked channels so far, retry */
2318 SLIST_FOREACH(sm_tmp, &sg->members, link) {
2319 /* sm is the member already locked */
2320 if (sm == sm_tmp)
2321 break;
2322 CHN_UNLOCK(sm_tmp->ch);
2323 }
2324
2325 /** @todo Is PRIBIO correct/ */
2326 ret = msleep(sm, &snd_pcm_syncgroups_mtx, PRIBIO | PCATCH, "pcmsgrp", timo);
2327 if (ret == EINTR || ret == ERESTART)
2328 break;
2329
2330 needlocks = 1;
2331 ret = 0; /* Assumes ret == EAGAIN... */
2332 }
2333 }
2334 } while (needlocks && ret == 0);
2335
2336 /* Proceed only if no errors encountered. */
2337 if (ret == 0) {
2338 /* Launch channels */
2339 while((sm = SLIST_FIRST(&sg->members)) != NULL) {
2340 SLIST_REMOVE_HEAD(&sg->members, link);
2341
2342 c = sm->ch;
2343 c->sm = NULL;
2344 chn_start(c, 1);
2345 c->flags &= ~CHN_F_NOTRIGGER;
2346 CHN_UNLOCK(c);
2347
2348 free(sm, M_DEVBUF);
2349 }
2350
2351 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2352 free(sg, M_DEVBUF);
2353 }
2354
2355 PCM_SG_UNLOCK();
2356
2357 /*
2358 * Free_unr() may sleep, so be sure to give up the syncgroup lock
2359 * first.
2360 */
2361 if (ret == 0)
2362 free_unr(pcmsg_unrhdr, sg_id);
2363
2364 return ret;
2365}
2366
2367/**
2368 * @brief Handler for SNDCTL_DSP_POLICY
2369 *
2370 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
2371 * size and count like with SNDCTL_DSP_SETFRAGMENT. Instead of the user
2372 * specifying those two parameters, s/he simply selects a number from 0..10
2373 * which corresponds to a buffer size. Smaller numbers request smaller
2374 * buffers with lower latencies (at greater overhead from more frequent
2375 * interrupts), while greater numbers behave in the opposite manner.
2376 *
2377 * The 4Front spec states that a value of 5 should be the default. However,
2378 * this implementation deviates slightly by using a linear scale without
2379 * consulting drivers. I.e., even though drivers may have different default
2380 * buffer sizes, a policy argument of 5 will have the same result across
2381 * all drivers.
2382 *
2383 * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for
2384 * more information.
2385 *
2386 * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to
2387 * work with hardware drivers directly.
2388 *
2389 * @note PCM channel arguments must not be locked by caller.
2390 *
2391 * @param wrch Pointer to opened playback channel (optional; may be NULL)
2392 * @param rdch " recording channel (optional; may be NULL)
2393 * @param policy Integer from [0:10]
2394 *
2395 * @retval 0 constant (for now)
2396 */
2397static int
2398dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
2399{
2400 int ret;
2401
2402 if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
2403 return EIO;
2404
2405 /* Default: success */
2406 ret = 0;
2407
2408 if (rdch) {
2409 CHN_LOCK(rdch);
2410 ret = chn_setlatency(rdch, policy);
2411 CHN_UNLOCK(rdch);
2412 }
2413
2414 if (wrch && ret == 0) {
2415 CHN_LOCK(wrch);
2416 ret = chn_setlatency(wrch, policy);
2417 CHN_UNLOCK(wrch);
2418 }
2419
2420 if (ret)
2421 ret = EIO;
2422
2423 return ret;
2424}
2425
2426#ifdef OSSV4_EXPERIMENT
2427/**
2428 * @brief Enable or disable "cooked" mode
2429 *
2430 * This is a handler for @c SNDCTL_DSP_COOKEDMODE. When in cooked mode, which
2431 * is the default, the sound system handles rate and format conversions
2432 * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only
2433 * operates with 44100Hz/16bit/signed samples).
2434 *
2435 * Disabling cooked mode is intended for applications wanting to mmap()
2436 * a sound card's buffer space directly, bypassing the FreeBSD 2-stage
2437 * feeder architecture, presumably to gain as much control over audio
2438 * hardware as possible.
2439 *
2440 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html
2441 * for more details.
2442 *
2443 * @note Currently, this function is just a stub that always returns EINVAL.
2444 *
2445 * @todo Figure out how to and actually implement this.
2446 *
2447 * @param wrch playback channel (optional; may be NULL)
2448 * @param rdch recording channel (optional; may be NULL)
2449 * @param enabled 0 = raw mode, 1 = cooked mode
2450 *
2451 * @retval EINVAL Operation not yet supported.
2452 */
2453static int
2454dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
2455{
2456 return EINVAL;
2457}
2458
2459/**
2460 * @brief Retrieve channel interleaving order
2461 *
2462 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
2463 *
2464 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html
2465 * for more details.
2466 *
2467 * @note As the ioctl definition is still under construction, FreeBSD
2468 * does not currently support SNDCTL_DSP_GET_CHNORDER.
2469 *
2470 * @param wrch playback channel (optional; may be NULL)
2471 * @param rdch recording channel (optional; may be NULL)
2472 * @param map channel map (result will be stored there)
2473 *
2474 * @retval EINVAL Operation not yet supported.
2475 */
2476static int
2477dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
2478{
2479 return EINVAL;
2480}
2481
2482/**
2483 * @brief Specify channel interleaving order
2484 *
2485 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
2486 *
2487 * @note As the ioctl definition is still under construction, FreeBSD
2488 * does not currently support @c SNDCTL_DSP_SET_CHNORDER.
2489 *
2490 * @param wrch playback channel (optional; may be NULL)
2491 * @param rdch recording channel (optional; may be NULL)
2492 * @param map channel map
2493 *
2494 * @retval EINVAL Operation not yet supported.
2495 */
2496static int
2497dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
2498{
2499 return EINVAL;
2500}
2501
2502/**
2503 * @brief Retrieve an audio device's label
2504 *
2505 * This is a handler for the @c SNDCTL_GETLABEL ioctl.
2506 *
2507 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
2508 * for more details.
2509 *
2510 * From Hannu@4Front: "For example ossxmix (just like some HW mixer
2511 * consoles) can show variable "labels" for certain controls. By default
2512 * the application name (say quake) is shown as the label but
2513 * applications may change the labels themselves."
2514 *
2515 * @note As the ioctl definition is still under construction, FreeBSD
2516 * does not currently support @c SNDCTL_GETLABEL.
2517 *
2518 * @param wrch playback channel (optional; may be NULL)
2519 * @param rdch recording channel (optional; may be NULL)
2520 * @param label label gets copied here
2521 *
2522 * @retval EINVAL Operation not yet supported.
2523 */
2524static int
2525dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
2526{
2527 return EINVAL;
2528}
2529
2530/**
2531 * @brief Specify an audio device's label
2532 *
2533 * This is a handler for the @c SNDCTL_SETLABEL ioctl. Please see the
2534 * comments for @c dsp_oss_getlabel immediately above.
2535 *
2536 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
2537 * for more details.
2538 *
2539 * @note As the ioctl definition is still under construction, FreeBSD
2540 * does not currently support SNDCTL_SETLABEL.
2541 *
2542 * @param wrch playback channel (optional; may be NULL)
2543 * @param rdch recording channel (optional; may be NULL)
2544 * @param label label gets copied from here
2545 *
2546 * @retval EINVAL Operation not yet supported.
2547 */
2548static int
2549dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
2550{
2551 return EINVAL;
2552}
2553
2554/**
2555 * @brief Retrieve name of currently played song
2556 *
2557 * This is a handler for the @c SNDCTL_GETSONG ioctl. Audio players could
2558 * tell the system the name of the currently playing song, which would be
2559 * visible in @c /dev/sndstat.
2560 *
2561 * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html
2562 * for more details.
2563 *
2564 * @note As the ioctl definition is still under construction, FreeBSD
2565 * does not currently support SNDCTL_GETSONG.
2566 *
2567 * @param wrch playback channel (optional; may be NULL)
2568 * @param rdch recording channel (optional; may be NULL)
2569 * @param song song name gets copied here
2570 *
2571 * @retval EINVAL Operation not yet supported.
2572 */
2573static int
2574dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
2575{
2576 return EINVAL;
2577}
2578
2579/**
2580 * @brief Retrieve name of currently played song
2581 *
2582 * This is a handler for the @c SNDCTL_SETSONG ioctl. Audio players could
2583 * tell the system the name of the currently playing song, which would be
2584 * visible in @c /dev/sndstat.
2585 *
2586 * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html
2587 * for more details.
2588 *
2589 * @note As the ioctl definition is still under construction, FreeBSD
2590 * does not currently support SNDCTL_SETSONG.
2591 *
2592 * @param wrch playback channel (optional; may be NULL)
2593 * @param rdch recording channel (optional; may be NULL)
2594 * @param song song name gets copied from here
2595 *
2596 * @retval EINVAL Operation not yet supported.
2597 */
2598static int
2599dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
2600{
2601 return EINVAL;
2602}
2603
2604/**
2605 * @brief Rename a device
2606 *
2607 * This is a handler for the @c SNDCTL_SETNAME ioctl.
2608 *
2609 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for
2610 * more details.
2611 *
2612 * From Hannu@4Front: "This call is used to change the device name
2613 * reported in /dev/sndstat and ossinfo. So instead of using some generic
2614 * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull
2615 * name depending on the current context (for example 'OSS virtual wave table
2616 * synth' or 'VoIP link to London')."
2617 *
2618 * @note As the ioctl definition is still under construction, FreeBSD
2619 * does not currently support SNDCTL_SETNAME.
2620 *
2621 * @param wrch playback channel (optional; may be NULL)
2622 * @param rdch recording channel (optional; may be NULL)
2623 * @param name new device name gets copied from here
2624 *
2625 * @retval EINVAL Operation not yet supported.
2626 */
2627static int
2628dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
2629{
2630 return EINVAL;
2631}
2632#endif /* !OSSV4_EXPERIMENT */