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