xref: /illumos-gate/usr/src/uts/common/pcmcia/pcs/pcs.c (revision 19397407)
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
511c2b4c0Srw  * Common Development and Distribution License (the "License").
611c2b4c0Srw  * 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 /*
2211c2b4c0Srw  * 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 #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/byteorder.h>
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
317c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate int pcs_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
347c478bd9Sstevel@tonic-gate int pcs_attach(dev_info_t *, ddi_attach_cmd_t);
357c478bd9Sstevel@tonic-gate int pcs_detach(dev_info_t *, ddi_detach_cmd_t);
367c478bd9Sstevel@tonic-gate dev_info_t *pcs_dip;
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static struct dev_ops pcs_devops = {
397c478bd9Sstevel@tonic-gate 	DEVO_REV,
407c478bd9Sstevel@tonic-gate 	0,
417c478bd9Sstevel@tonic-gate 	pcs_getinfo,
427c478bd9Sstevel@tonic-gate 	nulldev,
437c478bd9Sstevel@tonic-gate 	nulldev,
447c478bd9Sstevel@tonic-gate 	pcs_attach,
457c478bd9Sstevel@tonic-gate 	pcs_detach,
467c478bd9Sstevel@tonic-gate 	nulldev,
477c478bd9Sstevel@tonic-gate 	NULL,
48*19397407SSherry Moore 	NULL,
49*19397407SSherry Moore 	NULL,
50*19397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
517c478bd9Sstevel@tonic-gate };
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
607c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* Type of module. This one is a driver */
61*19397407SSherry Moore 	"PCMCIA Socket Driver",	/* Name of the module. */
627c478bd9Sstevel@tonic-gate 	&pcs_devops,		/* driver ops */
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
667c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate struct pcs_inst {
707c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
717c478bd9Sstevel@tonic-gate } *pcs_instances;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate int
_init()747c478bd9Sstevel@tonic-gate _init()
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	int ret;
777c478bd9Sstevel@tonic-gate 	if ((ret = ddi_soft_state_init((void **)&pcs_instances,
78*19397407SSherry Moore 	    sizeof (struct pcs_inst), 1)) != 0)
797c478bd9Sstevel@tonic-gate 		return (ret);
807c478bd9Sstevel@tonic-gate 	if ((ret = mod_install(&modlinkage)) != 0) {
817c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini((void **)&pcs_instances);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	return (ret);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate int
_fini()877c478bd9Sstevel@tonic-gate _fini()
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	int ret;
907c478bd9Sstevel@tonic-gate 	ret = mod_remove(&modlinkage);
917c478bd9Sstevel@tonic-gate 	if (ret == 0) {
927c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini((void **)&pcs_instances);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 	return (ret);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)987c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate int
pcs_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)1047c478bd9Sstevel@tonic-gate pcs_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	int error = DDI_SUCCESS;
1077c478bd9Sstevel@tonic-gate 	int inum;
1087c478bd9Sstevel@tonic-gate 	struct pcs_inst *inst;
1097c478bd9Sstevel@tonic-gate #ifdef lint
1107c478bd9Sstevel@tonic-gate 	dip = dip;
1117c478bd9Sstevel@tonic-gate #endif
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	switch (cmd) {
1147c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1157c478bd9Sstevel@tonic-gate 		inum = getminor((dev_t)arg);
1167c478bd9Sstevel@tonic-gate 		inst = (struct pcs_inst *)ddi_get_soft_state(pcs_instances,
117*19397407SSherry Moore 		    inum);
1187c478bd9Sstevel@tonic-gate 		if (inst == NULL)
1197c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
1207c478bd9Sstevel@tonic-gate 		else
1217c478bd9Sstevel@tonic-gate 			*result = inst->dip;
1227c478bd9Sstevel@tonic-gate 		break;
1237c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1247c478bd9Sstevel@tonic-gate 		inum = getminor((dev_t)arg);
1257c478bd9Sstevel@tonic-gate 		inst = (struct pcs_inst *)ddi_get_soft_state(pcs_instances,
126*19397407SSherry Moore 		    inum);
1277c478bd9Sstevel@tonic-gate 		if (inst == NULL)
1287c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
1297c478bd9Sstevel@tonic-gate 		else
1307c478bd9Sstevel@tonic-gate 			*result = (void *)(uintptr_t)inum;
1317c478bd9Sstevel@tonic-gate 		break;
1327c478bd9Sstevel@tonic-gate 	default:
1337c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	return (error);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate int
pcs_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)1397c478bd9Sstevel@tonic-gate pcs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
1427c478bd9Sstevel@tonic-gate 	int inum;
1437c478bd9Sstevel@tonic-gate 	struct pcs_inst *inst;
14411c2b4c0Srw 
14511c2b4c0Srw 	switch (cmd) {
14611c2b4c0Srw 	case DDI_RESUME:
14711c2b4c0Srw 		return (DDI_SUCCESS);
14811c2b4c0Srw 	case DDI_ATTACH:
14911c2b4c0Srw 		break;
15011c2b4c0Srw 	default:
15111c2b4c0Srw 		return (DDI_FAILURE);
15211c2b4c0Srw 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	inum = ddi_get_instance(dip);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pcs_instances, inum) == DDI_SUCCESS) {
1577c478bd9Sstevel@tonic-gate 		inst = (struct pcs_inst *)ddi_get_soft_state(pcs_instances,
158*19397407SSherry Moore 		    inum);
1597c478bd9Sstevel@tonic-gate 		if (inst == NULL)
1607c478bd9Sstevel@tonic-gate 			ret = DDI_FAILURE;
1617c478bd9Sstevel@tonic-gate 		else
1627c478bd9Sstevel@tonic-gate 			inst->dip = dip;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	return (ret);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate int
pcs_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)1697c478bd9Sstevel@tonic-gate pcs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1707c478bd9Sstevel@tonic-gate {
17111c2b4c0Srw 	switch (cmd) {
17211c2b4c0Srw 	case DDI_DETACH:
1737c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(pcs_instances, ddi_get_instance(dip));
1747c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
17511c2b4c0Srw 
17611c2b4c0Srw 	case DDI_SUSPEND:
17711c2b4c0Srw 	case DDI_PM_SUSPEND:
17811c2b4c0Srw 		return (DDI_SUCCESS);
17911c2b4c0Srw 	default:
18011c2b4c0Srw 		break;
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
1837c478bd9Sstevel@tonic-gate }
184