1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> /* ddi_create_minor_node S_IFCHR */ 31*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h> /* for modldrv */ 32*7c478bd9Sstevel@tonic-gate #include <sys/open.h> /* for open params. */ 33*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/conf.h> /* req. by dev_ops flags MTSAFE etc. */ 37*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/file.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/note.h> 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate #include <sys/i2c/clients/pic16f819_impl.h> 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate static void *pic16f819soft_statep; 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate static int pic16f819_set(struct pic16f819_unit *, int, uchar_t); 46*7c478bd9Sstevel@tonic-gate static int pic16f819_get(struct pic16f819_unit *, int, uchar_t *, int); 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate static int pic16f819_do_attach(dev_info_t *); 50*7c478bd9Sstevel@tonic-gate static int pic16f819_do_detach(dev_info_t *); 51*7c478bd9Sstevel@tonic-gate static int pic16f819_do_resume(void); 52*7c478bd9Sstevel@tonic-gate static int pic16f819_do_suspend(void); 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate /* 55*7c478bd9Sstevel@tonic-gate * cb ops (only need ioctl) 56*7c478bd9Sstevel@tonic-gate */ 57*7c478bd9Sstevel@tonic-gate static int pic16f819_open(dev_t *, int, int, cred_t *); 58*7c478bd9Sstevel@tonic-gate static int pic16f819_close(dev_t, int, int, cred_t *); 59*7c478bd9Sstevel@tonic-gate static int pic16f819_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate static struct cb_ops pic16f819_cbops = { 62*7c478bd9Sstevel@tonic-gate pic16f819_open, /* open */ 63*7c478bd9Sstevel@tonic-gate pic16f819_close, /* close */ 64*7c478bd9Sstevel@tonic-gate nodev, /* strategy */ 65*7c478bd9Sstevel@tonic-gate nodev, /* print */ 66*7c478bd9Sstevel@tonic-gate nodev, /* dump */ 67*7c478bd9Sstevel@tonic-gate nodev, /* read */ 68*7c478bd9Sstevel@tonic-gate nodev, /* write */ 69*7c478bd9Sstevel@tonic-gate pic16f819_ioctl, /* ioctl */ 70*7c478bd9Sstevel@tonic-gate nodev, /* devmap */ 71*7c478bd9Sstevel@tonic-gate nodev, /* mmap */ 72*7c478bd9Sstevel@tonic-gate nodev, /* segmap */ 73*7c478bd9Sstevel@tonic-gate nochpoll, /* poll */ 74*7c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 75*7c478bd9Sstevel@tonic-gate NULL, /* streamtab */ 76*7c478bd9Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */ 77*7c478bd9Sstevel@tonic-gate CB_REV, /* rev */ 78*7c478bd9Sstevel@tonic-gate nodev, /* int (*cb_aread)() */ 79*7c478bd9Sstevel@tonic-gate nodev /* int (*cb_awrite)() */ 80*7c478bd9Sstevel@tonic-gate }; 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate /* 83*7c478bd9Sstevel@tonic-gate * dev ops 84*7c478bd9Sstevel@tonic-gate */ 85*7c478bd9Sstevel@tonic-gate static int pic16f819_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 86*7c478bd9Sstevel@tonic-gate static int pic16f819_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate static struct dev_ops pic16f819_ops = { 89*7c478bd9Sstevel@tonic-gate DEVO_REV, 90*7c478bd9Sstevel@tonic-gate 0, 91*7c478bd9Sstevel@tonic-gate ddi_no_info, 92*7c478bd9Sstevel@tonic-gate nulldev, 93*7c478bd9Sstevel@tonic-gate nulldev, 94*7c478bd9Sstevel@tonic-gate pic16f819_attach, 95*7c478bd9Sstevel@tonic-gate pic16f819_detach, 96*7c478bd9Sstevel@tonic-gate nodev, 97*7c478bd9Sstevel@tonic-gate &pic16f819_cbops, 98*7c478bd9Sstevel@tonic-gate NULL 99*7c478bd9Sstevel@tonic-gate }; 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops; 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate static struct modldrv pic16f819_modldrv = { 104*7c478bd9Sstevel@tonic-gate &mod_driverops, /* type of module - driver */ 105*7c478bd9Sstevel@tonic-gate "PIC16F819 i2c device driver: v%I%", 106*7c478bd9Sstevel@tonic-gate &pic16f819_ops 107*7c478bd9Sstevel@tonic-gate }; 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate static struct modlinkage pic16f819_modlinkage = { 110*7c478bd9Sstevel@tonic-gate MODREV_1, 111*7c478bd9Sstevel@tonic-gate &pic16f819_modldrv, 112*7c478bd9Sstevel@tonic-gate 0 113*7c478bd9Sstevel@tonic-gate }; 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate int 117*7c478bd9Sstevel@tonic-gate _init(void) 118*7c478bd9Sstevel@tonic-gate { 119*7c478bd9Sstevel@tonic-gate int error; 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate error = mod_install(&pic16f819_modlinkage); 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate if (!error) 124*7c478bd9Sstevel@tonic-gate (void) ddi_soft_state_init(&pic16f819soft_statep, 125*7c478bd9Sstevel@tonic-gate sizeof (struct pic16f819_unit), 1); 126*7c478bd9Sstevel@tonic-gate return (error); 127*7c478bd9Sstevel@tonic-gate } 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate int 130*7c478bd9Sstevel@tonic-gate _fini(void) 131*7c478bd9Sstevel@tonic-gate { 132*7c478bd9Sstevel@tonic-gate int error; 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate error = mod_remove(&pic16f819_modlinkage); 135*7c478bd9Sstevel@tonic-gate if (!error) 136*7c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&pic16f819soft_statep); 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate return (error); 139*7c478bd9Sstevel@tonic-gate } 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate int 142*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 143*7c478bd9Sstevel@tonic-gate { 144*7c478bd9Sstevel@tonic-gate return (mod_info(&pic16f819_modlinkage, modinfop)); 145*7c478bd9Sstevel@tonic-gate } 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate static int 148*7c478bd9Sstevel@tonic-gate pic16f819_get(struct pic16f819_unit *unitp, int reg, uchar_t *byte, int flags) 149*7c478bd9Sstevel@tonic-gate { 150*7c478bd9Sstevel@tonic-gate i2c_transfer_t *i2c_tran_pointer; 151*7c478bd9Sstevel@tonic-gate int err; 152*7c478bd9Sstevel@tonic-gate 153*7c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->pic16f819_hdl, &i2c_tran_pointer, 154*7c478bd9Sstevel@tonic-gate 1, 1, flags); 155*7c478bd9Sstevel@tonic-gate if (i2c_tran_pointer == NULL) { 156*7c478bd9Sstevel@tonic-gate return (ENOMEM); 157*7c478bd9Sstevel@tonic-gate } 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate i2c_tran_pointer->i2c_flags = I2C_WR_RD; 160*7c478bd9Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg; 161*7c478bd9Sstevel@tonic-gate err = i2c_transfer(unitp->pic16f819_hdl, i2c_tran_pointer); 162*7c478bd9Sstevel@tonic-gate if (err) { 163*7c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: pic16f819_get failed reg=%x", 164*7c478bd9Sstevel@tonic-gate unitp->pic16f819_name, reg)); 165*7c478bd9Sstevel@tonic-gate } else { 166*7c478bd9Sstevel@tonic-gate *byte = i2c_tran_pointer->i2c_rbuf[0]; 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate i2c_transfer_free(unitp->pic16f819_hdl, i2c_tran_pointer); 170*7c478bd9Sstevel@tonic-gate return (err); 171*7c478bd9Sstevel@tonic-gate } 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate static int 174*7c478bd9Sstevel@tonic-gate pic16f819_set(struct pic16f819_unit *unitp, int reg, uchar_t byte) 175*7c478bd9Sstevel@tonic-gate { 176*7c478bd9Sstevel@tonic-gate i2c_transfer_t *i2c_tran_pointer; 177*7c478bd9Sstevel@tonic-gate int err; 178*7c478bd9Sstevel@tonic-gate 179*7c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->pic16f819_hdl, &i2c_tran_pointer, 180*7c478bd9Sstevel@tonic-gate 2, 0, I2C_SLEEP); 181*7c478bd9Sstevel@tonic-gate if (i2c_tran_pointer == NULL) { 182*7c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in pic16f819_set " 183*7c478bd9Sstevel@tonic-gate "i2c_tran_pointer not allocated", unitp->pic16f819_name)); 184*7c478bd9Sstevel@tonic-gate return (ENOMEM); 185*7c478bd9Sstevel@tonic-gate } 186*7c478bd9Sstevel@tonic-gate 187*7c478bd9Sstevel@tonic-gate i2c_tran_pointer->i2c_flags = I2C_WR; 188*7c478bd9Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg; 189*7c478bd9Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[1] = byte; 190*7c478bd9Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: set reg %x to %x", 191*7c478bd9Sstevel@tonic-gate unitp->pic16f819_name, reg, byte)); 192*7c478bd9Sstevel@tonic-gate 193*7c478bd9Sstevel@tonic-gate err = i2c_transfer(unitp->pic16f819_hdl, i2c_tran_pointer); 194*7c478bd9Sstevel@tonic-gate if (err) { 195*7c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the pic16f819_set" 196*7c478bd9Sstevel@tonic-gate " i2c_transfer routine", unitp->pic16f819_name)); 197*7c478bd9Sstevel@tonic-gate } 198*7c478bd9Sstevel@tonic-gate i2c_transfer_free(unitp->pic16f819_hdl, i2c_tran_pointer); 199*7c478bd9Sstevel@tonic-gate return (err); 200*7c478bd9Sstevel@tonic-gate } 201*7c478bd9Sstevel@tonic-gate 202*7c478bd9Sstevel@tonic-gate static int 203*7c478bd9Sstevel@tonic-gate pic16f819_open(dev_t *devp, int flags, int otyp, cred_t *credp) 204*7c478bd9Sstevel@tonic-gate { 205*7c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(credp)) 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate struct pic16f819_unit *unitp; 208*7c478bd9Sstevel@tonic-gate int instance; 209*7c478bd9Sstevel@tonic-gate int error = 0; 210*7c478bd9Sstevel@tonic-gate 211*7c478bd9Sstevel@tonic-gate instance = getminor(*devp); 212*7c478bd9Sstevel@tonic-gate 213*7c478bd9Sstevel@tonic-gate if (instance < 0) { 214*7c478bd9Sstevel@tonic-gate return (ENXIO); 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate unitp = (struct pic16f819_unit *) 218*7c478bd9Sstevel@tonic-gate ddi_get_soft_state(pic16f819soft_statep, instance); 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate if (unitp == NULL) { 221*7c478bd9Sstevel@tonic-gate return (ENXIO); 222*7c478bd9Sstevel@tonic-gate } 223*7c478bd9Sstevel@tonic-gate 224*7c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) { 225*7c478bd9Sstevel@tonic-gate return (EINVAL); 226*7c478bd9Sstevel@tonic-gate } 227*7c478bd9Sstevel@tonic-gate 228*7c478bd9Sstevel@tonic-gate mutex_enter(&unitp->pic16f819_mutex); 229*7c478bd9Sstevel@tonic-gate 230*7c478bd9Sstevel@tonic-gate if (flags & FEXCL) { 231*7c478bd9Sstevel@tonic-gate if (unitp->pic16f819_oflag != 0) { 232*7c478bd9Sstevel@tonic-gate error = EBUSY; 233*7c478bd9Sstevel@tonic-gate } else { 234*7c478bd9Sstevel@tonic-gate unitp->pic16f819_oflag = FEXCL; 235*7c478bd9Sstevel@tonic-gate } 236*7c478bd9Sstevel@tonic-gate } else { 237*7c478bd9Sstevel@tonic-gate if (unitp->pic16f819_oflag == FEXCL) { 238*7c478bd9Sstevel@tonic-gate error = EBUSY; 239*7c478bd9Sstevel@tonic-gate } else { 240*7c478bd9Sstevel@tonic-gate unitp->pic16f819_oflag = FOPEN; 241*7c478bd9Sstevel@tonic-gate } 242*7c478bd9Sstevel@tonic-gate } 243*7c478bd9Sstevel@tonic-gate 244*7c478bd9Sstevel@tonic-gate mutex_exit(&unitp->pic16f819_mutex); 245*7c478bd9Sstevel@tonic-gate 246*7c478bd9Sstevel@tonic-gate return (error); 247*7c478bd9Sstevel@tonic-gate } 248*7c478bd9Sstevel@tonic-gate 249*7c478bd9Sstevel@tonic-gate static int 250*7c478bd9Sstevel@tonic-gate pic16f819_close(dev_t dev, int flags, int otyp, cred_t *credp) 251*7c478bd9Sstevel@tonic-gate { 252*7c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(flags, otyp, credp)) 253*7c478bd9Sstevel@tonic-gate 254*7c478bd9Sstevel@tonic-gate struct pic16f819_unit *unitp; 255*7c478bd9Sstevel@tonic-gate int instance; 256*7c478bd9Sstevel@tonic-gate 257*7c478bd9Sstevel@tonic-gate instance = getminor(dev); 258*7c478bd9Sstevel@tonic-gate 259*7c478bd9Sstevel@tonic-gate if (instance < 0) { 260*7c478bd9Sstevel@tonic-gate return (ENXIO); 261*7c478bd9Sstevel@tonic-gate } 262*7c478bd9Sstevel@tonic-gate 263*7c478bd9Sstevel@tonic-gate unitp = (struct pic16f819_unit *) 264*7c478bd9Sstevel@tonic-gate ddi_get_soft_state(pic16f819soft_statep, instance); 265*7c478bd9Sstevel@tonic-gate 266*7c478bd9Sstevel@tonic-gate if (unitp == NULL) { 267*7c478bd9Sstevel@tonic-gate return (ENXIO); 268*7c478bd9Sstevel@tonic-gate } 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate mutex_enter(&unitp->pic16f819_mutex); 271*7c478bd9Sstevel@tonic-gate 272*7c478bd9Sstevel@tonic-gate unitp->pic16f819_oflag = 0; 273*7c478bd9Sstevel@tonic-gate 274*7c478bd9Sstevel@tonic-gate mutex_exit(&unitp->pic16f819_mutex); 275*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 276*7c478bd9Sstevel@tonic-gate } 277*7c478bd9Sstevel@tonic-gate 278*7c478bd9Sstevel@tonic-gate static int 279*7c478bd9Sstevel@tonic-gate pic16f819_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 280*7c478bd9Sstevel@tonic-gate cred_t *credp, int *rvalp) 281*7c478bd9Sstevel@tonic-gate { 282*7c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(credp, rvalp)) 283*7c478bd9Sstevel@tonic-gate 284*7c478bd9Sstevel@tonic-gate struct pic16f819_unit *unitp; 285*7c478bd9Sstevel@tonic-gate int instance; 286*7c478bd9Sstevel@tonic-gate int err = 0; 287*7c478bd9Sstevel@tonic-gate i2c_reg_t ioctl_reg; 288*7c478bd9Sstevel@tonic-gate uchar_t val8; 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate if (arg == NULL) { 291*7c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "PIC16F819: ioctl: arg passed in to ioctl " 292*7c478bd9Sstevel@tonic-gate "= NULL\n")); 293*7c478bd9Sstevel@tonic-gate err = EINVAL; 294*7c478bd9Sstevel@tonic-gate return (err); 295*7c478bd9Sstevel@tonic-gate } 296*7c478bd9Sstevel@tonic-gate instance = getminor(dev); 297*7c478bd9Sstevel@tonic-gate unitp = (struct pic16f819_unit *) 298*7c478bd9Sstevel@tonic-gate ddi_get_soft_state(pic16f819soft_statep, instance); 299*7c478bd9Sstevel@tonic-gate 300*7c478bd9Sstevel@tonic-gate mutex_enter(&unitp->pic16f819_mutex); 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate switch (cmd) { 303*7c478bd9Sstevel@tonic-gate 304*7c478bd9Sstevel@tonic-gate case I2C_GET_REG: 305*7c478bd9Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_reg, 306*7c478bd9Sstevel@tonic-gate sizeof (i2c_reg_t), mode) != DDI_SUCCESS) { 307*7c478bd9Sstevel@tonic-gate err = EFAULT; 308*7c478bd9Sstevel@tonic-gate break; 309*7c478bd9Sstevel@tonic-gate } 310*7c478bd9Sstevel@tonic-gate err = pic16f819_get(unitp, ioctl_reg.reg_num, &val8, 311*7c478bd9Sstevel@tonic-gate I2C_SLEEP); 312*7c478bd9Sstevel@tonic-gate if (err != I2C_SUCCESS) { 313*7c478bd9Sstevel@tonic-gate break; 314*7c478bd9Sstevel@tonic-gate } 315*7c478bd9Sstevel@tonic-gate 316*7c478bd9Sstevel@tonic-gate ioctl_reg.reg_value = val8; 317*7c478bd9Sstevel@tonic-gate if (ddi_copyout((caddr_t)&ioctl_reg, (caddr_t)arg, 318*7c478bd9Sstevel@tonic-gate sizeof (i2c_reg_t), mode) != DDI_SUCCESS) { 319*7c478bd9Sstevel@tonic-gate err = EFAULT; 320*7c478bd9Sstevel@tonic-gate } 321*7c478bd9Sstevel@tonic-gate break; 322*7c478bd9Sstevel@tonic-gate 323*7c478bd9Sstevel@tonic-gate case I2C_SET_REG: 324*7c478bd9Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_reg, 325*7c478bd9Sstevel@tonic-gate sizeof (i2c_reg_t), mode) != DDI_SUCCESS) { 326*7c478bd9Sstevel@tonic-gate err = EFAULT; 327*7c478bd9Sstevel@tonic-gate break; 328*7c478bd9Sstevel@tonic-gate } 329*7c478bd9Sstevel@tonic-gate err = pic16f819_set(unitp, ioctl_reg.reg_num, 330*7c478bd9Sstevel@tonic-gate ioctl_reg.reg_value); 331*7c478bd9Sstevel@tonic-gate break; 332*7c478bd9Sstevel@tonic-gate default: 333*7c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x\n", 334*7c478bd9Sstevel@tonic-gate unitp->pic16f819_name, cmd)); 335*7c478bd9Sstevel@tonic-gate err = EINVAL; 336*7c478bd9Sstevel@tonic-gate } 337*7c478bd9Sstevel@tonic-gate 338*7c478bd9Sstevel@tonic-gate mutex_exit(&unitp->pic16f819_mutex); 339*7c478bd9Sstevel@tonic-gate return (err); 340*7c478bd9Sstevel@tonic-gate } 341*7c478bd9Sstevel@tonic-gate 342*7c478bd9Sstevel@tonic-gate static int 343*7c478bd9Sstevel@tonic-gate pic16f819_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 344*7c478bd9Sstevel@tonic-gate { 345*7c478bd9Sstevel@tonic-gate switch (cmd) { 346*7c478bd9Sstevel@tonic-gate case DDI_ATTACH: 347*7c478bd9Sstevel@tonic-gate return (pic16f819_do_attach(dip)); 348*7c478bd9Sstevel@tonic-gate case DDI_RESUME: 349*7c478bd9Sstevel@tonic-gate return (pic16f819_do_resume()); 350*7c478bd9Sstevel@tonic-gate default: 351*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 352*7c478bd9Sstevel@tonic-gate } 353*7c478bd9Sstevel@tonic-gate } 354*7c478bd9Sstevel@tonic-gate 355*7c478bd9Sstevel@tonic-gate static int 356*7c478bd9Sstevel@tonic-gate pic16f819_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 357*7c478bd9Sstevel@tonic-gate { 358*7c478bd9Sstevel@tonic-gate switch (cmd) { 359*7c478bd9Sstevel@tonic-gate case DDI_DETACH: 360*7c478bd9Sstevel@tonic-gate return (pic16f819_do_detach(dip)); 361*7c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 362*7c478bd9Sstevel@tonic-gate return (pic16f819_do_suspend()); 363*7c478bd9Sstevel@tonic-gate default: 364*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 365*7c478bd9Sstevel@tonic-gate } 366*7c478bd9Sstevel@tonic-gate } 367*7c478bd9Sstevel@tonic-gate 368*7c478bd9Sstevel@tonic-gate static int 369*7c478bd9Sstevel@tonic-gate pic16f819_do_attach(dev_info_t *dip) 370*7c478bd9Sstevel@tonic-gate { 371*7c478bd9Sstevel@tonic-gate struct pic16f819_unit *unitp; 372*7c478bd9Sstevel@tonic-gate int instance; 373*7c478bd9Sstevel@tonic-gate 374*7c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 375*7c478bd9Sstevel@tonic-gate 376*7c478bd9Sstevel@tonic-gate if (ddi_soft_state_zalloc(pic16f819soft_statep, instance) != 0) { 377*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n", 378*7c478bd9Sstevel@tonic-gate ddi_get_name(dip), instance); 379*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 380*7c478bd9Sstevel@tonic-gate } 381*7c478bd9Sstevel@tonic-gate 382*7c478bd9Sstevel@tonic-gate unitp = ddi_get_soft_state(pic16f819soft_statep, instance); 383*7c478bd9Sstevel@tonic-gate 384*7c478bd9Sstevel@tonic-gate if (unitp == NULL) { 385*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: unitp not filled\n", 386*7c478bd9Sstevel@tonic-gate ddi_get_name(dip), instance); 387*7c478bd9Sstevel@tonic-gate return (ENOMEM); 388*7c478bd9Sstevel@tonic-gate } 389*7c478bd9Sstevel@tonic-gate 390*7c478bd9Sstevel@tonic-gate (void) snprintf(unitp->pic16f819_name, sizeof (unitp->pic16f819_name), 391*7c478bd9Sstevel@tonic-gate "%s%d", ddi_node_name(dip), instance); 392*7c478bd9Sstevel@tonic-gate 393*7c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(dip, "fan_1", S_IFCHR, instance, 394*7c478bd9Sstevel@tonic-gate "ddi_i2c:pic", NULL) == DDI_FAILURE) { 395*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s ddi_create_minor_node failed for " 396*7c478bd9Sstevel@tonic-gate "%s\n", unitp->pic16f819_name, "pic16f819"); 397*7c478bd9Sstevel@tonic-gate ddi_soft_state_free(pic16f819soft_statep, instance); 398*7c478bd9Sstevel@tonic-gate 399*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 400*7c478bd9Sstevel@tonic-gate } 401*7c478bd9Sstevel@tonic-gate 402*7c478bd9Sstevel@tonic-gate if (i2c_client_register(dip, &unitp->pic16f819_hdl) != I2C_SUCCESS) { 403*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s i2c_client_register failed\n", 404*7c478bd9Sstevel@tonic-gate unitp->pic16f819_name); 405*7c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 406*7c478bd9Sstevel@tonic-gate ddi_soft_state_free(pic16f819soft_statep, instance); 407*7c478bd9Sstevel@tonic-gate 408*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 409*7c478bd9Sstevel@tonic-gate } 410*7c478bd9Sstevel@tonic-gate 411*7c478bd9Sstevel@tonic-gate mutex_init(&unitp->pic16f819_mutex, NULL, MUTEX_DRIVER, NULL); 412*7c478bd9Sstevel@tonic-gate 413*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 414*7c478bd9Sstevel@tonic-gate } 415*7c478bd9Sstevel@tonic-gate 416*7c478bd9Sstevel@tonic-gate static int 417*7c478bd9Sstevel@tonic-gate pic16f819_do_resume() 418*7c478bd9Sstevel@tonic-gate { 419*7c478bd9Sstevel@tonic-gate int ret = DDI_SUCCESS; 420*7c478bd9Sstevel@tonic-gate 421*7c478bd9Sstevel@tonic-gate return (ret); 422*7c478bd9Sstevel@tonic-gate } 423*7c478bd9Sstevel@tonic-gate 424*7c478bd9Sstevel@tonic-gate static int 425*7c478bd9Sstevel@tonic-gate pic16f819_do_suspend() 426*7c478bd9Sstevel@tonic-gate { 427*7c478bd9Sstevel@tonic-gate int ret = DDI_SUCCESS; 428*7c478bd9Sstevel@tonic-gate 429*7c478bd9Sstevel@tonic-gate return (ret); 430*7c478bd9Sstevel@tonic-gate } 431*7c478bd9Sstevel@tonic-gate 432*7c478bd9Sstevel@tonic-gate static int 433*7c478bd9Sstevel@tonic-gate pic16f819_do_detach(dev_info_t *dip) 434*7c478bd9Sstevel@tonic-gate { 435*7c478bd9Sstevel@tonic-gate struct pic16f819_unit *unitp; 436*7c478bd9Sstevel@tonic-gate int instance; 437*7c478bd9Sstevel@tonic-gate 438*7c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 439*7c478bd9Sstevel@tonic-gate 440*7c478bd9Sstevel@tonic-gate unitp = ddi_get_soft_state(pic16f819soft_statep, instance); 441*7c478bd9Sstevel@tonic-gate 442*7c478bd9Sstevel@tonic-gate if (unitp == NULL) { 443*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: unitp not filled\n", 444*7c478bd9Sstevel@tonic-gate ddi_get_name(dip), instance); 445*7c478bd9Sstevel@tonic-gate return (ENOMEM); 446*7c478bd9Sstevel@tonic-gate } 447*7c478bd9Sstevel@tonic-gate 448*7c478bd9Sstevel@tonic-gate i2c_client_unregister(unitp->pic16f819_hdl); 449*7c478bd9Sstevel@tonic-gate 450*7c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 451*7c478bd9Sstevel@tonic-gate 452*7c478bd9Sstevel@tonic-gate mutex_destroy(&unitp->pic16f819_mutex); 453*7c478bd9Sstevel@tonic-gate 454*7c478bd9Sstevel@tonic-gate ddi_soft_state_free(pic16f819soft_statep, instance); 455*7c478bd9Sstevel@tonic-gate 456*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 457*7c478bd9Sstevel@tonic-gate } 458