pad.c revision 1.72 1 1.72 riastrad /* $NetBSD: pad.c,v 1.72 2021/06/14 10:21:21 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.72 riastrad __KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.72 2021/06/14 10:21:21 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.68 riastrad if (child == sc->sc_audiodev)
275 1.68 riastrad sc->sc_audiodev = NULL;
276 1.62 isaki }
277 1.62 isaki
278 1.62 isaki static int
279 1.62 isaki pad_add_block(struct pad_softc *sc, uint8_t *blk, int blksize)
280 1.1 jmcneill {
281 1.1 jmcneill int l;
282 1.1 jmcneill
283 1.1 jmcneill if (sc->sc_buflen + blksize > PAD_BUFSIZE)
284 1.1 jmcneill return ENOBUFS;
285 1.1 jmcneill
286 1.1 jmcneill if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
287 1.1 jmcneill memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
288 1.1 jmcneill else {
289 1.1 jmcneill l = PAD_BUFSIZE - sc->sc_wpos;
290 1.1 jmcneill memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
291 1.1 jmcneill memcpy(sc->sc_audiobuf, blk + l, blksize - l);
292 1.1 jmcneill }
293 1.1 jmcneill
294 1.1 jmcneill sc->sc_wpos += blksize;
295 1.59 isaki if (sc->sc_wpos >= PAD_BUFSIZE)
296 1.1 jmcneill sc->sc_wpos -= PAD_BUFSIZE;
297 1.1 jmcneill
298 1.1 jmcneill sc->sc_buflen += blksize;
299 1.1 jmcneill
300 1.1 jmcneill return 0;
301 1.1 jmcneill }
302 1.1 jmcneill
303 1.1 jmcneill static int
304 1.62 isaki pad_get_block(struct pad_softc *sc, pad_block_t *pb, int blksize)
305 1.1 jmcneill {
306 1.1 jmcneill int l;
307 1.1 jmcneill
308 1.1 jmcneill KASSERT(pb != NULL);
309 1.1 jmcneill
310 1.1 jmcneill pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
311 1.1 jmcneill if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
312 1.1 jmcneill pb->pb_len = blksize;
313 1.1 jmcneill sc->sc_rpos += blksize;
314 1.1 jmcneill } else {
315 1.1 jmcneill l = PAD_BUFSIZE - sc->sc_rpos;
316 1.1 jmcneill pb->pb_len = l;
317 1.1 jmcneill sc->sc_rpos = 0;
318 1.1 jmcneill }
319 1.1 jmcneill sc->sc_buflen -= pb->pb_len;
320 1.1 jmcneill
321 1.1 jmcneill return 0;
322 1.1 jmcneill }
323 1.1 jmcneill
324 1.71 riastrad static int
325 1.71 riastrad pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
326 1.57 nakayama {
327 1.71 riastrad struct file *fp = NULL;
328 1.71 riastrad device_t self;
329 1.71 riastrad struct pad_softc *sc = NULL;
330 1.71 riastrad cfdata_t cf = NULL;
331 1.71 riastrad int error, fd;
332 1.71 riastrad
333 1.71 riastrad error = fd_allocfile(&fp, &fd);
334 1.71 riastrad if (error)
335 1.71 riastrad goto out;
336 1.57 nakayama
337 1.71 riastrad cf = kmem_alloc(sizeof(*cf), KM_SLEEP);
338 1.57 nakayama cf->cf_name = pad_cd.cd_name;
339 1.57 nakayama cf->cf_atname = pad_cd.cd_name;
340 1.71 riastrad cf->cf_unit = 0;
341 1.57 nakayama cf->cf_fstate = FSTATE_STAR;
342 1.57 nakayama
343 1.71 riastrad self = config_attach_pseudo(cf);
344 1.71 riastrad if (self == NULL) {
345 1.71 riastrad error = ENXIO;
346 1.71 riastrad goto out;
347 1.71 riastrad }
348 1.71 riastrad sc = device_private(self);
349 1.71 riastrad KASSERT(sc->sc_dev == self);
350 1.71 riastrad cf = NULL;
351 1.71 riastrad
352 1.71 riastrad error = fd_clone(fp, fd, flags, &pad_fileops, sc);
353 1.71 riastrad KASSERT(error == EMOVEFD);
354 1.71 riastrad fp = NULL;
355 1.71 riastrad sc = NULL;
356 1.71 riastrad
357 1.71 riastrad out: if (sc)
358 1.71 riastrad pad_close(sc);
359 1.71 riastrad if (cf)
360 1.71 riastrad kmem_free(cf, sizeof(*cf));
361 1.71 riastrad if (fp)
362 1.71 riastrad fd_abort(curproc, fp, fd);
363 1.57 nakayama return error;
364 1.1 jmcneill }
365 1.1 jmcneill
366 1.71 riastrad static void
367 1.57 nakayama pad_close(struct pad_softc *sc)
368 1.41 nat {
369 1.71 riastrad device_t self = sc->sc_dev;
370 1.71 riastrad cfdata_t cf = device_cfdata(self);
371 1.50 nat
372 1.71 riastrad KERNEL_LOCK(1, NULL);
373 1.71 riastrad KASSERT(sc->sc_open);
374 1.71 riastrad sc->sc_open = 0;
375 1.71 riastrad (void)config_detach(self, DETACH_FORCE);
376 1.71 riastrad KERNEL_UNLOCK_ONE(NULL);
377 1.41 nat
378 1.71 riastrad kmem_free(cf, sizeof(*cf));
379 1.57 nakayama }
380 1.57 nakayama
381 1.57 nakayama static int
382 1.57 nakayama fops_pad_close(struct file *fp)
383 1.57 nakayama {
384 1.68 riastrad struct pad_softc *sc = fp->f_pad;
385 1.57 nakayama
386 1.68 riastrad pad_close(sc);
387 1.68 riastrad
388 1.68 riastrad return 0;
389 1.57 nakayama }
390 1.1 jmcneill
391 1.57 nakayama static int
392 1.62 isaki fops_pad_poll(struct file *fp, int events)
393 1.57 nakayama {
394 1.62 isaki
395 1.63 isaki return POLLERR;
396 1.57 nakayama }
397 1.57 nakayama
398 1.57 nakayama static int
399 1.62 isaki fops_pad_kqfilter(struct file *fp, struct knote *kn)
400 1.57 nakayama {
401 1.68 riastrad struct pad_softc *sc = fp->f_pad;
402 1.57 nakayama dev_t dev;
403 1.57 nakayama
404 1.62 isaki dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
405 1.62 isaki device_unit(sc->sc_dev));
406 1.57 nakayama
407 1.57 nakayama return seltrue_kqfilter(dev, kn);
408 1.57 nakayama }
409 1.57 nakayama
410 1.57 nakayama static int
411 1.62 isaki fops_pad_ioctl(struct file *fp, u_long cmd, void *data)
412 1.57 nakayama {
413 1.62 isaki
414 1.57 nakayama return ENODEV;
415 1.57 nakayama }
416 1.1 jmcneill
417 1.57 nakayama static int
418 1.62 isaki fops_pad_stat(struct file *fp, struct stat *st)
419 1.57 nakayama {
420 1.68 riastrad struct pad_softc *sc = fp->f_pad;
421 1.57 nakayama
422 1.57 nakayama memset(st, 0, sizeof(*st));
423 1.57 nakayama
424 1.62 isaki st->st_dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
425 1.62 isaki device_unit(sc->sc_dev));
426 1.57 nakayama
427 1.57 nakayama st->st_uid = kauth_cred_geteuid(fp->f_cred);
428 1.57 nakayama st->st_gid = kauth_cred_getegid(fp->f_cred);
429 1.57 nakayama st->st_mode = S_IFCHR;
430 1.57 nakayama
431 1.54 nakayama return 0;
432 1.41 nat }
433 1.39 nat
434 1.57 nakayama static int
435 1.62 isaki fops_pad_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
436 1.62 isaki int *advicep, struct uvm_object **uobjp, int *maxprotp)
437 1.57 nakayama {
438 1.62 isaki
439 1.57 nakayama return 1;
440 1.57 nakayama }
441 1.57 nakayama
442 1.57 nakayama static int
443 1.57 nakayama fops_pad_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
444 1.62 isaki int ioflag)
445 1.57 nakayama {
446 1.68 riastrad struct pad_softc *sc = fp->f_pad;
447 1.39 nat
448 1.57 nakayama return pad_read(sc, offp, uio, cred, ioflag);
449 1.1 jmcneill }
450 1.1 jmcneill
451 1.57 nakayama static int
452 1.57 nakayama pad_read(struct pad_softc *sc, off_t *offp, struct uio *uio, kauth_cred_t cred,
453 1.62 isaki int ioflag)
454 1.1 jmcneill {
455 1.1 jmcneill pad_block_t pb;
456 1.59 isaki int len;
457 1.59 isaki int err;
458 1.1 jmcneill
459 1.1 jmcneill err = 0;
460 1.59 isaki DPRINTF("%s: resid=%zu\n", __func__, uio->uio_resid);
461 1.27 nat while (uio->uio_resid > 0 && !err) {
462 1.69 riastrad mutex_enter(&sc->sc_intr_lock);
463 1.59 isaki if (sc->sc_buflen == 0) {
464 1.59 isaki DPRINTF("%s: wait\n", __func__);
465 1.69 riastrad err = cv_wait_sig(&sc->sc_condvar, &sc->sc_intr_lock);
466 1.59 isaki DPRINTF("%s: wake up %d\n", __func__, err);
467 1.69 riastrad mutex_exit(&sc->sc_intr_lock);
468 1.59 isaki if (err) {
469 1.59 isaki if (err == ERESTART)
470 1.59 isaki err = EINTR;
471 1.59 isaki break;
472 1.35 nat }
473 1.59 isaki if (sc->sc_buflen == 0)
474 1.59 isaki break;
475 1.17 jmcneill continue;
476 1.17 jmcneill }
477 1.1 jmcneill
478 1.59 isaki len = uimin(uio->uio_resid, sc->sc_buflen);
479 1.59 isaki err = pad_get_block(sc, &pb, len);
480 1.69 riastrad mutex_exit(&sc->sc_intr_lock);
481 1.59 isaki if (err)
482 1.24 nat break;
483 1.59 isaki DPRINTF("%s: move %d\n", __func__, pb.pb_len);
484 1.59 isaki uiomove(pb.pb_ptr, pb.pb_len, uio);
485 1.1 jmcneill }
486 1.1 jmcneill
487 1.1 jmcneill return err;
488 1.1 jmcneill }
489 1.1 jmcneill
490 1.1 jmcneill static int
491 1.62 isaki fops_pad_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
492 1.62 isaki int ioflag)
493 1.57 nakayama {
494 1.62 isaki
495 1.57 nakayama return EOPNOTSUPP;
496 1.57 nakayama }
497 1.57 nakayama
498 1.57 nakayama static int
499 1.59 isaki pad_query_format(void *opaque, audio_format_query_t *afp)
500 1.26 nat {
501 1.26 nat
502 1.59 isaki return audio_query_format(pad_formats, PAD_NFORMATS, afp);
503 1.26 nat }
504 1.26 nat
505 1.26 nat static int
506 1.59 isaki pad_set_format(void *opaque, int setmode,
507 1.59 isaki const audio_params_t *play, const audio_params_t *rec,
508 1.59 isaki audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
509 1.1 jmcneill {
510 1.69 riastrad struct pad_softc *sc = opaque;
511 1.1 jmcneill
512 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
513 1.17 jmcneill
514 1.59 isaki /* XXX playback only */
515 1.59 isaki pfil->codec = pad_swvol_codec;
516 1.59 isaki pfil->context = sc;
517 1.2 jmcneill
518 1.1 jmcneill return 0;
519 1.1 jmcneill }
520 1.1 jmcneill
521 1.1 jmcneill static int
522 1.1 jmcneill pad_start_output(void *opaque, void *block, int blksize,
523 1.1 jmcneill void (*intr)(void *), void *intrarg)
524 1.1 jmcneill {
525 1.69 riastrad struct pad_softc *sc = opaque;
526 1.1 jmcneill int err;
527 1.59 isaki int ms;
528 1.1 jmcneill
529 1.59 isaki KASSERT(mutex_owned(&sc->sc_intr_lock));
530 1.1 jmcneill
531 1.1 jmcneill sc->sc_intr = intr;
532 1.1 jmcneill sc->sc_intrarg = intrarg;
533 1.1 jmcneill sc->sc_blksize = blksize;
534 1.1 jmcneill
535 1.59 isaki DPRINTF("%s: blksize=%d\n", __func__, blksize);
536 1.1 jmcneill err = pad_add_block(sc, block, blksize);
537 1.59 isaki cv_broadcast(&sc->sc_condvar);
538 1.1 jmcneill
539 1.59 isaki ms = blksize * 1000 / PADCHAN / (PADPREC / NBBY) / PADFREQ;
540 1.59 isaki DPRINTF("%s: callout ms=%d\n", __func__, ms);
541 1.69 riastrad callout_schedule(&sc->sc_pcallout, mstohz(ms));
542 1.1 jmcneill
543 1.66 nia return err;
544 1.1 jmcneill }
545 1.1 jmcneill
546 1.1 jmcneill static int
547 1.1 jmcneill pad_halt_output(void *opaque)
548 1.1 jmcneill {
549 1.69 riastrad struct pad_softc *sc = opaque;
550 1.17 jmcneill
551 1.59 isaki DPRINTF("%s\n", __func__);
552 1.59 isaki KASSERT(mutex_owned(&sc->sc_intr_lock));
553 1.17 jmcneill
554 1.69 riastrad callout_halt(&sc->sc_pcallout, &sc->sc_intr_lock);
555 1.69 riastrad
556 1.1 jmcneill sc->sc_intr = NULL;
557 1.1 jmcneill sc->sc_intrarg = NULL;
558 1.1 jmcneill sc->sc_buflen = 0;
559 1.1 jmcneill sc->sc_rpos = sc->sc_wpos = 0;
560 1.69 riastrad cv_broadcast(&sc->sc_condvar);
561 1.1 jmcneill
562 1.1 jmcneill return 0;
563 1.1 jmcneill }
564 1.1 jmcneill
565 1.59 isaki static void
566 1.59 isaki pad_done_output(void *arg)
567 1.59 isaki {
568 1.69 riastrad struct pad_softc *sc = arg;
569 1.59 isaki
570 1.59 isaki DPRINTF("%s\n", __func__);
571 1.59 isaki
572 1.59 isaki mutex_enter(&sc->sc_intr_lock);
573 1.59 isaki (*sc->sc_intr)(sc->sc_intrarg);
574 1.59 isaki mutex_exit(&sc->sc_intr_lock);
575 1.59 isaki }
576 1.59 isaki
577 1.1 jmcneill static int
578 1.1 jmcneill pad_getdev(void *opaque, struct audio_device *ret)
579 1.1 jmcneill {
580 1.62 isaki
581 1.16 jmcneill strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
582 1.16 jmcneill strlcpy(ret->version, osrelease, sizeof(ret->version));
583 1.16 jmcneill strlcpy(ret->config, "pad", sizeof(ret->config));
584 1.1 jmcneill
585 1.1 jmcneill return 0;
586 1.1 jmcneill }
587 1.1 jmcneill
588 1.1 jmcneill static int
589 1.1 jmcneill pad_set_port(void *opaque, mixer_ctrl_t *mc)
590 1.1 jmcneill {
591 1.69 riastrad struct pad_softc *sc = opaque;
592 1.2 jmcneill
593 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
594 1.17 jmcneill
595 1.2 jmcneill switch (mc->dev) {
596 1.2 jmcneill case PAD_OUTPUT_MASTER_VOLUME:
597 1.2 jmcneill case PAD_INPUT_DAC_VOLUME:
598 1.58 nakayama if (mc->un.value.num_channels != 1)
599 1.58 nakayama return EINVAL;
600 1.2 jmcneill sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
601 1.2 jmcneill return 0;
602 1.2 jmcneill }
603 1.2 jmcneill
604 1.1 jmcneill return ENXIO;
605 1.1 jmcneill }
606 1.1 jmcneill
607 1.1 jmcneill static int
608 1.1 jmcneill pad_get_port(void *opaque, mixer_ctrl_t *mc)
609 1.1 jmcneill {
610 1.69 riastrad struct pad_softc *sc = opaque;
611 1.2 jmcneill
612 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
613 1.17 jmcneill
614 1.2 jmcneill switch (mc->dev) {
615 1.2 jmcneill case PAD_OUTPUT_MASTER_VOLUME:
616 1.2 jmcneill case PAD_INPUT_DAC_VOLUME:
617 1.58 nakayama if (mc->un.value.num_channels != 1)
618 1.58 nakayama return EINVAL;
619 1.2 jmcneill mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
620 1.2 jmcneill return 0;
621 1.2 jmcneill }
622 1.2 jmcneill
623 1.1 jmcneill return ENXIO;
624 1.1 jmcneill }
625 1.1 jmcneill
626 1.1 jmcneill static int
627 1.1 jmcneill pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
628 1.1 jmcneill {
629 1.69 riastrad struct pad_softc *sc __diagused = opaque;
630 1.2 jmcneill
631 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
632 1.17 jmcneill
633 1.2 jmcneill switch (di->index) {
634 1.2 jmcneill case PAD_OUTPUT_CLASS:
635 1.2 jmcneill di->mixer_class = PAD_OUTPUT_CLASS;
636 1.2 jmcneill strcpy(di->label.name, AudioCoutputs);
637 1.2 jmcneill di->type = AUDIO_MIXER_CLASS;
638 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
639 1.2 jmcneill return 0;
640 1.2 jmcneill case PAD_INPUT_CLASS:
641 1.2 jmcneill di->mixer_class = PAD_INPUT_CLASS;
642 1.2 jmcneill strcpy(di->label.name, AudioCinputs);
643 1.2 jmcneill di->type = AUDIO_MIXER_CLASS;
644 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
645 1.2 jmcneill return 0;
646 1.2 jmcneill case PAD_OUTPUT_MASTER_VOLUME:
647 1.2 jmcneill di->mixer_class = PAD_OUTPUT_CLASS;
648 1.2 jmcneill strcpy(di->label.name, AudioNmaster);
649 1.2 jmcneill di->type = AUDIO_MIXER_VALUE;
650 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
651 1.2 jmcneill di->un.v.num_channels = 1;
652 1.2 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
653 1.2 jmcneill return 0;
654 1.2 jmcneill case PAD_INPUT_DAC_VOLUME:
655 1.2 jmcneill di->mixer_class = PAD_INPUT_CLASS;
656 1.2 jmcneill strcpy(di->label.name, AudioNdac);
657 1.2 jmcneill di->type = AUDIO_MIXER_VALUE;
658 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
659 1.2 jmcneill di->un.v.num_channels = 1;
660 1.2 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
661 1.2 jmcneill return 0;
662 1.2 jmcneill }
663 1.2 jmcneill
664 1.1 jmcneill return ENXIO;
665 1.1 jmcneill }
666 1.1 jmcneill
667 1.1 jmcneill static int
668 1.1 jmcneill pad_get_props(void *opaque)
669 1.1 jmcneill {
670 1.17 jmcneill
671 1.59 isaki return AUDIO_PROP_PLAYBACK;
672 1.1 jmcneill }
673 1.13 ahoka
674 1.17 jmcneill static void
675 1.17 jmcneill pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
676 1.17 jmcneill {
677 1.69 riastrad struct pad_softc *sc = opaque;
678 1.17 jmcneill
679 1.17 jmcneill *intr = &sc->sc_intr_lock;
680 1.17 jmcneill *thread = &sc->sc_lock;
681 1.17 jmcneill }
682 1.17 jmcneill
683 1.22 jmcneill static void
684 1.59 isaki pad_swvol_codec(audio_filter_arg_t *arg)
685 1.22 jmcneill {
686 1.59 isaki struct pad_softc *sc = arg->context;
687 1.59 isaki const aint_t *src;
688 1.59 isaki aint_t *dst;
689 1.59 isaki u_int sample_count;
690 1.59 isaki u_int i;
691 1.59 isaki
692 1.59 isaki src = arg->src;
693 1.59 isaki dst = arg->dst;
694 1.59 isaki sample_count = arg->count * arg->srcfmt->channels;
695 1.59 isaki for (i = 0; i < sample_count; i++) {
696 1.59 isaki aint2_t v = (aint2_t)(*src++);
697 1.59 isaki v = v * sc->sc_swvol / 255;
698 1.59 isaki *dst++ = (aint_t)v;
699 1.59 isaki }
700 1.22 jmcneill }
701 1.22 jmcneill
702 1.44 pgoyette MODULE(MODULE_CLASS_DRIVER, pad, "audio");
703 1.44 pgoyette
704 1.13 ahoka #ifdef _MODULE
705 1.13 ahoka
706 1.57 nakayama #include "ioconf.c"
707 1.57 nakayama
708 1.57 nakayama devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
709 1.57 nakayama
710 1.57 nakayama /*
711 1.57 nakayama * We need our own version of cfattach since config(1)'s ioconf does not
712 1.57 nakayama * generate what we need
713 1.57 nakayama */
714 1.57 nakayama
715 1.57 nakayama static struct cfattach *pad_cfattachinit[] = { &pad_ca, NULL };
716 1.13 ahoka
717 1.57 nakayama static struct cfattachinit pad_cfattach[] = {
718 1.57 nakayama { "pad", pad_cfattachinit },
719 1.57 nakayama { NULL, NULL }
720 1.45 pgoyette };
721 1.47 pgoyette #endif
722 1.45 pgoyette
723 1.13 ahoka static int
724 1.13 ahoka pad_modcmd(modcmd_t cmd, void *arg)
725 1.13 ahoka {
726 1.44 pgoyette int error = 0;
727 1.13 ahoka
728 1.13 ahoka switch (cmd) {
729 1.13 ahoka case MODULE_CMD_INIT:
730 1.44 pgoyette #ifdef _MODULE
731 1.57 nakayama pad_cfattach[1] = cfattach_ioconf_pad[0];
732 1.57 nakayama error = config_init_component(cfdriver_ioconf_pad,
733 1.57 nakayama pad_cfattach, cfdata_ioconf_pad);
734 1.57 nakayama if (error)
735 1.44 pgoyette break;
736 1.13 ahoka
737 1.25 msaitoh error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
738 1.57 nakayama &pad_cdevsw, &cmajor);
739 1.13 ahoka if (error) {
740 1.57 nakayama config_fini_component(cfdriver_ioconf_pad,
741 1.57 nakayama pad_cfattach, cfdata_ioconf_pad);
742 1.44 pgoyette break;
743 1.13 ahoka }
744 1.13 ahoka
745 1.44 pgoyette #endif
746 1.57 nakayama break;
747 1.54 nakayama
748 1.13 ahoka case MODULE_CMD_FINI:
749 1.44 pgoyette #ifdef _MODULE
750 1.57 nakayama error = devsw_detach(NULL, &pad_cdevsw);
751 1.57 nakayama if (error)
752 1.57 nakayama break;
753 1.57 nakayama
754 1.57 nakayama error = config_fini_component(cfdriver_ioconf_pad,
755 1.57 nakayama pad_cfattach, cfdata_ioconf_pad);
756 1.13 ahoka if (error) {
757 1.57 nakayama devsw_attach(pad_cd.cd_name, NULL, &bmajor,
758 1.57 nakayama &pad_cdevsw, &cmajor);
759 1.44 pgoyette break;
760 1.13 ahoka }
761 1.44 pgoyette #endif
762 1.57 nakayama break;
763 1.54 nakayama
764 1.13 ahoka default:
765 1.44 pgoyette error = ENOTTY;
766 1.13 ahoka }
767 1.44 pgoyette
768 1.44 pgoyette return error;
769 1.13 ahoka }
770