1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <sys/types.h> 30*0Sstevel@tonic-gate #include <sys/stat.h> 31*0Sstevel@tonic-gate #include <sys/conf.h> 32*0Sstevel@tonic-gate #include <sys/modctl.h> 33*0Sstevel@tonic-gate #include <sys/sunddi.h> 34*0Sstevel@tonic-gate #include <sys/callb.h> 35*0Sstevel@tonic-gate #include <sys/strlog.h> 36*0Sstevel@tonic-gate #include <sys/file.h> 37*0Sstevel@tonic-gate #include <sys/lom_io.h> 38*0Sstevel@tonic-gate #include <sys/ddi.h> 39*0Sstevel@tonic-gate #include <sys/time.h> 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #define LOMIOCALCTL_OLD _IOW('a', 4, ts_aldata_t) 42*0Sstevel@tonic-gate #define LOMIOCALSTATE_OLD _IOWR('a', 5, ts_aldata_t) 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate struct tsalarm_softc { 45*0Sstevel@tonic-gate dev_info_t *dip; 46*0Sstevel@tonic-gate kmutex_t mutex; 47*0Sstevel@tonic-gate }; 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate #define getsoftc(minor) \ 50*0Sstevel@tonic-gate ((struct tsalarm_softc *)ddi_get_soft_state(statep, (minor))) 51*0Sstevel@tonic-gate /* 52*0Sstevel@tonic-gate * Driver entry points 53*0Sstevel@tonic-gate */ 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate /* dev_ops and cb_ops entry point function declarations */ 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate static int tsalarm_attach(dev_info_t *, ddi_attach_cmd_t); 58*0Sstevel@tonic-gate static int tsalarm_detach(dev_info_t *, ddi_detach_cmd_t); 59*0Sstevel@tonic-gate static int tsalarm_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate static int tsalarm_open(dev_t *, int, int, cred_t *); 62*0Sstevel@tonic-gate static int tsalarm_close(dev_t, int, int, cred_t *); 63*0Sstevel@tonic-gate static int tsalarm_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate static struct cb_ops tsalarm_cb_ops = { 66*0Sstevel@tonic-gate tsalarm_open, /* open */ 67*0Sstevel@tonic-gate tsalarm_close, /* close */ 68*0Sstevel@tonic-gate nodev, /* strategy() */ 69*0Sstevel@tonic-gate nodev, /* print() */ 70*0Sstevel@tonic-gate nodev, /* dump() */ 71*0Sstevel@tonic-gate nodev, /* read() */ 72*0Sstevel@tonic-gate nodev, /* write() */ 73*0Sstevel@tonic-gate tsalarm_ioctl, /* ioctl() */ 74*0Sstevel@tonic-gate nodev, /* devmap() */ 75*0Sstevel@tonic-gate nodev, /* mmap() */ 76*0Sstevel@tonic-gate ddi_segmap, /* segmap() */ 77*0Sstevel@tonic-gate nochpoll, /* poll() */ 78*0Sstevel@tonic-gate ddi_prop_op, /* prop_op() */ 79*0Sstevel@tonic-gate NULL, /* cb_str */ 80*0Sstevel@tonic-gate D_NEW | D_MP /* cb_flag */ 81*0Sstevel@tonic-gate }; 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate static struct dev_ops tsalarm_ops = { 85*0Sstevel@tonic-gate DEVO_REV, 86*0Sstevel@tonic-gate 0, /* ref count */ 87*0Sstevel@tonic-gate tsalarm_getinfo, /* getinfo() */ 88*0Sstevel@tonic-gate nulldev, /* identify() */ 89*0Sstevel@tonic-gate nulldev, /* probe() */ 90*0Sstevel@tonic-gate tsalarm_attach, /* attach() */ 91*0Sstevel@tonic-gate tsalarm_detach, /* detach */ 92*0Sstevel@tonic-gate nodev, /* reset */ 93*0Sstevel@tonic-gate &tsalarm_cb_ops, /* pointer to cb_ops structure */ 94*0Sstevel@tonic-gate (struct bus_ops *)NULL, 95*0Sstevel@tonic-gate nulldev /* power() */ 96*0Sstevel@tonic-gate }; 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate /* 99*0Sstevel@tonic-gate * Loadable module support. 100*0Sstevel@tonic-gate */ 101*0Sstevel@tonic-gate extern struct mod_ops mod_driverops; 102*0Sstevel@tonic-gate static void *statep; 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate static struct modldrv modldrv = { 105*0Sstevel@tonic-gate &mod_driverops, /* Type of module. This is a driver */ 106*0Sstevel@tonic-gate "tsalarm control driver v%I%", /* Name of the module */ 107*0Sstevel@tonic-gate &tsalarm_ops /* pointer to the dev_ops structure */ 108*0Sstevel@tonic-gate }; 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate static struct modlinkage modlinkage = { 111*0Sstevel@tonic-gate MODREV_1, 112*0Sstevel@tonic-gate &modldrv, 113*0Sstevel@tonic-gate NULL 114*0Sstevel@tonic-gate }; 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate extern int rmclomv_alarm_get(int alarm_type, int *alarm_state); 117*0Sstevel@tonic-gate extern int rmclomv_alarm_set(int alarm_type, int new_state); 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate int 120*0Sstevel@tonic-gate _init(void) 121*0Sstevel@tonic-gate { 122*0Sstevel@tonic-gate int e; 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate if (e = ddi_soft_state_init(&statep, 125*0Sstevel@tonic-gate sizeof (struct tsalarm_softc), 1)) { 126*0Sstevel@tonic-gate return (e); 127*0Sstevel@tonic-gate } 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate if ((e = mod_install(&modlinkage)) != 0) { 130*0Sstevel@tonic-gate ddi_soft_state_fini(&statep); 131*0Sstevel@tonic-gate } 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate return (e); 134*0Sstevel@tonic-gate } 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate int 138*0Sstevel@tonic-gate _fini(void) 139*0Sstevel@tonic-gate { 140*0Sstevel@tonic-gate int e; 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate if ((e = mod_remove(&modlinkage)) != 0) { 143*0Sstevel@tonic-gate return (e); 144*0Sstevel@tonic-gate } 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate ddi_soft_state_fini(&statep); 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate return (DDI_SUCCESS); 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate int 153*0Sstevel@tonic-gate _info(struct modinfo *modinfop) 154*0Sstevel@tonic-gate { 155*0Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 156*0Sstevel@tonic-gate } 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate /* ARGSUSED */ 160*0Sstevel@tonic-gate static int 161*0Sstevel@tonic-gate tsalarm_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 162*0Sstevel@tonic-gate { 163*0Sstevel@tonic-gate int inst = getminor((dev_t)arg); 164*0Sstevel@tonic-gate int retval = DDI_SUCCESS; 165*0Sstevel@tonic-gate struct tsalarm_softc *softc; 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate switch (cmd) { 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 170*0Sstevel@tonic-gate if ((softc = getsoftc(inst)) == NULL) { 171*0Sstevel@tonic-gate *result = (void *)NULL; 172*0Sstevel@tonic-gate retval = DDI_FAILURE; 173*0Sstevel@tonic-gate } else { 174*0Sstevel@tonic-gate *result = (void *)softc->dip; 175*0Sstevel@tonic-gate } 176*0Sstevel@tonic-gate break; 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 179*0Sstevel@tonic-gate *result = (void *)inst; 180*0Sstevel@tonic-gate break; 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate default: 183*0Sstevel@tonic-gate retval = DDI_FAILURE; 184*0Sstevel@tonic-gate } 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate return (retval); 187*0Sstevel@tonic-gate } 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate static int 190*0Sstevel@tonic-gate tsalarm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 191*0Sstevel@tonic-gate { 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate int inst; 194*0Sstevel@tonic-gate struct tsalarm_softc *softc = NULL; 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate switch (cmd) { 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate case DDI_ATTACH: 199*0Sstevel@tonic-gate inst = ddi_get_instance(dip); 200*0Sstevel@tonic-gate /* 201*0Sstevel@tonic-gate * Allocate a soft state structure for this instance. 202*0Sstevel@tonic-gate */ 203*0Sstevel@tonic-gate if (ddi_soft_state_zalloc(statep, inst) != DDI_SUCCESS) 204*0Sstevel@tonic-gate goto attach_failed; 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate softc = getsoftc(inst); 207*0Sstevel@tonic-gate softc->dip = dip; 208*0Sstevel@tonic-gate mutex_init(&softc->mutex, NULL, MUTEX_DRIVER, NULL); 209*0Sstevel@tonic-gate /* 210*0Sstevel@tonic-gate * Create minor node. The minor device number, inst, has no 211*0Sstevel@tonic-gate * meaning. The model number above, which will be added to 212*0Sstevel@tonic-gate * the device's softc, is used to direct peculiar behavior. 213*0Sstevel@tonic-gate */ 214*0Sstevel@tonic-gate if (ddi_create_minor_node(dip, "lom", S_IFCHR, 0, 215*0Sstevel@tonic-gate DDI_PSEUDO, NULL) == DDI_FAILURE) 216*0Sstevel@tonic-gate goto attach_failed; 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate ddi_report_dev(dip); 219*0Sstevel@tonic-gate return (DDI_SUCCESS); 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate case DDI_RESUME: 222*0Sstevel@tonic-gate return (DDI_SUCCESS); 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gate default: 225*0Sstevel@tonic-gate return (DDI_FAILURE); 226*0Sstevel@tonic-gate } 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate attach_failed: 229*0Sstevel@tonic-gate /* Free soft state, if allocated. remove minor node if added earlier */ 230*0Sstevel@tonic-gate if (softc) { 231*0Sstevel@tonic-gate mutex_destroy(&softc->mutex); 232*0Sstevel@tonic-gate ddi_soft_state_free(statep, inst); 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate return (DDI_FAILURE); 238*0Sstevel@tonic-gate } 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate static int 241*0Sstevel@tonic-gate tsalarm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 242*0Sstevel@tonic-gate { 243*0Sstevel@tonic-gate int inst; 244*0Sstevel@tonic-gate struct tsalarm_softc *softc; 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate switch (cmd) { 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate case DDI_DETACH: 249*0Sstevel@tonic-gate inst = ddi_get_instance(dip); 250*0Sstevel@tonic-gate if ((softc = getsoftc(inst)) == NULL) 251*0Sstevel@tonic-gate return (DDI_FAILURE); 252*0Sstevel@tonic-gate /* 253*0Sstevel@tonic-gate * Free the soft state and remove minor node added earlier. 254*0Sstevel@tonic-gate */ 255*0Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 256*0Sstevel@tonic-gate mutex_destroy(&softc->mutex); 257*0Sstevel@tonic-gate ddi_soft_state_free(statep, inst); 258*0Sstevel@tonic-gate return (DDI_SUCCESS); 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate case DDI_SUSPEND: 261*0Sstevel@tonic-gate return (DDI_SUCCESS); 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate default: 264*0Sstevel@tonic-gate return (DDI_FAILURE); 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gate } 267*0Sstevel@tonic-gate } 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate /* ARGSUSED */ 270*0Sstevel@tonic-gate static int 271*0Sstevel@tonic-gate tsalarm_open(dev_t *devp, int flag, int otyp, cred_t *credp) 272*0Sstevel@tonic-gate { 273*0Sstevel@tonic-gate int inst = getminor(*devp); 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate return (getsoftc(inst) == NULL ? ENXIO : 0); 276*0Sstevel@tonic-gate } 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate /* ARGSUSED */ 280*0Sstevel@tonic-gate static int 281*0Sstevel@tonic-gate tsalarm_close(dev_t dev, int flag, int otyp, cred_t *credp) 282*0Sstevel@tonic-gate { 283*0Sstevel@tonic-gate int inst = getminor(dev); 284*0Sstevel@tonic-gate 285*0Sstevel@tonic-gate return (getsoftc(inst) == NULL ? ENXIO : 0); 286*0Sstevel@tonic-gate } 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate /* ARGSUSED */ 290*0Sstevel@tonic-gate static int 291*0Sstevel@tonic-gate tsalarm_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 292*0Sstevel@tonic-gate cred_t *credp, int *rvalp) 293*0Sstevel@tonic-gate { 294*0Sstevel@tonic-gate int inst = getminor(dev); 295*0Sstevel@tonic-gate struct tsalarm_softc *softc; 296*0Sstevel@tonic-gate int retval = 0; 297*0Sstevel@tonic-gate ts_aldata_t ts_alinfo; 298*0Sstevel@tonic-gate int alarm_type, alarm_state = 0; 299*0Sstevel@tonic-gate 300*0Sstevel@tonic-gate if ((softc = getsoftc(inst)) == NULL) 301*0Sstevel@tonic-gate return (ENXIO); 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate mutex_enter(&softc->mutex); 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate switch (cmd) { 306*0Sstevel@tonic-gate 307*0Sstevel@tonic-gate case LOMIOCALSTATE: 308*0Sstevel@tonic-gate case LOMIOCALSTATE_OLD: 309*0Sstevel@tonic-gate { 310*0Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ts_alinfo, 311*0Sstevel@tonic-gate sizeof (ts_aldata_t), mode) != 0) { 312*0Sstevel@tonic-gate retval = EFAULT; 313*0Sstevel@tonic-gate goto end; 314*0Sstevel@tonic-gate } 315*0Sstevel@tonic-gate 316*0Sstevel@tonic-gate alarm_type = ts_alinfo.alarm_no; 317*0Sstevel@tonic-gate if ((alarm_type < ALARM_CRITICAL) || 318*0Sstevel@tonic-gate (alarm_type > ALARM_USER)) { 319*0Sstevel@tonic-gate retval = EINVAL; 320*0Sstevel@tonic-gate goto end; 321*0Sstevel@tonic-gate } 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate retval = rmclomv_alarm_get(alarm_type, &alarm_state); 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate if (retval != 0) 326*0Sstevel@tonic-gate goto end; 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gate if ((alarm_state != 0) && (alarm_state != 1)) { 329*0Sstevel@tonic-gate retval = EIO; 330*0Sstevel@tonic-gate goto end; 331*0Sstevel@tonic-gate } 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate ts_alinfo.alarm_state = alarm_state; 334*0Sstevel@tonic-gate if (ddi_copyout((caddr_t)&ts_alinfo, (caddr_t)arg, 335*0Sstevel@tonic-gate sizeof (ts_aldata_t), mode) != 0) { 336*0Sstevel@tonic-gate retval = EFAULT; 337*0Sstevel@tonic-gate goto end; 338*0Sstevel@tonic-gate } 339*0Sstevel@tonic-gate 340*0Sstevel@tonic-gate } 341*0Sstevel@tonic-gate break; 342*0Sstevel@tonic-gate 343*0Sstevel@tonic-gate case LOMIOCALCTL: 344*0Sstevel@tonic-gate case LOMIOCALCTL_OLD: 345*0Sstevel@tonic-gate { 346*0Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ts_alinfo, 347*0Sstevel@tonic-gate sizeof (ts_aldata_t), mode) != 0) { 348*0Sstevel@tonic-gate retval = EFAULT; 349*0Sstevel@tonic-gate goto end; 350*0Sstevel@tonic-gate } 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gate alarm_type = ts_alinfo.alarm_no; 353*0Sstevel@tonic-gate alarm_state = ts_alinfo.alarm_state; 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gate if ((alarm_type < ALARM_CRITICAL) || 356*0Sstevel@tonic-gate (alarm_type > ALARM_USER)) { 357*0Sstevel@tonic-gate retval = EINVAL; 358*0Sstevel@tonic-gate goto end; 359*0Sstevel@tonic-gate } 360*0Sstevel@tonic-gate if ((alarm_state < ALARM_OFF) || 361*0Sstevel@tonic-gate (alarm_state > ALARM_ON)) { 362*0Sstevel@tonic-gate retval = EINVAL; 363*0Sstevel@tonic-gate goto end; 364*0Sstevel@tonic-gate } 365*0Sstevel@tonic-gate 366*0Sstevel@tonic-gate retval = rmclomv_alarm_set(alarm_type, alarm_state); 367*0Sstevel@tonic-gate } 368*0Sstevel@tonic-gate break; 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gate default: 371*0Sstevel@tonic-gate retval = EINVAL; 372*0Sstevel@tonic-gate break; 373*0Sstevel@tonic-gate } 374*0Sstevel@tonic-gate 375*0Sstevel@tonic-gate end: 376*0Sstevel@tonic-gate mutex_exit(&softc->mutex); 377*0Sstevel@tonic-gate 378*0Sstevel@tonic-gate return (retval); 379*0Sstevel@tonic-gate } 380