10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*1004Smike_s * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/time.h> 310Sstevel@tonic-gate #include <sys/errno.h> 320Sstevel@tonic-gate #include <sys/cmn_err.h> 330Sstevel@tonic-gate #include <sys/param.h> 340Sstevel@tonic-gate #include <sys/modctl.h> 350Sstevel@tonic-gate #include <sys/conf.h> 360Sstevel@tonic-gate #include <sys/open.h> 370Sstevel@tonic-gate #include <sys/stat.h> 380Sstevel@tonic-gate #include <sys/clock.h> 390Sstevel@tonic-gate #include <sys/tod.h> 400Sstevel@tonic-gate #include <sys/todio.h> 410Sstevel@tonic-gate #include <sys/ddi.h> 420Sstevel@tonic-gate #include <sys/sunddi.h> 430Sstevel@tonic-gate #include <sys/file.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate 460Sstevel@tonic-gate #define getsoftc(minor) \ 470Sstevel@tonic-gate ((struct tod_softc *)ddi_get_soft_state(statep, (minor))) 480Sstevel@tonic-gate 490Sstevel@tonic-gate /* dev_ops and cb_ops entry point function declarations */ 500Sstevel@tonic-gate 510Sstevel@tonic-gate static int tod_attach(dev_info_t *, ddi_attach_cmd_t); 520Sstevel@tonic-gate static int tod_detach(dev_info_t *, ddi_detach_cmd_t); 530Sstevel@tonic-gate static int tod_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 540Sstevel@tonic-gate 550Sstevel@tonic-gate static int tod_open(dev_t *, int, int, cred_t *); 560Sstevel@tonic-gate static int tod_close(dev_t, int, int, cred_t *); 570Sstevel@tonic-gate static int tod_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 580Sstevel@tonic-gate 590Sstevel@tonic-gate struct cb_ops tod_cb_ops = { 600Sstevel@tonic-gate tod_open, 610Sstevel@tonic-gate tod_close, 620Sstevel@tonic-gate nodev, 630Sstevel@tonic-gate nodev, 640Sstevel@tonic-gate nodev, /* dump */ 650Sstevel@tonic-gate nodev, 660Sstevel@tonic-gate nodev, 670Sstevel@tonic-gate tod_ioctl, 680Sstevel@tonic-gate nodev, /* devmap */ 690Sstevel@tonic-gate nodev, 700Sstevel@tonic-gate ddi_segmap, /* segmap */ 710Sstevel@tonic-gate nochpoll, 720Sstevel@tonic-gate ddi_prop_op, 730Sstevel@tonic-gate NULL, /* for STREAMS drivers */ 740Sstevel@tonic-gate D_NEW | D_MP /* driver compatibility flag */ 750Sstevel@tonic-gate }; 760Sstevel@tonic-gate 770Sstevel@tonic-gate static struct dev_ops tod_dev_ops = { 780Sstevel@tonic-gate DEVO_REV, /* driver build version */ 790Sstevel@tonic-gate 0, /* device reference count */ 800Sstevel@tonic-gate tod_getinfo, 810Sstevel@tonic-gate nulldev, 820Sstevel@tonic-gate nulldev, /* probe */ 830Sstevel@tonic-gate tod_attach, 840Sstevel@tonic-gate tod_detach, 850Sstevel@tonic-gate nulldev, /* reset */ 860Sstevel@tonic-gate &tod_cb_ops, 870Sstevel@tonic-gate (struct bus_ops *)NULL, 880Sstevel@tonic-gate nulldev /* power */ 890Sstevel@tonic-gate }; 900Sstevel@tonic-gate 910Sstevel@tonic-gate /* module configuration stuff */ 920Sstevel@tonic-gate static void *statep; 930Sstevel@tonic-gate extern struct mod_ops mod_driverops; 940Sstevel@tonic-gate 950Sstevel@tonic-gate static struct modldrv modldrv = { 960Sstevel@tonic-gate &mod_driverops, 970Sstevel@tonic-gate "tod driver (v1.0) %I%", 980Sstevel@tonic-gate &tod_dev_ops 990Sstevel@tonic-gate }; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate static struct modlinkage modlinkage = { 1020Sstevel@tonic-gate MODREV_1, 1030Sstevel@tonic-gate &modldrv, 1040Sstevel@tonic-gate 0 1050Sstevel@tonic-gate }; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate int 1090Sstevel@tonic-gate _init(void) 1100Sstevel@tonic-gate { 1110Sstevel@tonic-gate int e; 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate if (e = ddi_soft_state_init(&statep, sizeof (struct tod_softc), 1)) { 1140Sstevel@tonic-gate return (e); 1150Sstevel@tonic-gate } 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate if ((e = mod_install(&modlinkage)) != 0) { 1180Sstevel@tonic-gate ddi_soft_state_fini(&statep); 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate return (e); 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate int 1260Sstevel@tonic-gate _fini(void) 1270Sstevel@tonic-gate { 1280Sstevel@tonic-gate int e; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate if ((e = mod_remove(&modlinkage)) != 0) { 1310Sstevel@tonic-gate return (e); 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate ddi_soft_state_fini(&statep); 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate return (DDI_SUCCESS); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate int 1410Sstevel@tonic-gate _info(struct modinfo *modinfop) 1420Sstevel@tonic-gate { 1430Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate /* ARGSUSED */ 1480Sstevel@tonic-gate static int 1490Sstevel@tonic-gate tod_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 1500Sstevel@tonic-gate { 1510Sstevel@tonic-gate int inst = getminor((dev_t)arg); 1520Sstevel@tonic-gate int retval = DDI_SUCCESS; 1530Sstevel@tonic-gate struct tod_softc *softc; 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate switch (cmd) { 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 1580Sstevel@tonic-gate if ((softc = getsoftc(inst)) == NULL) { 1590Sstevel@tonic-gate *result = (void *)NULL; 1600Sstevel@tonic-gate retval = DDI_FAILURE; 1610Sstevel@tonic-gate } else { 1620Sstevel@tonic-gate *result = (void *)softc->dip; 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate break; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 167*1004Smike_s *result = (void *)(uintptr_t)inst; 1680Sstevel@tonic-gate break; 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate default: 1710Sstevel@tonic-gate retval = DDI_FAILURE; 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate return (retval); 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate static int 1780Sstevel@tonic-gate tod_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 1790Sstevel@tonic-gate { 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate int inst; 1820Sstevel@tonic-gate struct tod_softc *softc = NULL; 1830Sstevel@tonic-gate char name[80]; 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate switch (cmd) { 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate case DDI_ATTACH: 1880Sstevel@tonic-gate inst = ddi_get_instance(dip); 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * Create minor node. The minor device number, inst, has no 1910Sstevel@tonic-gate * meaning. The model number above, which will be added to 1920Sstevel@tonic-gate * the device's softc, is used to direct peculiar behavior. 1930Sstevel@tonic-gate */ 1940Sstevel@tonic-gate (void) sprintf(name, "tod%d", inst); 1950Sstevel@tonic-gate if (ddi_create_minor_node(dip, name, S_IFCHR, inst, 1960Sstevel@tonic-gate DDI_PSEUDO, NULL) == DDI_FAILURE) 1970Sstevel@tonic-gate goto attach_failed; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * Allocate a soft state structure for this instance. 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate if (ddi_soft_state_zalloc(statep, inst) != DDI_SUCCESS) 2030Sstevel@tonic-gate goto attach_failed; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate softc = getsoftc(inst); 2060Sstevel@tonic-gate softc->dip = dip; 2070Sstevel@tonic-gate softc->cpr_stage = ~TOD_SUSPENDED; 2080Sstevel@tonic-gate mutex_init(&softc->mutex, NULL, MUTEX_DRIVER, NULL); 2090Sstevel@tonic-gate ddi_report_dev(dip); 2100Sstevel@tonic-gate return (DDI_SUCCESS); 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate case DDI_RESUME: 2130Sstevel@tonic-gate inst = ddi_get_instance(dip); 2140Sstevel@tonic-gate softc = getsoftc(inst); 2150Sstevel@tonic-gate mutex_enter(&softc->mutex); 2160Sstevel@tonic-gate softc->cpr_stage = ~TOD_SUSPENDED; 2170Sstevel@tonic-gate mutex_exit(&softc->mutex); 2180Sstevel@tonic-gate return (DDI_SUCCESS); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate default: 2210Sstevel@tonic-gate return (DDI_FAILURE); 2220Sstevel@tonic-gate } 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate attach_failed: 2250Sstevel@tonic-gate /* Free soft state, if allocated. remove minor node if added earlier */ 2260Sstevel@tonic-gate if (softc) 2270Sstevel@tonic-gate ddi_soft_state_free(statep, inst); 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate return (DDI_FAILURE); 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate static int 2350Sstevel@tonic-gate tod_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2360Sstevel@tonic-gate { 2370Sstevel@tonic-gate int inst; 2380Sstevel@tonic-gate struct tod_softc *softc; 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate switch (cmd) { 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate case DDI_DETACH: 2430Sstevel@tonic-gate inst = ddi_get_instance(dip); 2440Sstevel@tonic-gate if ((softc = getsoftc(inst)) == NULL) 2450Sstevel@tonic-gate return (ENXIO); 2460Sstevel@tonic-gate /* 2470Sstevel@tonic-gate * Free the soft state and remove minor node added earlier. 2480Sstevel@tonic-gate */ 2490Sstevel@tonic-gate mutex_destroy(&softc->mutex); 2500Sstevel@tonic-gate ddi_soft_state_free(statep, inst); 2510Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 2520Sstevel@tonic-gate return (DDI_SUCCESS); 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate case DDI_SUSPEND: 2550Sstevel@tonic-gate inst = ddi_get_instance(dip); 2560Sstevel@tonic-gate softc = getsoftc(inst); 2570Sstevel@tonic-gate mutex_enter(&softc->mutex); 2580Sstevel@tonic-gate softc->cpr_stage = TOD_SUSPENDED; 2590Sstevel@tonic-gate mutex_exit(&softc->mutex); 2600Sstevel@tonic-gate return (DDI_SUCCESS); 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate default: 2630Sstevel@tonic-gate return (DDI_FAILURE); 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate /* ARGSUSED */ 2690Sstevel@tonic-gate static int 2700Sstevel@tonic-gate tod_open(dev_t *devp, int flag, int otyp, cred_t *credp) 2710Sstevel@tonic-gate { 2720Sstevel@tonic-gate int inst = getminor(*devp); 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate return (getsoftc(inst) == NULL ? ENXIO : 0); 2750Sstevel@tonic-gate } 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate /* ARGSUSED */ 2790Sstevel@tonic-gate static int 2800Sstevel@tonic-gate tod_close(dev_t dev, int flag, int otyp, cred_t *credp) 2810Sstevel@tonic-gate { 2820Sstevel@tonic-gate int inst = getminor(dev); 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate return (getsoftc(inst) == NULL ? ENXIO : 0); 2850Sstevel@tonic-gate } 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate /* ARGSUSED */ 2890Sstevel@tonic-gate static int 2900Sstevel@tonic-gate tod_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp) 2910Sstevel@tonic-gate { 2920Sstevel@tonic-gate int inst = getminor(dev); 2930Sstevel@tonic-gate struct tod_softc *softc; 2940Sstevel@tonic-gate timestruc_t ts; 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate if ((softc = getsoftc(inst)) == NULL) 2970Sstevel@tonic-gate return (ENXIO); 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate mutex_enter(&softc->mutex); 3000Sstevel@tonic-gate while (softc->cpr_stage == TOD_SUSPENDED) { 3010Sstevel@tonic-gate mutex_exit(&softc->mutex); 3020Sstevel@tonic-gate (void) ddi_dev_is_needed(softc->dip, 0, 1); 3030Sstevel@tonic-gate mutex_enter(&softc->mutex); 3040Sstevel@tonic-gate } 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate switch (cmd) { 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate case TOD_CLEAR_ALARM: 3090Sstevel@tonic-gate mutex_enter(&tod_lock); 3100Sstevel@tonic-gate tod_ops.tod_clear_power_alarm(); 3110Sstevel@tonic-gate mutex_exit(&tod_lock); 3120Sstevel@tonic-gate break; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate case TOD_SET_ALARM: 3150Sstevel@tonic-gate if ((mode & FMODELS) == FNATIVE) { 3160Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ts.tv_sec, 3170Sstevel@tonic-gate sizeof (ts.tv_sec), mode) != 0) { 3180Sstevel@tonic-gate mutex_exit(&softc->mutex); 3190Sstevel@tonic-gate return (EFAULT); 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate } else { 3220Sstevel@tonic-gate time32_t time32; 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, 3250Sstevel@tonic-gate &time32, sizeof (time32), mode) != 0) { 3260Sstevel@tonic-gate mutex_exit(&softc->mutex); 3270Sstevel@tonic-gate return (EFAULT); 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate ts.tv_sec = (time_t)time32; 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate ts.tv_nsec = 0; 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate mutex_enter(&tod_lock); 3340Sstevel@tonic-gate tod_ops.tod_set_power_alarm(ts); 3350Sstevel@tonic-gate mutex_exit(&tod_lock); 3360Sstevel@tonic-gate break; 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate case TOD_GET_DATE: 3390Sstevel@tonic-gate mutex_enter(&tod_lock); 3400Sstevel@tonic-gate ts = tod_ops.tod_get(); 3410Sstevel@tonic-gate mutex_exit(&tod_lock); 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate if ((mode & FMODELS) == FNATIVE) { 3440Sstevel@tonic-gate if (ddi_copyout((caddr_t)&ts.tv_sec, (caddr_t)arg, 3450Sstevel@tonic-gate sizeof (ts.tv_sec), mode) != 0) { 3460Sstevel@tonic-gate mutex_exit(&softc->mutex); 3470Sstevel@tonic-gate return (EFAULT); 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate } else { 3500Sstevel@tonic-gate time32_t time32; 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate if (TIMEVAL_OVERFLOW(&ts)) { 3530Sstevel@tonic-gate mutex_exit(&softc->mutex); 3540Sstevel@tonic-gate return (EOVERFLOW); 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate time32 = (time32_t)ts.tv_sec; 3580Sstevel@tonic-gate if (ddi_copyout(&time32, 3590Sstevel@tonic-gate (caddr_t)arg, sizeof (time32), mode) != 0) { 3600Sstevel@tonic-gate mutex_exit(&softc->mutex); 3610Sstevel@tonic-gate return (EFAULT); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate break; 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate default: 3670Sstevel@tonic-gate mutex_exit(&softc->mutex); 3680Sstevel@tonic-gate return (EINVAL); 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate mutex_exit(&softc->mutex); 3720Sstevel@tonic-gate return (0); 3730Sstevel@tonic-gate } 374