pad.c revision 1.48 1 1.48 pgoyette /* $NetBSD: pad.c,v 1.48 2017/12/16 06:39:07 pgoyette 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.48 pgoyette __KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.48 2017/12/16 06:39:07 pgoyette Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/types.h>
33 1.1 jmcneill #include <sys/param.h>
34 1.1 jmcneill #include <sys/conf.h>
35 1.1 jmcneill #include <sys/buf.h>
36 1.39 nat #include <sys/file.h>
37 1.39 nat #include <sys/filedesc.h>
38 1.39 nat #include <sys/vnode.h>
39 1.39 nat #include <sys/kauth.h>
40 1.1 jmcneill #include <sys/kmem.h>
41 1.1 jmcneill #include <sys/kernel.h>
42 1.1 jmcneill #include <sys/device.h>
43 1.1 jmcneill #include <sys/proc.h>
44 1.1 jmcneill #include <sys/condvar.h>
45 1.1 jmcneill #include <sys/select.h>
46 1.39 nat #include <sys/stat.h>
47 1.1 jmcneill #include <sys/audioio.h>
48 1.5 dyoung #include <sys/vnode.h>
49 1.13 ahoka #include <sys/module.h>
50 1.17 jmcneill #include <sys/atomic.h>
51 1.24 nat #include <sys/time.h>
52 1.1 jmcneill
53 1.1 jmcneill #include <dev/audio_if.h>
54 1.1 jmcneill #include <dev/audiovar.h>
55 1.1 jmcneill #include <dev/auconv.h>
56 1.22 jmcneill #include <dev/auvolconv.h>
57 1.1 jmcneill
58 1.2 jmcneill #include <dev/pad/padvar.h>
59 1.2 jmcneill
60 1.39 nat #define MAXDEVS 128
61 1.41 nat #define PADCLONER 254
62 1.1 jmcneill #define PADUNIT(x) minor(x)
63 1.1 jmcneill
64 1.37 nat #define PADFREQ 44100
65 1.37 nat #define PADCHAN 2
66 1.37 nat #define PADPREC 16
67 1.37 nat #define PADENC AUDIO_ENCODING_SLINEAR_LE
68 1.37 nat
69 1.1 jmcneill extern struct cfdriver pad_cd;
70 1.1 jmcneill
71 1.1 jmcneill typedef struct pad_block {
72 1.1 jmcneill uint8_t *pb_ptr;
73 1.1 jmcneill int pb_len;
74 1.1 jmcneill } pad_block_t;
75 1.1 jmcneill
76 1.2 jmcneill enum {
77 1.2 jmcneill PAD_OUTPUT_CLASS,
78 1.2 jmcneill PAD_INPUT_CLASS,
79 1.2 jmcneill PAD_OUTPUT_MASTER_VOLUME,
80 1.2 jmcneill PAD_INPUT_DAC_VOLUME,
81 1.2 jmcneill PAD_ENUM_LAST,
82 1.2 jmcneill };
83 1.1 jmcneill
84 1.10 christos static int pad_match(device_t, cfdata_t, void *);
85 1.5 dyoung static void pad_attach(device_t, device_t, void *);
86 1.5 dyoung static int pad_detach(device_t, int);
87 1.5 dyoung static void pad_childdet(device_t, device_t);
88 1.1 jmcneill
89 1.26 nat static int pad_audio_open(void *, int);
90 1.1 jmcneill static int pad_query_encoding(void *, struct audio_encoding *);
91 1.1 jmcneill static int pad_set_params(void *, int, int,
92 1.1 jmcneill audio_params_t *, audio_params_t *,
93 1.1 jmcneill stream_filter_list_t *, stream_filter_list_t *);
94 1.1 jmcneill static int pad_start_output(void *, void *, int,
95 1.1 jmcneill void (*)(void *), void *);
96 1.1 jmcneill static int pad_start_input(void *, void *, int,
97 1.1 jmcneill void (*)(void *), void *);
98 1.1 jmcneill static int pad_halt_output(void *);
99 1.1 jmcneill static int pad_halt_input(void *);
100 1.1 jmcneill static int pad_getdev(void *, struct audio_device *);
101 1.1 jmcneill static int pad_set_port(void *, mixer_ctrl_t *);
102 1.1 jmcneill static int pad_get_port(void *, mixer_ctrl_t *);
103 1.1 jmcneill static int pad_query_devinfo(void *, mixer_devinfo_t *);
104 1.1 jmcneill static int pad_get_props(void *);
105 1.1 jmcneill static int pad_round_blocksize(void *, int, int, const audio_params_t *);
106 1.17 jmcneill static void pad_get_locks(void *, kmutex_t **, kmutex_t **);
107 1.1 jmcneill
108 1.22 jmcneill static stream_filter_t *pad_swvol_filter_le(struct audio_softc *,
109 1.22 jmcneill const audio_params_t *, const audio_params_t *);
110 1.22 jmcneill static stream_filter_t *pad_swvol_filter_be(struct audio_softc *,
111 1.22 jmcneill const audio_params_t *, const audio_params_t *);
112 1.22 jmcneill static void pad_swvol_dtor(stream_filter_t *);
113 1.22 jmcneill
114 1.41 nat static int pad_close(struct pad_softc *);
115 1.41 nat static int pad_read(struct pad_softc *, off_t *, struct uio *, kauth_cred_t, int);
116 1.41 nat
117 1.41 nat static int fops_pad_close(struct file *);
118 1.41 nat static int fops_pad_read(struct file *, off_t *, struct uio *, kauth_cred_t, int);
119 1.39 nat static int pad_write(struct file *, off_t *, struct uio *, kauth_cred_t, int);
120 1.39 nat static int pad_ioctl(struct file *, u_long, void *);
121 1.39 nat static int pad_kqfilter(struct file *, struct knote *);
122 1.39 nat static int pad_poll(struct file *, int);
123 1.39 nat static int pad_stat(struct file *, struct stat *);
124 1.39 nat static int pad_mmap(struct file *, off_t *, size_t, int, int *, int *,
125 1.39 nat struct uvm_object **, int *);
126 1.32 pgoyette
127 1.1 jmcneill static const struct audio_hw_if pad_hw_if = {
128 1.26 nat .open = pad_audio_open,
129 1.1 jmcneill .query_encoding = pad_query_encoding,
130 1.1 jmcneill .set_params = pad_set_params,
131 1.1 jmcneill .start_output = pad_start_output,
132 1.1 jmcneill .start_input = pad_start_input,
133 1.1 jmcneill .halt_output = pad_halt_output,
134 1.1 jmcneill .halt_input = pad_halt_input,
135 1.1 jmcneill .getdev = pad_getdev,
136 1.1 jmcneill .set_port = pad_set_port,
137 1.1 jmcneill .get_port = pad_get_port,
138 1.1 jmcneill .query_devinfo = pad_query_devinfo,
139 1.1 jmcneill .get_props = pad_get_props,
140 1.1 jmcneill .round_blocksize = pad_round_blocksize,
141 1.17 jmcneill .get_locks = pad_get_locks,
142 1.1 jmcneill };
143 1.1 jmcneill
144 1.1 jmcneill #define PAD_NFORMATS 1
145 1.1 jmcneill static const struct audio_format pad_formats[PAD_NFORMATS] = {
146 1.37 nat { NULL, AUMODE_PLAY|AUMODE_RECORD, PADENC, PADPREC, PADPREC,
147 1.37 nat PADCHAN, AUFMT_STEREO, 1, { PADFREQ } },
148 1.1 jmcneill };
149 1.1 jmcneill
150 1.1 jmcneill extern void padattach(int);
151 1.1 jmcneill
152 1.25 msaitoh static int pad_add_block(pad_softc_t *, uint8_t *, int);
153 1.25 msaitoh static int pad_get_block(pad_softc_t *, pad_block_t *, int);
154 1.1 jmcneill
155 1.1 jmcneill dev_type_open(pad_open);
156 1.41 nat dev_type_close(cdev_pad_close);
157 1.41 nat dev_type_read(cdev_pad_read);
158 1.1 jmcneill
159 1.1 jmcneill const struct cdevsw pad_cdevsw = {
160 1.1 jmcneill .d_open = pad_open,
161 1.41 nat .d_close = cdev_pad_close,
162 1.41 nat .d_read = cdev_pad_read,
163 1.1 jmcneill .d_write = nowrite,
164 1.1 jmcneill .d_ioctl = noioctl,
165 1.1 jmcneill .d_stop = nostop,
166 1.1 jmcneill .d_tty = notty,
167 1.1 jmcneill .d_poll = nopoll,
168 1.1 jmcneill .d_mmap = nommap,
169 1.1 jmcneill .d_kqfilter = nokqfilter,
170 1.21 dholland .d_discard = nodiscard,
171 1.17 jmcneill .d_flag = D_OTHER | D_MPSAFE,
172 1.1 jmcneill };
173 1.1 jmcneill
174 1.39 nat const struct fileops pad_fileops = {
175 1.43 christos .fo_name = "pad",
176 1.41 nat .fo_read = fops_pad_read,
177 1.39 nat .fo_write = pad_write,
178 1.39 nat .fo_ioctl = pad_ioctl,
179 1.39 nat .fo_fcntl = fnullop_fcntl,
180 1.39 nat .fo_stat = pad_stat,
181 1.39 nat .fo_poll = pad_poll,
182 1.41 nat .fo_close = fops_pad_close,
183 1.39 nat .fo_mmap = pad_mmap,
184 1.39 nat .fo_kqfilter = pad_kqfilter,
185 1.39 nat .fo_restart = fnullop_restart
186 1.39 nat };
187 1.39 nat
188 1.10 christos CFATTACH_DECL2_NEW(pad, sizeof(pad_softc_t), pad_match, pad_attach, pad_detach,
189 1.5 dyoung NULL, NULL, pad_childdet);
190 1.1 jmcneill
191 1.1 jmcneill void
192 1.1 jmcneill padattach(int n)
193 1.1 jmcneill {
194 1.39 nat int error;
195 1.1 jmcneill
196 1.39 nat error = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
197 1.39 nat if (error) {
198 1.1 jmcneill aprint_error("%s: couldn't register cfattach: %d\n",
199 1.39 nat pad_cd.cd_name, error);
200 1.1 jmcneill config_cfdriver_detach(&pad_cd);
201 1.1 jmcneill return;
202 1.1 jmcneill }
203 1.1 jmcneill
204 1.1 jmcneill return;
205 1.1 jmcneill }
206 1.1 jmcneill
207 1.1 jmcneill static int
208 1.1 jmcneill pad_add_block(pad_softc_t *sc, uint8_t *blk, int blksize)
209 1.1 jmcneill {
210 1.1 jmcneill int l;
211 1.1 jmcneill
212 1.16 jmcneill if (sc->sc_open == 0)
213 1.16 jmcneill return EIO;
214 1.16 jmcneill
215 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
216 1.17 jmcneill
217 1.1 jmcneill if (sc->sc_buflen + blksize > PAD_BUFSIZE)
218 1.1 jmcneill return ENOBUFS;
219 1.1 jmcneill
220 1.1 jmcneill if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
221 1.1 jmcneill memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
222 1.1 jmcneill else {
223 1.1 jmcneill l = PAD_BUFSIZE - sc->sc_wpos;
224 1.1 jmcneill memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
225 1.1 jmcneill memcpy(sc->sc_audiobuf, blk + l, blksize - l);
226 1.1 jmcneill }
227 1.1 jmcneill
228 1.1 jmcneill sc->sc_wpos += blksize;
229 1.1 jmcneill if (sc->sc_wpos > PAD_BUFSIZE)
230 1.1 jmcneill sc->sc_wpos -= PAD_BUFSIZE;
231 1.1 jmcneill
232 1.1 jmcneill sc->sc_buflen += blksize;
233 1.1 jmcneill
234 1.1 jmcneill return 0;
235 1.1 jmcneill }
236 1.1 jmcneill
237 1.1 jmcneill static int
238 1.1 jmcneill pad_get_block(pad_softc_t *sc, pad_block_t *pb, int blksize)
239 1.1 jmcneill {
240 1.1 jmcneill int l;
241 1.1 jmcneill
242 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
243 1.1 jmcneill KASSERT(pb != NULL);
244 1.1 jmcneill
245 1.42 nat if (sc->sc_buflen < (uint)blksize)
246 1.1 jmcneill return ERESTART;
247 1.1 jmcneill
248 1.1 jmcneill pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
249 1.1 jmcneill if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
250 1.1 jmcneill pb->pb_len = blksize;
251 1.1 jmcneill sc->sc_rpos += blksize;
252 1.1 jmcneill } else {
253 1.1 jmcneill l = PAD_BUFSIZE - sc->sc_rpos;
254 1.1 jmcneill pb->pb_len = l;
255 1.1 jmcneill sc->sc_rpos = 0;
256 1.1 jmcneill }
257 1.1 jmcneill sc->sc_buflen -= pb->pb_len;
258 1.1 jmcneill
259 1.1 jmcneill return 0;
260 1.1 jmcneill }
261 1.1 jmcneill
262 1.1 jmcneill static int
263 1.10 christos pad_match(device_t parent, cfdata_t data, void *opaque)
264 1.1 jmcneill {
265 1.17 jmcneill
266 1.1 jmcneill return 1;
267 1.1 jmcneill }
268 1.1 jmcneill
269 1.1 jmcneill static void
270 1.5 dyoung pad_childdet(device_t self, device_t child)
271 1.1 jmcneill {
272 1.5 dyoung pad_softc_t *sc = device_private(self);
273 1.5 dyoung
274 1.5 dyoung sc->sc_audiodev = NULL;
275 1.5 dyoung }
276 1.1 jmcneill
277 1.5 dyoung static void
278 1.5 dyoung pad_attach(device_t parent, device_t self, void *opaque)
279 1.5 dyoung {
280 1.1 jmcneill aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
281 1.1 jmcneill
282 1.1 jmcneill return;
283 1.1 jmcneill }
284 1.1 jmcneill
285 1.5 dyoung static int
286 1.5 dyoung pad_detach(device_t self, int flags)
287 1.5 dyoung {
288 1.39 nat pad_softc_t *sc;
289 1.5 dyoung int cmaj, mn, rc;
290 1.5 dyoung
291 1.39 nat sc = device_private(self);
292 1.39 nat config_deactivate(sc->sc_audiodev);
293 1.39 nat
294 1.39 nat /* Start draining existing accessors of the device. */
295 1.39 nat if ((rc = config_detach_children(self, flags)) != 0)
296 1.39 nat return rc;
297 1.39 nat
298 1.39 nat mutex_enter(&sc->sc_lock);
299 1.39 nat sc->sc_dying = true;
300 1.39 nat cv_broadcast(&sc->sc_condvar);
301 1.39 nat mutex_exit(&sc->sc_lock);
302 1.39 nat
303 1.39 nat KASSERT(sc->sc_open > 0);
304 1.39 nat sc->sc_open = 0;
305 1.32 pgoyette
306 1.5 dyoung cmaj = cdevsw_lookup_major(&pad_cdevsw);
307 1.39 nat mn = device_unit(sc->sc_dev);
308 1.5 dyoung vdevgone(cmaj, mn, mn, VCHR);
309 1.5 dyoung
310 1.39 nat pmf_device_deregister(sc->sc_dev);
311 1.5 dyoung
312 1.17 jmcneill mutex_destroy(&sc->sc_lock);
313 1.17 jmcneill mutex_destroy(&sc->sc_intr_lock);
314 1.5 dyoung cv_destroy(&sc->sc_condvar);
315 1.5 dyoung
316 1.5 dyoung auconv_delete_encodings(sc->sc_encodings);
317 1.5 dyoung
318 1.41 nat return 0;
319 1.5 dyoung }
320 1.5 dyoung
321 1.1 jmcneill int
322 1.1 jmcneill pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
323 1.1 jmcneill {
324 1.1 jmcneill pad_softc_t *sc;
325 1.39 nat struct file *fp;
326 1.39 nat device_t paddev;
327 1.39 nat cfdata_t cf;
328 1.39 nat int error, fd, i;
329 1.1 jmcneill
330 1.41 nat if (PADUNIT(dev) == PADCLONER) {
331 1.41 nat for (i = 0; i < MAXDEVS; i++) {
332 1.41 nat if (device_lookup(&pad_cd, i) == NULL)
333 1.41 nat break;
334 1.41 nat }
335 1.41 nat if (i == MAXDEVS)
336 1.41 nat return ENXIO;
337 1.41 nat } else {
338 1.41 nat if (PADUNIT(dev) >= MAXDEVS)
339 1.41 nat return ENXIO;
340 1.41 nat i = PADUNIT(dev);
341 1.39 nat }
342 1.1 jmcneill
343 1.39 nat cf = kmem_alloc(sizeof(struct cfdata), KM_SLEEP);
344 1.39 nat cf->cf_name = pad_cd.cd_name;
345 1.39 nat cf->cf_atname = pad_cd.cd_name;
346 1.39 nat cf->cf_unit = i;
347 1.39 nat cf->cf_fstate = FSTATE_STAR;
348 1.39 nat
349 1.48 pgoyette paddev = device_lookup(&pad_cd, minor(dev));
350 1.48 pgoyette if (paddev == NULL)
351 1.41 nat paddev = config_attach_pseudo(cf);
352 1.48 pgoyette if (paddev == NULL)
353 1.48 pgoyette return ENXIO;
354 1.41 nat
355 1.39 nat sc = device_private(paddev);
356 1.41 nat if (sc == NULL)
357 1.41 nat return ENXIO;
358 1.39 nat
359 1.39 nat if (sc->sc_open == 1)
360 1.1 jmcneill return EBUSY;
361 1.39 nat
362 1.41 nat sc->sc_dev = paddev;
363 1.41 nat sc->sc_dying = false;
364 1.41 nat
365 1.41 nat if (PADUNIT(dev) == PADCLONER) {
366 1.41 nat error = fd_allocfile(&fp, &fd);
367 1.41 nat if (error) {
368 1.41 nat config_detach(sc->sc_dev, 0);
369 1.41 nat return error;
370 1.41 nat }
371 1.39 nat }
372 1.39 nat
373 1.39 nat if (auconv_create_encodings(pad_formats, PAD_NFORMATS,
374 1.39 nat &sc->sc_encodings) != 0) {
375 1.39 nat aprint_error_dev(sc->sc_dev, "couldn't create encodings\n");
376 1.39 nat config_detach(sc->sc_dev, 0);
377 1.39 nat return EINVAL;
378 1.39 nat }
379 1.39 nat
380 1.39 nat cv_init(&sc->sc_condvar, device_xname(sc->sc_dev));
381 1.39 nat mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
382 1.39 nat mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);
383 1.39 nat
384 1.39 nat sc->sc_swvol = 255;
385 1.39 nat sc->sc_buflen = 0;
386 1.39 nat sc->sc_rpos = sc->sc_wpos = 0;
387 1.39 nat sc->sc_audiodev = audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);
388 1.39 nat
389 1.39 nat if (!pmf_device_register(sc->sc_dev, NULL, NULL))
390 1.39 nat aprint_error_dev(sc->sc_dev, "couldn't establish power handler\n");
391 1.39 nat
392 1.41 nat if (PADUNIT(dev) == PADCLONER) {
393 1.41 nat error = fd_clone(fp, fd, flags, &pad_fileops, sc);
394 1.41 nat KASSERT(error == EMOVEFD);
395 1.41 nat }
396 1.39 nat sc->sc_open = 1;
397 1.39 nat
398 1.39 nat return error;
399 1.1 jmcneill }
400 1.1 jmcneill
401 1.39 nat static int
402 1.41 nat pad_close(struct pad_softc *sc)
403 1.41 nat {
404 1.41 nat if (sc == NULL)
405 1.41 nat return ENXIO;
406 1.41 nat
407 1.41 nat return config_detach(sc->sc_dev, DETACH_FORCE);
408 1.41 nat }
409 1.41 nat
410 1.41 nat static int
411 1.41 nat fops_pad_close(struct file *fp)
412 1.1 jmcneill {
413 1.1 jmcneill pad_softc_t *sc;
414 1.41 nat int error;
415 1.1 jmcneill
416 1.39 nat sc = fp->f_pad;
417 1.1 jmcneill
418 1.41 nat error = pad_close(sc);
419 1.41 nat
420 1.41 nat if (error == 0)
421 1.41 nat fp->f_pad = NULL;
422 1.41 nat
423 1.41 nat return error;
424 1.41 nat }
425 1.39 nat
426 1.41 nat int
427 1.41 nat cdev_pad_close(dev_t dev, int flags, int ifmt, struct lwp *l)
428 1.41 nat {
429 1.41 nat pad_softc_t *sc;
430 1.41 nat sc = device_private(device_lookup(&pad_cd, PADUNIT(dev)));
431 1.39 nat
432 1.41 nat return pad_close(sc);
433 1.39 nat }
434 1.39 nat
435 1.39 nat static int
436 1.39 nat pad_poll(struct file *fp, int events)
437 1.39 nat {
438 1.39 nat return ENODEV;
439 1.39 nat }
440 1.39 nat
441 1.39 nat static int
442 1.39 nat pad_kqfilter(struct file *fp, struct knote *kn)
443 1.39 nat {
444 1.39 nat struct pad_softc *sc;
445 1.39 nat dev_t dev;
446 1.39 nat
447 1.39 nat sc = fp->f_pad;
448 1.39 nat if (sc == NULL)
449 1.39 nat return EIO;
450 1.39 nat
451 1.39 nat dev = makedev(cdevsw_lookup_major(&pad_cdevsw), device_unit(sc->sc_dev));
452 1.39 nat
453 1.39 nat return seltrue_kqfilter(dev, kn);
454 1.39 nat }
455 1.39 nat
456 1.39 nat static int
457 1.39 nat pad_ioctl(struct file *fp, u_long cmd, void *data)
458 1.39 nat {
459 1.39 nat return ENODEV;
460 1.39 nat }
461 1.39 nat
462 1.39 nat static int
463 1.39 nat pad_stat(struct file *fp, struct stat *st)
464 1.39 nat {
465 1.39 nat struct pad_softc *sc;
466 1.39 nat
467 1.39 nat sc = fp->f_pad;
468 1.39 nat if (sc == NULL)
469 1.39 nat return EIO;
470 1.39 nat
471 1.39 nat memset(st, 0, sizeof(*st));
472 1.39 nat
473 1.39 nat st->st_dev = makedev(cdevsw_lookup_major(&pad_cdevsw), device_unit(sc->sc_dev));
474 1.39 nat
475 1.39 nat st->st_uid = kauth_cred_geteuid(fp->f_cred);
476 1.39 nat st->st_gid = kauth_cred_getegid(fp->f_cred);
477 1.39 nat st->st_mode = S_IFCHR;
478 1.1 jmcneill
479 1.1 jmcneill return 0;
480 1.1 jmcneill }
481 1.1 jmcneill
482 1.39 nat static int
483 1.39 nat pad_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
484 1.39 nat int *advicep, struct uvm_object **uobjp, int *maxprotp)
485 1.39 nat {
486 1.39 nat return 1;
487 1.39 nat }
488 1.39 nat
489 1.37 nat #define PAD_BYTES_PER_SEC (PADFREQ * PADPREC / NBBY * PADCHAN)
490 1.37 nat #define BYTESTOSLEEP (int64_t)(PAD_BLKSIZE)
491 1.33 nat #define TIMENEXTREAD (int64_t)(BYTESTOSLEEP * 1000000 / PAD_BYTES_PER_SEC)
492 1.24 nat
493 1.41 nat int
494 1.41 nat cdev_pad_read(dev_t dev, struct uio *uio, int ioflag)
495 1.41 nat {
496 1.41 nat pad_softc_t *sc;
497 1.41 nat sc = device_private(device_lookup(&pad_cd, PADUNIT(dev)));
498 1.41 nat if (sc == NULL)
499 1.41 nat return ENXIO;
500 1.41 nat
501 1.41 nat return pad_read(sc, NULL, uio, NULL, ioflag);
502 1.41 nat }
503 1.41 nat
504 1.39 nat static int
505 1.41 nat fops_pad_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
506 1.41 nat int ioflag)
507 1.41 nat {
508 1.41 nat pad_softc_t *sc;
509 1.41 nat
510 1.41 nat sc = fp->f_pad;
511 1.41 nat if (sc == NULL)
512 1.41 nat return ENXIO;
513 1.41 nat
514 1.41 nat return pad_read(sc, offp, uio, cred, ioflag);
515 1.41 nat }
516 1.41 nat
517 1.41 nat static int
518 1.41 nat pad_read(struct pad_softc *sc, off_t *offp, struct uio *uio, kauth_cred_t cred,
519 1.39 nat int ioflag)
520 1.1 jmcneill {
521 1.24 nat struct timeval now;
522 1.24 nat uint64_t nowusec, lastusec;
523 1.1 jmcneill pad_block_t pb;
524 1.1 jmcneill void (*intr)(void *);
525 1.1 jmcneill void *intrarg;
526 1.24 nat int err, wait_ticks;
527 1.1 jmcneill
528 1.1 jmcneill err = 0;
529 1.1 jmcneill
530 1.27 nat while (uio->uio_resid > 0 && !err) {
531 1.27 nat mutex_enter(&sc->sc_lock);
532 1.39 nat if (sc->sc_dying == true) {
533 1.39 nat mutex_exit(&sc->sc_lock);
534 1.39 nat return EIO;
535 1.39 nat }
536 1.27 nat intr = sc->sc_intr;
537 1.27 nat intrarg = sc->sc_intrarg;
538 1.1 jmcneill
539 1.24 nat getmicrotime(&now);
540 1.24 nat nowusec = (now.tv_sec * 1000000) + now.tv_usec;
541 1.24 nat lastusec = (sc->sc_last.tv_sec * 1000000) +
542 1.24 nat sc->sc_last.tv_usec;
543 1.35 nat if (lastusec + TIMENEXTREAD > nowusec) {
544 1.35 nat if (sc->sc_bytes_count >= BYTESTOSLEEP) {
545 1.35 nat sc->sc_remainder +=
546 1.35 nat ((lastusec + TIMENEXTREAD) - nowusec);
547 1.35 nat }
548 1.29 nat
549 1.29 nat wait_ticks = (hz * sc->sc_remainder) / 1000000;
550 1.24 nat if (wait_ticks > 0) {
551 1.29 nat sc->sc_remainder -= wait_ticks * 1000000 / hz;
552 1.40 nat err = kpause("padwait", TRUE, wait_ticks,
553 1.28 nat &sc->sc_lock);
554 1.40 nat if (err != EWOULDBLOCK) {
555 1.40 nat mutex_exit(&sc->sc_lock);
556 1.40 nat continue;
557 1.40 nat }
558 1.24 nat }
559 1.35 nat }
560 1.29 nat
561 1.35 nat if (sc->sc_bytes_count >= BYTESTOSLEEP)
562 1.29 nat sc->sc_bytes_count -= BYTESTOSLEEP;
563 1.29 nat
564 1.1 jmcneill err = pad_get_block(sc, &pb, min(uio->uio_resid, PAD_BLKSIZE));
565 1.17 jmcneill if (!err) {
566 1.35 nat getmicrotime(&sc->sc_last);
567 1.30 nat sc->sc_bytes_count += pb.pb_len;
568 1.17 jmcneill mutex_exit(&sc->sc_lock);
569 1.1 jmcneill err = uiomove(pb.pb_ptr, pb.pb_len, uio);
570 1.17 jmcneill continue;
571 1.17 jmcneill }
572 1.1 jmcneill
573 1.17 jmcneill if (intr) {
574 1.17 jmcneill mutex_enter(&sc->sc_intr_lock);
575 1.24 nat kpreempt_disable();
576 1.17 jmcneill (*intr)(intrarg);
577 1.24 nat kpreempt_enable();
578 1.17 jmcneill mutex_exit(&sc->sc_intr_lock);
579 1.1 jmcneill intr = sc->sc_intr;
580 1.1 jmcneill intrarg = sc->sc_intrarg;
581 1.1 jmcneill err = 0;
582 1.27 nat mutex_exit(&sc->sc_lock);
583 1.17 jmcneill continue;
584 1.17 jmcneill }
585 1.17 jmcneill err = cv_wait_sig(&sc->sc_condvar, &sc->sc_lock);
586 1.27 nat if (err != 0) {
587 1.27 nat mutex_exit(&sc->sc_lock);
588 1.24 nat break;
589 1.27 nat }
590 1.24 nat
591 1.27 nat mutex_exit(&sc->sc_lock);
592 1.1 jmcneill }
593 1.1 jmcneill
594 1.1 jmcneill return err;
595 1.1 jmcneill }
596 1.1 jmcneill
597 1.1 jmcneill static int
598 1.39 nat pad_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
599 1.39 nat int ioflag)
600 1.39 nat {
601 1.39 nat return EOPNOTSUPP;
602 1.39 nat }
603 1.39 nat
604 1.39 nat static int
605 1.26 nat pad_audio_open(void *opaque, int flags)
606 1.26 nat {
607 1.26 nat pad_softc_t *sc;
608 1.26 nat sc = opaque;
609 1.26 nat
610 1.26 nat if (sc->sc_open == 0)
611 1.26 nat return EIO;
612 1.26 nat
613 1.26 nat getmicrotime(&sc->sc_last);
614 1.34 nat sc->sc_bytes_count = 0;
615 1.29 nat sc->sc_remainder = 0;
616 1.29 nat
617 1.26 nat return 0;
618 1.26 nat }
619 1.26 nat
620 1.26 nat static int
621 1.1 jmcneill pad_query_encoding(void *opaque, struct audio_encoding *ae)
622 1.1 jmcneill {
623 1.1 jmcneill pad_softc_t *sc;
624 1.1 jmcneill
625 1.1 jmcneill sc = (pad_softc_t *)opaque;
626 1.1 jmcneill
627 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
628 1.17 jmcneill
629 1.1 jmcneill return auconv_query_encoding(sc->sc_encodings, ae);
630 1.1 jmcneill }
631 1.1 jmcneill
632 1.1 jmcneill static int
633 1.1 jmcneill pad_set_params(void *opaque, int setmode, int usemode,
634 1.1 jmcneill audio_params_t *play, audio_params_t *rec,
635 1.1 jmcneill stream_filter_list_t *pfil, stream_filter_list_t *rfil)
636 1.1 jmcneill {
637 1.20 christos pad_softc_t *sc __diagused;
638 1.1 jmcneill
639 1.1 jmcneill sc = (pad_softc_t *)opaque;
640 1.1 jmcneill
641 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
642 1.17 jmcneill
643 1.1 jmcneill if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_PLAY,
644 1.1 jmcneill play, true, pfil) < 0)
645 1.1 jmcneill return EINVAL;
646 1.1 jmcneill if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_RECORD,
647 1.1 jmcneill rec, true, rfil) < 0)
648 1.1 jmcneill return EINVAL;
649 1.1 jmcneill
650 1.2 jmcneill if (pfil->req_size > 0)
651 1.2 jmcneill play = &pfil->filters[0].param;
652 1.2 jmcneill switch (play->encoding) {
653 1.2 jmcneill case AUDIO_ENCODING_SLINEAR_LE:
654 1.2 jmcneill if (play->precision == 16 && play->validbits == 16)
655 1.22 jmcneill pfil->prepend(pfil, pad_swvol_filter_le, play);
656 1.2 jmcneill break;
657 1.2 jmcneill case AUDIO_ENCODING_SLINEAR_BE:
658 1.2 jmcneill if (play->precision == 16 && play->validbits == 16)
659 1.22 jmcneill pfil->prepend(pfil, pad_swvol_filter_be, play);
660 1.2 jmcneill break;
661 1.2 jmcneill default:
662 1.2 jmcneill break;
663 1.2 jmcneill }
664 1.2 jmcneill
665 1.1 jmcneill return 0;
666 1.1 jmcneill }
667 1.1 jmcneill
668 1.1 jmcneill static int
669 1.1 jmcneill pad_start_output(void *opaque, void *block, int blksize,
670 1.1 jmcneill void (*intr)(void *), void *intrarg)
671 1.1 jmcneill {
672 1.1 jmcneill pad_softc_t *sc;
673 1.1 jmcneill int err;
674 1.1 jmcneill
675 1.1 jmcneill sc = (pad_softc_t *)opaque;
676 1.1 jmcneill
677 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
678 1.26 nat if (!sc->sc_open)
679 1.26 nat return EIO;
680 1.1 jmcneill
681 1.1 jmcneill sc->sc_intr = intr;
682 1.1 jmcneill sc->sc_intrarg = intrarg;
683 1.1 jmcneill sc->sc_blksize = blksize;
684 1.1 jmcneill
685 1.1 jmcneill err = pad_add_block(sc, block, blksize);
686 1.1 jmcneill
687 1.17 jmcneill cv_broadcast(&sc->sc_condvar);
688 1.1 jmcneill
689 1.1 jmcneill return err;
690 1.1 jmcneill }
691 1.1 jmcneill
692 1.1 jmcneill static int
693 1.1 jmcneill pad_start_input(void *opaque, void *block, int blksize,
694 1.1 jmcneill void (*intr)(void *), void *intrarg)
695 1.1 jmcneill {
696 1.20 christos pad_softc_t *sc __diagused;
697 1.17 jmcneill
698 1.17 jmcneill sc = (pad_softc_t *)opaque;
699 1.17 jmcneill
700 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
701 1.17 jmcneill
702 1.17 jmcneill return EOPNOTSUPP;
703 1.1 jmcneill }
704 1.1 jmcneill
705 1.1 jmcneill static int
706 1.1 jmcneill pad_halt_output(void *opaque)
707 1.1 jmcneill {
708 1.1 jmcneill pad_softc_t *sc;
709 1.1 jmcneill
710 1.1 jmcneill sc = (pad_softc_t *)opaque;
711 1.17 jmcneill
712 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
713 1.17 jmcneill
714 1.1 jmcneill sc->sc_intr = NULL;
715 1.1 jmcneill sc->sc_intrarg = NULL;
716 1.1 jmcneill sc->sc_buflen = 0;
717 1.1 jmcneill sc->sc_rpos = sc->sc_wpos = 0;
718 1.1 jmcneill
719 1.1 jmcneill return 0;
720 1.1 jmcneill }
721 1.1 jmcneill
722 1.1 jmcneill static int
723 1.1 jmcneill pad_halt_input(void *opaque)
724 1.1 jmcneill {
725 1.20 christos pad_softc_t *sc __diagused;
726 1.17 jmcneill
727 1.17 jmcneill sc = (pad_softc_t *)opaque;
728 1.17 jmcneill
729 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
730 1.17 jmcneill
731 1.1 jmcneill return 0;
732 1.1 jmcneill }
733 1.1 jmcneill
734 1.1 jmcneill static int
735 1.1 jmcneill pad_getdev(void *opaque, struct audio_device *ret)
736 1.1 jmcneill {
737 1.16 jmcneill strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
738 1.16 jmcneill strlcpy(ret->version, osrelease, sizeof(ret->version));
739 1.16 jmcneill strlcpy(ret->config, "pad", sizeof(ret->config));
740 1.1 jmcneill
741 1.1 jmcneill return 0;
742 1.1 jmcneill }
743 1.1 jmcneill
744 1.1 jmcneill static int
745 1.1 jmcneill pad_set_port(void *opaque, mixer_ctrl_t *mc)
746 1.1 jmcneill {
747 1.2 jmcneill pad_softc_t *sc;
748 1.2 jmcneill
749 1.2 jmcneill sc = (pad_softc_t *)opaque;
750 1.2 jmcneill
751 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
752 1.17 jmcneill
753 1.2 jmcneill switch (mc->dev) {
754 1.2 jmcneill case PAD_OUTPUT_MASTER_VOLUME:
755 1.2 jmcneill case PAD_INPUT_DAC_VOLUME:
756 1.2 jmcneill sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
757 1.2 jmcneill return 0;
758 1.2 jmcneill }
759 1.2 jmcneill
760 1.1 jmcneill return ENXIO;
761 1.1 jmcneill }
762 1.1 jmcneill
763 1.1 jmcneill static int
764 1.1 jmcneill pad_get_port(void *opaque, mixer_ctrl_t *mc)
765 1.1 jmcneill {
766 1.2 jmcneill pad_softc_t *sc;
767 1.2 jmcneill
768 1.2 jmcneill sc = (pad_softc_t *)opaque;
769 1.2 jmcneill
770 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
771 1.17 jmcneill
772 1.2 jmcneill switch (mc->dev) {
773 1.2 jmcneill case PAD_OUTPUT_MASTER_VOLUME:
774 1.2 jmcneill case PAD_INPUT_DAC_VOLUME:
775 1.2 jmcneill mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
776 1.2 jmcneill return 0;
777 1.2 jmcneill }
778 1.2 jmcneill
779 1.1 jmcneill return ENXIO;
780 1.1 jmcneill }
781 1.1 jmcneill
782 1.1 jmcneill static int
783 1.1 jmcneill pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
784 1.1 jmcneill {
785 1.20 christos pad_softc_t *sc __diagused;
786 1.2 jmcneill
787 1.2 jmcneill sc = (pad_softc_t *)opaque;
788 1.2 jmcneill
789 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
790 1.17 jmcneill
791 1.2 jmcneill switch (di->index) {
792 1.2 jmcneill case PAD_OUTPUT_CLASS:
793 1.2 jmcneill di->mixer_class = PAD_OUTPUT_CLASS;
794 1.2 jmcneill strcpy(di->label.name, AudioCoutputs);
795 1.2 jmcneill di->type = AUDIO_MIXER_CLASS;
796 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
797 1.2 jmcneill return 0;
798 1.2 jmcneill case PAD_INPUT_CLASS:
799 1.2 jmcneill di->mixer_class = PAD_INPUT_CLASS;
800 1.2 jmcneill strcpy(di->label.name, AudioCinputs);
801 1.2 jmcneill di->type = AUDIO_MIXER_CLASS;
802 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
803 1.2 jmcneill return 0;
804 1.2 jmcneill case PAD_OUTPUT_MASTER_VOLUME:
805 1.2 jmcneill di->mixer_class = PAD_OUTPUT_CLASS;
806 1.2 jmcneill strcpy(di->label.name, AudioNmaster);
807 1.2 jmcneill di->type = AUDIO_MIXER_VALUE;
808 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
809 1.2 jmcneill di->un.v.num_channels = 1;
810 1.2 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
811 1.2 jmcneill return 0;
812 1.2 jmcneill case PAD_INPUT_DAC_VOLUME:
813 1.2 jmcneill di->mixer_class = PAD_INPUT_CLASS;
814 1.2 jmcneill strcpy(di->label.name, AudioNdac);
815 1.2 jmcneill di->type = AUDIO_MIXER_VALUE;
816 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
817 1.2 jmcneill di->un.v.num_channels = 1;
818 1.2 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
819 1.2 jmcneill return 0;
820 1.2 jmcneill }
821 1.2 jmcneill
822 1.1 jmcneill return ENXIO;
823 1.1 jmcneill }
824 1.1 jmcneill
825 1.1 jmcneill static int
826 1.1 jmcneill pad_get_props(void *opaque)
827 1.1 jmcneill {
828 1.20 christos pad_softc_t *sc __diagused;
829 1.17 jmcneill
830 1.17 jmcneill sc = (pad_softc_t *)opaque;
831 1.17 jmcneill
832 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
833 1.17 jmcneill
834 1.1 jmcneill return 0;
835 1.1 jmcneill }
836 1.1 jmcneill
837 1.1 jmcneill static int
838 1.1 jmcneill pad_round_blocksize(void *opaque, int blksize, int mode,
839 1.1 jmcneill const audio_params_t *p)
840 1.1 jmcneill {
841 1.20 christos pad_softc_t *sc __diagused;
842 1.17 jmcneill
843 1.17 jmcneill sc = (pad_softc_t *)opaque;
844 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
845 1.17 jmcneill
846 1.1 jmcneill return PAD_BLKSIZE;
847 1.1 jmcneill }
848 1.13 ahoka
849 1.17 jmcneill static void
850 1.17 jmcneill pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
851 1.17 jmcneill {
852 1.17 jmcneill pad_softc_t *sc;
853 1.17 jmcneill
854 1.17 jmcneill sc = (pad_softc_t *)opaque;
855 1.17 jmcneill
856 1.17 jmcneill *intr = &sc->sc_intr_lock;
857 1.17 jmcneill *thread = &sc->sc_lock;
858 1.17 jmcneill }
859 1.17 jmcneill
860 1.22 jmcneill static stream_filter_t *
861 1.22 jmcneill pad_swvol_filter_le(struct audio_softc *asc,
862 1.22 jmcneill const audio_params_t *from, const audio_params_t *to)
863 1.22 jmcneill {
864 1.22 jmcneill auvolconv_filter_t *this;
865 1.22 jmcneill device_t dev = audio_get_device(asc);
866 1.22 jmcneill struct pad_softc *sc = device_private(dev);
867 1.22 jmcneill
868 1.22 jmcneill this = kmem_alloc(sizeof(auvolconv_filter_t), KM_SLEEP);
869 1.22 jmcneill this->base.base.fetch_to = auvolconv_slinear16_le_fetch_to;
870 1.22 jmcneill this->base.dtor = pad_swvol_dtor;
871 1.22 jmcneill this->base.set_fetcher = stream_filter_set_fetcher;
872 1.22 jmcneill this->base.set_inputbuffer = stream_filter_set_inputbuffer;
873 1.22 jmcneill this->vol = &sc->sc_swvol;
874 1.22 jmcneill
875 1.22 jmcneill return (stream_filter_t *)this;
876 1.22 jmcneill }
877 1.22 jmcneill
878 1.22 jmcneill static stream_filter_t *
879 1.22 jmcneill pad_swvol_filter_be(struct audio_softc *asc,
880 1.22 jmcneill const audio_params_t *from, const audio_params_t *to)
881 1.22 jmcneill {
882 1.22 jmcneill auvolconv_filter_t *this;
883 1.22 jmcneill device_t dev = audio_get_device(asc);
884 1.22 jmcneill struct pad_softc *sc = device_private(dev);
885 1.22 jmcneill
886 1.22 jmcneill this = kmem_alloc(sizeof(auvolconv_filter_t), KM_SLEEP);
887 1.22 jmcneill this->base.base.fetch_to = auvolconv_slinear16_be_fetch_to;
888 1.22 jmcneill this->base.dtor = pad_swvol_dtor;
889 1.22 jmcneill this->base.set_fetcher = stream_filter_set_fetcher;
890 1.22 jmcneill this->base.set_inputbuffer = stream_filter_set_inputbuffer;
891 1.22 jmcneill this->vol = &sc->sc_swvol;
892 1.22 jmcneill
893 1.22 jmcneill return (stream_filter_t *)this;
894 1.22 jmcneill }
895 1.22 jmcneill
896 1.22 jmcneill static void
897 1.22 jmcneill pad_swvol_dtor(stream_filter_t *this)
898 1.22 jmcneill {
899 1.22 jmcneill if (this)
900 1.22 jmcneill kmem_free(this, sizeof(auvolconv_filter_t));
901 1.22 jmcneill }
902 1.22 jmcneill
903 1.44 pgoyette MODULE(MODULE_CLASS_DRIVER, pad, "audio");
904 1.44 pgoyette
905 1.13 ahoka #ifdef _MODULE
906 1.13 ahoka
907 1.48 pgoyette #include "ioconf.c"
908 1.45 pgoyette
909 1.48 pgoyette devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
910 1.13 ahoka
911 1.48 pgoyette /*
912 1.48 pgoyette * We need our own version of cfattach since config(1)'s ioconf does not
913 1.48 pgoyette * generate what we need
914 1.48 pgoyette */
915 1.13 ahoka
916 1.48 pgoyette static struct cfattach *pad_cfattachinit[] = { &pad_ca, NULL };
917 1.45 pgoyette
918 1.48 pgoyette static struct cfattachinit pad_cfattach[] = {
919 1.45 pgoyette { "pad", pad_cfattachinit },
920 1.45 pgoyette { NULL, NULL }
921 1.45 pgoyette };
922 1.47 pgoyette #endif
923 1.45 pgoyette
924 1.13 ahoka static int
925 1.13 ahoka pad_modcmd(modcmd_t cmd, void *arg)
926 1.13 ahoka {
927 1.44 pgoyette int error = 0;
928 1.13 ahoka
929 1.13 ahoka switch (cmd) {
930 1.13 ahoka case MODULE_CMD_INIT:
931 1.44 pgoyette #ifdef _MODULE
932 1.48 pgoyette pad_cfattach[1] = cfattach_ioconf_pad[0];
933 1.48 pgoyette error = config_init_component(cfdriver_ioconf_pad,
934 1.48 pgoyette pad_cfattach, cfdata_ioconf_pad);
935 1.45 pgoyette if (error)
936 1.44 pgoyette break;
937 1.13 ahoka
938 1.25 msaitoh error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
939 1.45 pgoyette &pad_cdevsw, &cmajor);
940 1.13 ahoka if (error) {
941 1.48 pgoyette config_fini_component(cfdriver_ioconf_pad,
942 1.48 pgoyette pad_cfattach, cfdata_ioconf_pad);
943 1.44 pgoyette break;
944 1.13 ahoka }
945 1.13 ahoka
946 1.44 pgoyette #endif
947 1.45 pgoyette break;
948 1.13 ahoka
949 1.13 ahoka case MODULE_CMD_FINI:
950 1.44 pgoyette #ifdef _MODULE
951 1.45 pgoyette error = devsw_detach(NULL, &pad_cdevsw);
952 1.45 pgoyette if (error)
953 1.45 pgoyette break;
954 1.45 pgoyette
955 1.48 pgoyette error = config_fini_component(cfdriver_ioconf_pad,
956 1.48 pgoyette pad_cfattach, cfdata_ioconf_pad);
957 1.13 ahoka if (error) {
958 1.45 pgoyette error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
959 1.45 pgoyette &pad_cdevsw, &cmajor);
960 1.44 pgoyette break;
961 1.13 ahoka }
962 1.44 pgoyette #endif
963 1.45 pgoyette break;
964 1.13 ahoka
965 1.13 ahoka default:
966 1.44 pgoyette error = ENOTTY;
967 1.13 ahoka }
968 1.44 pgoyette
969 1.44 pgoyette return error;
970 1.13 ahoka }
971