xref: /illumos-gate/usr/src/uts/common/io/1394/targets/av1394/av1394.c (revision 2570281cf351044b6936651ce26dbe1f801dcbd8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*19397407SSherry Moore  * Common Development and Distribution License (the "License").
6*19397407SSherry Moore  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*19397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * av1394 driver
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/errno.h>
337c478bd9Sstevel@tonic-gate #include <sys/cred.h>
347c478bd9Sstevel@tonic-gate #include <sys/conf.h>
357c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/1394/targets/av1394/av1394_impl.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /* DDI/DKI entry points */
437c478bd9Sstevel@tonic-gate static int	av1394_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
447c478bd9Sstevel@tonic-gate static int	av1394_attach(dev_info_t *, ddi_attach_cmd_t);
457c478bd9Sstevel@tonic-gate static int	av1394_detach(dev_info_t *, ddi_detach_cmd_t);
467c478bd9Sstevel@tonic-gate static int 	av1394_open(dev_t *, int, int, cred_t *);
477c478bd9Sstevel@tonic-gate static int	av1394_close(dev_t, int, int, cred_t *);
487c478bd9Sstevel@tonic-gate static int	av1394_read(dev_t, struct uio *, cred_t *);
497c478bd9Sstevel@tonic-gate static int	av1394_write(dev_t, struct uio *, cred_t *);
507c478bd9Sstevel@tonic-gate static int	av1394_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
517c478bd9Sstevel@tonic-gate static int	av1394_devmap(dev_t, devmap_cookie_t, offset_t, size_t,
527c478bd9Sstevel@tonic-gate 		size_t *, uint_t);
537c478bd9Sstevel@tonic-gate static int	av1394_poll(dev_t, short, int, short *, struct pollhead **);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* configuration routines */
567c478bd9Sstevel@tonic-gate static void	av1394_cleanup(av1394_inst_t *, int);
577c478bd9Sstevel@tonic-gate static int	av1394_t1394_attach(av1394_inst_t *, dev_info_t *);
587c478bd9Sstevel@tonic-gate static void	av1394_t1394_detach(av1394_inst_t *);
597c478bd9Sstevel@tonic-gate static int	av1394_add_events(av1394_inst_t *);
607c478bd9Sstevel@tonic-gate static void	av1394_remove_events(av1394_inst_t *);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /* CPR */
637c478bd9Sstevel@tonic-gate static int	av1394_cpr_suspend(av1394_inst_t *);
647c478bd9Sstevel@tonic-gate static int 	av1394_cpr_resume(av1394_inst_t *);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* callbacks */
677c478bd9Sstevel@tonic-gate static void	av1394_bus_reset(dev_info_t *, ddi_eventcookie_t, void *,
687c478bd9Sstevel@tonic-gate 		void *);
697c478bd9Sstevel@tonic-gate static void	av1394_disconnect(dev_info_t *, ddi_eventcookie_t, void *,
707c478bd9Sstevel@tonic-gate 		void *);
717c478bd9Sstevel@tonic-gate static void	av1394_reconnect(dev_info_t *, ddi_eventcookie_t, void *,
727c478bd9Sstevel@tonic-gate 		void *);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate struct cb_ops av1394_cb_ops = {
777c478bd9Sstevel@tonic-gate 	av1394_open,		/* open  */
787c478bd9Sstevel@tonic-gate 	av1394_close,		/* close */
797c478bd9Sstevel@tonic-gate 	nulldev,		/* strategy */
807c478bd9Sstevel@tonic-gate 	nulldev,		/* print */
817c478bd9Sstevel@tonic-gate 	nulldev,		/* dump */
827c478bd9Sstevel@tonic-gate 	av1394_read,		/* read */
837c478bd9Sstevel@tonic-gate 	av1394_write,		/* write */
847c478bd9Sstevel@tonic-gate 	av1394_ioctl,		/* ioctl */
857c478bd9Sstevel@tonic-gate 	av1394_devmap,		/* devmap */
867c478bd9Sstevel@tonic-gate 	nulldev,		/* mmap */
877c478bd9Sstevel@tonic-gate 	nulldev,		/* segmap */
887c478bd9Sstevel@tonic-gate 	av1394_poll,		/* poll */
897c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
907c478bd9Sstevel@tonic-gate 	NULL,			/* streamtab  */
917c478bd9Sstevel@tonic-gate 	D_MP | D_NEW | D_HOTPLUG | D_DEVMAP
927c478bd9Sstevel@tonic-gate };
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static struct dev_ops av1394_ops = {
957c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
967c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
977c478bd9Sstevel@tonic-gate 	av1394_getinfo,		/* getinfo */
987c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
997c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
1007c478bd9Sstevel@tonic-gate 	av1394_attach,		/* attach */
1017c478bd9Sstevel@tonic-gate 	av1394_detach,		/* detach */
1027c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
1037c478bd9Sstevel@tonic-gate 	&av1394_cb_ops,		/* driver operations */
1047c478bd9Sstevel@tonic-gate 	NULL,			/* bus operations */
105*19397407SSherry Moore 	NULL,			/* power */
106*19397407SSherry Moore 	ddi_quiesce_not_supported,	/* devo_quiesce */
1077c478bd9Sstevel@tonic-gate };
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static struct modldrv av1394_modldrv =	{
1107c478bd9Sstevel@tonic-gate 	&mod_driverops,
111*19397407SSherry Moore 	"IEEE 1394 AV driver",
1127c478bd9Sstevel@tonic-gate 	&av1394_ops
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static struct modlinkage av1394_modlinkage = {
1167c478bd9Sstevel@tonic-gate 	MODREV_1,
1177c478bd9Sstevel@tonic-gate 	&av1394_modldrv,
1187c478bd9Sstevel@tonic-gate 	NULL,
1197c478bd9Sstevel@tonic-gate };
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static void *av1394_statep;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #define	AV1394_INST2STATE(inst)	(ddi_get_soft_state(av1394_statep, inst))
1247c478bd9Sstevel@tonic-gate #define	AV1394_DEV2STATE(dev)	\
1257c478bd9Sstevel@tonic-gate 		(ddi_get_soft_state(av1394_statep, AV1394_DEV2INST(dev)))
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  * --- DDI/DKI entry points
1307c478bd9Sstevel@tonic-gate  *
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate int
_init(void)1337c478bd9Sstevel@tonic-gate _init(void)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	int    error;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	error = ddi_soft_state_init(&av1394_statep, sizeof (av1394_inst_t), 1);
1387c478bd9Sstevel@tonic-gate 	if (error != 0) {
1397c478bd9Sstevel@tonic-gate 		return (error);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if ((error = mod_install(&av1394_modlinkage)) != 0) {
1437c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&av1394_statep);
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	return (error);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate int
_fini(void)1507c478bd9Sstevel@tonic-gate _fini(void)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	int    error;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if ((error = mod_remove(&av1394_modlinkage)) == 0) {
1557c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&av1394_statep);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	return (error);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1627c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	return (mod_info(&av1394_modlinkage, modinfop));
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * attach
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate static int
av1394_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)1717c478bd9Sstevel@tonic-gate av1394_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	int		instance = ddi_get_instance(dip);
1747c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	switch (cmd) {
1777c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
1787c478bd9Sstevel@tonic-gate 		break;
1797c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
1807c478bd9Sstevel@tonic-gate 		if ((avp = AV1394_INST2STATE(instance)) == NULL) {
1817c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1827c478bd9Sstevel@tonic-gate 		}
1837c478bd9Sstevel@tonic-gate 		return (av1394_cpr_resume(avp));
1847c478bd9Sstevel@tonic-gate 	default:
1857c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(av1394_statep, instance) != 0) {
1897c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 	avp = AV1394_INST2STATE(instance);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (av1394_t1394_attach(avp, dip) != DDI_SUCCESS) {
1947c478bd9Sstevel@tonic-gate 		av1394_cleanup(avp, 1);
1957c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	mutex_init(&avp->av_mutex, NULL, MUTEX_DRIVER,
1997c478bd9Sstevel@tonic-gate 	    avp->av_attachinfo.iblock_cookie);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate #ifndef __lock_lint
2027c478bd9Sstevel@tonic-gate 	avp->av_dip = dip;
2037c478bd9Sstevel@tonic-gate 	avp->av_instance = instance;
2047c478bd9Sstevel@tonic-gate #endif
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (av1394_add_events(avp) != DDI_SUCCESS) {
2077c478bd9Sstevel@tonic-gate 		av1394_cleanup(avp, 2);
2087c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (av1394_isoch_attach(avp) != DDI_SUCCESS) {
2127c478bd9Sstevel@tonic-gate 		av1394_cleanup(avp, 3);
2137c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (av1394_async_attach(avp) != DDI_SUCCESS) {
2177c478bd9Sstevel@tonic-gate 		av1394_cleanup(avp, 4);
2187c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate #ifndef __lock_lint
2227c478bd9Sstevel@tonic-gate 	avp->av_dev_state = AV1394_DEV_ONLINE;
2237c478bd9Sstevel@tonic-gate #endif
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static int
av1394_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)2317c478bd9Sstevel@tonic-gate av1394_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	int		instance = ddi_get_instance(dip);
2347c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if ((avp = AV1394_INST2STATE(instance)) == NULL) {
2377c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	switch (cmd) {
2417c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
2427c478bd9Sstevel@tonic-gate 		av1394_cleanup(avp, AV1394_CLEANUP_LEVEL_MAX);
2437c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2447c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
2457c478bd9Sstevel@tonic-gate 		return (av1394_cpr_suspend(avp));
2467c478bd9Sstevel@tonic-gate 	default:
2477c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2527c478bd9Sstevel@tonic-gate static int
av1394_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2537c478bd9Sstevel@tonic-gate av1394_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2547c478bd9Sstevel@tonic-gate 		void **result)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	dev_t		dev = (dev_t)arg;
2577c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp;
2587c478bd9Sstevel@tonic-gate 	int		rval = DDI_FAILURE;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	switch (infocmd) {
2617c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2627c478bd9Sstevel@tonic-gate 		if ((avp = AV1394_DEV2STATE(dev)) != NULL) {
2637c478bd9Sstevel@tonic-gate 			*result = avp->av_dip;
2647c478bd9Sstevel@tonic-gate 			rval = DDI_SUCCESS;
2657c478bd9Sstevel@tonic-gate 		} else {
2667c478bd9Sstevel@tonic-gate 			*result = NULL;
2677c478bd9Sstevel@tonic-gate 		}
2687c478bd9Sstevel@tonic-gate 		break;
2697c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2707c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)AV1394_DEV2INST(dev);
2717c478bd9Sstevel@tonic-gate 		rval = DDI_SUCCESS;
2727c478bd9Sstevel@tonic-gate 		break;
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	return (rval);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2797c478bd9Sstevel@tonic-gate static int
av1394_open(dev_t * dev,int flag,int otyp,cred_t * cr)2807c478bd9Sstevel@tonic-gate av1394_open(dev_t *dev, int flag, int otyp, cred_t *cr)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = AV1394_DEV2STATE(*dev);
2837c478bd9Sstevel@tonic-gate 	int		ret = ENXIO;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (avp != NULL) {
2867c478bd9Sstevel@tonic-gate 		if (AV1394_DEV_IS_ISOCH(*dev)) {
2877c478bd9Sstevel@tonic-gate 			ret = 0;
2887c478bd9Sstevel@tonic-gate 		} else if (AV1394_DEV_IS_ASYNC(*dev)) {
2897c478bd9Sstevel@tonic-gate 			ret = av1394_async_open(avp, flag);
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 	return (ret);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2967c478bd9Sstevel@tonic-gate static int
av1394_close(dev_t dev,int flag,int otyp,cred_t * cr)2977c478bd9Sstevel@tonic-gate av1394_close(dev_t dev, int flag, int otyp, cred_t *cr)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = AV1394_DEV2STATE(dev);
3007c478bd9Sstevel@tonic-gate 	int		ret = ENXIO;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (avp != NULL) {
3037c478bd9Sstevel@tonic-gate 		if (AV1394_DEV_IS_ISOCH(dev)) {
3047c478bd9Sstevel@tonic-gate 			ret = av1394_isoch_close(avp, flag);
3057c478bd9Sstevel@tonic-gate 		} else if (AV1394_DEV_IS_ASYNC(dev)) {
3067c478bd9Sstevel@tonic-gate 			ret = av1394_async_close(avp, flag);
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	return (ret);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3137c478bd9Sstevel@tonic-gate static int
av1394_read(dev_t dev,struct uio * uiop,cred_t * cr)3147c478bd9Sstevel@tonic-gate av1394_read(dev_t dev, struct uio *uiop, cred_t *cr)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = AV1394_DEV2STATE(dev);
3177c478bd9Sstevel@tonic-gate 	int		ret = ENXIO;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (avp != NULL) {
3207c478bd9Sstevel@tonic-gate 		if (AV1394_DEV_IS_ISOCH(dev)) {
3217c478bd9Sstevel@tonic-gate 			ret = av1394_isoch_read(avp, uiop);
3227c478bd9Sstevel@tonic-gate 		} else if (AV1394_DEV_IS_ASYNC(dev)) {
3237c478bd9Sstevel@tonic-gate 			ret = av1394_async_read(avp, uiop);
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	return (ret);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3307c478bd9Sstevel@tonic-gate static int
av1394_write(dev_t dev,struct uio * uiop,cred_t * cr)3317c478bd9Sstevel@tonic-gate av1394_write(dev_t dev, struct uio *uiop, cred_t *cr)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = AV1394_DEV2STATE(dev);
3347c478bd9Sstevel@tonic-gate 	int		ret = ENXIO;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (avp != NULL) {
3377c478bd9Sstevel@tonic-gate 		if (AV1394_DEV_IS_ISOCH(dev)) {
3387c478bd9Sstevel@tonic-gate 			ret = av1394_isoch_write(avp, uiop);
3397c478bd9Sstevel@tonic-gate 		} else if (AV1394_DEV_IS_ASYNC(dev)) {
3407c478bd9Sstevel@tonic-gate 			ret = av1394_async_write(avp, uiop);
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	return (ret);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3477c478bd9Sstevel@tonic-gate static int
av1394_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * cr,int * rvalp)3487c478bd9Sstevel@tonic-gate av1394_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cr, int *rvalp)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = AV1394_DEV2STATE(dev);
3517c478bd9Sstevel@tonic-gate 	int		ret = ENXIO;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (avp != NULL) {
3547c478bd9Sstevel@tonic-gate 		if (AV1394_DEV_IS_ISOCH(dev)) {
3557c478bd9Sstevel@tonic-gate 			ret = av1394_isoch_ioctl(avp, cmd, arg, mode, rvalp);
3567c478bd9Sstevel@tonic-gate 		} else if (AV1394_DEV_IS_ASYNC(dev)) {
3577c478bd9Sstevel@tonic-gate 			ret = av1394_async_ioctl(avp, cmd, arg, mode, rvalp);
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	return (ret);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3647c478bd9Sstevel@tonic-gate static int
av1394_devmap(dev_t dev,devmap_cookie_t dhp,offset_t off,size_t len,size_t * maplen,uint_t model)3657c478bd9Sstevel@tonic-gate av1394_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
3667c478bd9Sstevel@tonic-gate 	size_t *maplen, uint_t model)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = AV1394_DEV2STATE(dev);
3697c478bd9Sstevel@tonic-gate 	int		ret = ENXIO;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	if ((avp != NULL) && (AV1394_DEV_IS_ISOCH(dev))) {
3727c478bd9Sstevel@tonic-gate 		ret = av1394_isoch_devmap(avp, dhp, off, len, maplen, model);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 	return (ret);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate static int
av1394_poll(dev_t dev,short events,int anyyet,short * reventsp,struct pollhead ** phpp)3787c478bd9Sstevel@tonic-gate av1394_poll(dev_t dev, short events, int anyyet, short *reventsp,
3797c478bd9Sstevel@tonic-gate 		struct pollhead **phpp)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = AV1394_DEV2STATE(dev);
3827c478bd9Sstevel@tonic-gate 	int		ret = ENXIO;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if ((avp != NULL) && AV1394_DEV_IS_ASYNC(dev)) {
3857c478bd9Sstevel@tonic-gate 		ret = av1394_async_poll(avp, events, anyyet, reventsp, phpp);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 	return (ret);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  *
3937c478bd9Sstevel@tonic-gate  * --- configuration routines
3947c478bd9Sstevel@tonic-gate  *
3957c478bd9Sstevel@tonic-gate  * av1394_cleanup()
3967c478bd9Sstevel@tonic-gate  *    Cleanup after attach
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate static void
av1394_cleanup(av1394_inst_t * avp,int level)3997c478bd9Sstevel@tonic-gate av1394_cleanup(av1394_inst_t *avp, int level)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	ASSERT((level > 0) && (level <= AV1394_CLEANUP_LEVEL_MAX));
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	switch (level) {
4047c478bd9Sstevel@tonic-gate 	default:
4057c478bd9Sstevel@tonic-gate 		av1394_async_detach(avp);
4067c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
4077c478bd9Sstevel@tonic-gate 	case 4:
4087c478bd9Sstevel@tonic-gate 		av1394_isoch_detach(avp);
4097c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
4107c478bd9Sstevel@tonic-gate 	case 3:
4117c478bd9Sstevel@tonic-gate 		av1394_remove_events(avp);
4127c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
4137c478bd9Sstevel@tonic-gate 	case 2:
4147c478bd9Sstevel@tonic-gate 		av1394_t1394_detach(avp);
4157c478bd9Sstevel@tonic-gate 		mutex_destroy(&avp->av_mutex);
4167c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
4177c478bd9Sstevel@tonic-gate 	case 1:
4187c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(av1394_statep, avp->av_instance);
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate static int
av1394_t1394_attach(av1394_inst_t * avp,dev_info_t * dip)4237c478bd9Sstevel@tonic-gate av1394_t1394_attach(av1394_inst_t *avp, dev_info_t *dip)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	int	ret;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	ret = t1394_attach(dip, T1394_VERSION_V1, 0, &avp->av_attachinfo,
4287c478bd9Sstevel@tonic-gate 	    &avp->av_t1394_hdl);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	return (ret);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate static void
av1394_t1394_detach(av1394_inst_t * avp)4347c478bd9Sstevel@tonic-gate av1394_t1394_detach(av1394_inst_t *avp)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	(void) t1394_detach(&avp->av_t1394_hdl, 0);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate static int
av1394_add_events(av1394_inst_t * avp)4407c478bd9Sstevel@tonic-gate av1394_add_events(av1394_inst_t *avp)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t	br_evc, rem_evc, ins_evc;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if (ddi_get_eventcookie(avp->av_dip, DDI_DEVI_BUS_RESET_EVENT,
4457c478bd9Sstevel@tonic-gate 	    &br_evc) != DDI_SUCCESS) {
4467c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	if (ddi_add_event_handler(avp->av_dip, br_evc, av1394_bus_reset,
4497c478bd9Sstevel@tonic-gate 	    avp, &avp->av_reset_cb) != DDI_SUCCESS) {
4507c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if (ddi_get_eventcookie(avp->av_dip, DDI_DEVI_REMOVE_EVENT,
4547c478bd9Sstevel@tonic-gate 	    &rem_evc) != DDI_SUCCESS) {
4557c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(avp->av_reset_cb);
4567c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 	if (ddi_add_event_handler(avp->av_dip, rem_evc, av1394_disconnect,
4597c478bd9Sstevel@tonic-gate 	    avp, &avp->av_remove_cb) != DDI_SUCCESS) {
4607c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(avp->av_reset_cb);
4617c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (ddi_get_eventcookie(avp->av_dip, DDI_DEVI_INSERT_EVENT,
4657c478bd9Sstevel@tonic-gate 	    &ins_evc) != DDI_SUCCESS) {
4667c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(avp->av_remove_cb);
4677c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(avp->av_reset_cb);
4687c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 	if (ddi_add_event_handler(avp->av_dip, ins_evc, av1394_reconnect,
4717c478bd9Sstevel@tonic-gate 	    avp, &avp->av_insert_cb) != DDI_SUCCESS) {
4727c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(avp->av_remove_cb);
4737c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(avp->av_reset_cb);
4747c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static void
av1394_remove_events(av1394_inst_t * avp)4817c478bd9Sstevel@tonic-gate av1394_remove_events(av1394_inst_t *avp)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t	evc;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (ddi_get_eventcookie(avp->av_dip, DDI_DEVI_INSERT_EVENT,
4867c478bd9Sstevel@tonic-gate 	    &evc) == DDI_SUCCESS) {
4877c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(avp->av_insert_cb);
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if (ddi_get_eventcookie(avp->av_dip, DDI_DEVI_REMOVE_EVENT,
4917c478bd9Sstevel@tonic-gate 	    &evc) == DDI_SUCCESS) {
4927c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(avp->av_remove_cb);
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	if (ddi_get_eventcookie(avp->av_dip, DDI_DEVI_BUS_RESET_EVENT,
4967c478bd9Sstevel@tonic-gate 	    &evc) == DDI_SUCCESS) {
4977c478bd9Sstevel@tonic-gate 		(void) ddi_remove_event_handler(avp->av_reset_cb);
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate /*
5027c478bd9Sstevel@tonic-gate  *
5037c478bd9Sstevel@tonic-gate  * --- CPR
5047c478bd9Sstevel@tonic-gate  *
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate static int
av1394_cpr_suspend(av1394_inst_t * avp)5077c478bd9Sstevel@tonic-gate av1394_cpr_suspend(av1394_inst_t *avp)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	int	ret;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	ret = av1394_isoch_cpr_suspend(avp);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
5147c478bd9Sstevel@tonic-gate 		mutex_enter(&avp->av_mutex);
5157c478bd9Sstevel@tonic-gate 		avp->av_prev_dev_state = avp->av_dev_state;
5167c478bd9Sstevel@tonic-gate 		avp->av_dev_state = AV1394_DEV_SUSPENDED;
5177c478bd9Sstevel@tonic-gate 		mutex_exit(&avp->av_mutex);
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	return (ret);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate  * CPR resume should always succeed
5257c478bd9Sstevel@tonic-gate  */
5267c478bd9Sstevel@tonic-gate static int
av1394_cpr_resume(av1394_inst_t * avp)5277c478bd9Sstevel@tonic-gate av1394_cpr_resume(av1394_inst_t *avp)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	mutex_enter(&avp->av_mutex);
5307c478bd9Sstevel@tonic-gate 	avp->av_dev_state = avp->av_prev_dev_state;
5317c478bd9Sstevel@tonic-gate 	mutex_exit(&avp->av_mutex);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	(void) av1394_async_cpr_resume(avp);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  *
5407c478bd9Sstevel@tonic-gate  * --- callbacks
5417c478bd9Sstevel@tonic-gate  *
5427c478bd9Sstevel@tonic-gate  */
5437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5447c478bd9Sstevel@tonic-gate static void
av1394_bus_reset(dev_info_t * dip,ddi_eventcookie_t evc,void * arg,void * data)5457c478bd9Sstevel@tonic-gate av1394_bus_reset(dev_info_t *dip, ddi_eventcookie_t evc, void *arg, void *data)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = arg;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (avp == NULL) {
5507c478bd9Sstevel@tonic-gate 		return;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	mutex_enter(&avp->av_mutex);
5547c478bd9Sstevel@tonic-gate 	avp->av_attachinfo.localinfo = *(t1394_localinfo_t *)data;
5557c478bd9Sstevel@tonic-gate 	mutex_exit(&avp->av_mutex);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	av1394_async_bus_reset(avp);
5587c478bd9Sstevel@tonic-gate 	av1394_cmp_bus_reset(avp);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5627c478bd9Sstevel@tonic-gate static void
av1394_disconnect(dev_info_t * dip,ddi_eventcookie_t evc,void * arg,void * data)5637c478bd9Sstevel@tonic-gate av1394_disconnect(dev_info_t *dip, ddi_eventcookie_t evc, void *arg, void *data)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = arg;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	if (avp == NULL) {
5687c478bd9Sstevel@tonic-gate 		return;
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	mutex_enter(&avp->av_mutex);
5727c478bd9Sstevel@tonic-gate 	avp->av_dev_state = AV1394_DEV_DISCONNECTED;
5737c478bd9Sstevel@tonic-gate 	mutex_exit(&avp->av_mutex);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5777c478bd9Sstevel@tonic-gate static void
av1394_reconnect(dev_info_t * dip,ddi_eventcookie_t evc,void * arg,void * data)5787c478bd9Sstevel@tonic-gate av1394_reconnect(dev_info_t *dip, ddi_eventcookie_t evc, void *arg, void *data)
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate 	av1394_inst_t	*avp = arg;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if (avp == NULL) {
5837c478bd9Sstevel@tonic-gate 		return;
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	mutex_enter(&avp->av_mutex);
5877c478bd9Sstevel@tonic-gate 	avp->av_dev_state = AV1394_DEV_ONLINE;
5887c478bd9Sstevel@tonic-gate 	avp->av_attachinfo.localinfo = *(t1394_localinfo_t *)data;
5897c478bd9Sstevel@tonic-gate 	mutex_exit(&avp->av_mutex);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	av1394_async_reconnect(avp);
5927c478bd9Sstevel@tonic-gate }
593