xref: /illumos-gate/usr/src/uts/common/io/audio/impl/audio_ddi.c (revision e4d060fb4c00d44cd578713eb9a921f594b733b8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (C) 4Front Technologies 1996-2008.
23  *
24  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/sysmacros.h>
30 #include <sys/stropts.h>
31 #include <sys/strsun.h>
32 #include <sys/list.h>
33 #include <sys/mkdev.h>
34 #include <sys/conf.h>
35 #include <sys/note.h>
36 #include <sys/atomic.h>
37 #include <sys/ddi.h>
38 #include <sys/sunddi.h>
39 
40 #include "audio_impl.h"
41 
42 /*
43  * Audio DDI glue implementation.
44  */
45 
46 /*
47  * The audio module is itself a pseudo driver, as it contains the
48  * logic to support un-associated nodes.  (Think generic /dev/mixer
49  * and /dev/sndstat used by OSS.)
50  */
51 static int
52 audio_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
53 {
54 	audio_dev_t	*adev;
55 
56 	/* pseudo devices don't need S/R support */
57 	if ((cmd != DDI_ATTACH) || (dip == NULL)) {
58 		return (DDI_FAILURE);
59 	}
60 
61 	if (ddi_get_instance(dip) != 0) {
62 		return (DDI_FAILURE);
63 	}
64 
65 	/* this can't fail */
66 	adev = audio_dev_alloc(dip, 0);
67 	adev->d_flags = DEV_SNDSTAT_CAP;
68 	audio_dev_set_description(adev, "Audio Common Code");
69 	audio_dev_set_version(adev, "pseudo");
70 	ddi_set_driver_private(dip, adev);
71 
72 	/* look up our properties! */
73 
74 	if (audio_dev_register(adev) != NULL) {
75 		audio_dev_free(adev);
76 		return (DDI_FAILURE);
77 	}
78 
79 	ddi_report_dev(dip);
80 
81 	return (DDI_SUCCESS);
82 }
83 
84 static int
85 audio_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
86 {
87 	audio_dev_t	*adev;
88 
89 	/* pseudo devices don't need S/R support */
90 	if (cmd != DDI_DETACH) {
91 		return (DDI_FAILURE);
92 	}
93 
94 	if (dip == NULL) {
95 		return (DDI_FAILURE);
96 	}
97 
98 	if ((adev = ddi_get_driver_private(dip)) == NULL) {
99 		return (DDI_FAILURE);
100 	}
101 
102 	if (audio_dev_unregister(adev) != DDI_SUCCESS) {
103 		return (DDI_FAILURE);
104 	}
105 
106 	audio_dev_free(adev);
107 
108 	return (DDI_SUCCESS);
109 }
110 
111 static int
112 audio_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resp)
113 {
114 	dip = NULL;
115 
116 	if (getminor((dev_t)arg) & AUDIO_MN_CLONE_MASK) {
117 		audio_client_t *c;
118 		c = auclnt_hold_by_devt((dev_t)arg);
119 		if (c != NULL) {
120 			dip = c->c_dev->d_dip;
121 			auclnt_release(c);
122 		}
123 	} else {
124 		audio_dev_t	*adev;
125 		if ((adev = auimpl_dev_hold_by_devt((dev_t)arg)) != NULL) {
126 			dip = adev->d_dip;
127 			auimpl_dev_release(adev);
128 		}
129 	}
130 
131 	if (dip == NULL) {
132 		return (DDI_FAILURE);
133 	}
134 
135 	switch (cmd) {
136 	case DDI_INFO_DEVT2DEVINFO:
137 		*resp = dip;
138 		break;
139 	case DDI_INFO_DEVT2INSTANCE:
140 		*resp = (void *)(uintptr_t)ddi_get_instance(dip);
141 		break;
142 	default:
143 		*resp = NULL;
144 		return (DDI_FAILURE);
145 	}
146 	return (DDI_SUCCESS);
147 }
148 
149 static int
150 audio_open(dev_t *devp, int oflag, int otyp, cred_t *credp)
151 {
152 	int			rv;
153 	audio_client_t		*c;
154 
155 	if (otyp == OTYP_BLK) {
156 		return (ENXIO);
157 	}
158 
159 	if ((c = auimpl_client_create(*devp)) == NULL) {
160 		audio_dev_warn(NULL, "client create failed");
161 		return (ENXIO);
162 	}
163 
164 	c->c_omode = oflag;
165 	c->c_pid = ddi_get_pid();
166 	c->c_cred = credp;
167 
168 	/*
169 	 * Call client/personality specific open handler.  Note that
170 	 * we "insist" that there is an open.  The personality layer
171 	 * will initialize/allocate any engines required.
172 	 *
173 	 * Hmm... do we need to pass in the cred?
174 	 */
175 	if ((rv = c->c_open(c, oflag)) != 0) {
176 		audio_dev_warn(c->c_dev, "open failed (rv %d)", rv);
177 		auimpl_client_destroy(c);
178 		return (rv);
179 	}
180 
181 	/* we do device cloning! */
182 	*devp = makedevice(c->c_major, c->c_minor);
183 
184 	/* now we can receive upcalls */
185 	auimpl_client_activate(c);
186 
187 	atomic_inc_uint(&c->c_dev->d_serial);
188 
189 	return (0);
190 }
191 
192 static int
193 audio_stropen(queue_t *rq, dev_t *devp, int oflag, int sflag, cred_t *credp)
194 {
195 	int			rv;
196 	audio_client_t		*c;
197 
198 	if (sflag != 0) {
199 		/* no direct clone or module opens */
200 		return (ENXIO);
201 	}
202 
203 	/*
204 	 * Make sure its a STREAMS personality - only legacy Sun API uses
205 	 * STREAMS.
206 	 */
207 	switch (AUDIO_MN_TYPE_MASK & getminor(*devp)) {
208 	case AUDIO_MINOR_DEVAUDIO:
209 	case AUDIO_MINOR_DEVAUDIOCTL:
210 		break;
211 	default:
212 		return (ENOSTR);
213 	}
214 
215 	if ((c = auimpl_client_create(*devp)) == NULL) {
216 		audio_dev_warn(NULL, "client create failed");
217 		return (ENXIO);
218 	}
219 
220 	rq->q_ptr = WR(rq)->q_ptr = c;
221 	c->c_omode = oflag;
222 	c->c_pid = ddi_get_pid();
223 	c->c_cred = credp;
224 	c->c_rq = rq;
225 	c->c_wq = WR(rq);
226 
227 	/*
228 	 * Call client/personality specific open handler.  Note that
229 	 * we "insist" that there is an open.  The personality layer
230 	 * will initialize/allocate any engines required.
231 	 *
232 	 * Hmm... do we need to pass in the cred?
233 	 */
234 	if ((rv = c->c_open(c, oflag)) != 0) {
235 		audio_dev_warn(c->c_dev, "open failed (rv %d)", rv);
236 		auimpl_client_destroy(c);
237 		return (rv);
238 	}
239 
240 	/* we do device cloning! */
241 	*devp = makedevice(c->c_major, c->c_minor);
242 
243 	qprocson(rq);
244 
245 	/* now we can receive upcalls */
246 	auimpl_client_activate(c);
247 
248 	atomic_inc_uint(&c->c_dev->d_serial);
249 
250 	return (0);
251 }
252 
253 static int
254 audio_strclose(queue_t *rq, int flag, cred_t *credp)
255 {
256 	audio_client_t	*c;
257 	audio_dev_t	*d;
258 	int		rv;
259 
260 	_NOTE(ARGUNUSED(flag));
261 	_NOTE(ARGUNUSED(credp));
262 
263 	if ((c = rq->q_ptr) == NULL) {
264 		return (ENXIO);
265 	}
266 	if (ddi_can_receive_sig() || (ddi_get_pid() == 0)) {
267 		rv = auclnt_drain(c);
268 	}
269 
270 	/* make sure we won't get any upcalls */
271 	auimpl_client_deactivate(c);
272 
273 	/*
274 	 * Pick up any data sitting around in input buffers.  This
275 	 * avoids leaving record data stuck in queues.
276 	 */
277 	if (c->c_istream.s_engine != NULL)
278 		audio_engine_produce(c->c_istream.s_engine);
279 
280 	/* get a local hold on the device */
281 	d = c->c_dev;
282 	auimpl_dev_hold(c->c_dev);
283 
284 	/* Turn off queue processing... */
285 	qprocsoff(rq);
286 
287 	/* Call personality specific close handler */
288 	c->c_close(c);
289 
290 	auimpl_client_destroy(c);
291 
292 	/* notify peers that a change has occurred */
293 	atomic_inc_uint(&d->d_serial);
294 
295 	/* now we can drop the release we had on the device */
296 	auimpl_dev_release(d);
297 
298 	return (rv);
299 }
300 
301 static int
302 audio_close(dev_t dev, int flag, int otyp, cred_t *credp)
303 {
304 	audio_client_t	*c;
305 	audio_dev_t	*d;
306 
307 	_NOTE(ARGUNUSED(flag));
308 	_NOTE(ARGUNUSED(credp));
309 	_NOTE(ARGUNUSED(otyp));
310 
311 	if ((c = auclnt_hold_by_devt(dev)) == NULL) {
312 		audio_dev_warn(NULL, "close on bogus devt %x,%x",
313 		    getmajor(dev), getminor(dev));
314 		return (ENXIO);
315 	}
316 
317 	/* we don't want any upcalls anymore */
318 	auimpl_client_deactivate(c);
319 
320 	/*
321 	 * Pick up any data sitting around in input buffers.  This
322 	 * avoids leaving record data stuck in queues.
323 	 */
324 	if (c->c_istream.s_engine != NULL)
325 		audio_engine_produce(c->c_istream.s_engine);
326 
327 	/* get a local hold on the device */
328 	d = c->c_dev;
329 	auimpl_dev_hold(c->c_dev);
330 
331 	/*
332 	 * NB: This must be done before c->c_close, since it calls
333 	 * auclnt_close which will block waiting for the refence count
334 	 * to drop to zero.
335 	 */
336 	auclnt_release(c);
337 
338 	/* Call personality specific close handler */
339 	c->c_close(c);
340 
341 	auimpl_client_destroy(c);
342 
343 	/* notify peers that a change has occurred */
344 	atomic_inc_uint(&d->d_serial);
345 
346 	/* now we can drop the release we had on the device */
347 	auimpl_dev_release(d);
348 
349 	return (0);
350 }
351 
352 static int
353 audio_write(dev_t dev, struct uio *uio, cred_t *credp)
354 {
355 	audio_client_t *c;
356 	int rv;
357 
358 	if ((c = auclnt_hold_by_devt(dev)) == NULL) {
359 		return (ENXIO);
360 	}
361 	rv = (c->c_write == NULL) ? ENXIO : c->c_write(c, uio, credp);
362 	auclnt_release(c);
363 
364 	return (rv);
365 }
366 
367 static int
368 audio_read(dev_t dev, struct uio *uio, cred_t *credp)
369 {
370 	audio_client_t *c;
371 	int rv;
372 
373 	if ((c = auclnt_hold_by_devt(dev)) == NULL) {
374 		return (ENXIO);
375 	}
376 	rv = (c->c_read == NULL) ? ENXIO : c->c_read(c, uio, credp);
377 	auclnt_release(c);
378 
379 	return (rv);
380 }
381 
382 static int
383 audio_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
384     int *rvalp)
385 {
386 	audio_client_t *c;
387 	int rv;
388 
389 	if ((c = auclnt_hold_by_devt(dev)) == NULL) {
390 		return (ENXIO);
391 	}
392 	rv = (c->c_ioctl == NULL) ? ENXIO : c->c_ioctl(c, cmd, arg, mode,
393 	    credp, rvalp);
394 	auclnt_release(c);
395 
396 	return (rv);
397 }
398 
399 static int
400 audio_chpoll(dev_t dev, short events, int anyyet, short *reventsp,
401     struct pollhead **phpp)
402 {
403 	audio_client_t *c;
404 	int rv;
405 
406 	if ((c = auclnt_hold_by_devt(dev)) == NULL) {
407 		return (ENXIO);
408 	}
409 	rv = (c->c_chpoll == NULL) ?
410 	    ENXIO :
411 	    c->c_chpoll(c, events, anyyet, reventsp, phpp);
412 	auclnt_release(c);
413 
414 	return (rv);
415 }
416 
417 static int
418 audio_wput(queue_t *wq, mblk_t *mp)
419 {
420 	audio_client_t	*c;
421 
422 	c = wq->q_ptr;
423 	if (c->c_wput) {
424 		c->c_wput(c, mp);
425 	} else {
426 		freemsg(mp);
427 	}
428 	return (0);
429 }
430 
431 static int
432 audio_wsrv(queue_t *wq)
433 {
434 	audio_client_t	*c;
435 
436 	c = wq->q_ptr;
437 	if (c->c_wsrv) {
438 		c->c_wsrv(c);
439 	} else {
440 		flushq(wq, FLUSHALL);
441 	}
442 	return (0);
443 }
444 
445 static int
446 audio_rsrv(queue_t *rq)
447 {
448 	audio_client_t	*c;
449 
450 	c = rq->q_ptr;
451 	if (c->c_rsrv) {
452 		c->c_rsrv(c);
453 	} else {
454 		flushq(rq, FLUSHALL);
455 	}
456 	return (0);
457 }
458 
459 
460 static struct dev_ops audio_dev_ops = {
461 	DEVO_REV,		/* rev */
462 	0,			/* refcnt */
463 	NULL,			/* getinfo */
464 	nulldev,		/* identify */
465 	nulldev,		/* probe */
466 	audio_attach,		/* attach */
467 	audio_detach,		/* detach */
468 	nodev,			/* reset */
469 	NULL,			/* cb_ops */
470 	NULL,			/* bus_ops */
471 	NULL,			/* power */
472 };
473 
474 static struct modldrv modldrv = {
475 	&mod_driverops,
476 	"Audio Framework",
477 	&audio_dev_ops,
478 };
479 
480 static struct modlinkage modlinkage = {
481 	MODREV_1,			/* MODREV_1 indicated by manual */
482 	&modldrv,
483 	NULL
484 };
485 
486 struct audio_ops_helper {
487 	struct cb_ops		cbops;	/* NB: must be first */
488 	struct streamtab	strtab;
489 	struct qinit		rqinit;
490 	struct qinit		wqinit;
491 	struct module_info	minfo;
492 	char			name[MODMAXNAMELEN+1];
493 };
494 
495 void
496 audio_init_ops(struct dev_ops *devops, const char *name)
497 {
498 	struct audio_ops_helper	*helper;
499 
500 	helper = kmem_zalloc(sizeof (*helper), KM_SLEEP);
501 
502 	(void) strlcpy(helper->name, name, sizeof (helper->name));
503 
504 	helper->minfo.mi_idnum = 0;	/* only for strlog(1M) */
505 	helper->minfo.mi_idname = helper->name;
506 	helper->minfo.mi_minpsz = 0;
507 	helper->minfo.mi_maxpsz = 8192;
508 	helper->minfo.mi_hiwat = 65536;
509 	helper->minfo.mi_lowat = 32768;
510 
511 	helper->wqinit.qi_putp = audio_wput;
512 	helper->wqinit.qi_srvp = audio_wsrv;
513 	helper->wqinit.qi_qopen = NULL;
514 	helper->wqinit.qi_qclose = NULL;
515 	helper->wqinit.qi_qadmin = NULL;
516 	helper->wqinit.qi_minfo = &helper->minfo;
517 	helper->wqinit.qi_mstat = NULL;
518 
519 	helper->rqinit.qi_putp = putq;
520 	helper->rqinit.qi_srvp = audio_rsrv;
521 	helper->rqinit.qi_qopen = audio_stropen;
522 	helper->rqinit.qi_qclose = audio_strclose;
523 	helper->rqinit.qi_qadmin = NULL;
524 	helper->rqinit.qi_minfo = &helper->minfo;
525 	helper->rqinit.qi_mstat = NULL;
526 
527 	helper->strtab.st_rdinit = &helper->rqinit;
528 	helper->strtab.st_wrinit = &helper->wqinit;
529 	helper->strtab.st_muxrinit = NULL;
530 	helper->strtab.st_muxwinit = NULL;
531 
532 	helper->cbops.cb_open = audio_open;
533 	helper->cbops.cb_close = audio_close;
534 	helper->cbops.cb_strategy = nodev;
535 	helper->cbops.cb_print = nodev;
536 	helper->cbops.cb_dump = nodev;
537 	helper->cbops.cb_read = audio_read;
538 	helper->cbops.cb_write = audio_write;
539 	helper->cbops.cb_ioctl = audio_ioctl;
540 	helper->cbops.cb_devmap = nodev;
541 	helper->cbops.cb_mmap = nodev;
542 	helper->cbops.cb_segmap = nodev;
543 	helper->cbops.cb_chpoll = audio_chpoll;
544 	helper->cbops.cb_prop_op = ddi_prop_op;
545 	helper->cbops.cb_str = &helper->strtab;
546 	helper->cbops.cb_flag = D_MP | D_64BIT;
547 	helper->cbops.cb_rev = CB_REV;
548 	helper->cbops.cb_aread = nodev;
549 	helper->cbops.cb_awrite = nodev;
550 
551 	devops->devo_cb_ops = &helper->cbops;
552 	devops->devo_getinfo = audio_getinfo;
553 }
554 
555 void
556 audio_fini_ops(struct dev_ops *devops)
557 {
558 	kmem_free(devops->devo_cb_ops, sizeof (struct audio_ops_helper));
559 	devops->devo_cb_ops = NULL;
560 	devops->devo_getinfo = NULL;
561 }
562 
563 void
564 auimpl_dev_vwarn(audio_dev_t *dev, const char *fmt, va_list va)
565 {
566 	char	buf[256];
567 
568 	if (dev != NULL) {
569 		(void) snprintf(buf, sizeof (buf), "%s#%d: %s",
570 		    ddi_driver_name(dev->d_dip), ddi_get_instance(dev->d_dip),
571 		    fmt);
572 	} else {
573 		(void) snprintf(buf, sizeof (buf), "audio: %s", fmt);
574 	}
575 
576 	vcmn_err(CE_WARN, buf, va);
577 }
578 
579 
580 void
581 audio_dev_warn(audio_dev_t *dev, const char *fmt, ...)
582 {
583 	va_list	va;
584 
585 	va_start(va, fmt);
586 	auimpl_dev_vwarn(dev, fmt, va);
587 	va_end(va);
588 }
589 
590 /*
591  * _init, _info, and _fini DDI glue.
592  */
593 int
594 _init(void)
595 {
596 	int	rv;
597 
598 	auimpl_client_init();
599 	auimpl_dev_init();
600 	auimpl_sun_init();
601 	auimpl_oss_init();
602 
603 	audio_init_ops(&audio_dev_ops, "audio");
604 
605 	if ((rv = mod_install(&modlinkage)) != 0) {
606 		audio_fini_ops(&audio_dev_ops);
607 		auimpl_dev_fini();
608 		auimpl_client_fini();
609 	}
610 	return (rv);
611 }
612 
613 int
614 _info(struct modinfo *modinfop)
615 {
616 	return (mod_info(&modlinkage, modinfop));
617 }
618 
619 int
620 _fini(void)
621 {
622 	int rv;
623 
624 	if ((rv = mod_remove(&modlinkage)) != 0)
625 		return (rv);
626 
627 	auimpl_dev_fini();
628 	auimpl_client_fini();
629 
630 	return (rv);
631 }
632