pad.c revision 1.75 1 1.75 riastrad /* $NetBSD: pad.c,v 1.75 2021/06/14 18:44:53 riastradh Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.75 riastrad __KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.75 2021/06/14 18:44:53 riastradh Exp $");
31 1.1 jmcneill
32 1.70 riastrad #include <sys/param.h>
33 1.1 jmcneill #include <sys/types.h>
34 1.70 riastrad
35 1.70 riastrad #include <sys/audioio.h>
36 1.70 riastrad #include <sys/buf.h>
37 1.70 riastrad #include <sys/condvar.h>
38 1.1 jmcneill #include <sys/conf.h>
39 1.70 riastrad #include <sys/device.h>
40 1.57 nakayama #include <sys/file.h>
41 1.57 nakayama #include <sys/filedesc.h>
42 1.57 nakayama #include <sys/kauth.h>
43 1.70 riastrad #include <sys/kernel.h>
44 1.1 jmcneill #include <sys/kmem.h>
45 1.70 riastrad #include <sys/module.h>
46 1.63 isaki #include <sys/poll.h>
47 1.1 jmcneill #include <sys/proc.h>
48 1.1 jmcneill #include <sys/select.h>
49 1.57 nakayama #include <sys/stat.h>
50 1.70 riastrad #include <sys/vnode.h>
51 1.1 jmcneill
52 1.59 isaki #include <dev/audio/audio_if.h>
53 1.59 isaki #include <dev/audio/audiovar.h>
54 1.1 jmcneill
55 1.2 jmcneill #include <dev/pad/padvar.h>
56 1.2 jmcneill
57 1.71 riastrad #include "ioconf.h"
58 1.71 riastrad
59 1.59 isaki /* #define PAD_DEBUG */
60 1.59 isaki #ifdef PAD_DEBUG
61 1.59 isaki #define DPRINTF(fmt...) printf(fmt)
62 1.59 isaki #else
63 1.59 isaki #define DPRINTF(fmt...) /**/
64 1.59 isaki #endif
65 1.59 isaki
66 1.57 nakayama #define PADFREQ 44100
67 1.57 nakayama #define PADCHAN 2
68 1.57 nakayama #define PADPREC 16
69 1.57 nakayama
70 1.1 jmcneill typedef struct pad_block {
71 1.1 jmcneill uint8_t *pb_ptr;
72 1.1 jmcneill int pb_len;
73 1.1 jmcneill } pad_block_t;
74 1.1 jmcneill
75 1.2 jmcneill enum {
76 1.2 jmcneill PAD_OUTPUT_CLASS,
77 1.2 jmcneill PAD_INPUT_CLASS,
78 1.2 jmcneill PAD_OUTPUT_MASTER_VOLUME,
79 1.2 jmcneill PAD_INPUT_DAC_VOLUME,
80 1.2 jmcneill PAD_ENUM_LAST,
81 1.2 jmcneill };
82 1.1 jmcneill
83 1.10 christos static int pad_match(device_t, cfdata_t, void *);
84 1.5 dyoung static void pad_attach(device_t, device_t, void *);
85 1.5 dyoung static int pad_detach(device_t, int);
86 1.5 dyoung static void pad_childdet(device_t, device_t);
87 1.1 jmcneill
88 1.59 isaki static int pad_query_format(void *, audio_format_query_t *);
89 1.59 isaki static int pad_set_format(void *, int,
90 1.59 isaki const audio_params_t *, const audio_params_t *,
91 1.59 isaki audio_filter_reg_t *, audio_filter_reg_t *);
92 1.1 jmcneill static int pad_start_output(void *, void *, int,
93 1.62 isaki void (*)(void *), void *);
94 1.1 jmcneill static int pad_halt_output(void *);
95 1.1 jmcneill static int pad_getdev(void *, struct audio_device *);
96 1.1 jmcneill static int pad_set_port(void *, mixer_ctrl_t *);
97 1.1 jmcneill static int pad_get_port(void *, mixer_ctrl_t *);
98 1.1 jmcneill static int pad_query_devinfo(void *, mixer_devinfo_t *);
99 1.1 jmcneill static int pad_get_props(void *);
100 1.17 jmcneill static void pad_get_locks(void *, kmutex_t **, kmutex_t **);
101 1.1 jmcneill
102 1.59 isaki static void pad_done_output(void *);
103 1.59 isaki static void pad_swvol_codec(audio_filter_arg_t *);
104 1.22 jmcneill
105 1.71 riastrad static void pad_close(struct pad_softc *);
106 1.62 isaki static int pad_read(struct pad_softc *, off_t *, struct uio *,
107 1.62 isaki kauth_cred_t, int);
108 1.62 isaki
109 1.62 isaki static int fops_pad_close(struct file *);
110 1.62 isaki static int fops_pad_read(struct file *, off_t *, struct uio *,
111 1.62 isaki kauth_cred_t, int);
112 1.62 isaki static int fops_pad_write(struct file *, off_t *, struct uio *,
113 1.62 isaki kauth_cred_t, int);
114 1.62 isaki static int fops_pad_ioctl(struct file *, u_long, void *);
115 1.62 isaki static int fops_pad_kqfilter(struct file *, struct knote *);
116 1.62 isaki static int fops_pad_poll(struct file *, int);
117 1.62 isaki static int fops_pad_stat(struct file *, struct stat *);
118 1.62 isaki static int fops_pad_mmap(struct file *, off_t *, size_t, int, int *, int *,
119 1.62 isaki struct uvm_object **, int *);
120 1.32 pgoyette
121 1.1 jmcneill static const struct audio_hw_if pad_hw_if = {
122 1.62 isaki .query_format = pad_query_format,
123 1.62 isaki .set_format = pad_set_format,
124 1.62 isaki .start_output = pad_start_output,
125 1.62 isaki .halt_output = pad_halt_output,
126 1.62 isaki .getdev = pad_getdev,
127 1.62 isaki .set_port = pad_set_port,
128 1.62 isaki .get_port = pad_get_port,
129 1.62 isaki .query_devinfo = pad_query_devinfo,
130 1.62 isaki .get_props = pad_get_props,
131 1.62 isaki .get_locks = pad_get_locks,
132 1.1 jmcneill };
133 1.1 jmcneill
134 1.1 jmcneill #define PAD_NFORMATS 1
135 1.1 jmcneill static const struct audio_format pad_formats[PAD_NFORMATS] = {
136 1.59 isaki {
137 1.59 isaki .mode = AUMODE_PLAY,
138 1.60 isaki .encoding = AUDIO_ENCODING_SLINEAR_LE,
139 1.59 isaki .validbits = PADPREC,
140 1.59 isaki .precision = PADPREC,
141 1.59 isaki .channels = PADCHAN,
142 1.59 isaki .channel_mask = AUFMT_STEREO,
143 1.59 isaki .frequency_type = 1,
144 1.59 isaki .frequency = { PADFREQ },
145 1.59 isaki },
146 1.1 jmcneill };
147 1.1 jmcneill
148 1.1 jmcneill extern void padattach(int);
149 1.1 jmcneill
150 1.62 isaki static int pad_add_block(struct pad_softc *, uint8_t *, int);
151 1.62 isaki static int pad_get_block(struct pad_softc *, pad_block_t *, int);
152 1.1 jmcneill
153 1.71 riastrad static dev_type_open(pad_open);
154 1.1 jmcneill
155 1.1 jmcneill const struct cdevsw pad_cdevsw = {
156 1.71 riastrad .d_open = pad_open,
157 1.71 riastrad .d_close = noclose,
158 1.71 riastrad .d_read = noread,
159 1.62 isaki .d_write = nowrite,
160 1.62 isaki .d_ioctl = noioctl,
161 1.62 isaki .d_stop = nostop,
162 1.62 isaki .d_tty = notty,
163 1.62 isaki .d_poll = nopoll,
164 1.62 isaki .d_mmap = nommap,
165 1.62 isaki .d_kqfilter = nokqfilter,
166 1.62 isaki .d_discard = nodiscard,
167 1.62 isaki .d_flag = D_OTHER | D_MPSAFE,
168 1.1 jmcneill };
169 1.1 jmcneill
170 1.57 nakayama const struct fileops pad_fileops = {
171 1.62 isaki .fo_name = "pad",
172 1.62 isaki .fo_read = fops_pad_read,
173 1.62 isaki .fo_write = fops_pad_write,
174 1.62 isaki .fo_ioctl = fops_pad_ioctl,
175 1.62 isaki .fo_fcntl = fnullop_fcntl,
176 1.62 isaki .fo_stat = fops_pad_stat,
177 1.62 isaki .fo_poll = fops_pad_poll,
178 1.62 isaki .fo_close = fops_pad_close,
179 1.62 isaki .fo_mmap = fops_pad_mmap,
180 1.62 isaki .fo_kqfilter = fops_pad_kqfilter,
181 1.62 isaki .fo_restart = fnullop_restart
182 1.57 nakayama };
183 1.57 nakayama
184 1.62 isaki CFATTACH_DECL2_NEW(pad, sizeof(struct pad_softc),
185 1.62 isaki pad_match, pad_attach, pad_detach,
186 1.5 dyoung NULL, NULL, pad_childdet);
187 1.1 jmcneill
188 1.1 jmcneill void
189 1.1 jmcneill padattach(int n)
190 1.1 jmcneill {
191 1.57 nakayama int error;
192 1.1 jmcneill
193 1.57 nakayama error = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
194 1.57 nakayama if (error) {
195 1.1 jmcneill aprint_error("%s: couldn't register cfattach: %d\n",
196 1.57 nakayama pad_cd.cd_name, error);
197 1.1 jmcneill config_cfdriver_detach(&pad_cd);
198 1.1 jmcneill return;
199 1.1 jmcneill }
200 1.1 jmcneill }
201 1.1 jmcneill
202 1.1 jmcneill static int
203 1.62 isaki pad_match(device_t parent, cfdata_t data, void *opaque)
204 1.62 isaki {
205 1.62 isaki
206 1.62 isaki return 1;
207 1.62 isaki }
208 1.62 isaki
209 1.62 isaki static void
210 1.62 isaki pad_attach(device_t parent, device_t self, void *opaque)
211 1.62 isaki {
212 1.68 riastrad struct pad_softc *sc = device_private(self);
213 1.62 isaki
214 1.71 riastrad KASSERT(KERNEL_LOCKED_P());
215 1.71 riastrad
216 1.62 isaki aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
217 1.68 riastrad
218 1.68 riastrad sc->sc_dev = self;
219 1.68 riastrad
220 1.68 riastrad cv_init(&sc->sc_condvar, device_xname(sc->sc_dev));
221 1.68 riastrad mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
222 1.69 riastrad mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SOFTCLOCK);
223 1.69 riastrad callout_init(&sc->sc_pcallout, CALLOUT_MPSAFE);
224 1.69 riastrad callout_setfunc(&sc->sc_pcallout, pad_done_output, sc);
225 1.68 riastrad
226 1.68 riastrad sc->sc_swvol = 255;
227 1.68 riastrad sc->sc_buflen = 0;
228 1.68 riastrad sc->sc_rpos = sc->sc_wpos = 0;
229 1.68 riastrad sc->sc_audiodev = audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);
230 1.68 riastrad
231 1.68 riastrad if (!pmf_device_register(sc->sc_dev, NULL, NULL))
232 1.68 riastrad aprint_error_dev(sc->sc_dev,
233 1.68 riastrad "couldn't establish power handler\n");
234 1.71 riastrad
235 1.71 riastrad sc->sc_open = 1;
236 1.62 isaki }
237 1.62 isaki
238 1.62 isaki static int
239 1.62 isaki pad_detach(device_t self, int flags)
240 1.62 isaki {
241 1.68 riastrad struct pad_softc *sc = device_private(self);
242 1.62 isaki int cmaj, mn;
243 1.68 riastrad int error;
244 1.68 riastrad
245 1.71 riastrad KASSERT(KERNEL_LOCKED_P());
246 1.71 riastrad
247 1.71 riastrad /* Prevent detach without going through close -- e.g., drvctl. */
248 1.71 riastrad if (sc->sc_open)
249 1.71 riastrad return EBUSY;
250 1.71 riastrad
251 1.68 riastrad error = config_detach_children(self, flags);
252 1.68 riastrad if (error)
253 1.68 riastrad return error;
254 1.62 isaki
255 1.62 isaki cmaj = cdevsw_lookup_major(&pad_cdevsw);
256 1.62 isaki mn = device_unit(sc->sc_dev);
257 1.68 riastrad vdevgone(cmaj, mn, mn, VCHR);
258 1.68 riastrad
259 1.68 riastrad pmf_device_deregister(sc->sc_dev);
260 1.68 riastrad
261 1.72 riastrad callout_destroy(&sc->sc_pcallout);
262 1.68 riastrad mutex_destroy(&sc->sc_lock);
263 1.68 riastrad mutex_destroy(&sc->sc_intr_lock);
264 1.68 riastrad cv_destroy(&sc->sc_condvar);
265 1.62 isaki
266 1.62 isaki return 0;
267 1.62 isaki }
268 1.62 isaki
269 1.62 isaki static void
270 1.62 isaki pad_childdet(device_t self, device_t child)
271 1.62 isaki {
272 1.62 isaki struct pad_softc *sc = device_private(self);
273 1.62 isaki
274 1.73 riastrad KASSERT(KERNEL_LOCKED_P());
275 1.73 riastrad
276 1.68 riastrad if (child == sc->sc_audiodev)
277 1.68 riastrad sc->sc_audiodev = NULL;
278 1.62 isaki }
279 1.62 isaki
280 1.62 isaki static int
281 1.62 isaki pad_add_block(struct pad_softc *sc, uint8_t *blk, int blksize)
282 1.1 jmcneill {
283 1.1 jmcneill int l;
284 1.1 jmcneill
285 1.73 riastrad KASSERT(blksize >= 0);
286 1.73 riastrad KASSERT(mutex_owned(&sc->sc_intr_lock));
287 1.73 riastrad
288 1.73 riastrad if (blksize > PAD_BUFSIZE ||
289 1.73 riastrad sc->sc_buflen > PAD_BUFSIZE - (unsigned)blksize)
290 1.1 jmcneill return ENOBUFS;
291 1.1 jmcneill
292 1.1 jmcneill if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
293 1.1 jmcneill memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
294 1.1 jmcneill else {
295 1.1 jmcneill l = PAD_BUFSIZE - sc->sc_wpos;
296 1.1 jmcneill memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
297 1.1 jmcneill memcpy(sc->sc_audiobuf, blk + l, blksize - l);
298 1.1 jmcneill }
299 1.1 jmcneill
300 1.1 jmcneill sc->sc_wpos += blksize;
301 1.59 isaki if (sc->sc_wpos >= PAD_BUFSIZE)
302 1.1 jmcneill sc->sc_wpos -= PAD_BUFSIZE;
303 1.1 jmcneill
304 1.1 jmcneill sc->sc_buflen += blksize;
305 1.73 riastrad cv_broadcast(&sc->sc_condvar);
306 1.1 jmcneill
307 1.1 jmcneill return 0;
308 1.1 jmcneill }
309 1.1 jmcneill
310 1.1 jmcneill static int
311 1.73 riastrad pad_get_block(struct pad_softc *sc, pad_block_t *pb, int maxblksize)
312 1.1 jmcneill {
313 1.73 riastrad int l, blksize, error;
314 1.1 jmcneill
315 1.73 riastrad KASSERT(maxblksize > 0);
316 1.73 riastrad KASSERT(mutex_owned(&sc->sc_intr_lock));
317 1.73 riastrad
318 1.73 riastrad while (sc->sc_buflen == 0) {
319 1.73 riastrad DPRINTF("%s: wait\n", __func__);
320 1.73 riastrad error = cv_wait_sig(&sc->sc_condvar, &sc->sc_intr_lock);
321 1.73 riastrad DPRINTF("%s: wake up %d\n", __func__, err);
322 1.73 riastrad if (error)
323 1.73 riastrad return error;
324 1.73 riastrad }
325 1.73 riastrad blksize = uimin(maxblksize, sc->sc_buflen);
326 1.1 jmcneill
327 1.1 jmcneill pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
328 1.1 jmcneill if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
329 1.1 jmcneill pb->pb_len = blksize;
330 1.1 jmcneill sc->sc_rpos += blksize;
331 1.1 jmcneill } else {
332 1.1 jmcneill l = PAD_BUFSIZE - sc->sc_rpos;
333 1.1 jmcneill pb->pb_len = l;
334 1.1 jmcneill sc->sc_rpos = 0;
335 1.1 jmcneill }
336 1.1 jmcneill sc->sc_buflen -= pb->pb_len;
337 1.1 jmcneill
338 1.1 jmcneill return 0;
339 1.1 jmcneill }
340 1.1 jmcneill
341 1.71 riastrad static int
342 1.71 riastrad pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
343 1.57 nakayama {
344 1.71 riastrad struct file *fp = NULL;
345 1.71 riastrad device_t self;
346 1.71 riastrad struct pad_softc *sc = NULL;
347 1.71 riastrad cfdata_t cf = NULL;
348 1.71 riastrad int error, fd;
349 1.71 riastrad
350 1.71 riastrad error = fd_allocfile(&fp, &fd);
351 1.71 riastrad if (error)
352 1.71 riastrad goto out;
353 1.57 nakayama
354 1.71 riastrad cf = kmem_alloc(sizeof(*cf), KM_SLEEP);
355 1.57 nakayama cf->cf_name = pad_cd.cd_name;
356 1.57 nakayama cf->cf_atname = pad_cd.cd_name;
357 1.71 riastrad cf->cf_unit = 0;
358 1.57 nakayama cf->cf_fstate = FSTATE_STAR;
359 1.57 nakayama
360 1.71 riastrad self = config_attach_pseudo(cf);
361 1.71 riastrad if (self == NULL) {
362 1.71 riastrad error = ENXIO;
363 1.71 riastrad goto out;
364 1.71 riastrad }
365 1.71 riastrad sc = device_private(self);
366 1.71 riastrad KASSERT(sc->sc_dev == self);
367 1.71 riastrad cf = NULL;
368 1.71 riastrad
369 1.71 riastrad error = fd_clone(fp, fd, flags, &pad_fileops, sc);
370 1.71 riastrad KASSERT(error == EMOVEFD);
371 1.71 riastrad fp = NULL;
372 1.71 riastrad sc = NULL;
373 1.71 riastrad
374 1.71 riastrad out: if (sc)
375 1.71 riastrad pad_close(sc);
376 1.71 riastrad if (cf)
377 1.71 riastrad kmem_free(cf, sizeof(*cf));
378 1.71 riastrad if (fp)
379 1.71 riastrad fd_abort(curproc, fp, fd);
380 1.57 nakayama return error;
381 1.1 jmcneill }
382 1.1 jmcneill
383 1.71 riastrad static void
384 1.57 nakayama pad_close(struct pad_softc *sc)
385 1.41 nat {
386 1.71 riastrad device_t self = sc->sc_dev;
387 1.71 riastrad cfdata_t cf = device_cfdata(self);
388 1.50 nat
389 1.75 riastrad /*
390 1.75 riastrad * XXX This is not quite enough to prevent racing with drvctl
391 1.75 riastrad * detach. What can happen:
392 1.75 riastrad *
393 1.75 riastrad * cpu0 cpu1
394 1.75 riastrad *
395 1.75 riastrad * pad_close
396 1.75 riastrad * take kernel lock
397 1.75 riastrad * sc->sc_open = 0
398 1.75 riastrad * drop kernel lock
399 1.75 riastrad * wait for config_misc_lock
400 1.75 riastrad * drvctl detach
401 1.75 riastrad * take kernel lock
402 1.75 riastrad * drop kernel lock
403 1.75 riastrad * wait for config_misc_lock
404 1.75 riastrad * retake kernel lock
405 1.75 riastrad * drop config_misc_lock
406 1.75 riastrad * take config_misc_lock
407 1.75 riastrad * wait for kernel lock
408 1.75 riastrad * pad_detach (sc_open=0 already)
409 1.75 riastrad * free device
410 1.75 riastrad * drop kernel lock
411 1.75 riastrad * use device after free
412 1.75 riastrad *
413 1.75 riastrad * We need a way to grab a reference to the device so it won't
414 1.75 riastrad * be freed until we're done -- it's OK if we config_detach
415 1.75 riastrad * twice as long as it's idempotent, but not OK if the first
416 1.75 riastrad * config_detach frees the struct device before the second one
417 1.75 riastrad * has finished handling it.
418 1.75 riastrad */
419 1.71 riastrad KERNEL_LOCK(1, NULL);
420 1.71 riastrad KASSERT(sc->sc_open);
421 1.71 riastrad sc->sc_open = 0;
422 1.71 riastrad (void)config_detach(self, DETACH_FORCE);
423 1.71 riastrad KERNEL_UNLOCK_ONE(NULL);
424 1.41 nat
425 1.71 riastrad kmem_free(cf, sizeof(*cf));
426 1.57 nakayama }
427 1.57 nakayama
428 1.57 nakayama static int
429 1.57 nakayama fops_pad_close(struct file *fp)
430 1.57 nakayama {
431 1.68 riastrad struct pad_softc *sc = fp->f_pad;
432 1.57 nakayama
433 1.68 riastrad pad_close(sc);
434 1.68 riastrad
435 1.68 riastrad return 0;
436 1.57 nakayama }
437 1.1 jmcneill
438 1.57 nakayama static int
439 1.62 isaki fops_pad_poll(struct file *fp, int events)
440 1.57 nakayama {
441 1.62 isaki
442 1.63 isaki return POLLERR;
443 1.57 nakayama }
444 1.57 nakayama
445 1.57 nakayama static int
446 1.62 isaki fops_pad_kqfilter(struct file *fp, struct knote *kn)
447 1.57 nakayama {
448 1.68 riastrad struct pad_softc *sc = fp->f_pad;
449 1.57 nakayama dev_t dev;
450 1.57 nakayama
451 1.62 isaki dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
452 1.62 isaki device_unit(sc->sc_dev));
453 1.57 nakayama
454 1.57 nakayama return seltrue_kqfilter(dev, kn);
455 1.57 nakayama }
456 1.57 nakayama
457 1.57 nakayama static int
458 1.62 isaki fops_pad_ioctl(struct file *fp, u_long cmd, void *data)
459 1.57 nakayama {
460 1.62 isaki
461 1.57 nakayama return ENODEV;
462 1.57 nakayama }
463 1.1 jmcneill
464 1.57 nakayama static int
465 1.62 isaki fops_pad_stat(struct file *fp, struct stat *st)
466 1.57 nakayama {
467 1.68 riastrad struct pad_softc *sc = fp->f_pad;
468 1.57 nakayama
469 1.57 nakayama memset(st, 0, sizeof(*st));
470 1.57 nakayama
471 1.62 isaki st->st_dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
472 1.62 isaki device_unit(sc->sc_dev));
473 1.57 nakayama
474 1.57 nakayama st->st_uid = kauth_cred_geteuid(fp->f_cred);
475 1.57 nakayama st->st_gid = kauth_cred_getegid(fp->f_cred);
476 1.57 nakayama st->st_mode = S_IFCHR;
477 1.57 nakayama
478 1.54 nakayama return 0;
479 1.41 nat }
480 1.39 nat
481 1.57 nakayama static int
482 1.62 isaki fops_pad_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
483 1.62 isaki int *advicep, struct uvm_object **uobjp, int *maxprotp)
484 1.57 nakayama {
485 1.62 isaki
486 1.57 nakayama return 1;
487 1.57 nakayama }
488 1.57 nakayama
489 1.57 nakayama static int
490 1.57 nakayama fops_pad_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
491 1.62 isaki int ioflag)
492 1.57 nakayama {
493 1.68 riastrad struct pad_softc *sc = fp->f_pad;
494 1.39 nat
495 1.57 nakayama return pad_read(sc, offp, uio, cred, ioflag);
496 1.1 jmcneill }
497 1.1 jmcneill
498 1.57 nakayama static int
499 1.57 nakayama pad_read(struct pad_softc *sc, off_t *offp, struct uio *uio, kauth_cred_t cred,
500 1.62 isaki int ioflag)
501 1.1 jmcneill {
502 1.1 jmcneill pad_block_t pb;
503 1.59 isaki int err;
504 1.1 jmcneill
505 1.1 jmcneill err = 0;
506 1.59 isaki DPRINTF("%s: resid=%zu\n", __func__, uio->uio_resid);
507 1.73 riastrad while (uio->uio_resid > 0) {
508 1.69 riastrad mutex_enter(&sc->sc_intr_lock);
509 1.73 riastrad err = pad_get_block(sc, &pb, uio->uio_resid);
510 1.69 riastrad mutex_exit(&sc->sc_intr_lock);
511 1.59 isaki if (err)
512 1.24 nat break;
513 1.73 riastrad
514 1.59 isaki DPRINTF("%s: move %d\n", __func__, pb.pb_len);
515 1.73 riastrad err = uiomove(pb.pb_ptr, pb.pb_len, uio);
516 1.73 riastrad if (err)
517 1.73 riastrad break;
518 1.1 jmcneill }
519 1.1 jmcneill
520 1.1 jmcneill return err;
521 1.1 jmcneill }
522 1.1 jmcneill
523 1.1 jmcneill static int
524 1.62 isaki fops_pad_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
525 1.62 isaki int ioflag)
526 1.57 nakayama {
527 1.62 isaki
528 1.57 nakayama return EOPNOTSUPP;
529 1.57 nakayama }
530 1.57 nakayama
531 1.57 nakayama static int
532 1.59 isaki pad_query_format(void *opaque, audio_format_query_t *afp)
533 1.26 nat {
534 1.26 nat
535 1.59 isaki return audio_query_format(pad_formats, PAD_NFORMATS, afp);
536 1.26 nat }
537 1.26 nat
538 1.26 nat static int
539 1.59 isaki pad_set_format(void *opaque, int setmode,
540 1.59 isaki const audio_params_t *play, const audio_params_t *rec,
541 1.59 isaki audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
542 1.1 jmcneill {
543 1.69 riastrad struct pad_softc *sc = opaque;
544 1.1 jmcneill
545 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
546 1.17 jmcneill
547 1.59 isaki /* XXX playback only */
548 1.59 isaki pfil->codec = pad_swvol_codec;
549 1.59 isaki pfil->context = sc;
550 1.2 jmcneill
551 1.1 jmcneill return 0;
552 1.1 jmcneill }
553 1.1 jmcneill
554 1.1 jmcneill static int
555 1.1 jmcneill pad_start_output(void *opaque, void *block, int blksize,
556 1.1 jmcneill void (*intr)(void *), void *intrarg)
557 1.1 jmcneill {
558 1.69 riastrad struct pad_softc *sc = opaque;
559 1.1 jmcneill int err;
560 1.59 isaki int ms;
561 1.1 jmcneill
562 1.59 isaki KASSERT(mutex_owned(&sc->sc_intr_lock));
563 1.1 jmcneill
564 1.1 jmcneill sc->sc_intr = intr;
565 1.1 jmcneill sc->sc_intrarg = intrarg;
566 1.1 jmcneill
567 1.59 isaki DPRINTF("%s: blksize=%d\n", __func__, blksize);
568 1.1 jmcneill err = pad_add_block(sc, block, blksize);
569 1.1 jmcneill
570 1.59 isaki ms = blksize * 1000 / PADCHAN / (PADPREC / NBBY) / PADFREQ;
571 1.59 isaki DPRINTF("%s: callout ms=%d\n", __func__, ms);
572 1.69 riastrad callout_schedule(&sc->sc_pcallout, mstohz(ms));
573 1.1 jmcneill
574 1.66 nia return err;
575 1.1 jmcneill }
576 1.1 jmcneill
577 1.1 jmcneill static int
578 1.1 jmcneill pad_halt_output(void *opaque)
579 1.1 jmcneill {
580 1.69 riastrad struct pad_softc *sc = opaque;
581 1.17 jmcneill
582 1.59 isaki DPRINTF("%s\n", __func__);
583 1.59 isaki KASSERT(mutex_owned(&sc->sc_intr_lock));
584 1.17 jmcneill
585 1.69 riastrad callout_halt(&sc->sc_pcallout, &sc->sc_intr_lock);
586 1.69 riastrad
587 1.1 jmcneill sc->sc_intr = NULL;
588 1.1 jmcneill sc->sc_intrarg = NULL;
589 1.1 jmcneill sc->sc_buflen = 0;
590 1.1 jmcneill sc->sc_rpos = sc->sc_wpos = 0;
591 1.1 jmcneill
592 1.1 jmcneill return 0;
593 1.1 jmcneill }
594 1.1 jmcneill
595 1.59 isaki static void
596 1.59 isaki pad_done_output(void *arg)
597 1.59 isaki {
598 1.69 riastrad struct pad_softc *sc = arg;
599 1.59 isaki
600 1.59 isaki DPRINTF("%s\n", __func__);
601 1.59 isaki
602 1.59 isaki mutex_enter(&sc->sc_intr_lock);
603 1.59 isaki (*sc->sc_intr)(sc->sc_intrarg);
604 1.59 isaki mutex_exit(&sc->sc_intr_lock);
605 1.59 isaki }
606 1.59 isaki
607 1.1 jmcneill static int
608 1.1 jmcneill pad_getdev(void *opaque, struct audio_device *ret)
609 1.1 jmcneill {
610 1.62 isaki
611 1.16 jmcneill strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
612 1.16 jmcneill strlcpy(ret->version, osrelease, sizeof(ret->version));
613 1.16 jmcneill strlcpy(ret->config, "pad", sizeof(ret->config));
614 1.1 jmcneill
615 1.1 jmcneill return 0;
616 1.1 jmcneill }
617 1.1 jmcneill
618 1.1 jmcneill static int
619 1.1 jmcneill pad_set_port(void *opaque, mixer_ctrl_t *mc)
620 1.1 jmcneill {
621 1.69 riastrad struct pad_softc *sc = opaque;
622 1.2 jmcneill
623 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
624 1.17 jmcneill
625 1.2 jmcneill switch (mc->dev) {
626 1.2 jmcneill case PAD_OUTPUT_MASTER_VOLUME:
627 1.2 jmcneill case PAD_INPUT_DAC_VOLUME:
628 1.58 nakayama if (mc->un.value.num_channels != 1)
629 1.58 nakayama return EINVAL;
630 1.2 jmcneill sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
631 1.2 jmcneill return 0;
632 1.2 jmcneill }
633 1.2 jmcneill
634 1.1 jmcneill return ENXIO;
635 1.1 jmcneill }
636 1.1 jmcneill
637 1.1 jmcneill static int
638 1.1 jmcneill pad_get_port(void *opaque, mixer_ctrl_t *mc)
639 1.1 jmcneill {
640 1.69 riastrad struct pad_softc *sc = opaque;
641 1.2 jmcneill
642 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
643 1.17 jmcneill
644 1.2 jmcneill switch (mc->dev) {
645 1.2 jmcneill case PAD_OUTPUT_MASTER_VOLUME:
646 1.2 jmcneill case PAD_INPUT_DAC_VOLUME:
647 1.58 nakayama if (mc->un.value.num_channels != 1)
648 1.58 nakayama return EINVAL;
649 1.2 jmcneill mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
650 1.2 jmcneill return 0;
651 1.2 jmcneill }
652 1.2 jmcneill
653 1.1 jmcneill return ENXIO;
654 1.1 jmcneill }
655 1.1 jmcneill
656 1.1 jmcneill static int
657 1.1 jmcneill pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
658 1.1 jmcneill {
659 1.69 riastrad struct pad_softc *sc __diagused = opaque;
660 1.2 jmcneill
661 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
662 1.17 jmcneill
663 1.2 jmcneill switch (di->index) {
664 1.2 jmcneill case PAD_OUTPUT_CLASS:
665 1.2 jmcneill di->mixer_class = PAD_OUTPUT_CLASS;
666 1.2 jmcneill strcpy(di->label.name, AudioCoutputs);
667 1.2 jmcneill di->type = AUDIO_MIXER_CLASS;
668 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
669 1.2 jmcneill return 0;
670 1.2 jmcneill case PAD_INPUT_CLASS:
671 1.2 jmcneill di->mixer_class = PAD_INPUT_CLASS;
672 1.2 jmcneill strcpy(di->label.name, AudioCinputs);
673 1.2 jmcneill di->type = AUDIO_MIXER_CLASS;
674 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
675 1.2 jmcneill return 0;
676 1.2 jmcneill case PAD_OUTPUT_MASTER_VOLUME:
677 1.2 jmcneill di->mixer_class = PAD_OUTPUT_CLASS;
678 1.2 jmcneill strcpy(di->label.name, AudioNmaster);
679 1.2 jmcneill di->type = AUDIO_MIXER_VALUE;
680 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
681 1.2 jmcneill di->un.v.num_channels = 1;
682 1.2 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
683 1.2 jmcneill return 0;
684 1.2 jmcneill case PAD_INPUT_DAC_VOLUME:
685 1.2 jmcneill di->mixer_class = PAD_INPUT_CLASS;
686 1.2 jmcneill strcpy(di->label.name, AudioNdac);
687 1.2 jmcneill di->type = AUDIO_MIXER_VALUE;
688 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
689 1.2 jmcneill di->un.v.num_channels = 1;
690 1.2 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
691 1.2 jmcneill return 0;
692 1.2 jmcneill }
693 1.2 jmcneill
694 1.1 jmcneill return ENXIO;
695 1.1 jmcneill }
696 1.1 jmcneill
697 1.1 jmcneill static int
698 1.1 jmcneill pad_get_props(void *opaque)
699 1.1 jmcneill {
700 1.17 jmcneill
701 1.59 isaki return AUDIO_PROP_PLAYBACK;
702 1.1 jmcneill }
703 1.13 ahoka
704 1.17 jmcneill static void
705 1.17 jmcneill pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
706 1.17 jmcneill {
707 1.69 riastrad struct pad_softc *sc = opaque;
708 1.17 jmcneill
709 1.17 jmcneill *intr = &sc->sc_intr_lock;
710 1.17 jmcneill *thread = &sc->sc_lock;
711 1.17 jmcneill }
712 1.17 jmcneill
713 1.22 jmcneill static void
714 1.59 isaki pad_swvol_codec(audio_filter_arg_t *arg)
715 1.22 jmcneill {
716 1.59 isaki struct pad_softc *sc = arg->context;
717 1.59 isaki const aint_t *src;
718 1.59 isaki aint_t *dst;
719 1.59 isaki u_int sample_count;
720 1.59 isaki u_int i;
721 1.59 isaki
722 1.59 isaki src = arg->src;
723 1.59 isaki dst = arg->dst;
724 1.59 isaki sample_count = arg->count * arg->srcfmt->channels;
725 1.59 isaki for (i = 0; i < sample_count; i++) {
726 1.59 isaki aint2_t v = (aint2_t)(*src++);
727 1.59 isaki v = v * sc->sc_swvol / 255;
728 1.59 isaki *dst++ = (aint_t)v;
729 1.59 isaki }
730 1.22 jmcneill }
731 1.22 jmcneill
732 1.44 pgoyette MODULE(MODULE_CLASS_DRIVER, pad, "audio");
733 1.44 pgoyette
734 1.13 ahoka #ifdef _MODULE
735 1.13 ahoka
736 1.57 nakayama #include "ioconf.c"
737 1.57 nakayama
738 1.57 nakayama devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
739 1.57 nakayama
740 1.57 nakayama /*
741 1.57 nakayama * We need our own version of cfattach since config(1)'s ioconf does not
742 1.57 nakayama * generate what we need
743 1.57 nakayama */
744 1.57 nakayama
745 1.57 nakayama static struct cfattach *pad_cfattachinit[] = { &pad_ca, NULL };
746 1.13 ahoka
747 1.57 nakayama static struct cfattachinit pad_cfattach[] = {
748 1.57 nakayama { "pad", pad_cfattachinit },
749 1.57 nakayama { NULL, NULL }
750 1.45 pgoyette };
751 1.47 pgoyette #endif
752 1.45 pgoyette
753 1.13 ahoka static int
754 1.13 ahoka pad_modcmd(modcmd_t cmd, void *arg)
755 1.13 ahoka {
756 1.44 pgoyette int error = 0;
757 1.13 ahoka
758 1.13 ahoka switch (cmd) {
759 1.13 ahoka case MODULE_CMD_INIT:
760 1.44 pgoyette #ifdef _MODULE
761 1.57 nakayama pad_cfattach[1] = cfattach_ioconf_pad[0];
762 1.57 nakayama error = config_init_component(cfdriver_ioconf_pad,
763 1.57 nakayama pad_cfattach, cfdata_ioconf_pad);
764 1.57 nakayama if (error)
765 1.44 pgoyette break;
766 1.13 ahoka
767 1.25 msaitoh error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
768 1.57 nakayama &pad_cdevsw, &cmajor);
769 1.13 ahoka if (error) {
770 1.57 nakayama config_fini_component(cfdriver_ioconf_pad,
771 1.57 nakayama pad_cfattach, cfdata_ioconf_pad);
772 1.44 pgoyette break;
773 1.13 ahoka }
774 1.13 ahoka
775 1.44 pgoyette #endif
776 1.57 nakayama break;
777 1.54 nakayama
778 1.13 ahoka case MODULE_CMD_FINI:
779 1.44 pgoyette #ifdef _MODULE
780 1.57 nakayama error = devsw_detach(NULL, &pad_cdevsw);
781 1.57 nakayama if (error)
782 1.57 nakayama break;
783 1.57 nakayama
784 1.57 nakayama error = config_fini_component(cfdriver_ioconf_pad,
785 1.57 nakayama pad_cfattach, cfdata_ioconf_pad);
786 1.13 ahoka if (error) {
787 1.57 nakayama devsw_attach(pad_cd.cd_name, NULL, &bmajor,
788 1.57 nakayama &pad_cdevsw, &cmajor);
789 1.44 pgoyette break;
790 1.13 ahoka }
791 1.44 pgoyette #endif
792 1.57 nakayama break;
793 1.54 nakayama
794 1.13 ahoka default:
795 1.44 pgoyette error = ENOTTY;
796 1.13 ahoka }
797 1.44 pgoyette
798 1.44 pgoyette return error;
799 1.13 ahoka }
800